Merge pull request #18 from MineTheCube/master

- Improve listeners performances
- Fixes #17
This commit is contained in:
Eric 2016-08-18 15:03:37 +02:00 committed by GitHub
commit 6205dfbd65
3 changed files with 48 additions and 27 deletions

View File

@ -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,10 +19,19 @@ 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()
&& 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 +46,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);
}

View File

@ -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;
}
updateShopItemVisibility(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);
}
}
updateShopItemVisibility(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) {
updateShopItemVisibility(e.getPlayer(), false);
}
@EventHandler
@ -66,6 +56,27 @@ public class ShopItemListener implements Listener {
}
}
private void updateShopItemVisibility(Player p, boolean hideIfAway) {
updateShopItemVisibility(p, hideIfAway, p.getLocation());
}
private void updateShopItemVisibility(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();

View File

@ -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