Show and hide holograms asynchronously

This commit is contained in:
Eric 2017-02-05 17:22:37 +01:00
parent bae02c910a
commit 8b8c7d4d73
2 changed files with 41 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -124,39 +125,51 @@ public class Hologram {
/** /**
* @param p Player to which the hologram should be shown * @param p Player to which the hologram should be shown
*/ */
public void showPlayer(Player p) { public void showPlayer(final Player p) {
for (Object o : entityList) { new BukkitRunnable() {
try { @Override
Object entityLiving = entityLivingClass.cast(o); public void run() {
Object packet = packetPlayOutSpawnEntityLivingClass.getConstructor(entityLivingClass).newInstance(entityLiving); for (Object o : entityList) {
try {
Object entityLiving = entityLivingClass.cast(o);
Object packet = packetPlayOutSpawnEntityLivingClass.getConstructor(entityLivingClass).newInstance(entityLiving);
Utils.sendPacket(plugin, packet, p); Utils.sendPacket(plugin, packet, p);
} catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
plugin.getLogger().severe("Could not show Hologram to player with reflection"); plugin.getLogger().severe("Could not show Hologram to player with reflection");
plugin.debug("Could not show Hologram to player with reflection"); plugin.debug("Could not show Hologram to player with reflection");
plugin.debug(e); plugin.debug(e);
}
}
} }
} }.runTaskAsynchronously(plugin);
visible.add(p); visible.add(p);
} }
/** /**
* @param p Player from which the hologram should be hidden * @param p Player from which the hologram should be hidden
*/ */
public void hidePlayer(Player p) { public void hidePlayer(final Player p) {
for (Object o : entityList) { new BukkitRunnable() {
try { @Override
int id = (int) entityArmorStandClass.getMethod("getId").invoke(o); public void run() {
for (Object o : entityList) {
try {
int id = (int) entityArmorStandClass.getMethod("getId").invoke(o);
Object packet = packetPlayOutEntityDestroyClass.getConstructor(int[].class).newInstance((Object) new int[] {id}); Object packet = packetPlayOutEntityDestroyClass.getConstructor(int[].class).newInstance((Object) new int[] {id});
Utils.sendPacket(plugin, packet, p); Utils.sendPacket(plugin, packet, p);
} catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) { } catch (NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
plugin.getLogger().severe("Could not hide Hologram from player with reflection"); plugin.getLogger().severe("Could not hide Hologram from player with reflection");
plugin.debug("Could not hide Hologram from player with reflection"); plugin.debug("Could not hide Hologram from player with reflection");
plugin.debug(e); plugin.debug(e);
}
}
} }
} }.runTaskAsynchronously(plugin);
visible.remove(p); visible.remove(p);
} }
@ -185,7 +198,7 @@ public class Hologram {
/** /**
* Removes the hologram. <br> * Removes the hologram. <br>
* IHologram will be hidden from all players and will be killed * Hologram will be hidden from all players and will be killed
*/ */
public void remove() { public void remove() {
for (Object o : entityList) { for (Object o : entityList) {

View File

@ -247,19 +247,20 @@ public class ShopUtils {
if (distSqr <= holoDistSqr) { if (distSqr <= holoDistSqr) {
if (shop.getHologram() != null) { if (shop.getHologram() != null) {
Block b = shop.getLocation().getBlock(); Material type = shop.getLocation().getBlock().getType();
if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) { if (type != Material.CHEST && type != Material.TRAPPED_CHEST) {
plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error); plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error);
return; return;
} }
if (!shop.getHologram().isVisible(player)) { if (!shop.getHologram().isVisible(player)) {
shop.getHologram().showPlayer(player); shop.getHologram().showPlayer(player);
} }
} }
} else { } else {
if (shop.getHologram() != null) shop.getHologram().hidePlayer(player); if (shop.getHologram() != null) {
shop.getHologram().hidePlayer(player);
}
} }
if (distSqr <= itemDistSqr) { if (distSqr <= itemDistSqr) {