diff --git a/lib/WorldEdit-6.1.jar b/lib/WorldEdit-6.1.jar new file mode 100644 index 00000000..753c4f2e Binary files /dev/null and b/lib/WorldEdit-6.1.jar differ diff --git a/lib/WorldGuard-6.1.jar b/lib/WorldGuard-6.1.jar new file mode 100644 index 00000000..7824963f Binary files /dev/null and b/lib/WorldGuard-6.1.jar differ diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 5ede3138..eeab79df 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -36,6 +36,7 @@ import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.factions.entity.MConfColl; import com.massivecraft.factions.integration.herochat.IntegrationHerochat; import com.massivecraft.factions.integration.lwc.IntegrationLwc; +import com.massivecraft.factions.integration.worldguard.IntegrationWorldGuard; import com.massivecraft.factions.mixin.PowerMixin; import com.massivecraft.factions.mixin.PowerMixinDefault; import com.massivecraft.factions.spigot.SpigotFeatures; @@ -154,7 +155,8 @@ public class Factions extends MassivePlugin // Integrate this.integrate( IntegrationHerochat.get(), - IntegrationLwc.get() + IntegrationLwc.get(), + IntegrationWorldGuard.get() ); // Spigot diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index 5fc4bece..22ca876a 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -651,6 +651,17 @@ public class MConf extends Entity EventFactionsChunkChangeType.PILLAGE, false // when unclaiming (to wilderness) from another player faction ); + // -------------------------------------------- // + // INTEGRATION: WorldGuard + // -------------------------------------------- // + + // Global WorldGuard Integration Switch + public boolean worldguardCheckEnabled = false; + + // Enable the WorldGuard check per-world + // Specify which worlds the WorldGuard Check can be used in + public WorldExceptionSet worldguardCheckWorldsEnabled = new WorldExceptionSet(); + // -------------------------------------------- // // INTEGRATION: ECONOMY // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/integration/worldguard/EngineWorldGuard.java b/src/com/massivecraft/factions/integration/worldguard/EngineWorldGuard.java new file mode 100644 index 00000000..5bfca3c4 --- /dev/null +++ b/src/com/massivecraft/factions/integration/worldguard/EngineWorldGuard.java @@ -0,0 +1,143 @@ +package com.massivecraft.factions.integration.worldguard; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.plugin.Plugin; + +import com.massivecraft.factions.Factions; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MFlag; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.factions.event.EventFactionsChunksChange; +import com.massivecraft.massivecore.EngineAbstract; +import com.massivecraft.massivecore.ps.PS; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +public class EngineWorldGuard extends EngineAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EngineWorldGuard i = new EngineWorldGuard(); + public static EngineWorldGuard get() { return i; } + private EngineWorldGuard() {} + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + protected WorldGuardPlugin worldGuard; + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public Plugin getPlugin() + { + return Factions.get(); + } + + @Override + public void activate() + { + this.worldGuard = (WorldGuardPlugin) Bukkit.getPluginManager().getPlugin("WorldGuard"); + + super.activate(); + } + + @Override + public void deactivate() + { + this.worldGuard = null; + + super.deactivate(); + } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void checkForRegion(EventFactionsChunksChange event) + { + // Skip checks if the configuration has worldguardCheckEnabled disabled + if ( ! MConf.get().worldguardCheckEnabled) return; + + // Permanent Factions should not apply this rule + if (event.getNewFaction().getFlag(MFlag.ID_PERMANENT)) return; + + MPlayer mplayer = event.getMSender(); + + if ( ! MConf.get().worldguardCheckWorldsEnabled.contains(mplayer.getPlayer())) return; + + // For admins don't bother checking + if (mplayer.isUsingAdminMode()) return; + + for (PS chunkChecking : event.getChunks()) + { + // Grab any regions in the chunk + List regions = this.getProtectedRegionsFor(chunkChecking); + + // Ensure there are actually regions to go over + if (regions == null || regions.isEmpty()) continue; + + for (ProtectedRegion region : regions) + { + // Ensure it's not the global region, and check if they're a member + if (region.getId().equals("__global__") || region.getMembers().contains(mplayer.getUuid())) continue; + + // Check for a permission + if (mplayer.getPlayer().hasPermission("factions.allowregionclaim." + region.getId())) continue; + + // No permission, notify player and stop claiming + mplayer.msg("You cannot claim the chunk at %s, %s as there is a region in the way.", chunkChecking.getChunkX(), chunkChecking.getChunkZ()); + + event.setCancelled(true); + return; + } + } + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public List getProtectedRegionsFor(PS ps) + { + // Find overlaps in the chunk + int minChunkX = ps.getChunkX() << 4; + int minChunkZ = ps.getChunkZ() << 4; + int maxChunkX = minChunkX + 15; + int maxChunkZ = minChunkZ + 15; + + int worldHeight = ps.asBukkitWorld().getMaxHeight(); + + BlockVector minChunk = new BlockVector(minChunkX, 0, minChunkZ); + BlockVector maxChunk = new BlockVector(maxChunkX, worldHeight, maxChunkZ); + + RegionManager regionManager = this.worldGuard.getRegionManager(ps.asBukkitWorld()); + + String regionName = "factions_temp"; + ProtectedCuboidRegion region = new ProtectedCuboidRegion(regionName, minChunk, maxChunk); + + Map regionMap = regionManager.getRegions(); + List regionList = new ArrayList(regionMap.values()); + + // Let's find what we've overlapped + List overlapRegions = region.getIntersectingRegions(regionList); + + return overlapRegions; + } + +} diff --git a/src/com/massivecraft/factions/integration/worldguard/IntegrationWorldGuard.java b/src/com/massivecraft/factions/integration/worldguard/IntegrationWorldGuard.java new file mode 100644 index 00000000..b8382f39 --- /dev/null +++ b/src/com/massivecraft/factions/integration/worldguard/IntegrationWorldGuard.java @@ -0,0 +1,31 @@ +package com.massivecraft.factions.integration.worldguard; + +import com.massivecraft.massivecore.integration.IntegrationAbstract; + +public class IntegrationWorldGuard extends IntegrationAbstract +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static IntegrationWorldGuard i = new IntegrationWorldGuard(); + public static IntegrationWorldGuard get() { return i; } + private IntegrationWorldGuard() { super("WorldGuard"); } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void activate() + { + EngineWorldGuard.get().activate(); + } + + @Override + public void deactivate() + { + EngineWorldGuard.get().deactivate(); + } + +}