diff --git a/src/com/massivecraft/factions/Conf.java b/src/com/massivecraft/factions/Conf.java index 82c3ab1c..6c75eda9 100644 --- a/src/com/massivecraft/factions/Conf.java +++ b/src/com/massivecraft/factions/Conf.java @@ -63,6 +63,8 @@ public class Conf { public static double autoLeaveAfterDaysOfInactivity = 14.0; + public static boolean worldGuardChecking = true; + public static boolean homesEnabled = true; public static boolean homesMustBeInClaimedTerritory = true; public static boolean homesTeleportToOnDeath = true; diff --git a/src/com/massivecraft/factions/FPlayer.java b/src/com/massivecraft/factions/FPlayer.java index 5ac1350d..ccb5382c 100644 --- a/src/com/massivecraft/factions/FPlayer.java +++ b/src/com/massivecraft/factions/FPlayer.java @@ -508,6 +508,12 @@ public class FPlayer { FLocation flocation = new FLocation(this); Faction otherFaction = Board.getFactionAt(flocation); + if (Worldguard.checkForRegionsInChunk(this.getPlayer().getLocation())) { + // Checks for WorldGuard regions in the chunk attempting to be claimed + sendMessage("This land is protected"); + return false; + } + if (myFaction == otherFaction) { if (notifyFailure) sendMessage("You already own this land."); diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 58136cc9..5bd19217 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -151,6 +151,10 @@ public class Factions extends JavaPlugin { Econ.setup(this); Econ.monitorPlugins(); + if(Conf.worldGuardChecking) { + Worldguard.init(this); + } + // Register events PluginManager pm = this.getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_CHAT, this.playerListener, Event.Priority.Highest, this); diff --git a/src/com/massivecraft/factions/SpoutFeatures.java b/src/com/massivecraft/factions/SpoutFeatures.java index 725afedb..7a9335e0 100644 --- a/src/com/massivecraft/factions/SpoutFeatures.java +++ b/src/com/massivecraft/factions/SpoutFeatures.java @@ -147,7 +147,7 @@ public class SpoutFeatures { spoutApp.setPlayerTitle(sPlayer, viewed, addTag + "\n" + viewed.getDisplayName()); } else { - spoutApp.resetPlayerTitle(sPlayer, viewed); + spoutApp.setPlayerTitle(sPlayer, viewed, viewed.getDisplayName()); } } diff --git a/src/com/massivecraft/factions/Worldguard.java b/src/com/massivecraft/factions/Worldguard.java new file mode 100644 index 00000000..00750983 --- /dev/null +++ b/src/com/massivecraft/factions/Worldguard.java @@ -0,0 +1,115 @@ +package com.massivecraft.factions; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.UnsupportedIntersectionException; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import static com.sk89q.worldguard.bukkit.BukkitUtil.*; +import com.sk89q.worldguard.protection.flags.DefaultFlag; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.BlockVector; + +import org.bukkit.World; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.plugin.Plugin; +import org.bukkit.entity.Player; + +/* + * Worldguard Region Checking + * Author: Spathizilla + */ + +public class Worldguard { + private static WorldGuardPlugin wg; + private static boolean enabled = false; + + public static void init(Plugin plugin) { + Plugin wgplug = plugin.getServer().getPluginManager().getPlugin("WorldGuard"); + if (wgplug == null || !(wgplug instanceof WorldGuardPlugin)) { + enabled = false; + wg = null; + System.out.println("[Factions] Could not hook to WorldGuard. WorldGuard checks are disabled."); + } else { + wg = (WorldGuardPlugin) wgplug; + enabled = true; + System.out.println("[Factions] Successfully hooked to WorldGuard."); + } + } + + public static boolean isEnabled() { + return enabled; + } + + // PVP Flag check + // Returns: + // True: PVP is allowed + // False: PVP is disallowed + public static boolean isPVP(Player player) { + if(isEnabled()) { + Location loc = player.getLocation(); + World world = loc.getWorld(); + Vector pt = toVector(loc); + + RegionManager regionManager = wg.getRegionManager(world); + ApplicableRegionSet set = regionManager.getApplicableRegions(pt); + return set.allows(DefaultFlag.PVP); + } else { + // No WG hooks so we'll always bypass this check. + return true; + } + } + + // Check for Regions in chunk the chunk + // Returns: + // True: Regions found within chunk + // False: No regions found within chunk + public static boolean checkForRegionsInChunk(Location loc) { + if(isEnabled()) { + World world = loc.getWorld(); + Chunk chunk = world.getChunkAt(loc); + int minChunkX = chunk.getX() * 16; + int minChunkZ = chunk.getZ() * 16; + int maxChunkX = minChunkX + 15; + int maxChunkZ = minChunkZ + 15; + + int worldHeight = world.getMaxHeight(); // Allow for heights other than default + + BlockVector minChunk = new BlockVector(minChunkX, 0, minChunkZ); + BlockVector maxChunk = new BlockVector(maxChunkX, worldHeight, maxChunkZ); + + RegionManager regionManager = wg.getRegionManager(world); + ProtectedCuboidRegion region = new ProtectedCuboidRegion("wgfactionoverlapcheck", minChunk, maxChunk); + Map allregions = regionManager.getRegions(); + List allregionslist = new ArrayList(allregions.values()); + List overlaps; + boolean foundregions = false; + + try { + overlaps = region.getIntersectingRegions(allregionslist); + if(overlaps.isEmpty() || overlaps == null) { + foundregions = false; + } else { + foundregions = true; + } + } catch (UnsupportedIntersectionException e) { + e.printStackTrace(); + } + + region = null; + allregionslist = null; + overlaps = null; + + return foundregions; + } else { + return false; + } + } +} \ No newline at end of file