From bda38309ca161087319dc4d649cbd667cd0fce3d Mon Sep 17 00:00:00 2001 From: sandtechnology <20417547+sandtechnology@users.noreply.github.com> Date: Sun, 1 Mar 2020 20:13:35 +0800 Subject: [PATCH] Cached chunk location to save getting time (~100ms) Former-commit-id: 41f7b385968d1e2cf1b494016a8ff9ed5ba5570e --- .../java/org/maxgamer/quickshop/Shop/ShopChunk.java | 9 +++++++++ .../org/maxgamer/quickshop/Shop/ShopLoader.java | 2 +- .../maxgamer/quickshop/Shop/VirtualDisplayItem.java | 13 ++++++++----- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopChunk.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopChunk.java index abe42805d..c84db3ada 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopChunk.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopChunk.java @@ -23,6 +23,7 @@ import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; +import org.bukkit.World; @Getter @EqualsAndHashCode @@ -32,4 +33,12 @@ public class ShopChunk { private String world; private int x; private int z; + + public boolean isSame(World world, int x, int z){ + return isSame(world.getName(),x,z); + } + + public boolean isSame(String world,int x,int z){ + return this.x==x&&this.z==z&&this.world.equals(world); + } } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java index cb1191bbd..85a204c38 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoader.java @@ -46,7 +46,7 @@ import org.maxgamer.quickshop.Util.Util; /** A class allow plugin load shops fast and simply. */ public class ShopLoader { private final ArrayList loadTimes = new ArrayList<>(); - Map costCache = new HashMap<>(); + private Map costCache = new HashMap<>(); private int errors; private int loadAfterChunkLoaded = 0; private int loadAfterWorldLoaded = 0; diff --git a/src/main/java/org/maxgamer/quickshop/Shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/VirtualDisplayItem.java index 9bd17de69..1c1867a77 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/VirtualDisplayItem.java @@ -62,6 +62,8 @@ public class VirtualDisplayItem extends DisplayItem { private PacketContainer fakeItemDestroyPacket; //packetListener private PacketAdapter packetAdapter; + //cache chunk x and z + private ShopChunk chunkLocation; //The List which store packet sender private Set packetSenders = new ConcurrentSkipListSet<>(); @@ -100,11 +102,12 @@ public class VirtualDisplayItem extends DisplayItem { int z = event.getPacket().getIntegers().read(1); //check later to prevent deadlock Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,()->{ - World world = shop.getLocation().getWorld(); - Chunk chunk = shop.getLocation().getChunk(); - if (world.getName().equals(event.getPlayer().getWorld().getName()) - && chunk.getX() == x - && chunk.getZ() == z) { + if(chunkLocation==null){ + World world = shop.getLocation().getWorld(); + Chunk chunk = shop.getLocation().getChunk(); + chunkLocation=new ShopChunk(world.getName(),chunk.getX(),chunk.getZ()); + } + if (chunkLocation.isSame(event.getPlayer().getWorld().getName(),x,z)) { packetSenders.add(event.getPlayer().getUniqueId()); sendFakeItem(event.getPlayer()); }