From 23e63a5a63ba51e52553a4e252860f102cc3ebee Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 16 Jan 2019 19:58:53 +1300 Subject: [PATCH] Minor code cleanup, cancel velocity packets the player shouldn't see. Fixes #331 --- .../disguise/DisguiseListener.java | 5 +++++ .../commands/DisguisePlayerCommand.java | 16 ++++++++-------- .../disguise/disguisetypes/Disguise.java | 12 +++++++++--- .../disguise/utilities/DisguiseUtilities.java | 19 +++++++++++++++++++ .../PacketListenerViewSelfDisguise.java | 6 ++++++ 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java index 923778ce..9f2da72a 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseListener.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseListener.java @@ -244,6 +244,11 @@ public class DisguiseListener implements Listener { } } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onVelocity(PlayerVelocityEvent event) { + DisguiseUtilities.setPlayerVelocity(event.getPlayer()); + } + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public void onAttack(EntityDamageByEntityEvent event) { if (DisguiseConfig.isDisguiseBlownWhenAttacked() && event.getEntity() instanceof Player) { diff --git a/src/main/java/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java b/src/main/java/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java index 0c51db14..c14730ea 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/DisguisePlayerCommand.java @@ -44,19 +44,19 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom return true; } - Entity player = Bukkit.getPlayer(args[0]); + Entity entity = Bukkit.getPlayer(args[0]); - if (player == null) { + if (entity == null) { if (args[0].contains("-")) { try { - player = Bukkit.getEntity(UUID.fromString(args[0])); + entity = Bukkit.getEntity(UUID.fromString(args[0])); } catch (Exception ignored) { } } } - if (player == null) { + if (entity == null) { sender.sendMessage(LibsMsg.CANNOT_FIND_PLAYER.get(args[0])); return true; } @@ -95,14 +95,14 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom if (DisguiseConfig.isNameOfPlayerShownAboveDisguise()) { if (disguise.getWatcher() instanceof LivingWatcher) { - disguise.getWatcher().setCustomName(getDisplayName(player)); + disguise.getWatcher().setCustomName(getDisplayName(entity)); if (DisguiseConfig.isNameAboveHeadAlwaysVisible()) { disguise.getWatcher().setCustomNameVisible(true); } } } - disguise.setEntity(player); + disguise.setEntity(entity); if (!setViewDisguise(args)) { // They prefer to have the opposite of whatever the view disguises option is @@ -115,11 +115,11 @@ public class DisguisePlayerCommand extends DisguiseBaseCommand implements TabCom if (disguise.isDisguiseInUse()) { sender.sendMessage(LibsMsg.DISG_PLAYER_AS_DISG - .get(player instanceof Player ? player.getName() : DisguiseType.getType(player).toReadable(), + .get(entity instanceof Player ? entity.getName() : DisguiseType.getType(entity).toReadable(), disguise.getType().toReadable())); } else { sender.sendMessage(LibsMsg.DISG_PLAYER_AS_DISG_FAIL - .get(player instanceof Player ? player.getName() : DisguiseType.getType(player).toReadable(), + .get(entity instanceof Player ? entity.getName() : DisguiseType.getType(entity).toReadable(), disguise.getType().toReadable())); } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index 158159c4..8760ada5 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -257,18 +257,24 @@ public abstract class Disguise { StructureModifier mods = velocityPacket.getIntegers(); + // Write entity ID + mods.write(0, getEntity().getEntityId()); mods.write(1, (int) (vector.getX() * 8000)); mods.write(3, (int) (vector.getZ() * 8000)); for (Player player : DisguiseUtilities.getPerverts(disguise)) { + PacketContainer tempVelocityPacket = velocityPacket.shallowClone(); + mods = tempVelocityPacket.getIntegers(); + + // If the viewing player is the disguised player if (getEntity() == player) { + // If not using self disguise, continue if (!isSelfDisguiseVisible()) { continue; } + // Write self disguise ID mods.write(0, DisguiseAPI.getSelfDisguiseId()); - } else { - mods.write(0, getEntity().getEntityId()); } mods.write(2, @@ -280,7 +286,7 @@ public abstract class Disguise { } ProtocolLibrary.getProtocolManager() - .sendServerPacket(player, velocityPacket.shallowClone(), false); + .sendServerPacket(player, tempVelocityPacket, false); } } catch (Exception e) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 29bd8fa9..85df8371 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -81,6 +81,25 @@ public class DisguiseUtilities { private static Gson gson; private static boolean pluginsUsed, commandsUsed; private static long libsDisguisesCalled; + /** + * Keeps track of what tick this occured + */ + private static long velocityTime; + private static int velocityID; + + public static void setPlayerVelocity(Player player) { + velocityID = player.getEntityId(); + velocityTime = player.getWorld().getTime(); + } + + /** + * Returns if this velocity is due to a PlayerVelocityEvent + */ + public static boolean isPlayerVelocity(Player player) { + // Be generous with how many ticks they have until they jump, the server could be lagging and the player + // would effectively have anti-knockback + return player.getEntityId() == velocityID && (player.getWorld().getTime() - velocityTime) < 3; + } public static void setPluginsUsed() { if (libsDisguisesCalled > System.currentTimeMillis()) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerViewSelfDisguise.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerViewSelfDisguise.java index 80da250d..389e1fda 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerViewSelfDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerViewSelfDisguise.java @@ -12,6 +12,7 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.packets.LibsPackets; import me.libraryaddict.disguise.utilities.packets.PacketsManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; @@ -168,6 +169,11 @@ public class PacketListenerViewSelfDisguise extends PacketAdapter { packet.getBytes().read(0) == 2) { event.setCancelled(true); } + } else if (event.getPacketType() == Server.ENTITY_VELOCITY && + !DisguiseUtilities.isPlayerVelocity(observer)) { + // The player only sees velocity changes when there is a velocity event. As the method claims there + // was no velocity event... + event.setCancelled(true); } } catch (Exception ex) {