From 08b298922e75fb61cc32db7cb69c1d281a6e5125 Mon Sep 17 00:00:00 2001 From: MineTheCube Date: Thu, 27 Jul 2017 01:02:21 +0200 Subject: [PATCH] Re-add teleport listener and few minor changes --- .../listeners/ShopUpdateListener.java | 41 +++++++++++++++++++ .../de/epiceric/shopchest/nms/Hologram.java | 23 +++++------ .../de/epiceric/shopchest/shop/ShopItem.java | 11 ++--- .../epiceric/shopchest/utils/ShopUpdater.java | 32 ++++++++++----- .../epiceric/shopchest/utils/ShopUtils.java | 18 ++++---- 5 files changed, 87 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index ec03010..5639d3f 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -1,10 +1,16 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.Callback; +import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.scheduler.BukkitRunnable; public class ShopUpdateListener implements Listener { @@ -14,6 +20,41 @@ public class ShopUpdateListener implements Listener { this.plugin = plugin; } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(PlayerTeleportEvent e) { + Location from = e.getFrom(); + Location to = e.getTo(); + final Player p = e.getPlayer(); + + // Wait till the chunk should have loaded on the client + // Update IF worlds are different OR chunks are different (as many teleports are in same chunk) + if (!from.getWorld().equals(to.getWorld()) + || from.getChunk().getX() != to.getChunk().getX() + || from.getChunk().getZ() != to.getChunk().getZ()) { + // Wait for 15 ticks before we actually put it in the queue + new BukkitRunnable() { + @Override + public void run() { + plugin.getUpdater().beforeNext(new Runnable() { + @Override + public void run() { + if (p.isOnline()) { + for (Shop shop : plugin.getShopUtils().getShops()) { + if (shop.getItem() != null) { + shop.getItem().setVisible(p, false); + } + if (shop.getHologram() != null) { + shop.getHologram().hidePlayer(p); + } + } + } + } + }); + } + }.runTaskLater(plugin, 15L); + } + } + @EventHandler public void onWorldLoad(WorldLoadEvent e) { final String worldName = e.getWorld().getName(); diff --git a/src/main/java/de/epiceric/shopchest/nms/Hologram.java b/src/main/java/de/epiceric/shopchest/nms/Hologram.java index 233a401..a4dd3c0 100644 --- a/src/main/java/de/epiceric/shopchest/nms/Hologram.java +++ b/src/main/java/de/epiceric/shopchest/nms/Hologram.java @@ -10,18 +10,20 @@ import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; public class Hologram { private static List holograms = new ArrayList<>(); + private final Set visibility = Collections.newSetFromMap(new ConcurrentHashMap()); + private final List wrappers = new ArrayList<>(); + private final Location location; + private final ShopChest plugin; + private final Config config; + private boolean exists = false; - private List wrappers = new ArrayList<>(); private ArmorStandWrapper interactArmorStandWrapper; - private Location location; - private Set visibility = new HashSet<>(); - private ShopChest plugin; - private Config config; public Hologram(ShopChest plugin, String[] lines, Location location) { this.plugin = plugin; @@ -254,7 +256,9 @@ public class Hologram { */ public static Hologram getHologram(ArmorStand armorStand) { for (Hologram hologram : holograms) { - if (hologram.contains(armorStand)) return hologram; + if (hologram.contains(armorStand)) { + return hologram; + } } return null; @@ -265,12 +269,7 @@ public class Hologram { * @return Whether the armor stand is part of a hologram */ public static boolean isPartOfHologram(ArmorStand armorStand) { - for (Hologram hologram : holograms) { - if (hologram.contains(armorStand)) { - return true; - } - } - return false; + return getHologram(armorStand) != null; } } diff --git a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java index a972b31..add75dc 100644 --- a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java +++ b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java @@ -10,16 +10,17 @@ import org.bukkit.inventory.ItemStack; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.util.HashSet; +import java.util.Collections; import java.util.Set; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public class ShopItem { - private ShopChest plugin; - private Set visibility = new HashSet<>(); - private ItemStack itemStack; - private Location location; + private final ShopChest plugin; + private final Set visibility = Collections.newSetFromMap(new ConcurrentHashMap()); + private final ItemStack itemStack; + private final Location location; private Object entityItem; private int entityId; diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java index 9461867..14e542a 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java @@ -5,6 +5,9 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + public class ShopUpdater { public enum UpdateQuality { @@ -28,8 +31,7 @@ public class ShopUpdater { } private final ShopChest plugin; - - private long interval = UpdateQuality.NORMAL.getInterval(); + private final Queue beforeNext = new ConcurrentLinkedQueue<>(); private volatile BukkitTask running; @@ -42,8 +44,8 @@ public class ShopUpdater { */ public void start() { if (!isRunning()) { - interval = plugin.getShopChestConfig().update_quality.getInterval(); - running = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new ShopUpdaterTask(plugin), interval, interval); + long interval = plugin.getShopChestConfig().update_quality.getInterval(); + running = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new ShopUpdaterTask(), interval, interval); } } @@ -72,16 +74,26 @@ public class ShopUpdater { return running != null; } - private static class ShopUpdaterTask implements Runnable { + /** + * Register a task to run before next loop + * + * @param runnable task to run + */ + public void beforeNext(Runnable runnable) { + beforeNext.add(runnable); + } - private final ShopChest plugin; - - private ShopUpdaterTask(ShopChest plugin) { - this.plugin = plugin; - } + private class ShopUpdaterTask implements Runnable { @Override public void run() { + if (!beforeNext.isEmpty()) { + for (Runnable runnable : beforeNext) { + runnable.run(); + } + beforeNext.clear(); + } + for (Player p : Bukkit.getOnlinePlayers()) { plugin.getShopUtils().updateShops(p); } diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 4e97196..029d091 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -13,16 +13,13 @@ import org.bukkit.inventory.InventoryHolder; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.util.Vector; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Set; +import java.util.*; public class ShopUtils { - private HashMap shopLocation = new HashMap<>(); - private HashMap playerLocation = new HashMap<>(); - private ShopChest plugin; + private final HashMap shopLocation = new HashMap<>(); + private final HashMap playerLocation = new HashMap<>(); + private final ShopChest plugin; public ShopUtils(ShopChest plugin) { this.plugin = plugin; @@ -55,8 +52,7 @@ public class ShopUtils { * @return Array of all Shops */ public Shop[] getShops() { - Collection shops = shopLocation.values(); - return shops.toArray(new Shop[shops.size()]); + return shopLocation.values().toArray(new Shop[0]); } /** @@ -274,7 +270,7 @@ public class ShopUtils { * @param force Whether update should be forced even if player has not moved */ public void updateShops(Player player, boolean force) { - if (!force && player.getLocation().equals(playerLocation.get(player))) { + if (!force && player.getLocation().equals(playerLocation.get(player.getUniqueId()))) { // Player has not moved, so don't calculate shops again. return; } @@ -314,7 +310,7 @@ public class ShopUtils { updateNearestShops(player); } - playerLocation.put(player, player.getLocation()); + playerLocation.put(player.getUniqueId(), player.getLocation()); } private Set getShopsInSight(Player player) {