From 4cde6a53ba690420305b29dae0f3b440abd4441b Mon Sep 17 00:00:00 2001 From: StevenLawson Date: Wed, 4 Sep 2013 15:17:22 -0400 Subject: [PATCH] Started work on WorldEdit protected area support. --- .../Listener/TFM_PlayerListener.java | 6 +- .../TotalFreedomMod/TFM_ProtectedArea.java | 62 +++++++++++++++++++ .../TotalFreedomMod/TFM_WorldEditBridge.java | 31 ++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index 30a429ab..15c89da7 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map.Entry; import java.util.regex.Pattern; import me.StevenLawson.TotalFreedomMod.*; -import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager.EntryType; import me.StevenLawson.TotalFreedomMod.TFM_RollbackManager.RollbackEntry; import org.apache.commons.lang3.StringUtils; import org.bukkit.Bukkit; @@ -263,6 +262,11 @@ public class TFM_PlayerListener implements Listener break; } } + + if (event.getMaterial() == Material.WOOD_AXE) + { + TFM_WorldEditBridge.getInstance().validateSelection(player); + } } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java b/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java index bac1e17a..e82295b2 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_ProtectedArea.java @@ -12,6 +12,7 @@ import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.util.Vector; public class TFM_ProtectedArea implements Serializable { @@ -49,6 +50,67 @@ public class TFM_ProtectedArea implements Serializable return false; } + public static boolean isInProtectedArea(Vector min, Vector max, String worldName) + { + for (Map.Entry protectedArea : TFM_ProtectedArea.protectedAreas.entrySet()) + { + Location protectedAreaCenter = SerializableLocation.returnLocation(protectedArea.getValue().center); + if (protectedAreaCenter != null) + { + if (worldName.equals(protectedAreaCenter.getWorld().getName())) + { + double sphereRadius = protectedArea.getValue().radius; + Vector sphereCenter = protectedAreaCenter.toVector(); + if (cubeIntersectsSphere(min, max, sphereCenter, sphereRadius)) + { + return true; + } + } + } + } + + return false; + } + // + + private static boolean cubeIntersectsSphere(Vector min, Vector max, Vector sphere, double radius) + { + double d = square(radius); + + if (sphere.getX() < min.getX()) + { + d -= square(sphere.getX() - min.getX()); + } + else if (sphere.getX() > max.getX()) + { + d -= square(sphere.getX() - max.getX()); + } + if (sphere.getY() < min.getY()) + { + d -= square(sphere.getY() - min.getY()); + } + else if (sphere.getY() > max.getY()) + { + d -= square(sphere.getY() - max.getY()); + } + if (sphere.getZ() < min.getZ()) + { + d -= square(sphere.getZ() - min.getZ()); + } + else if (sphere.getZ() > max.getZ()) + { + d -= square(sphere.getZ() - max.getZ()); + } + + return d > 0; + } + + private static double square(double v) + { + return v * v; + } + + // public static void addProtectedArea(String label, Location location, double radius) { TFM_ProtectedArea.protectedAreas.put(label.toLowerCase(), new TFM_ProtectedArea(location, radius)); diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_WorldEditBridge.java b/src/me/StevenLawson/TotalFreedomMod/TFM_WorldEditBridge.java index c0ca3ba5..da9ec659 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_WorldEditBridge.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_WorldEditBridge.java @@ -1,8 +1,10 @@ package me.StevenLawson.TotalFreedomMod; import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.LocalWorld; import com.sk89q.worldedit.bukkit.BukkitPlayer; import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.regions.Region; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -111,6 +113,35 @@ public class TFM_WorldEditBridge } } + public void validateSelection(Player player) + { + try + { + LocalSession session = getPlayerSession(player); + if (session != null) + { + LocalWorld selectionWorld = session.getSelectionWorld(); + Region selection = session.getSelection(selectionWorld); + if (TFM_ProtectedArea.isInProtectedArea( + getBukkitVector(selection.getMinimumPoint()), + getBukkitVector(selection.getMaximumPoint()), + selectionWorld.getName())) + { + TFM_Util.bcastMsg("(DEBUG MSG: " + player.getName() + " has selected part of a protected area."); + } + } + } + catch (Exception ex) + { + TFM_Log.severe(ex); + } + } + + private static org.bukkit.util.Vector getBukkitVector(com.sk89q.worldedit.Vector worldEditVector) + { + return new org.bukkit.util.Vector(worldEditVector.getX(), worldEditVector.getY(), worldEditVector.getZ()); + } + public static TFM_WorldEditBridge getInstance() { return TFM_WorldEditBridgeHolder.INSTANCE;