diff --git a/pom.xml b/pom.xml index 34c3b9c9..8fbdd88b 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,46 @@ + + com.sk89q + worldguard + 6.1.1-SNAPSHOT + provided + + + bukkit + org.bukkit + + + bukkit-classloader-check + com.sk89q.spigot + + + commandbook + com.sk89q + + + jsr305 + com.google.code.findbugs + + + js + rhino + + + truezip + de.schlichtherle + + + jchronic + com.sk89q + + + worldedit + com.sk89q + + + com.sk89q.worldguard worldguard-legacy diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index 483080be..ad53fab3 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -5,9 +5,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.cmd.CmdAutoHelp; import com.massivecraft.factions.cmd.FCmdRoot; -import com.massivecraft.factions.integration.Econ; -import com.massivecraft.factions.integration.Essentials; -import com.massivecraft.factions.integration.Worldguard; +import com.massivecraft.factions.integration.*; import com.massivecraft.factions.integration.dynmap.EngineDynmap; import com.massivecraft.factions.listeners.*; import com.massivecraft.factions.struct.ChatMode; @@ -23,13 +21,10 @@ import com.massivecraft.factions.zcore.MPlugin; import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.Permissable; import com.massivecraft.factions.zcore.fperms.PermissableAction; -import com.massivecraft.factions.zcore.util.TextUtil; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.plugin.Plugin; @@ -37,7 +32,9 @@ import org.bukkit.plugin.RegisteredServiceProvider; import java.lang.reflect.Modifier; import java.lang.reflect.Type; -import java.util.*; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import java.util.logging.Level; public class P extends MPlugin { @@ -71,6 +68,7 @@ public class P extends MPlugin { public SeeChunkUtil seeChunkUtil; public ParticleProvider particleProvider; + public IWorldguard worldguard; public P() { p = this; @@ -125,9 +123,7 @@ public class P extends MPlugin { Econ.setup(); setupPermissions(); - if (Conf.worldGuardChecking || Conf.worldGuardBuildPriority) { - Worldguard.init(this); - } + loadWorldguard(); EngineDynmap.getInstance().init(); @@ -174,6 +170,33 @@ public class P extends MPlugin { this.loadSuccessful = true; } + private void loadWorldguard() { + if (!Conf.worldGuardChecking && !Conf.worldGuardBuildPriority) { + log(Level.INFO, "Not enabling WorldGuard check since no options for it are enabled."); + return; + } + + Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard"); + if (plugin != null) { + String version = plugin.getDescription().getVersion(); + if (version.startsWith("6")) { + this.worldguard = new Worldguard6(); + log(Level.INFO, "Found support for WorldGuard version " + version); + } else if (version.startsWith("7")) { + this.worldguard = new Worldguard7(); + log(Level.INFO, "Found support for WorldGuard version " + version); + } else { + P.p.log(Level.WARNING, "Loaded WorldGuard but couldn't support this version: " + version); + } + } else { + P.p.log(Level.WARNING, "WorldGuard checks were turned in on conf.json, but WorldGuard isn't present on the server."); + } + } + + public IWorldguard getWorldguard() { + return this.worldguard; + } + private void setupPlaceholderAPI() { Plugin clip = getServer().getPluginManager().getPlugin("PlaceholderAPI"); if (clip != null && clip.isEnabled()) { @@ -223,10 +246,10 @@ public class P extends MPlugin { Type accessTypeAdatper = new TypeToken>>() { }.getType(); - Type factionMaterialType = new TypeToken(){ + Type factionMaterialType = new TypeToken() { }.getType(); - Type materialType = new TypeToken(){ + Type materialType = new TypeToken() { }.getType(); return new GsonBuilder() diff --git a/src/main/java/com/massivecraft/factions/integration/IWorldguard.java b/src/main/java/com/massivecraft/factions/integration/IWorldguard.java new file mode 100644 index 00000000..25ff0081 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/integration/IWorldguard.java @@ -0,0 +1,15 @@ +package com.massivecraft.factions.integration; + +import com.massivecraft.factions.FLocation; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public interface IWorldguard { + + public boolean isPVP(Player player); + + public boolean playerCanBuild(Player player, Location loc); + + public boolean checkForRegionsInChunk(FLocation flocation); + +} diff --git a/src/main/java/com/massivecraft/factions/integration/Worldguard6.java b/src/main/java/com/massivecraft/factions/integration/Worldguard6.java new file mode 100644 index 00000000..2525fc5b --- /dev/null +++ b/src/main/java/com/massivecraft/factions/integration/Worldguard6.java @@ -0,0 +1,71 @@ +package com.massivecraft.factions.integration; + +import com.massivecraft.factions.FLocation; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.LocalPlayer; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.protection.regions.RegionContainer; +import com.sk89q.worldguard.protection.regions.RegionQuery; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; + +public class Worldguard6 implements IWorldguard { + + // PVP Flag check + // Returns: + // True: PVP is allowed + // False: PVP is disallowed + public boolean isPVP(Player player) { + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); + + return query.testState(localPlayer.getLocation(), localPlayer, Flags.PVP); + } + + // Check if player can build at location by worldguards rules. + // Returns: + // True: Player can build in the region. + // False: Player can not build in the region. + public boolean playerCanBuild(Player player, Location loc) { + LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionQuery query = container.createQuery(); + + return query.testBuild(localPlayer.getLocation(), localPlayer); + } + + public boolean checkForRegionsInChunk(FLocation flocation) { + Chunk chunk = flocation.getChunk(); + + RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); + RegionManager regions = container.get(BukkitAdapter.adapt(chunk.getWorld())); + if (regions == null) { + return false; + } + + World world = chunk.getWorld(); + int minChunkX = chunk.getX() << 4; + int minChunkZ = chunk.getZ() << 4; + int maxChunkX = minChunkX + 15; + int maxChunkZ = minChunkZ + 15; + + int worldHeight = world.getMaxHeight(); // Allow for heights other than default + + BlockVector3 min = BlockVector3.at(minChunkX, 0, minChunkZ); + BlockVector3 max = BlockVector3.at(maxChunkX, worldHeight, maxChunkZ); + ProtectedRegion region = new ProtectedCuboidRegion("wgregionflagcheckforfactions", min, max); + ApplicableRegionSet set = regions.getApplicableRegions(region); + + return set.size() > 1; + } +} \ No newline at end of file diff --git a/src/main/java/com/massivecraft/factions/integration/Worldguard.java b/src/main/java/com/massivecraft/factions/integration/Worldguard7.java similarity index 67% rename from src/main/java/com/massivecraft/factions/integration/Worldguard.java rename to src/main/java/com/massivecraft/factions/integration/Worldguard7.java index 8a403841..8f3c93c3 100644 --- a/src/main/java/com/massivecraft/factions/integration/Worldguard.java +++ b/src/main/java/com/massivecraft/factions/integration/Worldguard7.java @@ -1,7 +1,6 @@ package com.massivecraft.factions.integration; import com.massivecraft.factions.FLocation; -import com.massivecraft.factions.P; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldguard.LocalPlayer; @@ -18,37 +17,14 @@ import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -public class Worldguard { - - private static boolean enabled = false; - - public static void init(Plugin plugin) { - Plugin wgplug = plugin.getServer().getPluginManager().getPlugin("WorldGuard"); - if (wgplug == null) { - enabled = false; - P.p.log("Could not hook to WorldGuard. WorldGuard checks are disabled."); - } else { - enabled = true; - P.p.log("Successfully hooked to WorldGuard."); - } - } - - public static boolean isEnabled() { - return enabled; - } +public class Worldguard7 implements IWorldguard { // PVP Flag check // Returns: // True: PVP is allowed // False: PVP is disallowed - public static boolean isPVP(Player player) { - if (!enabled) { - // No WG hooks so we'll always bypass this check. - return true; - } - + public boolean isPVP(Player player) { LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionQuery query = container.createQuery(); @@ -60,12 +36,7 @@ public class Worldguard { // Returns: // True: Player can build in the region. // False: Player can not build in the region. - public static boolean playerCanBuild(Player player, Location loc) { - if (!enabled) { - // No WG hooks so we'll always bypass this check. - return false; - } - + public boolean playerCanBuild(Player player, Location loc) { LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionQuery query = container.createQuery(); @@ -73,17 +44,12 @@ public class Worldguard { return query.testBuild(localPlayer.getLocation(), localPlayer); } - public static boolean checkForRegionsInChunk(FLocation flocation) { - if (!enabled) { - // No WG hooks so we'll always bypass this check. - return false; - } - + public boolean checkForRegionsInChunk(FLocation flocation) { Chunk chunk = flocation.getChunk(); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionManager regions = container.get(BukkitAdapter.adapt(chunk.getWorld())); - if (regions == null){ + if (regions == null) { return false; } diff --git a/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java b/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java index 552134e2..98b7b9a2 100644 --- a/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java +++ b/src/main/java/com/massivecraft/factions/listeners/FactionsBlockListener.java @@ -1,12 +1,11 @@ package com.massivecraft.factions.listeners; import com.massivecraft.factions.*; -import com.massivecraft.factions.integration.Worldguard; +import com.massivecraft.factions.integration.Worldguard7; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.zcore.fperms.Access; import com.massivecraft.factions.zcore.fperms.PermissableAction; -import com.massivecraft.factions.zcore.util.TL; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -192,7 +191,7 @@ public class FactionsBlockListener implements Listener { Faction otherFaction = Board.getInstance().getFactionAt(loc); if (otherFaction.isWilderness()) { - if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) { + if (Conf.worldGuardBuildPriority && P.p.getWorldguard() != null && P.p.getWorldguard().playerCanBuild(player, location)) { return true; } @@ -206,7 +205,7 @@ public class FactionsBlockListener implements Listener { return false; } else if (otherFaction.isSafeZone()) { - if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) { + if (Conf.worldGuardBuildPriority && P.p.getWorldguard() != null && P.p.getWorldguard().playerCanBuild(player, location)) { return true; } @@ -220,7 +219,7 @@ public class FactionsBlockListener implements Listener { return false; } else if (otherFaction.isWarZone()) { - if (Conf.worldGuardBuildPriority && Worldguard.playerCanBuild(player, location)) { + if (Conf.worldGuardBuildPriority && P.p.getWorldguard() != null && P.p.getWorldguard().playerCanBuild(player, location)) { return true; } 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 e8fae54a..56ad0dfc 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryFPlayer.java @@ -7,7 +7,7 @@ import com.massivecraft.factions.iface.EconomyParticipator; import com.massivecraft.factions.iface.RelationParticipator; import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.integration.Essentials; -import com.massivecraft.factions.integration.Worldguard; +import com.massivecraft.factions.integration.Worldguard7; import com.massivecraft.factions.scoreboards.FScoreboard; import com.massivecraft.factions.scoreboards.sidebar.FInfoSidebar; import com.massivecraft.factions.struct.ChatMode; @@ -731,7 +731,7 @@ public abstract class MemoryFPlayer implements FPlayer { int factionBuffer = P.p.getConfig().getInt("hcf.buffer-zone", 0); int worldBuffer = P.p.getConfig().getInt("world-border.buffer", 0); - if (Conf.worldGuardChecking && Worldguard.checkForRegionsInChunk(flocation)) { + if (Conf.worldGuardChecking && P.p.getWorldguard() != null && P.p.getWorldguard().checkForRegionsInChunk(flocation)) { // Checks for WorldGuard regions in the chunk attempting to be claimed error = P.p.txt.parse(TL.CLAIM_PROTECTED.toString()); } else if (Conf.worldsNoClaiming.contains(flocation.getWorldName())) {