From c2b628141011b730588463b36d629fd1024dbb04 Mon Sep 17 00:00:00 2001 From: MineTheCube Date: Thu, 18 Aug 2016 02:14:59 +0200 Subject: [PATCH] Improve listeners performances This should slightly improve performances, especially with a lot of shops. Now shops are updated only on block changes and not every move event. It also uses distanceSquared instead of distance to avoid sqrt operations. --- .../listeners/HologramUpdateListener.java | 11 +++- .../shopchest/listeners/ShopItemListener.java | 59 +++++++++++-------- src/main/resources/config.yml | 2 +- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java index 3025627..5494144 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java @@ -20,8 +20,17 @@ public class HologramUpdateListener implements Listener { @EventHandler public void onPlayerMove(PlayerMoveEvent e) { + + if (e.getFrom().getBlockX() == e.getTo().getBlockX() + && e.getFrom().getBlockZ() == e.getTo().getBlockZ() + && e.getFrom().getBlockY() == e.getTo().getBlockY()) { + return; + } + Player p = e.getPlayer(); Location playerLocation = p.getLocation(); + double hologramDistanceSquared = plugin.getShopChestConfig().maximal_distance; + hologramDistanceSquared *= hologramDistanceSquared; for (Shop shop : plugin.getShopUtils().getShops()) { Block b = shop.getLocation().getBlock(); @@ -36,7 +45,7 @@ public class HologramUpdateListener implements Listener { Location shopLocation = shop.getLocation(); if (playerLocation.getWorld().equals(shopLocation.getWorld())) { - if (playerLocation.distance(shop.getHologram().getLocation()) <= plugin.getShopChestConfig().maximal_distance) { + if (playerLocation.distanceSquared(shop.getHologram().getLocation()) <= hologramDistanceSquared) { if (!shop.getHologram().isVisible(p)) { shop.getHologram().showPlayer(p); } diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java index 2213b5e..0e9f1dd 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java @@ -4,7 +4,9 @@ import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.ShopUtils; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -25,38 +27,26 @@ public class ShopItemListener implements Listener { this.shopUtils = plugin.getShopUtils(); } - @EventHandler + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPlayerMove(PlayerMoveEvent e) { - Player p = e.getPlayer(); - for (Shop shop : shopUtils.getShops()) { - if (shop.getLocation().distance(p.getLocation()) <= plugin.getShopChestConfig().maximal_item_distance) { - shop.getItem().setVisible(p, true); - } else { - shop.getItem().setVisible(p, false); - } + + if (e.getFrom().getBlockX() == e.getTo().getBlockX() + && e.getFrom().getBlockZ() == e.getTo().getBlockZ() + && e.getFrom().getBlockY() == e.getTo().getBlockY()) { + return; } + + updateShopVisibility(e.getPlayer(), true); } - @EventHandler + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPlayerTeleport(PlayerTeleportEvent e) { - Player p = e.getPlayer(); - for (Shop shop : shopUtils.getShops()) { - if (shop.getLocation().distance(e.getTo()) <= plugin.getShopChestConfig().maximal_item_distance) { - shop.getItem().setVisible(p, true); - } else { - shop.getItem().setVisible(p, false); - } - } + updateShopVisibility(e.getPlayer(), true, e.getTo()); } @EventHandler - public void onPlayerJoin(final PlayerJoinEvent e) { - Player p = e.getPlayer(); - for (Shop shop : shopUtils.getShops()) { - if (shop.getLocation().distance(p.getLocation()) <= plugin.getShopChestConfig().maximal_item_distance) { - shop.getItem().setVisible(p, true); - } - } + public void onPlayerJoin(PlayerJoinEvent e) { + updateShopVisibility(e.getPlayer(), false); } @EventHandler @@ -66,6 +56,27 @@ public class ShopItemListener implements Listener { } } + private void updateShopVisibility(Player p, boolean hideIfAway) { + updateShopVisibility(p, hideIfAway, p.getLocation()); + } + + private void updateShopVisibility(Player p, boolean hideIfAway, Location playerLocation) { + + double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance; + itemDistanceSquared *= itemDistanceSquared; + World w = p.getWorld(); + + for (Shop shop : shopUtils.getShops()) { + Location shopLocation = shop.getLocation(); + if (w.equals(shopLocation.getWorld()) && shopLocation.distanceSquared(playerLocation) <= itemDistanceSquared) { + shop.getItem().setVisible(p, true); + } else if (hideIfAway) { + shop.getItem().setVisible(p, false); + } + } + + } + @EventHandler(priority = EventPriority.HIGH) public void onBlockPlace(BlockPlaceEvent e) { Block b = e.getBlockPlaced(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a24c239..8bf986e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -55,7 +55,7 @@ append-potion-level-to-item-name: false remove-shop-on-error: false # Set the maximal distance (in blocks) to the shop where the player can see the hologram. -maximal-distance: 1.75 +maximal-distance: 2 # Set the maximal distance (in blocks) to the shop where the player can see the floatig shop item. maximal-item-distance: 40