diff --git a/src/main/java/com/massivecraft/factions/Conf.java b/src/main/java/com/massivecraft/factions/Conf.java index a27e6f12..023ac49a 100644 --- a/src/main/java/com/massivecraft/factions/Conf.java +++ b/src/main/java/com/massivecraft/factions/Conf.java @@ -82,6 +82,7 @@ public class Conf { public static String truceChatFormat = ChatColor.DARK_PURPLE + "%s:" + ChatColor.WHITE + " %s"; public static String modChatFormat = ChatColor.RED + "%s:" + ChatColor.WHITE + " %s"; public static int stealthFlyCheckRadius = 32; + public static int factionBufferSize = 20; public static boolean gracePeriod = false; public static boolean noEnderpearlsInFly = false; public static boolean broadcastDescriptionChanges = false; diff --git a/src/main/java/com/massivecraft/factions/FLocation.java b/src/main/java/com/massivecraft/factions/FLocation.java index 0f9c71fd..f966303f 100644 --- a/src/main/java/com/massivecraft/factions/FLocation.java +++ b/src/main/java/com/massivecraft/factions/FLocation.java @@ -78,7 +78,6 @@ public class FLocation implements Serializable { public static int blockToChunk(int blockVal) { // 1 chunk is 16x16 blocks return blockVal >> 4; // ">> 4" == "/ 16" } - public static int blockToRegion(int blockVal) { // 1 region is 512x512 blocks return blockVal >> 9; // ">> 9" == "/ 512" } diff --git a/src/main/java/com/massivecraft/factions/SavageFactions.java b/src/main/java/com/massivecraft/factions/SavageFactions.java index f3d767f8..774b622a 100644 --- a/src/main/java/com/massivecraft/factions/SavageFactions.java +++ b/src/main/java/com/massivecraft/factions/SavageFactions.java @@ -57,6 +57,7 @@ public class SavageFactions extends MPlugin { // Plugins can check this boolean while hooking in have // a green light to use the api. public static boolean startupFinished = false; + private FactionsPlayerListener factionsPlayerListener; public boolean PlaceholderApi; @@ -238,10 +239,10 @@ public class SavageFactions extends MPlugin { log("Skript addon registered!"); } + getServer().getPluginManager().registerEvents(factionsPlayerListener = new FactionsPlayerListener(), this); // Register Event Handlers eventsListener = new Listener[]{ - new FactionsPlayerListener(), new FactionsChatListener(), new FactionsEntityListener(), new FactionsExploitListener(), @@ -778,6 +779,9 @@ public class SavageFactions extends MPlugin { getLogger().log(level, s); } } + public FactionsPlayerListener getFactionsPlayerListener() { + return this.factionsPlayerListener; + } public void debug(String s) { debug(Level.INFO, s); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdCorner.java b/src/main/java/com/massivecraft/factions/cmd/CmdCorner.java new file mode 100644 index 00000000..e624dd95 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/cmd/CmdCorner.java @@ -0,0 +1,64 @@ +package com.massivecraft.factions.cmd; + + +import com.massivecraft.factions.*; +import com.massivecraft.factions.struct.Permission; +import com.massivecraft.factions.util.CornerTask; +import com.massivecraft.factions.zcore.util.TL; +import org.bukkit.Bukkit; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class CmdCorner extends FCommand { + + public CmdCorner() { + this.aliases.add("corner"); + + this.permission = Permission.CLAIM_RADIUS.node; + + this.senderMustBePlayer = true; + this.senderMustBeMember = true; + this.senderMustBeModerator = false; + this.senderMustBeAdmin = false; + } + + @Override + public void perform() { + FLocation to = new FLocation(me.getLocation()); + if (SavageFactions.plugin.getFactionsPlayerListener().getCorners().contains(to)) { + Faction cornerAt = Board.getInstance().getFactionAt(to); + if (cornerAt != null && cornerAt.isNormal() && !cornerAt.equals(fme.getFaction())) { + msg(TL.COMMAND_CORNER_CANT_CLAIM); + } else { + msg(TL.COMMAND_CORNER_ATTEMPTING_CLAIM); + List surrounding = new ArrayList<>(400); + for (int x = 0; x < Conf.factionBufferSize; ++x) { + for (int z = 0; z < Conf.factionBufferSize; ++z) { + int newX = (int) ((to.getX() > 0L) ? (to.getX() - x) : (to.getX() + x)); + int newZ = (int) ((to.getZ() > 0L) ? (to.getZ() - z) : (to.getZ() + z)); + FLocation location = new FLocation(me.getWorld().getName(), newX, newZ); + Faction at = Board.getInstance().getFactionAt(location); + if (at == null || !at.isNormal()) { + surrounding.add(location); + } + } + } + surrounding.sort(Comparator.comparingInt(fLocation -> (int) fLocation.getDistanceTo(to))); + if (surrounding.isEmpty()) { + msg(TL.COMMAND_CORNER_CANT_CLAIM); + } else { + new CornerTask(fme, surrounding).runTaskTimer(SavageFactions.plugin, 1L, 1L); + } + } + } else { + msg(TL.COMMAND_CORNER_NOT_CORNER); + } + } + + @Override + public TL getUsageTranslation() { + return TL.COMMAND_CORNER_DESCRIPTION; + } +} diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index 03e906d2..20553c9d 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -109,6 +109,7 @@ public class FCmdRoot extends FCommand { public CmdStrikeSet cmdStrikeSet = new CmdStrikeSet(); public CmdAlts cmdAlts = new CmdAlts(); public CmdSpam cmdSpam = new CmdSpam(); + public CmdCorner cmdCorner = new CmdCorner(); @@ -224,6 +225,7 @@ public class FCmdRoot extends FCommand { this.addSubCommand(this.cmdSetBanner); this.addSubCommand(this.cmdStrikeSet); this.addSubCommand(this.cmdSpam); + this.addSubCommand(this.cmdCorner); if(SavageFactions.plugin.getConfig().getBoolean("f-alts.Enabled")){ diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java index ffb4a587..edc6016c 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsPlayerListener.java @@ -1,6 +1,5 @@ package com.massivecraft.factions.listeners; -import com.earth2me.essentials.User; import com.massivecraft.factions.*; import com.massivecraft.factions.cmd.CmdFly; import com.massivecraft.factions.cmd.CmdSeeChunk; @@ -24,10 +23,7 @@ import com.massivecraft.factions.zcore.util.TagUtil; import com.massivecraft.factions.zcore.util.TextUtil; import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtectAPI; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.Material; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.Event; @@ -47,7 +43,7 @@ import java.util.logging.Level; public class FactionsPlayerListener implements Listener { - + private Set corners; HashMap fallMap = new HashMap<>(); // Holds the next time a player can have a map shown. @@ -56,9 +52,20 @@ public class FactionsPlayerListener implements Listener { private Map interactSpammers = new HashMap<>(); public FactionsPlayerListener() { + this.corners = new HashSet<>(); for (Player player : SavageFactions.plugin.getServer().getOnlinePlayers()) { initPlayer(player); } + for (World world : SavageFactions.plugin.getServer().getWorlds()) { + WorldBorder border = world.getWorldBorder(); + if (border != null) { + int cornerCoord = (int) ((border.getSize() - 1.0) / 2.0); + this.corners.add(new FLocation(world.getName(), FLocation.blockToChunk(cornerCoord), FLocation.blockToChunk(cornerCoord))); + this.corners.add(new FLocation(world.getName(), FLocation.blockToChunk(cornerCoord), FLocation.blockToChunk(-cornerCoord))); + this.corners.add(new FLocation(world.getName(), FLocation.blockToChunk(-cornerCoord), FLocation.blockToChunk(cornerCoord))); + this.corners.add(new FLocation(world.getName(), FLocation.blockToChunk(-cornerCoord), FLocation.blockToChunk(-cornerCoord))); + } + } } public static Boolean isSystemFaction(Faction faction) { @@ -778,6 +785,10 @@ public class FactionsPlayerListener implements Listener { } } + public Set getCorners() { + return this.corners; + } + /// /// This checks if the current player can execute an action based on it's factions access and surroundings /// It will grant access in the following priorities: diff --git a/src/main/java/com/massivecraft/factions/util/CornerTask.java b/src/main/java/com/massivecraft/factions/util/CornerTask.java new file mode 100644 index 00000000..874bd158 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/CornerTask.java @@ -0,0 +1,34 @@ +package com.massivecraft.factions.util; + +import com.massivecraft.factions.FLocation; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.zcore.util.TL; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.List; + +public class CornerTask extends BukkitRunnable { + private FPlayer fPlayer; + private List surrounding; + private int amount; + + public CornerTask(FPlayer fPlayer, List surrounding) { + this.amount = 0; + this.fPlayer = fPlayer; + this.surrounding = surrounding; + } + + public void run() { + if (surrounding.isEmpty()) { + fPlayer.sendMessage(TL.COMMAND_CORNER_CLAIMED.format(amount)); + cancel(); + } else if (fPlayer.isOffline()) { + cancel(); + } else { + FLocation fLocation = surrounding.remove(0); + if (fPlayer.attemptClaim(fPlayer.getFaction(), fLocation, true)) { + ++amount; + } + } + } +} diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java index 1aca7a00..e97e3743 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -859,6 +859,7 @@ public abstract class MemoryFPlayer implements FPlayer { return attemptClaim(forFaction, new FLocation(location), notifyFailure); } + public boolean shouldBeSaved() { return this.hasFaction() || (this.getPowerRounded() != this.getPowerMaxRounded() && this.getPowerRounded() != (int) Math.round(Conf.powerPlayerStarting)); } @@ -1173,7 +1174,6 @@ public abstract class MemoryFPlayer implements FPlayer { public boolean attemptClaim(Faction forFaction, FLocation flocation, boolean notifyFailure) { // notifyFailure is false if called by auto-claim; no need to notify on every failure for it // return value is false on failure, true on success - Faction currentFaction = Board.getInstance().getFactionAt(flocation); int ownedLand = forFaction.getLandRounded(); @@ -1239,6 +1239,7 @@ public abstract class MemoryFPlayer implements FPlayer { } + @Override public String getRolePrefix() { diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index 112636ef..7e790a01 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -114,6 +114,12 @@ public enum TL { */ COMMAND_UPGRADES_DESCRIPTION("&cOpen the Upgrades Menu"), + COMMAND_CORNER_CANT_CLAIM("&c&l[!] &cYou may not claim this corner!"), + COMMAND_CORNER_CLAIMED("\n&2&l[!] &aYou have claimed the corner successfully, totalling in &b%1$d &achunks!\n"), + COMMAND_CORNER_ATTEMPTING_CLAIM("&c&l[!] &7Attempting to claim corner..."), + COMMAND_CORNER_NOT_CORNER("&c&l[!] &7You must be in a corner to use this command!"), + COMMAND_CORNER_DESCRIPTION("claim a corner at world border"), + COMMAND_CORNERLIST_DESCRIPTION("list of all corners"), COMMAND_ADMIN_NOTMEMBER("&c&l[!] &7%1$s &cis not a member in your faction."), COMMAND_ADMIN_NOTADMIN("&c&l[!] &cYou are not the faction admin."),