2017-01-04 17:07:00 +01:00
|
|
|
package de.epiceric.shopchest.listeners;
|
|
|
|
|
|
|
|
import de.epiceric.shopchest.ShopChest;
|
2017-07-27 01:02:21 +02:00
|
|
|
import de.epiceric.shopchest.shop.Shop;
|
2017-04-10 20:05:31 +02:00
|
|
|
import de.epiceric.shopchest.utils.Callback;
|
2017-07-27 01:02:21 +02:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.entity.Player;
|
2017-01-04 17:07:00 +01:00
|
|
|
import org.bukkit.event.EventHandler;
|
2017-07-27 01:02:21 +02:00
|
|
|
import org.bukkit.event.EventPriority;
|
2017-01-04 17:07:00 +01:00
|
|
|
import org.bukkit.event.Listener;
|
2017-07-27 01:02:21 +02:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
2017-04-10 20:05:31 +02:00
|
|
|
import org.bukkit.event.world.WorldLoadEvent;
|
2017-07-27 01:02:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:02:21 +02:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
|
|
public void onPlayerTeleport(PlayerTeleportEvent e) {
|
|
|
|
Location from = e.getFrom();
|
|
|
|
Location to = e.getTo();
|
|
|
|
final Player p = e.getPlayer();
|
|
|
|
|
|
|
|
// Wait till the chunk should have loaded on the client
|
|
|
|
// Update IF worlds are different OR chunks are different (as many teleports are in same chunk)
|
|
|
|
if (!from.getWorld().equals(to.getWorld())
|
|
|
|
|| from.getChunk().getX() != to.getChunk().getX()
|
|
|
|
|| from.getChunk().getZ() != to.getChunk().getZ()) {
|
|
|
|
// Wait for 15 ticks before we actually put it in the queue
|
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
plugin.getUpdater().beforeNext(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (p.isOnline()) {
|
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
|
|
|
if (shop.getItem() != null) {
|
|
|
|
shop.getItem().setVisible(p, false);
|
|
|
|
}
|
|
|
|
if (shop.getHologram() != null) {
|
|
|
|
shop.getHologram().hidePlayer(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 15L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:05:31 +02:00
|
|
|
@EventHandler
|
|
|
|
public void onWorldLoad(WorldLoadEvent e) {
|
|
|
|
final String worldName = e.getWorld().getName();
|
|
|
|
|
|
|
|
plugin.getShopUtils().reloadShops(false, false, new Callback(plugin) {
|
|
|
|
@Override
|
|
|
|
public void onResult(Object result) {
|
|
|
|
int amount = -1;
|
|
|
|
if (result instanceof Integer) {
|
|
|
|
amount = (int) result;
|
|
|
|
}
|
|
|
|
plugin.getLogger().info(String.format("Reloaded %d shops because a new world '%s' was loaded", amount, worldName));
|
|
|
|
plugin.debug(String.format("Reloaded %d shops because a new world '%s' was loaded", amount, worldName));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-04 17:07:00 +01:00
|
|
|
}
|