From 3a653c2deadb4064c81f3a67c5bc675503836ca0 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Tue, 30 Sep 2014 22:08:38 +1000 Subject: [PATCH] worldguard fixes --- .../plot/commands/Merge.java | 10 --- .../plot/listeners/WorldGuardListener.java | 83 +++++++++++++++++-- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/PlotSquared/src/com/intellectualcrafters/plot/commands/Merge.java b/PlotSquared/src/com/intellectualcrafters/plot/commands/Merge.java index 028d5a724..2feac7cf5 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/commands/Merge.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/commands/Merge.java @@ -35,13 +35,6 @@ public class Merge extends SubCommand { public static String direction(float yaw) { yaw = yaw / 90; - // yaw = (float)Math.round(yaw); - /* - * if (yaw == -4 || yaw == 0 || yaw == 4) {return "SOUTH";} - * if (yaw == -1 || yaw == 3) {return "EAST";} - * if (yaw == -2 || yaw == 2) {return "NORTH";} - * if (yaw == -3 || yaw == 1) {return "WEST";} - */ int i = Math.round(yaw); switch(i) { case -4: @@ -60,9 +53,6 @@ public class Merge extends SubCommand { default: return ""; } - ///////////////////////////////////////////////////////////////////////////////////////// - // Had to... I'm sorry xD But it looks soo much better, and should be equally as fast. // - ///////////////////////////////////////////////////////////////////////////////////////// } @Override diff --git a/PlotSquared/src/com/intellectualcrafters/plot/listeners/WorldGuardListener.java b/PlotSquared/src/com/intellectualcrafters/plot/listeners/WorldGuardListener.java index f4604b6d0..6e5bb975c 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/listeners/WorldGuardListener.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/listeners/WorldGuardListener.java @@ -1,39 +1,106 @@ package com.intellectualcrafters.plot.listeners; +import java.util.ArrayList; +import java.util.Map; +import java.util.UUID; + import com.intellectualcrafters.plot.Plot; import com.intellectualcrafters.plot.PlotHelper; import com.intellectualcrafters.plot.PlotId; import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.events.PlayerClaimPlotEvent; import com.intellectualcrafters.plot.events.PlotDeleteEvent; +import com.intellectualcrafters.plot.events.PlotMergeEvent; +import com.intellectualcrafters.plot.events.PlotUnlinkEvent; import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.domains.DefaultDomain; +import com.sk89q.worldguard.protection.flags.Flag; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; /** * Created by Citymonstret on 2014-09-24. */ public class WorldGuardListener implements Listener { - - /* - * TODO recreate WG region when a plot is merged - * - It should use the region of the main plot, and delete the other ones. - */ - - private PlotMain plugin; - public WorldGuardListener(PlotMain plugin) { plugin.getServer().getPluginManager().registerEvents(this, plugin); } + @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true) + public void onMerge(PlotMergeEvent event) { + Plot main = event.getPlot(); + ArrayList plots = event.getPlots(); + World world = event.getWorld(); + RegionManager manager = PlotMain.worldGuard.getRegionManager(world); + for (PlotId plot:plots) { + if (!plot.equals(main)) + manager.removeRegion(plot.x + "-" + plot.y); + } + ProtectedRegion region = manager.getRegion(main.id.x + "-" + main.id.y); + DefaultDomain owner = region.getOwners(); + Map, Object> flags = region.getFlags(); + DefaultDomain members = region.getMembers(); + manager.removeRegion(main.id.x + "-" + main.id.y); + + Location location1 = PlotHelper.getPlotBottomLocAbs(world, plots.get(0)); + Location location2 = PlotHelper.getPlotTopLocAbs(world, plots.get(plots.size()-1)); + + BlockVector vector1 = new BlockVector(location1.getBlockX(), 1, location1.getBlockZ()); + BlockVector vector2 = new BlockVector(location2.getBlockX(), world.getMaxHeight(), location2.getBlockZ()); + ProtectedRegion rg = new ProtectedCuboidRegion(main.id.x + "-" + main.id.y, vector1, vector2); + + rg.setFlags(flags); + + rg.setOwners(owner); + + rg.setMembers(members); + + manager.addRegion(rg); + } + + @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true) + public void onUnlink(PlotUnlinkEvent event) { + World w = event.getWorld(); + ArrayList plots = event.getPlots(); + Plot main = PlotMain.getPlots(w).get(plots.get(0)); + + RegionManager manager = PlotMain.worldGuard.getRegionManager(w); + ProtectedRegion region = manager.getRegion(main.id.x + "-" + main.id.y); + + DefaultDomain owner = region.getOwners(); + Map, Object> flags = region.getFlags(); + DefaultDomain members = region.getMembers(); + + manager.removeRegion(main.id.x + "-" + main.id.y); + for (int i = 1;i