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.EventPriority;
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(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onPlayerMove(PlayerMoveEvent e) {
        Player p = e.getPlayer();
        Location playerLocation = p.getLocation();
        double hologramDistanceSquared = Math.pow(plugin.getShopChestConfig().maximal_distance, 2);

        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);
                    }
                }
            }

        }


    }

}

@@ -342,24 +342,72 @@
 t.Listener;%0A
+import org.bukkit.event.player.PlayerJoinEvent;%0A
 import org.b
@@ -442,16 +442,16 @@
 eEvent;%0A
-
 %0Apublic 
@@ -620,24 +620,139 @@
 gin;%0A    %7D%0A%0A
+    @EventHandler%0A    public void onPlayerJoin(PlayerJoinEvent e) %7B%0A        updateHolograms(e.getPlayer());%0A    %7D%0A%0A
     @EventHa
@@ -874,33 +874,91 @@
     
-Player p = e.get
+updateHolograms(e.getPlayer());%0A    %7D%0A%0A    private void updateHolograms(
 Player
-();
+ p) %7B
 %0A   
@@ -2109,18 +2109,16 @@
       %7D%0A
-%0A%0A
     %7D%0A%0A%7D

Fix hologram visibility on join