Saber-Factions/src/main/java/com/massivecraft/factions/integration/Worldguard.java

152 lines
4.0 KiB
Java
Raw Normal View History

package com.massivecraft.factions.integration;
2011-08-29 05:50:45 +02:00
import com.massivecraft.factions.P;
2011-08-29 05:50:45 +02:00
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.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import static com.sk89q.worldguard.bukkit.BukkitUtil.*;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
2011-08-31 03:14:46 +02:00
2011-08-29 05:50:45 +02:00
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.BlockVector;
import org.bukkit.World;
import org.bukkit.Chunk;
import org.bukkit.Location;
2011-08-29 05:50:45 +02:00
import org.bukkit.plugin.Plugin;
import org.bukkit.entity.Player;
/*
* Worldguard Region Checking
* Author: Spathizilla
*/
public class Worldguard
{
2011-08-29 05:50:45 +02:00
private static WorldGuardPlugin wg;
private static boolean enabled = false;
2011-08-29 05:59:49 +02:00
public static void init(Plugin plugin)
{
2011-08-29 05:50:45 +02:00
Plugin wgplug = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
if (wgplug == null || !(wgplug instanceof WorldGuardPlugin))
{
2011-08-29 05:50:45 +02:00
enabled = false;
wg = null;
P.p.log("Could not hook to WorldGuard. WorldGuard checks are disabled.");
}
else
{
2011-08-29 05:59:49 +02:00
wg = (WorldGuardPlugin) wgplug;
enabled = true;
P.p.log("Successfully hooked to WorldGuard.");
2011-08-29 05:59:49 +02:00
}
2011-08-29 05:50:45 +02:00
}
public static boolean isEnabled()
{
2011-08-29 05:50:45 +02:00
return enabled;
}
// PVP Flag check
// Returns:
// True: PVP is allowed
// False: PVP is disallowed
public static boolean isPVP(Player player)
{
if( ! enabled)
{
2011-08-29 05:50:45 +02:00
// No WG hooks so we'll always bypass this check.
return true;
}
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);
2011-08-29 05:50:45 +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;
}
World world = loc.getWorld();
Vector pt = toVector(loc);
if (wg.getRegionManager(world).getApplicableRegions(pt).size() > 0)
return wg.canBuild(player, loc);
return false;
}
2011-08-29 05:50:45 +02:00
// 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;
}
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;
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());
List<ProtectedRegion> overlaps;
boolean foundregions = false;
try
{
overlaps = region.getIntersectingRegions(allregionslist);
if(overlaps == null || overlaps.isEmpty())
{
foundregions = false;
}
else
{
foundregions = true;
2011-08-29 05:50:45 +02:00
}
}
catch (Exception e)
{
e.printStackTrace();
}
2011-08-29 05:50:45 +02:00
region = null;
allregionslist = null;
overlaps = null;
2011-08-29 05:59:49 +02:00
return foundregions;
2011-08-29 05:50:45 +02:00
}
}