2017-01-04 17:07:00 +01:00
|
|
|
package de.epiceric.shopchest.listeners;
|
|
|
|
|
|
|
|
import de.epiceric.shopchest.ShopChest;
|
|
|
|
import de.epiceric.shopchest.event.ShopUpdateEvent;
|
2017-01-21 13:30:39 +01:00
|
|
|
import de.epiceric.shopchest.shop.Shop;
|
2017-02-08 21:49:23 +01:00
|
|
|
import de.epiceric.shopchest.utils.ShopUpdater;
|
2017-01-04 17:07:00 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2017-01-21 13:30:39 +01:00
|
|
|
import org.bukkit.event.EventPriority;
|
2017-01-04 17:07:00 +01:00
|
|
|
import org.bukkit.event.Listener;
|
2017-04-10 14:35:14 +02:00
|
|
|
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
2017-01-21 13:30:39 +01:00
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
2017-04-10 14:35:14 +02:00
|
|
|
import org.bukkit.event.player.PlayerRespawnEvent;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2017-01-04 17:07:00 +01:00
|
|
|
|
|
|
|
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()) {
|
2017-03-17 14:04:05 +01:00
|
|
|
plugin.getShopUtils().updateShops(p);
|
2017-01-04 17:07:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 13:30:39 +01:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerJoin(PlayerJoinEvent e) {
|
2017-04-10 14:35:14 +02:00
|
|
|
restartShopUpdater(e.getPlayer());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Bukkit::getOnlinePlayers() list does not include players that
|
|
|
|
// are currently respawning or chaning worlds, so when only one player is
|
|
|
|
// online and is currently respawning, the updater will think that no player
|
|
|
|
// is online, so it will stop. To prevent that, a delay of 1 tick is needed.
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerChangedWorld(final PlayerChangedWorldEvent e) {
|
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
restartShopUpdater(e.getPlayer());
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerRespawn(final PlayerRespawnEvent e) {
|
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
restartShopUpdater(e.getPlayer());
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void restartShopUpdater(Player p) {
|
2017-02-08 21:49:23 +01:00
|
|
|
if (!plugin.getUpdater().isRunning()) {
|
|
|
|
plugin.setUpdater(new ShopUpdater(plugin));
|
|
|
|
plugin.getUpdater().start();
|
|
|
|
}
|
|
|
|
|
2017-01-21 13:30:39 +01:00
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
2017-04-10 14:35:14 +02:00
|
|
|
if (shop.getHologram() != null) shop.getHologram().hidePlayer(p);
|
|
|
|
if (shop.getItem() != null) shop.getItem().setVisible(p, false);
|
2017-01-21 13:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 17:07:00 +01:00
|
|
|
}
|