package de.epiceric.shopchest.listeners;

import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.event.ShopUpdateEvent;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUpdater;
import org.bukkit.Bukkit;
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.PlayerJoinEvent;

public class ShopUpdateListener implements Listener {

    private ShopChest plugin;

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

    @EventHandler
    public void onShopUpdate(ShopUpdateEvent e) {
        for (Player p : Bukkit.getOnlinePlayers()) {
            plugin.getShopUtils().updateShops(p);
        }
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerJoin(PlayerJoinEvent e) {
        if (!plugin.getUpdater().isRunning()) {
            plugin.setUpdater(new ShopUpdater(plugin));
            plugin.getUpdater().start();
        }

        for (Shop shop : plugin.getShopUtils().getShops()) {
            if (shop.getHologram() != null) shop.getHologram().hidePlayer(e.getPlayer());
            if (shop.getItem() != null) shop.getItem().setVisible(e.getPlayer(), false);
        }
    }

}

@@ -422,25 +422,176 @@
 r.Player
-JoinEvent
+ChangedWorldEvent;%0Aimport org.bukkit.event.player.PlayerJoinEvent;%0Aimport org.bukkit.event.player.PlayerRespawnEvent;%0Aimport org.bukkit.scheduler.BukkitRunnable
 ;%0A%0Apubli
@@ -1040,32 +1040,1000 @@
 rJoinEvent e) %7B%0A
+        restartShopUpdater(e.getPlayer());%0A    %7D%0A%0A    // The Bukkit::getOnlinePlayers() list does not include players that%0A    // are currently respawning or chaning worlds, so when only one player is%0A    // online and is currently respawning, the updater will think that no player%0A    // is online, so it will stop. To prevent that, a delay of 1 tick is needed.%0A%0A    @EventHandler%0A    public void onPlayerChangedWorld(final PlayerChangedWorldEvent e) %7B%0A        new BukkitRunnable() %7B%0A            @Override%0A            public void run() %7B%0A                restartShopUpdater(e.getPlayer());%0A            %7D%0A        %7D.runTaskLater(plugin, 1L);%0A    %7D%0A%0A    @EventHandler%0A    public void onPlayerRespawn(final PlayerRespawnEvent e) %7B%0A        new BukkitRunnable() %7B%0A            @Override%0A            public void run() %7B%0A                restartShopUpdater(e.getPlayer());%0A            %7D%0A        %7D.runTaskLater(plugin, 1L);%0A    %7D%0A%0A    private void restartShopUpdater(Player p) %7B%0A
         if (!plu
@@ -2307,29 +2307,17 @@
 ePlayer(
-e.getPlayer()
+p
 );%0A     
@@ -2377,29 +2377,17 @@
 Visible(
-e.getPlayer()
+p
 , false)

Fix shop visibility after respawn or world change