From ca5d3a818b701628b96efe6a5256d6f27755d487 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 4 Jun 2016 06:20:13 +1000 Subject: [PATCH] PlotBlock cache / events / flags --- .../bukkit/listeners/ForceFieldListener.java | 30 +++----- .../bukkit/listeners/PlayerEvents.java | 15 ++-- .../bukkit/listeners/PlotPlusListener.java | 6 +- .../bukkit/object/BukkitLazyBlock.java | 2 +- .../bukkit/util/BukkitChunkManager.java | 2 +- .../plotsquared/bukkit/util/BukkitUtil.java | 6 +- .../bukkit/util/block/SlowChunk.java | 2 +- .../plot/commands/FlagCmd.java | 2 +- .../plot/commands/Music.java | 2 +- .../intellectualcrafters/plot/flag/Flag.java | 15 ++++ .../plot/flag/FlagManager.java | 64 ++++++++-------- .../intellectualcrafters/plot/flag/Flags.java | 76 ++++++++++--------- .../plot/flag/IntegerFlag.java | 17 +++++ .../plot/flag/PlotBlockListFlag.java | 2 +- .../plot/generator/AugmentedUtils.java | 2 +- .../plot/generator/ClassicPlotManager.java | 18 ++--- .../plot/generator/ClassicPlotWorld.java | 12 +-- .../plot/generator/HybridPlotManager.java | 6 +- .../plot/generator/HybridPlotWorld.java | 4 +- .../plot/generator/HybridUtils.java | 2 +- .../plot/object/PlotBlock.java | 12 +++ .../plot/util/SchematicHandler.java | 2 +- .../listener/ProcessedWEExtent.java | 2 +- .../intellectualcrafters/plot/FlagTest.java | 2 +- .../plotsquared/sponge/util/SpongeUtil.java | 4 +- .../sponge/util/block/SlowChunk.java | 2 +- 26 files changed, 171 insertions(+), 138 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/ForceFieldListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/ForceFieldListener.java index 65e8e2f0f..881c444ae 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/ForceFieldListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/ForceFieldListener.java @@ -1,26 +1,22 @@ package com.plotsquared.bukkit.listeners; -import com.google.common.base.Optional; import com.intellectualcrafters.plot.flag.Flags; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotPlayer; import com.plotsquared.bukkit.object.BukkitPlayer; import com.plotsquared.bukkit.util.BukkitUtil; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.util.Vector; - import java.util.HashSet; import java.util.Set; import java.util.UUID; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.util.Vector; public class ForceFieldListener implements Listener { - private Set getNearbyPlayers(Player player, Plot plot) { + private static Set getNearbyPlayers(Player player, Plot plot) { Set players = new HashSet<>(); for (Entity entity : player.getNearbyEntities(5d, 5d, 5d)) { PlotPlayer plotPlayer; @@ -34,7 +30,7 @@ public class ForceFieldListener implements Listener { return players; } - private PlotPlayer hasNearbyPermitted(Player player, Plot plot) { + private static PlotPlayer hasNearbyPermitted(Player player, Plot plot) { for (Entity entity : player.getNearbyEntities(5d, 5d, 5d)) { if (!(entity instanceof Player)) { continue; @@ -53,7 +49,7 @@ public class ForceFieldListener implements Listener { return null; } - private Vector calculateVelocity(PlotPlayer player, PlotPlayer e) { + private static Vector calculateVelocity(PlotPlayer player, PlotPlayer e) { Location playerLocation = player.getLocationFull(); Location oPlayerLocation = e.getLocation(); double playerX = playerLocation.getX(); @@ -83,16 +79,8 @@ public class ForceFieldListener implements Listener { return new Vector(x, y, z); } - @EventHandler - public void onPlotEntry(PlayerMoveEvent event) { - Player player = event.getPlayer(); - PlotPlayer plotPlayer = BukkitUtil.getPlayer(player); - Plot plot = plotPlayer.getCurrentPlot(); - if (plot == null) { - return; - } - Optional forcefield = plot.getFlag(Flags.FORCEFIELD); - if (forcefield.isPresent() && forcefield.get()) { + public static void handleForcefield(Player player, PlotPlayer plotPlayer, Plot plot) { + if (Flags.FORCEFIELD.isTrue(plot)) { UUID uuid = plotPlayer.getUUID(); if (plot.isAdded(uuid)) { Set players = getNearbyPlayers(player, plot); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java index f0c05b0f9..631a65b20 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java @@ -498,6 +498,7 @@ public class PlayerEvents extends PlotListener implements Listener { return; } } else if (now.equals(lastPlot)) { + ForceFieldListener.handleForcefield(player, pp, now); return; } else if (!plotEntry(pp, now)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_ENTRY_DENIED); @@ -548,6 +549,7 @@ public class PlayerEvents extends PlotListener implements Listener { return; } } else if (now.equals(lastPlot)) { + ForceFieldListener.handleForcefield(player, pp, now); return; } else if (!plotEntry(pp, now)) { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_ENTRY_DENIED); @@ -637,7 +639,7 @@ public class PlayerEvents extends PlotListener implements Listener { Optional> destroy = plot.getFlag(Flags.BREAK); Block block = event.getBlock(); if (destroy.isPresent() && destroy.get() - .contains(new PlotBlock((short) block.getTypeId(), block.getData()))) { + .contains(PlotBlock.get((short) block.getTypeId(), block.getData()))) { return; } if (Permissions.hasPermission(plotPlayer, C.PERMISSION_ADMIN_DESTROY_OTHER)) { @@ -834,7 +836,7 @@ public class PlayerEvents extends PlotListener implements Listener { if (!plot.isAdded(plotPlayer.getUUID())) { Optional> destroy = plot.getFlag(Flags.BREAK); Block block = event.getBlock(); - if (destroy.isPresent() && destroy.get().contains(new PlotBlock((short) block.getTypeId(), block.getData())) || Permissions + if (destroy.isPresent() && destroy.get().contains(PlotBlock.get((short) block.getTypeId(), block.getData())) || Permissions .hasPermission(plotPlayer, C.PERMISSION_ADMIN_DESTROY_OTHER)) { return; } @@ -1189,7 +1191,7 @@ public class PlayerEvents extends PlotListener implements Listener { break; } Material handType = hand.getType(); - lb = new BukkitLazyBlock(new PlotBlock((short) handType.getId(), (byte) 0)); + lb = new BukkitLazyBlock(PlotBlock.get((short) handType.getId(), (byte) 0)); switch (handType) { case MONSTER_EGG: case MONSTER_EGGS: @@ -1787,8 +1789,7 @@ public class PlayerEvents extends PlotListener implements Listener { MainUtil.sendMessage(pp, C.NO_PERMISSION_EVENT, C.PERMISSION_ADMIN_BUILD_UNOWNED); event.setCancelled(true); } else if (!plot.isAdded(pp.getUUID())) { - Optional> use = plot.getFlag(Flags.USE); - if (use.isPresent() && use.get().contains(new PlotBlock((short) event.getBucket().getId(), (byte) 0))) { + if (Flags.USE.contains(plot, PlotBlock.get(event.getBucket().getId(), 0))) { return; } if (Permissions.hasPermission(pp, C.PERMISSION_ADMIN_BUILD_OTHER)) { @@ -1858,7 +1859,7 @@ public class PlayerEvents extends PlotListener implements Listener { } else if (!plot.isAdded(plotPlayer.getUUID())) { Optional> use = plot.getFlag(Flags.USE); Block block = event.getBlockClicked(); - if (use.isPresent() && use.get().contains(new PlotBlock((short) block.getTypeId(), block.getData()))) { + if (use.isPresent() && use.get().contains(PlotBlock.get(block.getTypeId(), block.getData()))) { return; } if (Permissions.hasPermission(plotPlayer, C.PERMISSION_ADMIN_BUILD_OTHER)) { @@ -2292,7 +2293,7 @@ public class PlayerEvents extends PlotListener implements Listener { Set place = plot.getFlag(Flags.PLACE, null); if (place != null) { Block block = event.getBlock(); - if (place.contains(new PlotBlock((short) block.getTypeId(), block.getData()))) { + if (place.contains(PlotBlock.get((short) block.getTypeId(), block.getData()))) { return; } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlotPlusListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlotPlusListener.java index 4e8ec310b..a6bfe059d 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlotPlusListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listeners/PlotPlusListener.java @@ -88,7 +88,7 @@ public class PlotPlusListener extends PlotListener implements Listener { if (plot == null) { return; } - if (plot.getFlag(Flags.INSTABREAK).or(false)) { + if (Flags.INSTABREAK.isTrue(plot)) { event.getBlock().breakNaturally(); } } @@ -103,7 +103,7 @@ public class PlotPlusListener extends PlotListener implements Listener { if (plot == null) { return; } - if (plot.getFlag(Flags.INVINCIBLE).or(false)) { + if (Flags.INVINCIBLE.isTrue(plot)) { event.setCancelled(true); } } @@ -117,7 +117,7 @@ public class PlotPlusListener extends PlotListener implements Listener { return; } UUID uuid = pp.getUUID(); - if (plot.isAdded(uuid) && plot.getFlag(Flags.DROP_PROTECTION).or(false)) { + if (plot.isAdded(uuid) && Flags.DROP_PROTECTION.isTrue(plot)) { event.setCancelled(true); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitLazyBlock.java b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitLazyBlock.java index 6f4fa0cfc..7a26d21a5 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitLazyBlock.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/object/BukkitLazyBlock.java @@ -122,7 +122,7 @@ public class BukkitLazyBlock extends LazyBlock { data = this.block.getData(); break; } - this.pb = new PlotBlock((short) this.id, data); + this.pb = PlotBlock.get((short) this.id, data); return this.pb; } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java index f5496fedb..d4e71f680 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitChunkManager.java @@ -1066,7 +1066,7 @@ public class BukkitChunkManager extends ChunkManager { if (typeId == 0) { ids[y] = PlotBlock.EVERYTHING; } else { - ids[y] = new PlotBlock((short) typeId, block.getData()); + ids[y] = PlotBlock.get((short) typeId, block.getData()); } } if (!id.equals(Material.AIR)) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java index 1627b1c25..884103a25 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java @@ -270,7 +270,7 @@ public class BukkitUtil extends WorldUtil { public StringComparison.ComparisonResult getClosestBlock(String name) { try { Material material = Material.valueOf(name.toUpperCase()); - return new StringComparison().new ComparisonResult(0, new PlotBlock((short) material.getId(), (byte) 0)); + return new StringComparison().new ComparisonResult(0, PlotBlock.get((short) material.getId(), (byte) 0)); } catch (IllegalArgumentException ignored) {} try { byte data; @@ -291,7 +291,7 @@ public class BukkitUtil extends WorldUtil { match = comparison.match; id = (short) comparison.best.getId(); } - PlotBlock block = new PlotBlock(id, data); + PlotBlock block = PlotBlock.get(id, data); StringComparison outer = new StringComparison<>(); return outer.new ComparisonResult(match, block); @@ -317,7 +317,7 @@ public class BukkitUtil extends WorldUtil { if (block == null) { return PlotBlock.EVERYTHING; } - return new PlotBlock((short) block.getTypeId(), block.getData()); + return PlotBlock.get((short) block.getTypeId(), block.getData()); } @Override diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/block/SlowChunk.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/block/SlowChunk.java index d31262fbb..a87b4cdea 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/block/SlowChunk.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/block/SlowChunk.java @@ -35,7 +35,7 @@ public class SlowChunk extends PlotChunk { if (this.result[y >> 4] == null) { this.result[y >> 4] = new PlotBlock[4096]; } - this.result[MainUtil.CACHE_I[y][x][z]][MainUtil.CACHE_J[y][x][z]] = new PlotBlock((short) id, data); + this.result[MainUtil.CACHE_I[y][x][z]][MainUtil.CACHE_J[y][x][z]] = PlotBlock.get((short) id, data); } @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java index 8a187c314..9fcaf65f6 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/FlagCmd.java @@ -62,7 +62,7 @@ public class FlagCmd extends SubCommand { Flag flag = null; if (args.length > 1) { flag = FlagManager.getFlag(args[1]); - if (flag == null || FlagManager.isReserved(flag)) { + if (flag == null || flag.isReserved()) { MainUtil.sendMessage(player, C.NOT_VALID_FLAG); return false; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Music.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Music.java index 76bdea67e..5f3a12dc9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Music.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Music.java @@ -49,7 +49,7 @@ public class Music extends SubCommand { }; int index = 0; for (int i = 2256; i < 2268; i++) { - String name = "&r&6" + WorldUtil.IMP.getClosestMatchingName(new PlotBlock((short) i, (byte) 0)); + String name = "&r&6" + WorldUtil.IMP.getClosestMatchingName(PlotBlock.get((short) i, (byte) 0)); String[] lore = {"&r&aClick to play!"}; PlotItemStack item = new PlotItemStack(i, (byte) 0, 1, name, lore); inv.setItem(index, item); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java index ac1827bd4..b38ed170c 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flag.java @@ -5,6 +5,7 @@ import com.intellectualcrafters.plot.object.Plot; public abstract class Flag { private final String name; + private boolean reserved = false; /** * Flag object used to store basic information for a Plot. Flags are a @@ -17,6 +18,20 @@ public abstract class Flag { this.name = name; } + public Flag reserve() { + reserved = true; + return this; + } + + public boolean isReserved() { + return reserved; + } + + public Flag unreserve() { + reserved = false; + return this; + } + public abstract String valueToString(Object value); @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java index b75222d46..89393b2b9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java @@ -1,7 +1,6 @@ package com.intellectualcrafters.plot.flag; import com.google.common.base.Optional; -import com.google.common.collect.Sets; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.object.Plot; @@ -11,7 +10,6 @@ import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotSettings; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.Permissions; - import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; @@ -28,8 +26,6 @@ import java.util.Set; public class FlagManager { - private static final HashSet> reserved = Sets.newHashSet(Flags.ANALYSIS, Flags.DONE); - /** * Some events can be called millions of times each second (e.g. physics) and reusing is a lot faster. */ @@ -67,7 +63,11 @@ public class FlagManager { * @return false if the flag was already reserved, otherwise true */ public static boolean reserveFlag(Flag flag) { - return reserved.add(flag); + if (flag.isReserved()) { + return false; + } + flag.reserve(); + return true; } /** @@ -76,7 +76,7 @@ public class FlagManager { * @return true if the flag is reserved, false otherwise */ public static boolean isReserved(Flag flag) { - return reserved.contains(flag); + return flag.isReserved(); } /** @@ -84,7 +84,13 @@ public class FlagManager { * @return a set of reserved flags */ public static Set> getReservedFlags() { - return Collections.unmodifiableSet(reserved); + HashSet> reserved = new HashSet<>(); + for (Flag flag : Flags.getFlags()) { + if (flag.isReserved()) { + reserved.add(flag); + } + } + return reserved; } /** @@ -93,7 +99,11 @@ public class FlagManager { * @return true if the flag was unreserved */ public static boolean unreserveFlag(Flag flag) { - return reserved.remove(flag); + if (flag.isReserved()) { + flag.unreserve(); + return true; + } + return false; } public static String toString(HashMap, Object> flags) { @@ -175,22 +185,24 @@ public class FlagManager { } public static HashMap, Object> getPlotFlags(PlotArea area, PlotSettings settings, boolean ignorePluginflags) { - HashMap, Object> flags = new HashMap<>(); + HashMap, Object> flags = null; if (area != null && !area.DEFAULT_FLAGS.isEmpty()) { + flags = new HashMap<>(area.DEFAULT_FLAGS.size()); flags.putAll(area.DEFAULT_FLAGS); } if (ignorePluginflags) { + if (flags == null) { + flags = new HashMap<>(settings.flags.size()); + } for (Map.Entry, Object> flag : settings.flags.entrySet()) { - if (isReserved(flag.getKey())) { + if (flag.getKey().isReserved()) { continue; } flags.put(flag.getKey(), flag.getValue()); } - } else { - flags.putAll(settings.flags); + return flags; } - - return flags; + return settings.flags; } public static Map, Object> getSettingFlags(PlotArea area, PlotSettings settings) { @@ -288,29 +300,15 @@ public class FlagManager { * @return the {@code Flag} object defined by {@code string} */ public static Flag getFlag(String string) { - for (Flag flag : Flags.getFlags()) { - if (flag.getName().equalsIgnoreCase(string)) { - if (isReserved(flag)) { - return null; - } - return flag; - } - } - return null; + return Flags.getFlag(string); } public static Flag getFlag(String string, boolean ignoreReserved) { - for (Flag flag : Flags.getFlags()) { - if (flag.getName().equalsIgnoreCase(string)) { - if (!ignoreReserved) { - if (isReserved(flag)) { - return null; - } - } - return flag; - } + Flag flag = Flags.getFlag(string); + if (!ignoreReserved && flag != null && flag.isReserved()) { + return null; } - return null; + return flag; } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java index 467751b29..5a45b5e32 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/Flags.java @@ -1,17 +1,14 @@ package com.intellectualcrafters.plot.flag; -import com.google.common.collect.Sets; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.RunnableVal; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.MathMan; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.HashMap; public class Flags { @@ -115,46 +112,51 @@ public class Flags { }; public static final BooleanFlag SLEEP = new BooleanFlag("sleep"); - private static final HashSet> flags = Sets.newHashSet(MUSIC, DESCRIPTION, ANALYSIS, GREETING, FAREWELL, FEED, HEAL, - GAMEMODE, - DONE, - REDSTONE, - FLY, NOTIFY_LEAVE, NOTIFY_ENTER, TIME, WEATHER, KEEP, PRICE, EXPLOSION, GRASS_GROW, VINE_GROW, MYCEL_GROW, DISABLE_PHYSICS, SNOW_MELT, - ICE_MELT, - FIRE_SPREAD, BLOCK_BURN, BLOCK_IGNITION, SOIL_DRY, BLOCKED_CMDS, USE, BREAK, PLACE, DEVICE_INTERACT, VEHICLE_BREAK, VEHICLE_PLACE, - VEHICLE_USE, - HANGING_BREAK, HANGING_PLACE, HANGING_INTERACT, MISC_PLACE, MISC_BREAK, MISC_INTERACT, PLAYER_INTERACT, TAMED_ATTACK, TAMED_INTERACT, - ANIMAL_ATTACK, ANIMAL_INTERACT, HOSTILE_ATTACK, HOSTILE_INTERACT, MOB_PLACE, FORCEFIELD, INVINCIBLE, ITEM_DROP, INSTABREAK, - DROP_PROTECTION, PVP, - PVE, NO_WORLDEDIT, MISC_CAP, ENTITY_CAP, MOB_CAP, ANIMAL_CAP, HOSTILE_CAP, VEHICLE_CAP); + private static final HashMap> flags; + static { + flags = new HashMap<>(); + try { + for (Field field : Flags.class.getDeclaredFields()) { + if (!field.isAccessible()) { + field.setAccessible(true); + } + String fieldName = field.getName().replace("_","-").toLowerCase(); + Object fieldValue = field.get(null); + if (!(fieldValue instanceof Flag)) { + continue; + } + Flag flag = (Flag) fieldValue; + if (!flag.getName().equals(fieldName)) { + PS.debug(Flags.class + "Field doesn't match: " + fieldName + " != " + flag.getName()); + } + flags.put(flag.getName(), flag); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } /** * Get an immutable set of registered flags. * * @return a set of registered flags. */ - public static Set> getFlags() { - return Collections.unmodifiableSet(flags); + public static Collection> getFlags() { + return flags.values(); + } + + public static Flag getFlag(String flag) { + return flags.get(flag); } public static void registerFlag(final Flag flag) { - Iterator> iterator = flags.iterator(); - Flag duplicate = null; - while (iterator.hasNext()){ - duplicate = iterator.next(); - if (flag.getName().equalsIgnoreCase(duplicate.getName())) { - iterator.remove(); - flags.add(flag); - break; - } - } - final Flag dupFinal = duplicate; + final Flag duplicate = flags.put(flag.getName(), flag); PS.get().foreachPlotArea(new RunnableVal() { @Override public void run(PlotArea value) { - if (dupFinal != null) { + if (duplicate != null) { Object remove; - if (value.DEFAULT_FLAGS.containsKey(dupFinal)) { - remove = value.DEFAULT_FLAGS.remove(dupFinal); + if (value.DEFAULT_FLAGS.containsKey(duplicate)) { + remove = value.DEFAULT_FLAGS.remove(duplicate); if (!(remove instanceof String)) { //error message? maybe? return; @@ -166,10 +168,10 @@ public class Flags { }); PS.get().foreachPlotRaw(new RunnableVal() { @Override public void run(Plot value) { - if (dupFinal != null) { + if (duplicate != null) { Object remove = null; - if (value.getFlags().containsKey(dupFinal)) { - remove = value.getFlags().remove(dupFinal); + if (value.getFlags().containsKey(duplicate)) { + remove = value.getFlags().remove(duplicate); } if (!(remove instanceof String)) { //error message? maybe? diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/IntegerFlag.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/IntegerFlag.java index 0e06eef6d..56d5fc319 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/IntegerFlag.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/IntegerFlag.java @@ -1,5 +1,7 @@ package com.intellectualcrafters.plot.flag; +import com.intellectualcrafters.plot.object.Plot; + public class IntegerFlag extends Flag { public IntegerFlag(String name) { @@ -21,4 +23,19 @@ public class IntegerFlag extends Flag { return null; } } + + public boolean isEqual(Plot plot, int value) { + Integer existing = FlagManager.getPlotFlagRaw(plot, this); + return existing != null && existing == value; + } + + public boolean isMore(Plot plot, int value) { + Integer existing = FlagManager.getPlotFlagRaw(plot, this); + return existing != null && existing > value; + } + + public boolean isLess(Plot plot, int value) { + Integer existing = FlagManager.getPlotFlagRaw(plot, this); + return existing != null && existing < value; + } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/flag/PlotBlockListFlag.java b/Core/src/main/java/com/intellectualcrafters/plot/flag/PlotBlockListFlag.java index ad70aee3e..de26a16ff 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/flag/PlotBlockListFlag.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/flag/PlotBlockListFlag.java @@ -34,7 +34,7 @@ public class PlotBlockListFlag extends ListFlag> { data = -1; } short id = Short.parseShort(split[0]); - block = new PlotBlock(id, data); + block = PlotBlock.get(id, data); } catch (NumberFormatException ignored) { StringComparison.ComparisonResult str = WorldUtil.IMP.getClosestBlock(value); if (str == null || str.match > 1) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java index 019b383ee..21322cfa9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/AugmentedUtils.java @@ -106,7 +106,7 @@ public class AugmentedUtils { primaryMask = result; } PlotChunk secondaryMask; - PlotBlock air = new PlotBlock((short) 0, (byte) 0); + PlotBlock air = PlotBlock.get((short) 0, (byte) 0); if (area.TERRAIN == 2) { PlotManager manager = area.getPlotManager(); final boolean[][] canPlace = new boolean[16][16]; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotManager.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotManager.java index ad583ec92..8551f4fe4 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotManager.java @@ -272,9 +272,9 @@ public class ClassicPlotManager extends SquarePlotManager { int ez = pos2.getZ() + 2; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, Math.min(dpw.WALL_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz + 1), - new Location(plotArea.worldname, ex, 255, ez - 1), new PlotBlock((short) 0, (byte) 0)); + new Location(plotArea.worldname, ex, 255, ez - 1), PlotBlock.get((short) 0, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 0, sz + 1), - new Location(plotArea.worldname, ex, 0, ez - 1), new PlotBlock((short) 7, + new Location(plotArea.worldname, ex, 0, ez - 1), PlotBlock.get((short) 7, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz + 1), new Location(plotArea.worldname, sx, dpw.WALL_HEIGHT, ez - 1), dpw.WALL_FILLING); @@ -302,9 +302,9 @@ public class ClassicPlotManager extends SquarePlotManager { int ex = pos2.getX() + 2; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, Math.min(dpw.WALL_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz), - new Location(plotArea.worldname, ex - 1, 255, ez), new PlotBlock((short) 0, (byte) 0)); + new Location(plotArea.worldname, ex - 1, 255, ez), PlotBlock.get((short) 0, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 0, sz), - new Location(plotArea.worldname, ex - 1, 0, ez), new PlotBlock((short) 7, (byte) 0)); + new Location(plotArea.worldname, ex - 1, 0, ez), PlotBlock.get((short) 7, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz), new Location(plotArea.worldname, ex - 1, dpw.WALL_HEIGHT, sz), dpw.WALL_FILLING); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.WALL_HEIGHT + 1, sz), @@ -329,10 +329,10 @@ public class ClassicPlotManager extends SquarePlotManager { int sz = pos2.getZ() + 1; int ez = sz + dpw.ROAD_WIDTH - 1; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.ROAD_HEIGHT + 1, sz + 1), - new Location(plotArea.worldname, ex - 1, 255, ez - 1), new PlotBlock( + new Location(plotArea.worldname, ex - 1, 255, ez - 1), PlotBlock.get( (short) 0, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 0, sz + 1), - new Location(plotArea.worldname, ex - 1, 0, ez - 1), new PlotBlock((short) 7, (byte) 0)); + new Location(plotArea.worldname, ex - 1, 0, ez - 1), PlotBlock.get((short) 7, (byte) 0)); MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz + 1), new Location(plotArea.worldname, ex - 1, dpw.ROAD_HEIGHT, ez - 1), dpw.ROAD_BLOCK); return true; @@ -348,7 +348,7 @@ public class ClassicPlotManager extends SquarePlotManager { int sz = pos1.getZ() - 1; int ez = pos2.getZ() + 1; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, Math.min(dpw.PLOT_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz), - new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0)); + new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0)); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz + 1), new Location(plotArea.worldname, ex, dpw.PLOT_HEIGHT - 1, ez - 1), dpw.MAIN_BLOCK); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.PLOT_HEIGHT, sz + 1), @@ -366,7 +366,7 @@ public class ClassicPlotManager extends SquarePlotManager { int sx = pos1.getX() - 1; int ex = pos2.getX() + 1; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, Math.min(dpw.PLOT_HEIGHT, dpw.ROAD_HEIGHT) + 1, sz), - new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0)); + new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0)); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, 1, sz), new Location(plotArea.worldname, ex - 1, dpw.PLOT_HEIGHT - 1, ez), dpw.MAIN_BLOCK); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx + 1, dpw.PLOT_HEIGHT, sz), @@ -383,7 +383,7 @@ public class ClassicPlotManager extends SquarePlotManager { int sz = location.getZ() + 1; int ez = sz + dpw.ROAD_WIDTH - 1; MainUtil.setSimpleCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.ROAD_HEIGHT + 1, sz), - new Location(plotArea.worldname, ex, 255, ez), new PlotBlock((short) 0, (byte) 0)); + new Location(plotArea.worldname, ex, 255, ez), PlotBlock.get((short) 0, (byte) 0)); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, 1, sz), new Location(plotArea.worldname, ex, dpw.ROAD_HEIGHT - 1, ez), dpw.MAIN_BLOCK); MainUtil.setCuboidAsync(plotArea.worldname, new Location(plotArea.worldname, sx, dpw.ROAD_HEIGHT, sz), diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotWorld.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotWorld.java index bf0d77fce..da121d6aa 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotWorld.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/ClassicPlotWorld.java @@ -12,12 +12,12 @@ public abstract class ClassicPlotWorld extends SquarePlotWorld { public int ROAD_HEIGHT = 64; public int PLOT_HEIGHT = 64; public int WALL_HEIGHT = 64; - public PlotBlock[] MAIN_BLOCK = new PlotBlock[] { new PlotBlock((short) 1, (byte) 0) }; - public PlotBlock[] TOP_BLOCK = new PlotBlock[] { new PlotBlock((short) 2, (byte) 0) }; - public PlotBlock WALL_BLOCK = new PlotBlock((short) 44, (byte) 0); - public PlotBlock CLAIMED_WALL_BLOCK = new PlotBlock((short) 44, (byte) 1); - public PlotBlock WALL_FILLING = new PlotBlock((short) 1, (byte) 0); - public PlotBlock ROAD_BLOCK = new PlotBlock((short) 155, (byte) 0); + public PlotBlock[] MAIN_BLOCK = new PlotBlock[] { PlotBlock.get((short) 1, (byte) 0) }; + public PlotBlock[] TOP_BLOCK = new PlotBlock[] { PlotBlock.get((short) 2, (byte) 0) }; + public PlotBlock WALL_BLOCK = PlotBlock.get((short) 44, (byte) 0); + public PlotBlock CLAIMED_WALL_BLOCK = PlotBlock.get((short) 44, (byte) 1); + public PlotBlock WALL_FILLING = PlotBlock.get((short) 1, (byte) 0); + public PlotBlock ROAD_BLOCK = PlotBlock.get((short) 155, (byte) 0); public boolean PLOT_BEDROCK = true; public ClassicPlotWorld(String worldName, String id, IndependentPlotGenerator generator, PlotId min, PlotId max) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java index 7be624388..406524980 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -151,11 +151,11 @@ public class HybridPlotManager extends ClassicPlotManager { final PlotBlock[] filling = dpw.MAIN_BLOCK; final PlotBlock bedrock; if (dpw.PLOT_BEDROCK) { - bedrock = new PlotBlock((short) 7, (byte) 0); + bedrock = PlotBlock.get((short) 7, (byte) 0); } else { - bedrock = new PlotBlock((short) 0, (byte) 0); + bedrock = PlotBlock.get((short) 0, (byte) 0); } - final PlotBlock air = new PlotBlock((short) 0, (byte) 0); + final PlotBlock air = PlotBlock.get((short) 0, (byte) 0); final String biome = WorldUtil.IMP.getBiomeList()[dpw.PLOT_BIOME]; ChunkManager.chunkTask(pos1, pos2, new RunnableVal() { @Override diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java index 267e36750..513fee9a5 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java @@ -237,7 +237,7 @@ public class HybridPlotWorld extends ClassicPlotWorld { } this.ROAD_SCHEMATIC_ENABLED = true; // Do not populate road if using schematic population - this.ROAD_BLOCK = new PlotBlock(this.ROAD_BLOCK.id, (byte) 0); + this.ROAD_BLOCK = PlotBlock.get(this.ROAD_BLOCK.id, (byte) 0); short[] ids1 = schematic1.getIds(); byte[] datas1 = schematic1.getDatas(); @@ -300,6 +300,6 @@ public class HybridPlotWorld extends ClassicPlotWorld { existing = new HashMap<>(); this.G_SCH.put(pair, existing); } - existing.put((int) y, new PlotBlock(id, data)); + existing.put((int) y, PlotBlock.get(id, data)); } } diff --git a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridUtils.java b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridUtils.java index 965660ad7..ea004e5f8 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridUtils.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/generator/HybridUtils.java @@ -163,7 +163,7 @@ public abstract class HybridUtils { whenDone.value += checkModified(plot.getArea().worldname, bx, ex, cpw.PLOT_HEIGHT, cpw.PLOT_HEIGHT, bz, ez, cpw.TOP_BLOCK); whenDone.value += checkModified( plot.getArea().worldname, bx, ex, cpw.PLOT_HEIGHT + 1, 255, bz, ez, - new PlotBlock[]{new PlotBlock((short) 0, (byte) 0)}); + new PlotBlock[]{PlotBlock.get((short) 0, (byte) 0)}); } }, this, 5); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotBlock.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotBlock.java index 42700cd8d..48a2d1ce9 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotBlock.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotBlock.java @@ -3,6 +3,18 @@ package com.intellectualcrafters.plot.object; public class PlotBlock { public static final PlotBlock EVERYTHING = new PlotBlock((short) 0, (byte) 0); + private static final PlotBlock[] CACHE = new PlotBlock[65535]; + static { + for (int i = 0; i < 65535; i++) { + short id = (short) (i >> 4); + byte data = (byte) (i & 15); + CACHE[i] = new PlotBlock(id, data); + } + } + + public static PlotBlock get(int id, int data) { + return CACHE[(id << 4) + data]; + } public final short id; public final byte data; diff --git a/Core/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java b/Core/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java index 86bec79a8..736443ef2 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java @@ -325,7 +325,7 @@ public abstract class SchematicHandler { SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, id); break; default: - SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, new PlotBlock((short) id, datas[i])); + SetQueue.IMP.setBlock(plot.getArea().worldname, xx, yy, zz, PlotBlock.get((short) id, datas[i])); break; } } diff --git a/Core/src/main/java/com/plotsquared/listener/ProcessedWEExtent.java b/Core/src/main/java/com/plotsquared/listener/ProcessedWEExtent.java index 1f11b023f..c27f8c177 100644 --- a/Core/src/main/java/com/plotsquared/listener/ProcessedWEExtent.java +++ b/Core/src/main/java/com/plotsquared/listener/ProcessedWEExtent.java @@ -217,7 +217,7 @@ public class ProcessedWEExtent extends AbstractDelegateExtent { break; default: if (Settings.EXPERIMENTAL_FAST_ASYNC_WORLDEDIT) { - SetQueue.IMP.setBlock(this.world, x, y, z, new PlotBlock((short) id, (byte) block.getData())); + SetQueue.IMP.setBlock(this.world, x, y, z, PlotBlock.get((short) id, (byte) block.getData())); } else { super.setBlock(location, block); } diff --git a/Core/src/test/java/com/intellectualcrafters/plot/FlagTest.java b/Core/src/test/java/com/intellectualcrafters/plot/FlagTest.java index e1d2d1078..01405ba07 100644 --- a/Core/src/test/java/com/intellectualcrafters/plot/FlagTest.java +++ b/Core/src/test/java/com/intellectualcrafters/plot/FlagTest.java @@ -38,7 +38,7 @@ public class FlagTest { Optional flag = plot.getFlag(use); if (flag.isPresent()) { System.out.println(Flags.USE.valueToString(flag.get())); - testBlock = new PlotBlock((short) 1, (byte) 0); + testBlock = PlotBlock.get((short) 1, (byte) 0); flag.get().add(testBlock); } if (flag.isPresent()) { diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java index 482530170..76a2450b3 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java @@ -153,7 +153,7 @@ public class SpongeUtil extends WorldUtil { if (state.getType() == BlockTypes.AIR) { continue; } - PlotBlock plotBlock = new PlotBlock((short) (i & 0xFFF), (byte) (i >> 12 & 0xF)); + PlotBlock plotBlock = PlotBlock.get((short) (i & 0xFFF), (byte) (i >> 12 & 0xF)); stateArray[i] = state; stateMap.put(state, plotBlock); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) { @@ -301,7 +301,7 @@ public class SpongeUtil extends WorldUtil { match = comparison.match; id = SpongeUtil.getPlotBlock(comparison.best.getDefaultState()).id; } - PlotBlock block = new PlotBlock(id, data); + PlotBlock block = PlotBlock.get(id, data); StringComparison outer = new StringComparison(); return outer.new ComparisonResult(match, block); diff --git a/Sponge/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java b/Sponge/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java index f8e789407..4c5635d3e 100644 --- a/Sponge/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java +++ b/Sponge/src/main/java/com/plotsquared/sponge/util/block/SlowChunk.java @@ -39,7 +39,7 @@ public class SlowChunk extends PlotChunk { if (id == this.lastBlock.id && data == this.lastBlock.data) { this.result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = this.lastBlock; } else { - this.result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = new PlotBlock((short) id, data); + this.result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = PlotBlock.get((short) id, data); } }