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

View File

@ -247,19 +247,20 @@ public class ShopUtils {
if (distSqr <= holoDistSqr) {
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);
return;
}
if (!shop.getHologram().isVisible(player)) {
shop.getHologram().showPlayer(player);
}
}
} else {
if (shop.getHologram() != null) shop.getHologram().hidePlayer(player);
if (shop.getHologram() != null) {
shop.getHologram().hidePlayer(player);
}
}
if (distSqr <= itemDistSqr) {