Move a self disguises setter to a scheduled task

This commit is contained in:
libraryaddict 2016-06-07 14:42:38 +12:00
parent 60730f4dcd
commit 8e255c5ccf
2 changed files with 167 additions and 136 deletions

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EntityEquipment;
@ -195,7 +196,22 @@ public class FlagWatcher
if (newHealth > 0 && hasDied) if (newHealth > 0 && hasDied)
{ {
hasDied = false; hasDied = false;
DisguiseUtilities.sendSelfDisguise((Player) getDisguise().getEntity(), disguise);
Bukkit.getScheduler().scheduleSyncDelayedTask(DisguiseUtilities.getPlugin(), new Runnable()
{
@Override
public void run()
{
try
{
DisguiseUtilities.sendSelfDisguise((Player) getDisguise().getEntity(), disguise);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
}, 2);
} }
else if (newHealth <= 0 && !hasDied) else if (newHealth <= 0 && !hasDied)
{ {

View File

@ -880,92 +880,96 @@ public class DisguiseUtilities
if (mainThread != Thread.currentThread()) if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread"); throw new IllegalStateException("Cannot modify disguises on an async thread");
if (disguise.getEntity() != null && disguise.getEntity().isValid()) if (disguise.getEntity() == null || !disguise.getEntity().isValid())
return;
try
{ {
try PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId());
if (disguise.isDisguiseInUse() && disguise.getEntity() instanceof Player
&& disguise.getEntity().getName().equalsIgnoreCase(player))
{ {
PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId()); removeSelfDisguise((Player) disguise.getEntity());
if (disguise.isDisguiseInUse() && disguise.getEntity() instanceof Player if (disguise.isSelfDisguiseVisible())
&& disguise.getEntity().getName().equalsIgnoreCase(player))
{ {
removeSelfDisguise((Player) disguise.getEntity()); selfDisguised.add(disguise.getEntity().getUniqueId());
if (disguise.isSelfDisguiseVisible())
{
selfDisguised.add(disguise.getEntity().getUniqueId());
}
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) disguise.getEntity(), destroyPacket);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{
@Override
public void run()
{
try
{
DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
}, 2);
} }
else
ProtocolLibrary.getProtocolManager().sendServerPacket((Player) disguise.getEntity(), destroyPacket);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{ {
final Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); @Override
public void run()
if (entityTrackerEntry != null)
{ {
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") try
.get(entityTrackerEntry);
Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
ReflectionManager.getNmsClass("EntityPlayer"));
final Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
for (final Object p : trackedPlayers)
{ {
Player pl = (Player) ReflectionManager.getBukkitEntity(p); DisguiseUtilities.sendSelfDisguise((Player) disguise.getEntity(), disguise);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
}, 2);
}
else
{
final Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
if (player.equalsIgnoreCase((pl).getName())) if (entityTrackerEntry != null)
{
Set trackedPlayers = (Set) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
.get(entityTrackerEntry);
Method clear = ReflectionManager.getNmsMethod("EntityTrackerEntry", "clear",
ReflectionManager.getNmsClass("EntityPlayer"));
final Method updatePlayer = ReflectionManager.getNmsMethod("EntityTrackerEntry", "updatePlayer",
ReflectionManager.getNmsClass("EntityPlayer"));
trackedPlayers = (Set) new HashSet(trackedPlayers).clone(); // Copy before iterating to prevent
// ConcurrentModificationException
for (final Object p : trackedPlayers)
{
Player pl = (Player) ReflectionManager.getBukkitEntity(p);
if (player.equalsIgnoreCase((pl).getName()))
{
clear.invoke(entityTrackerEntry, p);
ProtocolLibrary.getProtocolManager().sendServerPacket(pl, destroyPacket);
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{ {
clear.invoke(entityTrackerEntry, p);
ProtocolLibrary.getProtocolManager().sendServerPacket(pl, destroyPacket); @Override
public void run()
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{ {
@Override try
public void run()
{ {
try updatePlayer.invoke(entityTrackerEntry, p);
{
updatePlayer.invoke(entityTrackerEntry, p);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
} }
}, 2); catch (Exception ex)
break; {
} ex.printStackTrace(System.out);
}
}
}, 2);
break;
} }
} }
} }
} }
catch (Exception ex) }
{ catch (
ex.printStackTrace(System.out);
} Exception ex)
{
ex.printStackTrace(System.out);
} }
} }
@ -1099,6 +1103,7 @@ public class DisguiseUtilities
Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable() Bukkit.getScheduler().scheduleSyncDelayedTask(libsDisguises, new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
@ -1116,9 +1121,12 @@ public class DisguiseUtilities
} }
} }
} }
catch (Exception ex) catch (
Exception ex)
{ {
ex.printStackTrace(System.out); ex.printStackTrace(System.out);
} }
} }
} }
@ -1155,80 +1163,82 @@ public class DisguiseUtilities
if (mainThread != Thread.currentThread()) if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread"); throw new IllegalStateException("Cannot modify disguises on an async thread");
if (selfDisguised.contains(player.getUniqueId())) if (!selfDisguised.contains(player.getUniqueId()))
{ {
// Send a packet to destroy the fake entity return;
PacketContainer packet = getDestroyPacket(DisguiseAPI.getSelfDisguiseId()); }
try // Send a packet to destroy the fake entity
PacketContainer packet = getDestroyPacket(DisguiseAPI.getSelfDisguiseId());
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// Code to stop player pushing in 1.9
Scoreboard scoreboard = player.getScoreboard();
Team t;
if ((t = scoreboard.getTeam("LDPushing")) != null)
{
t.setOption(Option.COLLISION_RULE, OptionStatus.ALWAYS);
t.removeEntry(player.getName());
t.unregister();
}
// player.spigot().setCollidesWithEntities(true);
// Finish up
// Remove the fake entity ID from the disguise bin
selfDisguised.remove(player.getUniqueId());
// Get the entity tracker
try
{
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
if (entityTrackerEntry != null)
{ {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); Object trackedPlayersObj = ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers")
} .get(entityTrackerEntry);
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// Code to stop player pushing in 1.9 // If the tracker exists. Remove himself from his tracker
Scoreboard scoreboard = player.getScoreboard(); if (isHashSet(trackedPlayersObj))
Team t;
if ((t = scoreboard.getTeam("LDPushing")) != null)
{
t.setOption(Option.COLLISION_RULE, OptionStatus.ALWAYS);
t.removeEntry(player.getName());
t.unregister();
}
// player.spigot().setCollidesWithEntities(true);
// Finish up
// Remove the fake entity ID from the disguise bin
selfDisguised.remove(player.getUniqueId());
// Get the entity tracker
try
{
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(player);
if (entityTrackerEntry != null)
{ {
Object trackedPlayersObj = ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") ((Set<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers").get(entityTrackerEntry))
.get(entityTrackerEntry); .remove(ReflectionManager.getNmsEntity(player));
}
// If the tracker exists. Remove himself from his tracker else
if (isHashSet(trackedPlayersObj)) {
{ ((Map<Object, Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayerMap")
((Set<Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayers") .get(entityTrackerEntry)).remove(ReflectionManager.getNmsEntity(player));
.get(entityTrackerEntry)).remove(ReflectionManager.getNmsEntity(player));
}
else
{
((Map<Object, Object>) ReflectionManager.getNmsField("EntityTrackerEntry", "trackedPlayerMap")
.get(entityTrackerEntry)).remove(ReflectionManager.getNmsEntity(player));
}
} }
} }
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// Resend entity metadata else he will be invisible to himself until its resent
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(player,
ProtocolLibrary.getProtocolManager()
.createPacketConstructor(Server.ENTITY_METADATA, player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true)
.createPacket(player.getEntityId(), WrappedDataWatcher.getEntityWatcher(player), true));
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
player.updateInventory();
} }
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
// Resend entity metadata else he will be invisible to himself until its resent
try
{
ProtocolLibrary.getProtocolManager().sendServerPacket(player,
ProtocolLibrary.getProtocolManager()
.createPacketConstructor(Server.ENTITY_METADATA, player.getEntityId(),
WrappedDataWatcher.getEntityWatcher(player), true)
.createPacket(player.getEntityId(), WrappedDataWatcher.getEntityWatcher(player), true));
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
player.updateInventory();
} }
/** /**
@ -1394,6 +1404,11 @@ public class DisguiseUtilities
} }
} }
public static LibsDisguises getPlugin()
{
return libsDisguises;
}
/** /**
* Method to send a packet to the self disguise, translate his entity ID to the fake id. * Method to send a packet to the self disguise, translate his entity ID to the fake id.
*/ */