Only update shops if player has moved

This commit is contained in:
Eric 2017-03-17 14:04:05 +01:00
parent 598f9434e6
commit 268d69c077
2 changed files with 14 additions and 8 deletions

View File

@ -22,7 +22,7 @@ public class ShopUpdateListener implements Listener {
@EventHandler @EventHandler
public void onShopUpdate(ShopUpdateEvent e) { public void onShopUpdate(ShopUpdateEvent e) {
for (Player p : Bukkit.getOnlinePlayers()) { for (Player p : Bukkit.getOnlinePlayers()) {
plugin.getShopUtils().updateShops(p, p.getLocation()); plugin.getShopUtils().updateShops(p);
} }
} }

View File

@ -23,6 +23,7 @@ import java.util.List;
public class ShopUtils { public class ShopUtils {
private HashMap<Location, Shop> shopLocation = new HashMap<>(); private HashMap<Location, Shop> shopLocation = new HashMap<>();
private HashMap<Player, Location> playerLocation = new HashMap<>();
private ShopChest plugin; private ShopChest plugin;
public ShopUtils(ShopChest plugin) { public ShopUtils(ShopChest plugin) {
@ -227,9 +228,13 @@ public class ShopUtils {
/** /**
* Update hologram and item of all shops for a player * Update hologram and item of all shops for a player
* @param player Player to show the updates * @param player Player to show the updates
* @param location Location of the player
*/ */
public void updateShops(Player player, Location location) { public void updateShops(Player player) {
if (player.getLocation().equals(playerLocation.get(player))) {
// Player has not moved, so don't calculate shops again.
return;
}
if (plugin.getShopChestConfig().only_show_shops_in_sight) { if (plugin.getShopChestConfig().only_show_shops_in_sight) {
HashSet<Material> transparent = new HashSet<>(); HashSet<Material> transparent = new HashSet<>();
transparent.add(Material.AIR); transparent.add(Material.AIR);
@ -288,23 +293,24 @@ public class ShopUtils {
} }
} else { } else {
for (Shop shop : getShops()) { for (Shop shop : getShops()) {
updateShop(shop, player, location); updateShop(shop, player);
} }
} }
playerLocation.put(player, player.getLocation());
} }
/** /**
* Update hologram and item of the shop for a player based on their distance to each other * Update hologram and item of the shop for a player based on their distance to each other
* @param shop Shop to update * @param shop Shop to update
* @param player Player to show the update * @param player Player to show the update
* @param location Location of the player
*/ */
public void updateShop(Shop shop, Player player, Location location) { public void updateShop(Shop shop, Player player) {
double holoDistSqr = Math.pow(plugin.getShopChestConfig().maximal_distance, 2); double holoDistSqr = Math.pow(plugin.getShopChestConfig().maximal_distance, 2);
double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2); double itemDistSqr = Math.pow(plugin.getShopChestConfig().maximal_item_distance, 2);
if (location.getWorld().getName().equals(shop.getLocation().getWorld().getName())) { if (player.getLocation().getWorld().getName().equals(shop.getLocation().getWorld().getName())) {
double distSqr = shop.getLocation().distanceSquared(location); double distSqr = shop.getLocation().distanceSquared(player.getLocation());
if (distSqr <= holoDistSqr) { if (distSqr <= holoDistSqr) {
if (shop.getHologram() != null) { if (shop.getHologram() != null) {