package de.epiceric.shopchest.listeners;

import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

public class HologramUpdateListener implements Listener {

    private ShopChest plugin;

    public HologramUpdateListener(ShopChest plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    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();

            if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) {
                plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error);
                continue;
            }

            if (shop.getHologram() == null) continue;

            Location shopLocation = shop.getLocation();

            if (playerLocation.getWorld().equals(shopLocation.getWorld())) {
                if (playerLocation.distanceSquared(shop.getHologram().getLocation()) <= hologramDistanceSquared) {
                    if (!shop.getHologram().isVisible(p)) {
                        shop.getHologram().showPlayer(p);
                    }
                } else {
                    if (shop.getHologram().isVisible(p)) {
                        shop.getHologram().hidePlayer(p);
                    }
                }
            }

        }


    }

}

@@ -269,24 +269,63 @@
 entHandler;%0A
+import org.bukkit.event.EventPriority;%0A
 import org.b
@@ -593,16 +593,71 @@
 tHandler
+(priority = EventPriority.HIGH, ignoreCancelled = true)
 %0A    pub

Change event priority and ignore cancelled