Fixed shop items after teleporting far or changing worlds

This commit is contained in:
Eric 2016-08-19 14:34:02 +02:00
parent 418c82e4bf
commit 1b51318dbe
2 changed files with 28 additions and 14 deletions

View File

@ -27,26 +27,41 @@ public class ShopItemListener implements Listener {
this.shopUtils = plugin.getShopUtils(); this.shopUtils = plugin.getShopUtils();
} }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) { public void onPlayerMove(PlayerMoveEvent e) {
if (e.getFrom().getBlockX() == e.getTo().getBlockX() if (e.getFrom().getBlockX() == e.getTo().getBlockX()
&& e.getFrom().getBlockZ() == e.getTo().getBlockZ() && e.getFrom().getBlockZ() == e.getTo().getBlockZ()
&& e.getFrom().getBlockY() == e.getTo().getBlockY()) { && e.getFrom().getBlockY() == e.getTo().getBlockY()) {
return; return;
} }
updateShopItemVisibility(e.getPlayer(), true); updateShopItemVisibility(e.getPlayer(), true, false);
} }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent e) { public void onPlayerTeleport(final PlayerTeleportEvent e) {
updateShopItemVisibility(e.getPlayer(), true, e.getTo()); if (e.getFrom().getWorld().equals(e.getTo().getWorld())) {
if (e.getFrom().distanceSquared(e.getTo()) > 22500) {
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
updateShopItemVisibility(e.getPlayer(), true, true, e.getTo());
}
}, 20L);
return;
}
updateShopItemVisibility(e.getPlayer(), true, false, e.getTo());
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerChangedWorld(PlayerChangedWorldEvent e) {
updateShopItemVisibility(e.getPlayer(), true, true);
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
updateShopItemVisibility(e.getPlayer(), false); updateShopItemVisibility(e.getPlayer(), false, false);
} }
@EventHandler @EventHandler
@ -56,12 +71,11 @@ public class ShopItemListener implements Listener {
} }
} }
private void updateShopItemVisibility(Player p, boolean hideIfAway) { private void updateShopItemVisibility(Player p, boolean hideIfAway, boolean reset) {
updateShopItemVisibility(p, hideIfAway, p.getLocation()); updateShopItemVisibility(p, hideIfAway, reset, p.getLocation());
} }
private void updateShopItemVisibility(Player p, boolean hideIfAway, Location playerLocation) { private void updateShopItemVisibility(Player p, boolean hideIfAway, boolean reset, Location playerLocation) {
double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance; double itemDistanceSquared = plugin.getShopChestConfig().maximal_item_distance;
itemDistanceSquared *= itemDistanceSquared; itemDistanceSquared *= itemDistanceSquared;
World w = playerLocation.getWorld(); World w = playerLocation.getWorld();
@ -69,12 +83,12 @@ public class ShopItemListener implements Listener {
for (Shop shop : shopUtils.getShops()) { for (Shop shop : shopUtils.getShops()) {
Location shopLocation = shop.getLocation(); Location shopLocation = shop.getLocation();
if (w.equals(shopLocation.getWorld()) && shopLocation.distanceSquared(playerLocation) <= itemDistanceSquared) { if (w.equals(shopLocation.getWorld()) && shopLocation.distanceSquared(playerLocation) <= itemDistanceSquared) {
shop.getItem().setVisible(p, true); if (reset) shop.getItem().resetForPlayer(p);
else shop.getItem().setVisible(p, true);
} else if (hideIfAway) { } else if (hideIfAway) {
shop.getItem().setVisible(p, false); shop.getItem().setVisible(p, false);
} }
} }
} }
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)

View File

@ -106,7 +106,7 @@ public class ShopItem {
} }
public void setVisible(final Player p, boolean visible) { public void setVisible(final Player p, boolean visible) {
if (this.visible.containsKey(p) && this.visible.get(p) == visible) if (isVisible(p) == visible)
return; return;
if (visible) { if (visible) {