Deny async

This commit is contained in:
libraryaddict 2016-06-06 01:31:34 +12:00
parent 91b64b5920
commit 60730f4dcd
4 changed files with 286 additions and 200 deletions

View File

@ -11,7 +11,6 @@ import org.bukkit.entity.Zombie;
public enum DisguiseType public enum DisguiseType
{ {
AREA_EFFECT_CLOUD(3, 0), AREA_EFFECT_CLOUD(3, 0),
ARMOR_STAND(78), ARMOR_STAND(78),

View File

@ -34,6 +34,7 @@ public class PlayerDisguise extends TargetedDisguise
public PlayerDisguise(String name, String skinToUse) public PlayerDisguise(String name, String skinToUse)
{ {
this(name); this(name);
setSkin(skinToUse); setSkin(skinToUse);
} }
@ -42,6 +43,11 @@ public class PlayerDisguise extends TargetedDisguise
this(ReflectionManager.getGameProfile(player)); this(ReflectionManager.getGameProfile(player));
} }
public PlayerDisguise(Player player, Player skinToUse)
{
this(ReflectionManager.getGameProfile(player), ReflectionManager.getGameProfile(skinToUse));
}
public PlayerDisguise(WrappedGameProfile gameProfile) public PlayerDisguise(WrappedGameProfile gameProfile)
{ {
this(gameProfile.getName()); this(gameProfile.getName());
@ -49,6 +55,13 @@ public class PlayerDisguise extends TargetedDisguise
this.gameProfile = gameProfile; this.gameProfile = gameProfile;
} }
public PlayerDisguise(WrappedGameProfile gameProfile, WrappedGameProfile skinToUse)
{
this(gameProfile);
setSkin(skinToUse);
}
@Override @Override
public PlayerDisguise addPlayer(Player player) public PlayerDisguise addPlayer(Player player)
{ {

View File

@ -1,199 +1,246 @@
package me.libraryaddict.disguise.disguisetypes.watchers; package me.libraryaddict.disguise.disguisetypes.watchers;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedGameProfile;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise; import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
public class PlayerWatcher extends LivingWatcher { public class PlayerWatcher extends LivingWatcher
{
private boolean isInBed; private boolean isInBed;
private BlockFace sleepingDirection; private BlockFace sleepingDirection;
public PlayerWatcher(Disguise disguise) { public PlayerWatcher(Disguise disguise)
super(disguise); {
} super(disguise);
}
@Override
public PlayerWatcher clone(Disguise disguise) { @Override
PlayerWatcher watcher = (PlayerWatcher) super.clone(disguise); public PlayerWatcher clone(Disguise disguise)
watcher.isInBed = isInBed; {
return watcher; PlayerWatcher watcher = (PlayerWatcher) super.clone(disguise);
} watcher.isInBed = isInBed;
return watcher;
public BlockFace getSleepingDirection() { }
if (sleepingDirection == null) {
if (this.getDisguise().getEntity() != null && isSleeping()) { public BlockFace getSleepingDirection()
this.sleepingDirection = BlockFace.values()[Math {
.round(this.getDisguise().getEntity().getLocation().getYaw() / 90F) & 0x3]; if (sleepingDirection == null)
} else { {
return BlockFace.EAST; if (this.getDisguise().getEntity() != null && isSleeping())
} {
} this.sleepingDirection = BlockFace
return sleepingDirection; .values()[Math.round(this.getDisguise().getEntity().getLocation().getYaw() / 90F) & 0x3];
} }
else
{
// Bit 0 (0x01): Cape enabled return BlockFace.EAST;
// Bit 1 (0x02): Jacket enabled }
// Bit 2 (0x04): Left Sleeve enabled }
// Bit 3 (0x08): Right Sleeve enabled return sleepingDirection;
// Bit 4 (0x10): Left Pants Leg enabled }
// Bit 5 (0x20): Right Pants Leg enabled
// Bit 6 (0x40): Hat enabled // Bit 0 (0x01): Cape enabled
// Bit 1 (0x02): Jacket enabled
private boolean isSkinFlag(int i) { // Bit 2 (0x04): Left Sleeve enabled
return ((byte) getValue(12, (byte) 0) & 1 << i) != 0; // Bit 3 (0x08): Right Sleeve enabled
} // Bit 4 (0x10): Left Pants Leg enabled
// Bit 5 (0x20): Right Pants Leg enabled
public boolean isCapeEnabled() { // Bit 6 (0x40): Hat enabled
return isSkinFlag(1);
} private boolean isSkinFlag(int i)
{
public boolean isJackedEnabled() { return ((byte) getValue(12, (byte) 0) & 1 << i) != 0;
return isSkinFlag(2); }
}
public boolean isCapeEnabled()
public boolean isLeftSleeveEnabled() { {
return isSkinFlag(3); return isSkinFlag(1);
} }
public boolean isRightSleeveEnabled() { public boolean isJackedEnabled()
return isSkinFlag(4); {
} return isSkinFlag(2);
}
public boolean isLeftPantsEnabled() {
return isSkinFlag(5); public boolean isLeftSleeveEnabled()
} {
return isSkinFlag(3);
public boolean isRightPantsEnabled() { }
return isSkinFlag(6);
} public boolean isRightSleeveEnabled()
{
public boolean isHatEnabled() { return isSkinFlag(4);
return isSkinFlag(7); }
}
public boolean isLeftPantsEnabled()
public void setCapeEnabled(boolean enabled) { {
setSkinFlags(1, enabled); return isSkinFlag(5);
sendData(12); }
}
public boolean isRightPantsEnabled()
public void setJackedEnabled(boolean enabled) { {
setSkinFlags(2, enabled); return isSkinFlag(6);
sendData(12); }
}
public boolean isHatEnabled()
public void setLeftSleeveEnabled(boolean enabled) { {
setSkinFlags(3, enabled); return isSkinFlag(7);
sendData(12); }
}
public void setCapeEnabled(boolean enabled)
public void setRightSleeveEnabled(boolean enabled) { {
setSkinFlags(4, enabled); setSkinFlags(1, enabled);
sendData(12); sendData(12);
} }
public void setLeftPantsEnabled(boolean enabled) { public void setJackedEnabled(boolean enabled)
setSkinFlags(5, enabled); {
sendData(12); setSkinFlags(2, enabled);
} sendData(12);
}
public void setRightPantsEnabled(boolean enabled) {
setSkinFlags(6, enabled); public void setLeftSleeveEnabled(boolean enabled)
sendData(12); {
} setSkinFlags(3, enabled);
sendData(12);
public void setHatEnabled(boolean enabled) { }
setSkinFlags(7, enabled);
sendData(12); public void setRightSleeveEnabled(boolean enabled)
} {
setSkinFlags(4, enabled);
sendData(12);
public boolean isSleeping() { }
return isInBed;
} public void setLeftPantsEnabled(boolean enabled)
{
public void setSkin(String playerName) { setSkinFlags(5, enabled);
((PlayerDisguise) getDisguise()).setSkin(playerName); sendData(12);
} }
public void setSkin(WrappedGameProfile profile) { public void setRightPantsEnabled(boolean enabled)
((PlayerDisguise) getDisguise()).setSkin(profile); {
} setSkinFlags(6, enabled);
sendData(12);
public void setSleeping(BlockFace sleepingDirection) { }
setSleeping(true, sleepingDirection);
} public void setHatEnabled(boolean enabled)
{
public void setSleeping(boolean sleep) { setSkinFlags(7, enabled);
setSleeping(sleep, null); sendData(12);
} }
/** public boolean isSleeping()
* If no BlockFace is supplied. It grabs it from the entities facing direction if applicable. {
* return isInBed;
* @param sleeping }
* @param sleepingDirection
*/ public void setSkin(String playerName)
public void setSleeping(boolean sleeping, BlockFace sleepingDirection) { {
if (sleepingDirection != null) { ((PlayerDisguise) getDisguise()).setSkin(playerName);
this.sleepingDirection = BlockFace.values()[sleepingDirection.ordinal() % 4]; }
}
if (sleeping != isSleeping()) { public void setSkin(WrappedGameProfile profile)
isInBed = sleeping; {
if (DisguiseConfig.isBedPacketsEnabled() && DisguiseUtilities.isDisguiseInUse(getDisguise())) { ((PlayerDisguise) getDisguise()).setSkin(profile);
try { }
if (isSleeping()) {
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { public void setSleeping(BlockFace sleepingDirection)
PacketContainer[] packets = DisguiseUtilities.getBedPackets(player, this.getDisguise().getEntity() {
.getLocation(), player.getLocation(), (PlayerDisguise) this.getDisguise()); setSleeping(true, sleepingDirection);
if (getDisguise().getEntity() == player) { }
for (PacketContainer packet : packets) {
packet = packet.shallowClone(); public void setSleeping(boolean sleep)
packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId()); {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); setSleeping(sleep, null);
} }
} else {
for (PacketContainer packet : packets) { /**
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); * If no BlockFace is supplied. It grabs it from the entities facing direction if applicable.
} *
} * @param sleeping
} * @param sleepingDirection
} else { */
PacketContainer packet = new PacketContainer(Server.ANIMATION); public void setSleeping(boolean sleeping, BlockFace sleepingDirection)
StructureModifier<Integer> mods = packet.getIntegers(); {
mods.write(0, getDisguise().getEntity().getEntityId()); if (sleepingDirection != null)
mods.write(1, 3); {
for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { this.sleepingDirection = BlockFace.values()[sleepingDirection.ordinal() % 4];
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); }
if (sleeping != isSleeping())
} {
} isInBed = sleeping;
} catch (Exception ex) { if (DisguiseConfig.isBedPacketsEnabled() && DisguiseUtilities.isDisguiseInUse(getDisguise()))
ex.printStackTrace(System.out); {
} try
} {
} if (isSleeping())
} {
for (Player player : DisguiseUtilities.getPerverts(getDisguise()))
private void setSkinFlags(int i, boolean flag) { {
byte b0 = (byte) getValue(12, (byte) 0); PacketContainer[] packets = DisguiseUtilities.getBedPackets(player,
if (flag) { this.getDisguise().getEntity().getLocation(), player.getLocation(),
setValue(12, (byte) (b0 | 1 << i)); (PlayerDisguise) this.getDisguise());
} else { if (getDisguise().getEntity() == player)
setValue(12, (byte) (b0 & (~1 << i))); {
} for (PacketContainer packet : packets)
} {
packet = packet.shallowClone();
} packet.getIntegers().write(0, DisguiseAPI.getSelfDisguiseId());
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
}
else
{
for (PacketContainer packet : packets)
{
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
}
}
}
else
{
PacketContainer packet = new PacketContainer(Server.ANIMATION);
StructureModifier<Integer> mods = packet.getIntegers();
mods.write(0, getDisguise().getEntity().getEntityId());
mods.write(1, 3);
for (Player player : DisguiseUtilities.getPerverts(getDisguise()))
{
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
}
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
}
}
}
private void setSkinFlags(int i, boolean flag)
{
byte b0 = (byte) getValue(12, (byte) 0);
if (flag)
{
setValue(12, (byte) (b0 | 1 << i));
}
else
{
setValue(12, (byte) (b0 & (~1 << i)));
}
}
}

View File

@ -83,6 +83,7 @@ public class DisguiseUtilities
private static HashMap<String, ArrayList<Object>> runnables = new HashMap<>(); private static HashMap<String, ArrayList<Object>> runnables = new HashMap<>();
private static HashSet<UUID> selfDisguised = new HashSet<>(); private static HashSet<UUID> selfDisguised = new HashSet<>();
private static Field xChunk, zChunk; private static Field xChunk, zChunk;
private static Thread mainThread;
static static
{ {
@ -146,6 +147,11 @@ public class DisguiseUtilities
zChunk = bedChunk.getClass().getField("locZ"); zChunk = bedChunk.getClass().getField("locZ");
zChunk.setAccessible(true); zChunk.setAccessible(true);
Field threadField = ReflectionManager.getNmsField("MinecraftServer", "primaryThread");
threadField.setAccessible(true);
mainThread = (Thread) threadField.get(ReflectionManager.getMinecraftServer());
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -346,6 +352,9 @@ public class DisguiseUtilities
*/ */
public static void destroyEntity(TargetedDisguise disguise) public static void destroyEntity(TargetedDisguise disguise)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
try try
{ {
Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity()); Object entityTrackerEntry = ReflectionManager.getEntityTrackerEntry(disguise.getEntity());
@ -637,6 +646,9 @@ public class DisguiseUtilities
*/ */
public static List<Player> getPerverts(Disguise disguise) public static List<Player> getPerverts(Disguise disguise)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
List<Player> players = new ArrayList<>(); List<Player> players = new ArrayList<>();
try try
@ -865,6 +877,9 @@ public class DisguiseUtilities
*/ */
public static void refreshTracker(final TargetedDisguise disguise, String player) public static void refreshTracker(final TargetedDisguise disguise, String player)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (disguise.getEntity() != null && disguise.getEntity().isValid()) if (disguise.getEntity() != null && disguise.getEntity().isValid())
{ {
try try
@ -959,6 +974,9 @@ public class DisguiseUtilities
*/ */
public static void refreshTrackers(Entity entity) public static void refreshTrackers(Entity entity)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (entity.isValid()) if (entity.isValid())
{ {
try try
@ -1021,6 +1039,9 @@ public class DisguiseUtilities
*/ */
public static void refreshTrackers(final TargetedDisguise disguise) public static void refreshTrackers(final TargetedDisguise disguise)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
if (disguise.getEntity().isValid()) if (disguise.getEntity().isValid())
{ {
PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId()); PacketContainer destroyPacket = getDestroyPacket(disguise.getEntity().getEntityId());
@ -1131,6 +1152,9 @@ public class DisguiseUtilities
public static void removeSelfDisguise(Player player) public static void removeSelfDisguise(Player player)
{ {
if (mainThread != Thread.currentThread())
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 // Send a packet to destroy the fake entity
@ -1212,6 +1236,9 @@ public class DisguiseUtilities
*/ */
public static void sendSelfDisguise(final Player player, final TargetedDisguise disguise) public static void sendSelfDisguise(final Player player, final TargetedDisguise disguise)
{ {
if (mainThread != Thread.currentThread())
throw new IllegalStateException("Cannot modify disguises on an async thread");
try try
{ {
if (!disguise.isDisguiseInUse() || !player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible() if (!disguise.isDisguiseInUse() || !player.isValid() || !player.isOnline() || !disguise.isSelfDisguiseVisible()