From cc4156f9142b14bba833d8e9d271f9c94e08ab40 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Tue, 5 Nov 2019 17:26:38 +1300 Subject: [PATCH] Add setGridLocked to falling block disguise --- pom.xml | 2 +- .../watchers/FallingBlockWatcher.java | 53 ++++++++ .../packethandlers/PacketHandlerMovement.java | 115 +++++++++++++----- .../packethandlers/PacketHandlerSpawn.java | 14 ++- 4 files changed, 149 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index bd46a4dc..d5e9b946 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ LibsDisguises LibsDisguises - 9.8.3 + 9.8.3-SNAPSHOT clean install diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java index 7459ab92..e85b47fe 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java @@ -1,14 +1,24 @@ package me.libraryaddict.disguise.disguisetypes.watchers; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLib; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.reflect.StructureModifier; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.lang.reflect.InvocationTargetException; + public class FallingBlockWatcher extends FlagWatcher { private ItemStack block = new ItemStack(Material.STONE); + private boolean gridLocked; public FallingBlockWatcher(Disguise disguise) { super(disguise); @@ -22,6 +32,49 @@ public class FallingBlockWatcher extends FlagWatcher { return watcher; } + public boolean isGridLocked() { + return gridLocked; + } + + private short conRel(double oldCord, double newCord) { + return (short) (((oldCord - newCord) * 4096) * (isGridLocked() ? -1 : 1)); + } + + public void setGridLocked(boolean gridLocked) { + if (isGridLocked() == gridLocked) { + return; + } + + this.gridLocked = gridLocked; + + if (getDisguise().isDisguiseInUse() && getDisguise().getEntity() != null) { + PacketContainer relMove = new PacketContainer(PacketType.Play.Server.REL_ENTITY_MOVE); + StructureModifier shorts = relMove.getShorts(); + Location loc = getDisguise().getEntity().getLocation(); + + relMove.getModifier().write(0, getDisguise().getEntity().getEntityId()); + shorts.write(0, conRel(loc.getX(), loc.getBlockX() + 0.5)); + shorts.write(1, conRel(loc.getY(), loc.getBlockY())); + shorts.write(2, conRel(loc.getZ(), loc.getBlockZ() + 0.5)); + + try { + for (Player player : DisguiseUtilities.getPerverts(getDisguise())) { + if (player == getDisguise().getEntity()) { + PacketContainer temp = relMove.shallowClone(); + temp.getModifier().write(0, DisguiseAPI.getSelfDisguiseId()); + + ProtocolLibrary.getProtocolManager().sendServerPacket(player, temp, isGridLocked()); + } else { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, relMove, isGridLocked()); + } + } + } + catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + public ItemStack getBlock() { return block; } 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 fea98e91..11ce9a48 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 @@ -6,6 +6,7 @@ import com.comphenix.protocol.reflect.StructureModifier; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.DisguiseType; +import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.packets.IPacketHandler; import me.libraryaddict.disguise.utilities.packets.LibsPackets; @@ -13,6 +14,7 @@ import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.util.Vector; /** * Created by libraryaddict on 3/01/2019. @@ -24,11 +26,61 @@ public class PacketHandlerMovement implements IPacketHandler { PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.REL_ENTITY_MOVE}; } + private short conRel(int oldCord, int newCord) { + return (short) ((oldCord - newCord) * 4096); + } + @Override public void handle(Disguise disguise, PacketContainer sentPacket, LibsPackets packets, Player observer, Entity entity) { + // If falling block should be appearing in center of blocks + if (sentPacket.getType() != PacketType.Play.Server.ENTITY_LOOK && + disguise.getType() == DisguiseType.FALLING_BLOCK && + ((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) { + packets.clear(); - if (disguise.getType() == DisguiseType.RABBIT && + PacketContainer movePacket = sentPacket.shallowClone(); + + // If relational movement + if (sentPacket.getType() != PacketType.Play.Server.ENTITY_TELEPORT) { + StructureModifier shorts = movePacket.getShorts(); + + Location current = entity.getLocation(); + Vector diff = new Vector(shorts.read(0) / 4096D, shorts.read(1) / 4096D, shorts.read(2) / 4096D); + Location newLoc = current.clone().subtract(diff); + + boolean sameBlock = + current.getBlockX() == newLoc.getBlockX() && current.getBlockY() == newLoc.getBlockY() && + current.getBlockZ() == newLoc.getBlockZ(); + + if (sameBlock) { + // Make no modifications + return; + } else { + shorts.write(0, conRel(current.getBlockX(), newLoc.getBlockX())); + shorts.write(1, conRel(current.getBlockY(), newLoc.getBlockY())); + shorts.write(2, conRel(current.getBlockZ(), newLoc.getBlockZ())); + } + } else { + Location loc = entity.getLocation(); + + StructureModifier doubles = movePacket.getDoubles(); + // Center the block + doubles.write(0, loc.getBlockX() + 0.5); + doubles.write(1, (double) loc.getBlockY()); + doubles.write(2, loc.getBlockZ() + 0.5); + } + + 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)) { // When did the rabbit disguise last hop @@ -55,49 +107,48 @@ public class PacketHandlerMovement implements IPacketHandler { statusPacket.getIntegers().write(0, entity.getEntityId()); statusPacket.getBytes().write(0, (byte) 1); } - } + } else + // Stop wither skulls from looking + if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK && + disguise.getType() == DisguiseType.WITHER_SKULL) { + packets.clear(); + } else if (sentPacket.getType() != PacketType.Play.Server.REL_ENTITY_MOVE) { + packets.clear(); - // Stop wither skulls from looking - if (sentPacket.getType() == PacketType.Play.Server.ENTITY_LOOK && - disguise.getType() == DisguiseType.WITHER_SKULL) { - packets.clear(); - } else if (sentPacket.getType() != PacketType.Play.Server.REL_ENTITY_MOVE) { - packets.clear(); + PacketContainer movePacket = sentPacket.shallowClone(); - PacketContainer movePacket = sentPacket.shallowClone(); + packets.addPacket(movePacket); - packets.addPacket(movePacket); + StructureModifier bytes = movePacket.getBytes(); - StructureModifier bytes = movePacket.getBytes(); + byte yawValue = bytes.read(0); + byte pitchValue = bytes.read(1); - 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)); - bytes.write(0, DisguiseUtilities.getYaw(disguise.getType(), entity.getType(), yawValue)); - bytes.write(1, DisguiseUtilities.getPitch(disguise.getType(), entity.getType(), pitchValue)); + if (sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT && + disguise.getType() == DisguiseType.ITEM_FRAME) { + StructureModifier doubles = movePacket.getDoubles(); - if (sentPacket.getType() == PacketType.Play.Server.ENTITY_TELEPORT && - disguise.getType() == DisguiseType.ITEM_FRAME) { - StructureModifier doubles = movePacket.getDoubles(); + Location loc = entity.getLocation(); - Location loc = entity.getLocation(); + double data = (((loc.getYaw() % 360) + 720 + 45) / 90) % 4; - double data = (((loc.getYaw() % 360) + 720 + 45) / 90) % 4; - - if (data % 2 == 0) { if (data % 2 == 0) { - doubles.write(3, loc.getZ()); - } else { - doubles.write(1, loc.getZ()); + if (data % 2 == 0) { + doubles.write(3, loc.getZ()); + } else { + doubles.write(1, loc.getZ()); + } + } + + double y = DisguiseUtilities.getYModifier(entity, disguise); + + if (y != 0) { + doubles.write(2, doubles.read(2) + y); } } - - double y = DisguiseUtilities.getYModifier(entity, disguise); - - if (y != 0) { - doubles.write(2, doubles.read(2) + y); - } } - } } } 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 f220d60d..feb33299 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 @@ -303,11 +303,21 @@ public class PacketHandlerSpawn implements IPacketHandler { disguise.getWatcher())); } else if (disguise.getType().isMisc()) { int data = ((MiscDisguise) disguise).getData(); + double x = loc.getX(); + double y = loc.getY(); + double z = loc.getZ(); if (disguise.getType() == DisguiseType.FALLING_BLOCK) { ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock(); data = ReflectionManager.getCombinedIdByItemStack(block); + + if (((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) { + // Center the block + x = loc.getBlockX() + 0.5; + y = loc.getBlockY(); + z = loc.getBlockZ() + 0.5; + } } else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) { // If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners // entity id @@ -318,8 +328,8 @@ public class PacketHandlerSpawn implements IPacketHandler { Object entityType = ReflectionManager.getEntityType(disguise.getType().getEntityType()); - Object[] params = new Object[]{disguisedEntity.getEntityId(), disguisedEntity.getUniqueId(), loc.getX(), - loc.getY(), loc.getZ(), loc.getPitch(), loc.getYaw(), entityType, data, + Object[] params = new Object[]{disguisedEntity.getEntityId(), disguisedEntity.getUniqueId(), x, y, z, + loc.getPitch(), loc.getYaw(), entityType, data, ReflectionManager.getVec3D(disguisedEntity.getVelocity())}; PacketContainer spawnEntity = ProtocolLibrary.getProtocolManager()