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;
|
2019-06-16 15:06:35 +02:00
|
|
|
|
2020-01-21 20:00:08 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.bukkit.Chunk;
|
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;
|
2018-08-19 23:07:58 +02:00
|
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
2017-08-10 17:02:24 +02:00
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
2017-07-27 01:02:21 +02:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
2020-01-21 20:00:08 +01:00
|
|
|
import org.bukkit.event.world.ChunkLoadEvent;
|
|
|
|
import org.bukkit.event.world.ChunkUnloadEvent;
|
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 {
|
|
|
|
|
2020-01-21 20:00:08 +01:00
|
|
|
private final ShopChest plugin;
|
|
|
|
private final Set<Chunk> newLoadedChunks = new HashSet<>();
|
2017-01-04 17:07:00 +01:00
|
|
|
|
|
|
|
public ShopUpdateListener(ShopChest plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:02:24 +02:00
|
|
|
@EventHandler
|
|
|
|
public void onPlayerLeave(PlayerQuitEvent e) {
|
2018-07-29 14:20:16 +02:00
|
|
|
// If done without delay, Bukkit#getOnlinePlayers() would still
|
|
|
|
// contain the player even though he left, so the shop updater
|
|
|
|
// would show the shop again.
|
|
|
|
new BukkitRunnable(){
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
|
|
|
if (shop.hasItem()) {
|
|
|
|
shop.getItem().resetVisible(e.getPlayer());
|
|
|
|
}
|
|
|
|
if (shop.hasHologram()) {
|
|
|
|
shop.getHologram().resetVisible(e.getPlayer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.getShopUtils().resetPlayerLocation(e.getPlayer());
|
2017-08-10 17:02:24 +02:00
|
|
|
}
|
2018-07-29 14:20:16 +02:00
|
|
|
}.runTaskLater(plugin, 1L);
|
2017-08-10 17:02:24 +02:00
|
|
|
}
|
|
|
|
|
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
|
2017-08-10 17:02:24 +02:00
|
|
|
if (!from.getWorld().getName().equals(to.getWorld().getName())
|
2017-07-27 01:02:21 +02:00
|
|
|
|| from.getChunk().getX() != to.getChunk().getX()
|
|
|
|
|| from.getChunk().getZ() != to.getChunk().getZ()) {
|
|
|
|
new BukkitRunnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-08-19 23:07:58 +02:00
|
|
|
plugin.getUpdater().queue(() -> {
|
|
|
|
if (p.isOnline()) {
|
|
|
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
|
|
|
if (shop.hasItem()) {
|
|
|
|
shop.getItem().hidePlayer(p);
|
|
|
|
}
|
|
|
|
if (shop.hasHologram()) {
|
|
|
|
shop.getHologram().hidePlayer(p);
|
2017-07-27 01:02:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 23:07:58 +02:00
|
|
|
plugin.getShopUtils().resetPlayerLocation(p);
|
2017-07-27 01:02:21 +02:00
|
|
|
}
|
|
|
|
});
|
2018-08-19 23:07:58 +02:00
|
|
|
plugin.getUpdater().updateShops(p);
|
2017-07-27 01:02:21 +02:00
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 15L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 23:07:58 +02:00
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
|
|
public void onPlayerMove(PlayerMoveEvent e) {
|
|
|
|
plugin.getUpdater().updateShops(e.getPlayer());
|
|
|
|
}
|
|
|
|
|
2017-04-10 20:05:31 +02:00
|
|
|
@EventHandler
|
2020-01-21 20:00:08 +01:00
|
|
|
public void onChunkLoad(ChunkLoadEvent e) {
|
|
|
|
if (!plugin.getShopDatabase().isInitialized()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-10 20:05:31 +02:00
|
|
|
|
2020-01-21 20:00:08 +01:00
|
|
|
// Wait 10 ticks after first event is triggered, so that multiple
|
|
|
|
// chunk loads can be handled at the same time without having to
|
|
|
|
// send a database request for each chunk.
|
|
|
|
if (newLoadedChunks.isEmpty()) {
|
|
|
|
new BukkitRunnable(){
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int chunkCount = newLoadedChunks.size();
|
|
|
|
plugin.getShopUtils().loadShops(newLoadedChunks.toArray(new Chunk[chunkCount]), new Callback<Integer>(plugin) {
|
|
|
|
@Override
|
|
|
|
public void onResult(Integer result) {
|
|
|
|
if (result == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
plugin.debug("Loaded " + result + " shops in " + chunkCount + " chunks");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
// Database connection probably failed => disable plugin to prevent more errors
|
|
|
|
plugin.getLogger().severe("Failed to load shops in newly loaded chunks");
|
|
|
|
plugin.debug("Failed to load shops in newly loaded chunks");
|
|
|
|
if (throwable != null) plugin.debug(throwable);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
newLoadedChunks.clear();
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 10L);
|
|
|
|
}
|
2019-06-16 15:06:35 +02:00
|
|
|
|
2020-01-21 20:00:08 +01:00
|
|
|
newLoadedChunks.add(e.getChunk());
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onChunkUnload(ChunkUnloadEvent e) {
|
|
|
|
if (!plugin.getShopDatabase().isInitialized()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int num = plugin.getShopUtils().unloadShops(e.getChunk());
|
|
|
|
|
|
|
|
if (num > 0) {
|
|
|
|
String chunkStr = "[" + e.getChunk().getX() + "; " + e.getChunk().getZ() + "]";
|
|
|
|
plugin.debug("Unloaded " + num + " shops in chunk " + chunkStr);
|
|
|
|
}
|
2017-04-10 20:05:31 +02:00
|
|
|
}
|
2017-01-04 17:07:00 +01:00
|
|
|
}
|