2011-10-05 12:13:54 +02:00
|
|
|
package com.massivecraft.factions.integration;
|
2011-08-29 05:50:45 +02:00
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
import com.massivecraft.factions.P;
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.sk89q.worldedit.BlockVector;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
2011-08-29 05:50:45 +02:00
|
|
|
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
|
|
|
import com.sk89q.worldguard.protection.ApplicableRegionSet;
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.sk89q.worldguard.protection.flags.DefaultFlag;
|
|
|
|
import com.sk89q.worldguard.protection.managers.RegionManager;
|
2011-08-29 05:50:45 +02:00
|
|
|
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
|
|
|
|
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
2011-08-31 03:13:16 +02:00
|
|
|
import org.bukkit.Chunk;
|
|
|
|
import org.bukkit.Location;
|
2014-04-04 20:55:21 +02:00
|
|
|
import org.bukkit.World;
|
2011-08-29 05:50:45 +02:00
|
|
|
import org.bukkit.entity.Player;
|
2014-04-04 20:55:21 +02:00
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2014-04-05 22:42:01 +02:00
|
|
|
import static com.sk89q.worldguard.bukkit.BukkitUtil.toVector;
|
2011-08-29 05:50:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Worldguard Region Checking
|
|
|
|
* Author: Spathizilla
|
|
|
|
*/
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
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)) {
|
2014-07-01 21:52:40 +02:00
|
|
|
enabled = false; wg = null; P.p.log("Could not hook to WorldGuard. WorldGuard checks are disabled.");
|
|
|
|
} else {
|
|
|
|
wg = (WorldGuardPlugin) wgplug; enabled = true; P.p.log("Successfully hooked to WorldGuard.");
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (!enabled) {
|
|
|
|
// No WG hooks so we'll always bypass this check.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
Location loc = player.getLocation(); World world = loc.getWorld(); Vector pt = toVector(loc);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
RegionManager regionManager = wg.getRegionManager(world);
|
2014-07-01 21:52:40 +02:00
|
|
|
ApplicableRegionSet set = regionManager.getApplicableRegions(pt); return set.allows(DefaultFlag.PVP);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 static boolean playerCanBuild(Player player, Location loc) {
|
|
|
|
if (!enabled) {
|
|
|
|
// No WG hooks so we'll always bypass this check.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
World world = loc.getWorld(); Vector pt = toVector(loc);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 21:49:42 +02:00
|
|
|
if (wg.getRegionManager(world).getApplicableRegions(pt).size() > 0) { return wg.canBuild(player, loc); }
|
2014-04-04 20:55:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (!enabled) {
|
|
|
|
// No WG hooks so we'll always bypass this check.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-01 21:52:40 +02:00
|
|
|
World world = loc.getWorld(); Chunk chunk = world.getChunkAt(loc); int minChunkX = chunk.getX() << 4;
|
|
|
|
int minChunkZ = chunk.getZ() << 4; int maxChunkX = minChunkX + 15; int maxChunkZ = minChunkZ + 15;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
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<String, ProtectedRegion> allregions = regionManager.getRegions();
|
|
|
|
List<ProtectedRegion> allregionslist = new ArrayList<ProtectedRegion>(allregions.values());
|
2014-07-01 21:52:40 +02:00
|
|
|
List<ProtectedRegion> overlaps; boolean foundregions = false;
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
try {
|
2014-07-01 21:52:40 +02:00
|
|
|
overlaps = region.getIntersectingRegions(allregionslist); if (overlaps == null || overlaps.isEmpty()) {
|
2014-04-04 20:55:21 +02:00
|
|
|
foundregions = false;
|
2014-07-01 21:52:40 +02:00
|
|
|
} else {
|
2014-04-04 20:55:21 +02:00
|
|
|
foundregions = true;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundregions;
|
|
|
|
}
|
2011-08-29 05:50:45 +02:00
|
|
|
}
|