mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-13 22:21:06 +00:00
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.
This commit is contained in:
parent
be52d2c623
commit
c2b6281410
@ -20,8 +20,17 @@ public class HologramUpdateListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerMove(PlayerMoveEvent e) {
|
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();
|
Player p = e.getPlayer();
|
||||||
Location playerLocation = p.getLocation();
|
Location playerLocation = p.getLocation();
|
||||||
|
double hologramDistanceSquared = plugin.getShopChestConfig().maximal_distance;
|
||||||
|
hologramDistanceSquared *= hologramDistanceSquared;
|
||||||
|
|
||||||
for (Shop shop : plugin.getShopUtils().getShops()) {
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
||||||
Block b = shop.getLocation().getBlock();
|
Block b = shop.getLocation().getBlock();
|
||||||
@ -36,7 +45,7 @@ public class HologramUpdateListener implements Listener {
|
|||||||
Location shopLocation = shop.getLocation();
|
Location shopLocation = shop.getLocation();
|
||||||
|
|
||||||
if (playerLocation.getWorld().equals(shopLocation.getWorld())) {
|
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)) {
|
if (!shop.getHologram().isVisible(p)) {
|
||||||
shop.getHologram().showPlayer(p);
|
shop.getHologram().showPlayer(p);
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,9 @@ import de.epiceric.shopchest.ShopChest;
|
|||||||
import de.epiceric.shopchest.shop.Shop;
|
import de.epiceric.shopchest.shop.Shop;
|
||||||
import de.epiceric.shopchest.utils.ShopUtils;
|
import de.epiceric.shopchest.utils.ShopUtils;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.block.BlockState;
|
import org.bukkit.block.BlockState;
|
||||||
@ -25,38 +27,26 @@ public class ShopItemListener implements Listener {
|
|||||||
this.shopUtils = plugin.getShopUtils();
|
this.shopUtils = plugin.getShopUtils();
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||||
public void onPlayerMove(PlayerMoveEvent e) {
|
public void onPlayerMove(PlayerMoveEvent e) {
|
||||||
Player p = e.getPlayer();
|
|
||||||
for (Shop shop : shopUtils.getShops()) {
|
if (e.getFrom().getBlockX() == e.getTo().getBlockX()
|
||||||
if (shop.getLocation().distance(p.getLocation()) <= plugin.getShopChestConfig().maximal_item_distance) {
|
&& e.getFrom().getBlockZ() == e.getTo().getBlockZ()
|
||||||
shop.getItem().setVisible(p, true);
|
&& e.getFrom().getBlockY() == e.getTo().getBlockY()) {
|
||||||
} else {
|
return;
|
||||||
shop.getItem().setVisible(p, false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateShopVisibility(e.getPlayer(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||||
public void onPlayerTeleport(PlayerTeleportEvent e) {
|
public void onPlayerTeleport(PlayerTeleportEvent e) {
|
||||||
Player p = e.getPlayer();
|
updateShopVisibility(e.getPlayer(), true, e.getTo());
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerJoin(final PlayerJoinEvent e) {
|
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||||
Player p = e.getPlayer();
|
updateShopVisibility(e.getPlayer(), false);
|
||||||
for (Shop shop : shopUtils.getShops()) {
|
|
||||||
if (shop.getLocation().distance(p.getLocation()) <= plugin.getShopChestConfig().maximal_item_distance) {
|
|
||||||
shop.getItem().setVisible(p, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@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)
|
@EventHandler(priority = EventPriority.HIGH)
|
||||||
public void onBlockPlace(BlockPlaceEvent e) {
|
public void onBlockPlace(BlockPlaceEvent e) {
|
||||||
Block b = e.getBlockPlaced();
|
Block b = e.getBlockPlaced();
|
||||||
|
@ -55,7 +55,7 @@ append-potion-level-to-item-name: false
|
|||||||
remove-shop-on-error: false
|
remove-shop-on-error: false
|
||||||
|
|
||||||
# Set the maximal distance (in blocks) to the shop where the player can see the hologram.
|
# 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.
|
# Set the maximal distance (in blocks) to the shop where the player can see the floatig shop item.
|
||||||
maximal-item-distance: 40
|
maximal-item-distance: 40
|
||||||
|
Loading…
Reference in New Issue
Block a user