Re-add teleport listener and few minor changes

This commit is contained in:
MineTheCube 2017-07-27 01:02:21 +02:00
parent 67f12872a9
commit 08b298922e
5 changed files with 87 additions and 38 deletions

View File

@ -1,10 +1,16 @@
package de.epiceric.shopchest.listeners;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.Callback;
import org.bukkit.Location;
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.PlayerTeleportEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.scheduler.BukkitRunnable;
public class ShopUpdateListener implements Listener {
@ -14,6 +20,41 @@ public class ShopUpdateListener implements Listener {
this.plugin = plugin;
}
@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);
}
}
@EventHandler
public void onWorldLoad(WorldLoadEvent e) {
final String worldName = e.getWorld().getName();

View File

@ -10,18 +10,20 @@ import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Hologram {
private static List<Hologram> holograms = new ArrayList<>();
private final Set<UUID> visibility = Collections.newSetFromMap(new ConcurrentHashMap<UUID, Boolean>());
private final List<ArmorStandWrapper> wrappers = new ArrayList<>();
private final Location location;
private final ShopChest plugin;
private final Config config;
private boolean exists = false;
private List<ArmorStandWrapper> wrappers = new ArrayList<>();
private ArmorStandWrapper interactArmorStandWrapper;
private Location location;
private Set<UUID> visibility = new HashSet<>();
private ShopChest plugin;
private Config config;
public Hologram(ShopChest plugin, String[] lines, Location location) {
this.plugin = plugin;
@ -254,7 +256,9 @@ public class Hologram {
*/
public static Hologram getHologram(ArmorStand armorStand) {
for (Hologram hologram : holograms) {
if (hologram.contains(armorStand)) return hologram;
if (hologram.contains(armorStand)) {
return hologram;
}
}
return null;
@ -265,12 +269,7 @@ public class Hologram {
* @return Whether the armor stand is part of a hologram
*/
public static boolean isPartOfHologram(ArmorStand armorStand) {
for (Hologram hologram : holograms) {
if (hologram.contains(armorStand)) {
return true;
}
}
return false;
return getHologram(armorStand) != null;
}
}

View File

@ -10,16 +10,17 @@ import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class ShopItem {
private ShopChest plugin;
private Set<UUID> visibility = new HashSet<>();
private ItemStack itemStack;
private Location location;
private final ShopChest plugin;
private final Set<UUID> visibility = Collections.newSetFromMap(new ConcurrentHashMap<UUID, Boolean>());
private final ItemStack itemStack;
private final Location location;
private Object entityItem;
private int entityId;

View File

@ -5,6 +5,9 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ShopUpdater {
public enum UpdateQuality {
@ -28,8 +31,7 @@ public class ShopUpdater {
}
private final ShopChest plugin;
private long interval = UpdateQuality.NORMAL.getInterval();
private final Queue<Runnable> beforeNext = new ConcurrentLinkedQueue<>();
private volatile BukkitTask running;
@ -42,8 +44,8 @@ public class ShopUpdater {
*/
public void start() {
if (!isRunning()) {
interval = plugin.getShopChestConfig().update_quality.getInterval();
running = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new ShopUpdaterTask(plugin), interval, interval);
long interval = plugin.getShopChestConfig().update_quality.getInterval();
running = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new ShopUpdaterTask(), interval, interval);
}
}
@ -72,16 +74,26 @@ public class ShopUpdater {
return running != null;
}
private static class ShopUpdaterTask implements Runnable {
/**
* Register a task to run before next loop
*
* @param runnable task to run
*/
public void beforeNext(Runnable runnable) {
beforeNext.add(runnable);
}
private final ShopChest plugin;
private ShopUpdaterTask(ShopChest plugin) {
this.plugin = plugin;
}
private class ShopUpdaterTask implements Runnable {
@Override
public void run() {
if (!beforeNext.isEmpty()) {
for (Runnable runnable : beforeNext) {
runnable.run();
}
beforeNext.clear();
}
for (Player p : Bukkit.getOnlinePlayers()) {
plugin.getShopUtils().updateShops(p);
}

View File

@ -13,16 +13,13 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
public class ShopUtils {
private HashMap<Location, Shop> shopLocation = new HashMap<>();
private HashMap<Player, Location> playerLocation = new HashMap<>();
private ShopChest plugin;
private final HashMap<Location, Shop> shopLocation = new HashMap<>();
private final HashMap<UUID, Location> playerLocation = new HashMap<>();
private final ShopChest plugin;
public ShopUtils(ShopChest plugin) {
this.plugin = plugin;
@ -55,8 +52,7 @@ public class ShopUtils {
* @return Array of all Shops
*/
public Shop[] getShops() {
Collection<Shop> shops = shopLocation.values();
return shops.toArray(new Shop[shops.size()]);
return shopLocation.values().toArray(new Shop[0]);
}
/**
@ -274,7 +270,7 @@ public class ShopUtils {
* @param force Whether update should be forced even if player has not moved
*/
public void updateShops(Player player, boolean force) {
if (!force && player.getLocation().equals(playerLocation.get(player))) {
if (!force && player.getLocation().equals(playerLocation.get(player.getUniqueId()))) {
// Player has not moved, so don't calculate shops again.
return;
}
@ -314,7 +310,7 @@ public class ShopUtils {
updateNearestShops(player);
}
playerLocation.put(player, player.getLocation());
playerLocation.put(player.getUniqueId(), player.getLocation());
}
private Set<Shop> getShopsInSight(Player player) {