From c2b628141011b730588463b36d629fd1024dbb04 Mon Sep 17 00:00:00 2001 From: MineTheCube Date: Thu, 18 Aug 2016 02:14:59 +0200 Subject: [PATCH 1/3] 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 From 55a3bb87d3919859cd2bd3b7e714072746120ec0 Mon Sep 17 00:00:00 2001 From: MineTheCube Date: Thu, 18 Aug 2016 14:34:43 +0200 Subject: [PATCH 2/3] Change event priority and ignore cancelled Make it consistent with ShopItemListener#onPlayerMove(PlayerMoveEvent) --- .../epiceric/shopchest/listeners/HologramUpdateListener.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java index 5494144..3fe9fc7 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java @@ -7,6 +7,7 @@ import org.bukkit.Material; import org.bukkit.block.Block; 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.PlayerMoveEvent; @@ -18,7 +19,7 @@ public class HologramUpdateListener implements Listener { this.plugin = plugin; } - @EventHandler + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPlayerMove(PlayerMoveEvent e) { if (e.getFrom().getBlockX() == e.getTo().getBlockX() From 1c079b5abe6474f0b6ac63c91811d83c1de4034f Mon Sep 17 00:00:00 2001 From: MineTheCube Date: Thu, 18 Aug 2016 14:43:36 +0200 Subject: [PATCH 3/3] Change method name to what it really does --- .../shopchest/listeners/ShopItemListener.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java index 0e9f1dd..a0069e9 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java @@ -36,17 +36,17 @@ public class ShopItemListener implements Listener { return; } - updateShopVisibility(e.getPlayer(), true); + updateShopItemVisibility(e.getPlayer(), true); } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onPlayerTeleport(PlayerTeleportEvent e) { - updateShopVisibility(e.getPlayer(), true, e.getTo()); + updateShopItemVisibility(e.getPlayer(), true, e.getTo()); } @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { - updateShopVisibility(e.getPlayer(), false); + updateShopItemVisibility(e.getPlayer(), false); } @EventHandler @@ -56,11 +56,11 @@ public class ShopItemListener implements Listener { } } - private void updateShopVisibility(Player p, boolean hideIfAway) { - updateShopVisibility(p, hideIfAway, p.getLocation()); + private void updateShopItemVisibility(Player p, boolean hideIfAway) { + updateShopItemVisibility(p, hideIfAway, p.getLocation()); } - private void updateShopVisibility(Player p, boolean hideIfAway, Location playerLocation) { + private void updateShopItemVisibility(Player p, boolean hideIfAway, Location playerLocation) { double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance; itemDistanceSquared *= itemDistanceSquared;