Minor code cleanup, cancel velocity packets the player shouldn't see. Fixes #331
This commit is contained in:
		@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -257,18 +257,24 @@ public abstract class Disguise {
 | 
			
		||||
 | 
			
		||||
                                StructureModifier<Integer> 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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -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) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user