From 1b51318dbe3b857c2f0fc788bb61562c20ba110e Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 19 Aug 2016 14:34:02 +0200 Subject: [PATCH] Fixed shop items after teleporting far or changing worlds --- .../shopchest/listeners/ShopItemListener.java | 40 +++++++++++++------ .../de/epiceric/shopchest/shop/ShopItem.java | 2 +- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java index bb0748a..f0136a9 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopItemListener.java @@ -27,26 +27,41 @@ public class ShopItemListener implements Listener { this.shopUtils = plugin.getShopUtils(); } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) + @EventHandler(priority = EventPriority.MONITOR, 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; } - updateShopItemVisibility(e.getPlayer(), true); + updateShopItemVisibility(e.getPlayer(), true, false); } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onPlayerTeleport(PlayerTeleportEvent e) { - updateShopItemVisibility(e.getPlayer(), true, e.getTo()); + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerTeleport(final PlayerTeleportEvent e) { + 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 public void onPlayerJoin(PlayerJoinEvent e) { - updateShopItemVisibility(e.getPlayer(), false); + updateShopItemVisibility(e.getPlayer(), false, false); } @EventHandler @@ -56,12 +71,11 @@ public class ShopItemListener implements Listener { } } - private void updateShopItemVisibility(Player p, boolean hideIfAway) { - updateShopItemVisibility(p, hideIfAway, p.getLocation()); + private void updateShopItemVisibility(Player p, boolean hideIfAway, boolean reset) { + 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; itemDistanceSquared *= itemDistanceSquared; World w = playerLocation.getWorld(); @@ -69,12 +83,12 @@ public class ShopItemListener implements Listener { for (Shop shop : shopUtils.getShops()) { Location shopLocation = shop.getLocation(); 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) { shop.getItem().setVisible(p, false); } } - } @EventHandler(priority = EventPriority.HIGH) diff --git a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java index 145b17e..d85f8e9 100644 --- a/src/main/java/de/epiceric/shopchest/shop/ShopItem.java +++ b/src/main/java/de/epiceric/shopchest/shop/ShopItem.java @@ -106,7 +106,7 @@ public class ShopItem { } public void setVisible(final Player p, boolean visible) { - if (this.visible.containsKey(p) && this.visible.get(p) == visible) + if (isVisible(p) == visible) return; if (visible) {