From c1a8001b5496190e67e3bd96183b970b352c9023 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Thu, 10 Sep 2020 15:19:42 +1200 Subject: [PATCH] Add setPitchLock and setYawLock on FlagWatcher, closes #507 --- .../disguise/disguisetypes/FlagWatcher.java | 71 ++++++++++++++++++- .../PacketHandlerHeadRotation.java | 54 ++++++++++---- .../packethandlers/PacketHandlerMovement.java | 34 ++++++--- .../packethandlers/PacketHandlerSpawn.java | 40 ++++++++--- .../utilities/params/ParamInfoTypes.java | 1 + .../params/types/base/ParamInfoFloat.java | 7 +- .../types/base/ParamInfoFloatNullable.java | 31 ++++++++ 7 files changed, 196 insertions(+), 42 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloatNullable.java diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index 085f011f..0efe4774 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -1,5 +1,6 @@ package me.libraryaddict.disguise.disguisetypes; +import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; @@ -25,12 +26,12 @@ import net.md_5.bungee.chat.ComponentSerializer; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; -import org.bukkit.scoreboard.Team; import java.lang.reflect.InvocationTargetException; import java.util.*; @@ -58,12 +59,80 @@ public class FlagWatcher { @Getter private boolean upsideDown; private ChatColor glowColor; + @Getter + private Float pitchLock; + @Getter + private Float yawLock; public FlagWatcher(Disguise disguise) { this.disguise = (TargetedDisguise) disguise; equipment = new LibsEquipment(this); } + public boolean isPitchLocked() { + return pitchLock != null; + } + + public void setPitchLocked(boolean pitchLocked) { + if (isPitchLocked() == pitchLocked) { + return; + } + + setPitchLock(pitchLocked ? 0F : null); + } + + public boolean isYawLocked() { + return yawLock != null; + } + + public void setYawLocked(boolean yawLocked) { + if (isYawLocked() == yawLocked) { + return; + } + + setYawLock(yawLocked ? 0F : null); + } + + public void setPitchLock(Float pitch) { + this.pitchLock = pitch; + + if (!getDisguise().isDisguiseInUse()) { + return; + } + + sendHeadPacket(); + } + + private void sendHeadPacket() { + PacketContainer rotateHead = new PacketContainer(Server.ENTITY_HEAD_ROTATION); + + StructureModifier mods = rotateHead.getModifier(); + + mods.write(0, getDisguise().getEntity().getEntityId()); + + Location loc = getDisguise().getEntity().getLocation(); + + mods.write(1, (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + + try { + for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, rotateHead); + } + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public void setYawLock(Float yaw) { + this.yawLock = yaw; + + if (!getDisguise().isDisguiseInUse()) { + return; + } + + sendHeadPacket(); + } + private byte addEntityAnimations(byte originalValue, byte entityValue) { for (int i = 0; i < 6; i++) { if ((entityValue & 1 << i) != 0 && !modifiedEntityAnimations[i]) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerHeadRotation.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerHeadRotation.java index f4c98a07..2fb0d997 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerHeadRotation.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerHeadRotation.java @@ -24,8 +24,12 @@ public class PacketHandlerHeadRotation implements IPacketHandler { @Override public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { - if (!disguise.getType().isPlayer() || entity.getType() == EntityType.PLAYER) { + Entity entity) { + Float pitchLock = disguise.getWatcher().getPitchLock(); + Float yawLock = disguise.getWatcher().getYawLock(); + + if (pitchLock == null && yawLock == null && + (!disguise.getType().isPlayer() || entity.getType() == EntityType.PLAYER)) { return; } @@ -33,8 +37,16 @@ public class PacketHandlerHeadRotation implements IPacketHandler { DisguiseType entityType = DisguiseType.getType(entity); - byte pitch; - byte yaw; + byte pitch = 0; + byte yaw = 0; + + if (pitchLock != null) { + pitch = (byte) (int) (pitchLock * 256.0F / 360.0F); + } + + if (yawLock != null) { + yaw = (byte) (int) (yawLock * 256.0F / 360.0F); + } switch (entityType) { case LLAMA_SPIT: @@ -61,22 +73,38 @@ public class PacketHandlerHeadRotation implements IPacketHandler { case SNOWBALL: case PAINTING: case PRIMED_TNT: - if (sentPacket.getBytes().read(0) == 0 && entity.getVelocity().lengthSquared() > 0) { + if ((pitchLock == null || yawLock == null) && sentPacket.getBytes().read(0) == 0 && + entity.getVelocity().lengthSquared() > 0) { loc.setDirection(entity.getVelocity()); - pitch = DisguiseUtilities.getPitch(disguise.getType(), DisguiseType.PLAYER, - (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); - yaw = DisguiseUtilities.getYaw(disguise.getType(), DisguiseType.PLAYER, - (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + + if (pitchLock == null) { + pitch = DisguiseUtilities + .getPitch(DisguiseType.PLAYER, (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); + } + + if (yawLock == null) { + yaw = DisguiseUtilities + .getYaw(DisguiseType.PLAYER, (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + } + break; } default: - pitch = DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), - (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); - yaw = DisguiseUtilities - .getYaw(disguise.getType(), entity.getType(), (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + if (pitchLock == null) { + pitch = DisguiseUtilities.getPitch(DisguiseType.getType(entity.getType()), + (byte) (int) (loc.getPitch() * 256.0F / 360.0F)); + } + + if (yawLock == null) { + yaw = DisguiseUtilities.getYaw(DisguiseType.getType(entity.getType()), + (byte) (int) (loc.getYaw() * 256.0F / 360.0F)); + } break; } + pitch = DisguiseUtilities.getPitch(disguise.getType(), pitch); + yaw = DisguiseUtilities.getYaw(disguise.getType(), yaw); + PacketContainer rotation = new PacketContainer(PacketType.Play.Server.ENTITY_HEAD_ROTATION); StructureModifier mods = rotation.getModifier(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerMovement.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerMovement.java index 049f08be..3065355a 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerMovement.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerMovement.java @@ -122,14 +122,6 @@ public class PacketHandlerMovement implements IPacketHandler { } packets.addPacket(movePacket); - - StructureModifier bytes = movePacket.getBytes(); - - byte yawValue = bytes.read(0); - byte pitchValue = bytes.read(1); - - bytes.write(0, DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue)); - bytes.write(1, DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue)); } else if (disguise.getType() == DisguiseType.RABBIT && (sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE || sentPacket.getType() == PacketType.Play.Server.REL_ENTITY_MOVE_LOOK)) { @@ -157,7 +149,9 @@ public class PacketHandlerMovement implements IPacketHandler { statusPacket.getIntegers().write(0, entity.getEntityId()); statusPacket.getBytes().write(0, (byte) 1); } - } else if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK && + } + + if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK && disguise.getType() == DisguiseType.WITHER_SKULL) { // Stop wither skulls from looking packets.clear(); @@ -174,8 +168,26 @@ public class PacketHandlerMovement implements IPacketHandler { byte yawValue = bytes.read(0); byte pitchValue = bytes.read(1); - bytes.write(0, DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue)); - bytes.write(1, DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue)); + Float pitchLock = disguise.getWatcher().getPitchLock(); + Float yawLock = disguise.getWatcher().getYawLock(); + + if (pitchLock != null) { + pitchValue = (byte) (int) (pitchLock * 256.0F / 360.0F); + pitchValue = DisguiseUtilities.getPitch(disguise.getType(), pitchValue); + } else { + pitchValue = DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue); + + } + + if (yawLock != null) { + yawValue = (byte) (int) (yawLock * 256.0F / 360.0F); + yawValue = DisguiseUtilities.getYaw(disguise.getType(), yawValue); + } else { + yawValue = DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue); + } + + bytes.write(0, yawValue); + bytes.write(1, pitchValue); if (sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT && disguise.getType() == DisguiseType.ITEM_FRAME) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java index 41761a34..b63cba4f 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerSpawn.java @@ -58,7 +58,7 @@ public class PacketHandlerSpawn implements IPacketHandler { @Override public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, - Entity entity) { + Entity entity) { packets.clear(); @@ -106,12 +106,24 @@ public class PacketHandlerSpawn implements IPacketHandler { Location loc = disguisedEntity.getLocation().clone() .add(0, DisguiseUtilities.getYModifier(disguisedEntity, disguise), 0); - byte yaw = (byte) (int) (loc.getYaw() * 256.0F / 360.0F); - byte pitch = (byte) (int) (loc.getPitch() * 256.0F / 360.0F); + Float pitchLock = DisguiseConfig.isMovementPacketsEnabled() ? disguise.getWatcher().getPitchLock() : null; + Float yawLock = DisguiseConfig.isMovementPacketsEnabled() ? disguise.getWatcher().getYawLock() : null; + + byte yaw = (byte) (int) ((yawLock == null ? loc.getYaw() : yawLock) * 256.0F / 360.0F); + byte pitch = (byte) (int) ((pitchLock == null ? loc.getPitch() : pitchLock) * 256.0F / 360.0F); if (DisguiseConfig.isMovementPacketsEnabled()) { - yaw = DisguiseUtilities.getYaw(disguise.getType(), disguisedEntity.getType(), yaw); - pitch = DisguiseUtilities.getPitch(disguise.getType(), disguisedEntity.getType(), pitch); + if (yawLock == null) { + yaw = DisguiseUtilities.getYaw(DisguiseType.getType(disguisedEntity.getType()), yaw); + } + + if (pitchLock == null) { + pitch = DisguiseUtilities.getPitch(DisguiseType.getType(disguisedEntity.getType()), pitch); + } + + yaw = DisguiseUtilities.getYaw(disguise.getType(), yaw); + pitch = DisguiseUtilities.getPitch(disguise.getType(), pitch); + } if (disguise.getType() == DisguiseType.EXPERIENCE_ORB) { @@ -261,18 +273,24 @@ public class PacketHandlerSpawn implements IPacketHandler { double d2 = vec.getX(); double d3 = vec.getY(); double d4 = vec.getZ(); - if (d2 < -d1) + if (d2 < -d1) { d2 = -d1; - if (d3 < -d1) + } + if (d3 < -d1) { d3 = -d1; - if (d4 < -d1) + } + if (d4 < -d1) { d4 = -d1; - if (d2 > d1) + } + if (d2 > d1) { d2 = d1; - if (d3 > d1) + } + if (d3 > d1) { d3 = d1; - if (d4 > d1) + } + if (d4 > d1) { d4 = d1; + } // endregion mods.write(3, loc.getX()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java index 12561fa2..c7b0b8fc 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/ParamInfoTypes.java @@ -135,6 +135,7 @@ public class ParamInfoTypes { paramInfos.add(new ParamInfoString(String.class, "Text", "A line of text")); paramInfos.add(new ParamInfoInteger("Number", "A whole number without decimals")); paramInfos.add(new ParamInfoFloat("Number.0", "A number which can have decimal places")); + paramInfos.add(new ParamInfoFloatNullable("Number.0", "A number which can have decimal places or be null")); paramInfos.add(new ParamInfoDouble("Number.0", "A number which can have decimal places")); paramInfos.add(new ParamInfoSoundGroup()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloat.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloat.java index 03c3dc9d..ec3da2c5 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloat.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloat.java @@ -7,12 +7,7 @@ import me.libraryaddict.disguise.utilities.params.ParamInfo; */ public class ParamInfoFloat extends ParamInfo { public ParamInfoFloat(String name, String description) { - super(Number.class, name, description); - } - - @Override - public boolean isParam(Class classType) { - return classType == Float.class || classType == Float.TYPE; + super(float.class, name, description); } @Override diff --git a/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloatNullable.java b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloatNullable.java new file mode 100644 index 00000000..f0ba002f --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/utilities/params/types/base/ParamInfoFloatNullable.java @@ -0,0 +1,31 @@ +package me.libraryaddict.disguise.utilities.params.types.base; + +import me.libraryaddict.disguise.utilities.params.ParamInfo; + +/** + * Created by libraryaddict on 7/09/2018. + */ +public class ParamInfoFloatNullable extends ParamInfo { + public ParamInfoFloatNullable(String name, String description) { + super(Float.class, name, description); + } + + @Override + protected Object fromString(String string) { + if (string == null || string.equals("null")) { + return null; + } + + return Float.parseFloat(string); + } + + @Override + public boolean canReturnNull() { + return true; + } + + @Override + public String toString(Object object) { + return object == null ? "null" : object.toString(); + } +}