Cached chunk location to save getting time (~100ms)

Former-commit-id: 41f7b385968d1e2cf1b494016a8ff9ed5ba5570e
This commit is contained in:
sandtechnology committed 2020-03-01 20:13:35 +08:00
1 parent 08516b0db8
commit bda38309ca
3 files changed
+18 -6

No files matched your search

@@ -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);
}
}
@@ -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<Long> loadTimes = new ArrayList<>();
Map<Timer, Double> costCache = new HashMap<>();
private Map<Timer, Double> costCache = new HashMap<>();
private int errors;
private int loadAfterChunkLoaded = 0;
private int loadAfterWorldLoaded = 0;
@@ -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<UUID> 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());
}