From 036f298003259d9fe0ec937028a52c03638fe984 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 20 Jul 2020 13:56:12 +1200 Subject: [PATCH] Smarter falling block snapping to grid, allow 0.5 Y and snap to block above if Y above 0.85 --- .../watchers/FallingBlockWatcher.java | 3 +- .../packethandlers/PacketHandlerMovement.java | 31 ++++++++++++------- .../packethandlers/PacketHandlerSpawn.java | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) 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 9b37d88f..b5c6706b 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/FallingBlockWatcher.java @@ -51,7 +51,8 @@ public class FallingBlockWatcher extends FlagWatcher { 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(1, conRel(loc.getY(), + loc.getBlockY() + (loc.getY() % 1 >= 0.85 ? 1 : loc.getY() % 1 >= 0.35 ? .5 : 0))); shorts.write(2, conRel(loc.getZ(), loc.getBlockZ() + 0.5)); try { 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 26a53b2e..a49ea613 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 @@ -33,7 +33,7 @@ public class PacketHandlerMovement implements IPacketHandler { PacketType.Play.Server.ENTITY_TELEPORT, PacketType.Play.Server.REL_ENTITY_MOVE}; } - private short conRel(int oldCord, int newCord) { + private short conRel(double oldCord, double newCord) { return (short) ((oldCord - newCord) * 4096); } @@ -87,21 +87,24 @@ public class PacketHandlerMovement implements IPacketHandler { if (sentPacket.getType() != PacketType.Play.Server.ENTITY_TELEPORT) { StructureModifier shorts = movePacket.getShorts(); - Location current = entity.getLocation(); + Location origLoc = entity.getLocation(); Vector diff = new Vector(shorts.read(0) / 4096D, shorts.read(1) / 4096D, shorts.read(2) / 4096D); - Location newLoc = current.clone().subtract(diff); + Location newLoc = origLoc.clone().subtract(diff); - boolean sameBlock = - current.getBlockX() == newLoc.getBlockX() && current.getBlockY() == newLoc.getBlockY() && - current.getBlockZ() == newLoc.getBlockZ(); + double origY = + origLoc.getBlockY() + (origLoc.getY() % 1 >= 0.85 ? 1 : origLoc.getY() % 1 >= 0.35 ? .5 : 0); + double newY = newLoc.getBlockY() + (newLoc.getY() % 1 >= 0.85 ? 1 : newLoc.getY() % 1 >= 0.35 ? .5 : 0); + + boolean sameBlock = origLoc.getBlockX() == newLoc.getBlockX() && newY == origY && + origLoc.getBlockZ() == newLoc.getBlockZ(); if (sameBlock) { - // Make no modifications + // Make no modifications but don't send anything 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())); + shorts.write(0, conRel(origLoc.getBlockX(), newLoc.getBlockX())); + shorts.write(1, conRel(origY, newY)); + shorts.write(2, conRel(origLoc.getBlockZ(), newLoc.getBlockZ())); } } else { Location loc = entity.getLocation(); @@ -109,7 +112,13 @@ public class PacketHandlerMovement implements IPacketHandler { StructureModifier doubles = movePacket.getDoubles(); // Center the block doubles.write(0, loc.getBlockX() + 0.5); - doubles.write(1, (double) loc.getBlockY()); + + double y = loc.getBlockY(); + + // Force into a multiple of 0.25 + y += Math.floor((loc.getY() % 1) * 4) / 4D; + + doubles.write(1, y); doubles.write(2, loc.getBlockZ() + 0.5); } 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 13567500..3fd92465 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 @@ -311,7 +311,7 @@ public class PacketHandlerSpawn implements IPacketHandler { if (((FallingBlockWatcher) disguise.getWatcher()).isGridLocked()) { // Center the block x = loc.getBlockX() + 0.5; - y = loc.getBlockY(); + y = loc.getBlockY() + (loc.getY() % 1 >= 0.85 ? 1 : loc.getY() % 1 >= 0.35 ? .5 : 0); z = loc.getBlockZ() + 0.5; } } else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) {