From 1d1834699a02fdd039cf2791c5d579b526c0b8ac Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 25 Sep 2014 22:44:23 +1000 Subject: [PATCH] Mega plots: Tell me if I missed any important API methods Modified methods: - getCurrentPlot - getPlotBottomLoc - getPlotTopLoc TODO: - Commands - Plot unlinking on unclaim - Anything else? --- .../plot/PlayerFunctions.java | 102 ++++++--------- .../plot/PlotCollection.java | 106 ---------------- .../intellectualcrafters/plot/PlotHelper.java | 12 +- .../intellectualcrafters/plot/PlotMain.java | 120 +++++++++--------- .../plot/PlotSettings.java | 6 +- .../plot/listeners/PlayerEvents.java | 3 +- 6 files changed, 118 insertions(+), 231 deletions(-) delete mode 100644 PlotSquared/src/com/intellectualcrafters/plot/PlotCollection.java diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlayerFunctions.java b/PlotSquared/src/com/intellectualcrafters/plot/PlayerFunctions.java index 26f076670..aa8df7c35 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlayerFunctions.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlayerFunctions.java @@ -55,7 +55,36 @@ public class PlayerFunctions { long cu = System.currentTimeMillis(); return (lp - cu) > 30l; } - + + public static Set getPlotSelectionIds(World world, PlotId pos1, PlotId pos2) { + Set myplots = new HashSet(); + for (int x = pos1.x; x <= pos2.x; x++) { + for (int y = pos1.y; x <= pos2.y; x++) { + myplots.add(new PlotId(x,y)); + } + } + + return myplots; + } + + public static Plot getBottomPlot(World world, Plot plot) { + if (plot.settings.getMerged(2)) { + return getBottomPlot(world, PlotMain.getPlots(world).get(new PlotId(plot.id.x, plot.id.y-1))); + } + if (plot.settings.getMerged(3)) { + return getBottomPlot(world, PlotMain.getPlots(world).get(new PlotId(plot.id.x-1, plot.id.y))); + } + return plot; + } + public static Plot getTopPlot(World world, Plot plot) { + if (plot.settings.getMerged(0)) { + return getBottomPlot(world, PlotMain.getPlots(world).get(new PlotId(plot.id.x, plot.id.y-1))); + } + if (plot.settings.getMerged(1)) { + return getBottomPlot(world, PlotMain.getPlots(world).get(new PlotId(plot.id.x-1, plot.id.y))); + } + return plot; + } /** * * @param loc @@ -65,7 +94,8 @@ public class PlayerFunctions { int x = loc.getBlockX(); int z = loc.getBlockZ(); - PlotWorld plotworld = PlotMain.getWorldSettings(loc.getWorld()); + String world = loc.getWorld().getName(); + PlotWorld plotworld = PlotMain.getWorldSettings(world); int size = plotworld.PLOT_WIDTH + plotworld.ROAD_WIDTH; int pathWidthLower; if ((plotworld.ROAD_WIDTH % 2) == 0) { @@ -93,75 +123,27 @@ public class PlayerFunctions { int end = pathWidthLower+plotworld.PLOT_WIDTH; if (rx<=pathWidthLower) { -// west > return null for now +// System.out.print("WEST"); return null; } if (rx>end) { -// east > return null for now +// System.out.print("EAST"); return null; } if (rz<=pathWidthLower) { -// north > return null for now +// System.out.print("NORTH"); return null; } if (rz>pathWidthLower+plotworld.PLOT_WIDTH) { -// south > return null for now +// System.out.print("SOUTH"); return null; } - return new PlotId(dx,dz); - -// -// double n3; -// -// int mod2 = 0; -// int mod1 = 1; -// -// int x = (int) Math.ceil((double) valx / size); -// int z = (int) Math.ceil((double) valz / size); -// -// if ((pathsize % 2) == 1) { -// n3 = Math.ceil(((double) pathsize) / 2); -// mod2 = -1; -// } else { -// n3 = Math.floor(((double) pathsize) / 2); -// } -// -// /* -// * If Road 1 + Road 2 are true, then it is in the middle between 4 plots and more checks might be required. -// */ -// boolean road1 = false, road2 = false; -// -// for (double i = n3; i >= 0; i--) { -// if (((((valx - i) + mod1) % size) == 0) || (((valx + i + mod2) % size) == 0)) { -// -// /* -// * Road 1 -// */ -// -// road1 = true; -// x = (int) Math.ceil((valx - n3) / size); -// } -// if (((((valz - i) + mod1) % size) == 0) || (((valz + i + mod2) % size) == 0)) { -// /* -// * Road 2 -// */ -// -// road2 = true; -// z = (int) Math.ceil((valz - n3) / size); -// } -// } -// if (road1 && road2) { -// return null; -// } -// else if (road1) { -// return null; -// } -// else if (road2) { -// return null; -// } -// else { -// return new PlotId(x, z); -// } + PlotId id = new PlotId(dx,dz); + Plot plot = PlotMain.getPlots(loc.getWorld()).get(id); + if (plot==null) { + return id; + } + return getBottomPlot(loc.getWorld(), plot).id; } /** diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotCollection.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotCollection.java deleted file mode 100644 index 2ea4b4eac..000000000 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotCollection.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.intellectualcrafters.plot; - -import java.util.Collection; -import java.util.Iterator; - -/** - * Created by Citymonstret on 2014-09-25. - */ -public class PlotCollection implements Collection { - - private Plot[] collection; - - public PlotCollection(int size) { - this.collection = new Plot[size]; - } - - @Override - public int size() { - return this.collection.length; - } - - @Override - public boolean isEmpty() { - return this.collection.length == 0; - } - - @Override - public boolean contains(Object o) { - if(!(o instanceof Plot)) { - throw new IllegalArgumentException("Object not instance of Plot.class"); - } - Plot p = (Plot) o; - for(Plot plot : this.collection) { - if(plot.getId().toString().equals(p.getId().toString())) { - return true; - } - } - return false; - } - - @Override - public Iterator iterator() { - return null; - } - - @Override - public Object[] toArray() { - return this.collection; - } - - @Override - public boolean add(Object o) { - Plot[] pre = this.collection; - this.collection = new Plot[pre.length + 1]; - for(int x = 0; x < pre.length; x++) { - this.collection[x] = pre[x]; - } - this.collection[collection.length - 1] = (Plot) o; - return true; - } - - @Override - public boolean remove(Object o) { - Plot[] pre = this.collection; - this.collection = new Plot[pre.length - 1]; - int x = 0; - for(Plot plot : pre) { - if(plot != (Plot) o) { - this.collection[x++] = plot; - } - } - return false; - } - - @Override - public boolean addAll(Collection c) { - return false; - } - - @Override - public void clear() { - - } - - @Override - public boolean retainAll(Collection c) { - return false; - } - - @Override - public boolean removeAll(Collection c) { - return false; - } - - @Override - public boolean containsAll(Collection c) { - return false; - } - - @Override - public T[] toArray(Object[] a) { - return new T[0]; - } - - public -} diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java index 86c817e88..a73bb11c3 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotHelper.java @@ -577,7 +577,7 @@ public class PlotHelper { } if ((pos2.getBlockX() - pos1.getBlockX()) < 16) { setSimpleCuboid(world, new Location(world, pos1.getBlockX(), 0, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), 1, pos2.getBlockZ()), (short) 7); - setSimpleCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), world.getMaxHeight(), pos2.getBlockZ()), (short) 0); + setSimpleCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT + 1, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), world.getMaxHeight(), pos2.getBlockZ()), (short) 0); setCuboid(world, new Location(world, pos1.getBlockX(), 1, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), plotworld.PLOT_HEIGHT, pos2.getBlockZ()), filling, filling_data); setCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), plotworld.PLOT_HEIGHT + 1, pos2.getBlockZ()), plotfloors, plotfloors_data); PlayerFunctions.sendMessage(requester, "&c(depreciated) &r" + C.CLEARING_DONE.s().replaceAll("%time%", "" + ((System.nanoTime() - start) / 1000000.0))); @@ -626,7 +626,7 @@ public class PlotHelper { } if (min == null) { setSimpleCuboid(world, new Location(world, pos1.getBlockX(), 0, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), 1, pos2.getBlockZ()), (short) 7); - setSimpleCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), world.getMaxHeight(), pos2.getBlockZ()), (short) 0); + setSimpleCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT + 1, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), world.getMaxHeight(), pos2.getBlockZ()), (short) 0); setCuboid(world, new Location(world, pos1.getBlockX(), 1, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), plotworld.PLOT_HEIGHT, pos2.getBlockZ()), filling, filling_data); setCuboid(world, new Location(world, pos1.getBlockX(), plotworld.PLOT_HEIGHT, pos1.getBlockZ()), new Location(world, pos2.getBlockX(), plotworld.PLOT_HEIGHT + 1, pos2.getBlockZ()), plotfloors, plotfloors_data); } else { @@ -782,6 +782,10 @@ public class PlotHelper { } public static Location getPlotTopLoc(World world, PlotId id) { + Plot plot = PlotMain.getPlots(world).get(id); + if (id!=null) { + id = PlayerFunctions.getTopPlot(world, plot).id; + } PlotWorld plotworld = PlotMain.getWorldSettings(world); int px = id.x; int pz = id.y; @@ -793,6 +797,10 @@ public class PlotHelper { } public static Location getPlotBottomLoc(World world, PlotId id) { + Plot plot = PlotMain.getPlots(world).get(id); + if (id!=null) { + id = PlayerFunctions.getBottomPlot(world, plot).id; + } PlotWorld plotworld = PlotMain.getWorldSettings(world); int px = id.x; int pz = id.y; diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java index 425046cf5..f17172ae2 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java @@ -188,7 +188,6 @@ public class PlotMain extends JavaPlugin { } return new HashMap(); } - /** * get all plot worlds */ @@ -713,7 +712,7 @@ public class PlotMain extends JavaPlugin { this.error = 0l; } for (String w : getPlotWorlds()) { - getWorldSettings(w); + PlotWorld plotworld = getWorldSettings(w); World world = Bukkit.getServer().getWorld(w); try { if (world.getLoadedChunks().length < 1) { @@ -723,69 +722,67 @@ public class PlotMain extends JavaPlugin { Entity[] entities = chunk.getEntities(); for (int i = entities.length - 1; i >= 0; i--) { Entity entity = entities[i]; - if (entity.getType() == EntityType.PLAYER) { - continue; - } - this.location = entity.getLocation(); - if (!PlayerEvents.isInPlot(this.location)) { - boolean tamed = false; - if (Settings.MOB_PATHFINDING) { - - // TODO make this more efficient - - if (entity instanceof Tameable) { - Tameable tameable = (Tameable) entity; - if (tameable.isTamed()) { - tamed = true; - } - } else if (entity instanceof LivingEntity) { - LivingEntity livingEntity = ((LivingEntity) entity); - if (livingEntity.getCustomName() != null) { - tamed = true; - } - } - if (tamed) { - boolean found = false; - int radius = 1; - int dir = 0; - int x = this.location.getBlockX(); - int y = this.location.getBlockY(); - int z = this.location.getBlockZ(); - while (!found || (radius > PlotWorld.ROAD_WIDTH_DEFAULT)) { - Location pos; - switch (dir) { - case 0: - pos = new Location(world, x + radius, y, z); - dir++; - break; - case 1: - pos = new Location(world, x, y, z + radius); - dir++; - break; - case 2: - pos = new Location(world, x - radius, y, z); - dir++; - break; - case 3: - pos = new Location(world, x, y, z - radius); - dir = 0; - radius++; - break; - default: - pos = this.location; - break; - + if (!(entity instanceof Player)) { + this.location = entity.getLocation(); + if (!PlayerEvents.isInPlot(this.location)) { + boolean tamed = false; + if (Settings.MOB_PATHFINDING) { + if (entity instanceof Tameable) { + Tameable tameable = (Tameable) entity; + if (tameable.isTamed()) { + tamed = true; } - if (PlayerEvents.isInPlot(pos)) { - entity.teleport(pos.add(0.5, 0, 0.5)); - found = true; - break; + } else if (entity instanceof LivingEntity) { + LivingEntity livingEntity = ((LivingEntity) entity); + if (livingEntity.getCustomName() != null) { + tamed = true; } } + if (tamed) { + boolean found = false; + int radius = 1; + int dir = 0; + int x = this.location.getBlockX(); + int y = this.location.getBlockY(); + int z = this.location.getBlockZ(); + while (!found || (radius > plotworld.ROAD_WIDTH)) { + Location pos; + switch (dir) { + case 0: + pos = new Location(world, x + radius, y, z); + dir++; + break; + case 1: + pos = new Location(world, x, y, z + radius); + dir++; + break; + case 2: + pos = new Location(world, x - radius, y, z); + dir++; + break; + case 3: + pos = new Location(world, x, y, z - radius); + dir = 0; + radius++; + break; + default: + pos = this.location; + break; + + } + if (PlayerEvents.isInPlot(pos)) { + entity.teleport(pos.add(0.5, 0, 0.5)); + found = true; + break; + } + } + // Welp! how did this entity get here? + entity.teleport(location.subtract(location.getDirection().normalize().multiply(2))); + } + } + if (!tamed) { + entity.remove(); } - } - if (!tamed) { - entity.remove(); } } } @@ -911,4 +908,5 @@ public class PlotMain extends JavaPlugin { plots.put(world, new HashMap()); } } + } diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotSettings.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotSettings.java index 2d6e347cb..6313e8746 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotSettings.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotSettings.java @@ -27,7 +27,7 @@ public class PlotSettings { /** * merged plots */ - private boolean[] merged = new boolean[] {false,false,false,false}; // 1111 + private boolean[] merged = new boolean[] {false,false,false,false}; /** * plot alias */ @@ -76,6 +76,10 @@ public class PlotSettings { return merged[direction]; } + public boolean isMerged() { + return (merged[0] || merged[1] || merged[2] || merged[3]); + } + public boolean[] getMerged() { return this.merged; } diff --git a/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 3c68920b6..f23e4a79b 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -65,6 +65,7 @@ import org.bukkit.event.world.StructureGrowEvent; import com.intellectualcrafters.plot.C; import com.intellectualcrafters.plot.PlayerFunctions; import com.intellectualcrafters.plot.Plot; +import com.intellectualcrafters.plot.PlotHelper; import com.intellectualcrafters.plot.PlotId; import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.PlotWorld; @@ -493,7 +494,7 @@ public class PlayerEvents implements Listener { if (!isPlotWorld(world)) { return; } - if ((isInPlot(event.getLocation()) && (event.getSpawnReason() != SpawnReason.SPAWNER_EGG)) || !isInPlot(event.getLocation())) { + if ((event.getSpawnReason() != SpawnReason.SPAWNER_EGG) || !isInPlot(event.getLocation())) { event.setCancelled(true); } }