diff --git a/src/main/java/net/wurstclient/hacks/AntiHungerHack.java b/src/main/java/net/wurstclient/hacks/AntiHungerHack.java index 0a04534856..355badfd7f 100644 --- a/src/main/java/net/wurstclient/hacks/AntiHungerHack.java +++ b/src/main/java/net/wurstclient/hacks/AntiHungerHack.java @@ -7,13 +7,13 @@ */ package net.wurstclient.hacks; -import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; import net.wurstclient.Category; import net.wurstclient.SearchTags; import net.wurstclient.events.PacketOutputListener; import net.wurstclient.hack.DontSaveState; import net.wurstclient.hack.Hack; +import net.wurstclient.util.PacketUtils; @DontSaveState @SearchTags({"anti hunger"}) @@ -41,7 +41,7 @@ protected void onDisable() @Override public void onSentPacket(PacketOutputEvent event) { - if(!(event.getPacket() instanceof PlayerMoveC2SPacket oldPacket)) + if(!(event.getPacket() instanceof PlayerMoveC2SPacket packet)) return; if(!MC.player.isOnGround() || MC.player.fallDistance > 0.5) @@ -50,28 +50,6 @@ public void onSentPacket(PacketOutputEvent event) if(MC.interactionManager.isBreakingBlock()) return; - double x = oldPacket.getX(-1); - double y = oldPacket.getY(-1); - double z = oldPacket.getZ(-1); - float yaw = oldPacket.getYaw(-1); - float pitch = oldPacket.getPitch(-1); - boolean horizontalCollision = oldPacket.horizontalCollision(); - - Packet newPacket; - if(oldPacket.changesPosition()) - if(oldPacket.changesLook()) - newPacket = new PlayerMoveC2SPacket.Full(x, y, z, yaw, pitch, - false, horizontalCollision); - else - newPacket = new PlayerMoveC2SPacket.PositionAndOnGround(x, y, z, - false, horizontalCollision); - else if(oldPacket.changesLook()) - newPacket = new PlayerMoveC2SPacket.LookAndOnGround(yaw, pitch, - false, horizontalCollision); - else - newPacket = new PlayerMoveC2SPacket.OnGroundOnly(false, - horizontalCollision); - - event.setPacket(newPacket); + event.setPacket(PacketUtils.modifyOnGround(packet, false)); } } diff --git a/src/main/java/net/wurstclient/util/PacketUtils.java b/src/main/java/net/wurstclient/util/PacketUtils.java new file mode 100644 index 0000000000..92fddeda8a --- /dev/null +++ b/src/main/java/net/wurstclient/util/PacketUtils.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2014-2024 Wurst-Imperium and contributors. + * + * This source code is subject to the terms of the GNU General Public + * License, version 3. If a copy of the GPL was not distributed with this + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt + */ +package net.wurstclient.util; + +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket; +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full; +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.LookAndOnGround; +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.OnGroundOnly; +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround; + +public enum PacketUtils +{ + ; + + /** + * Creates a new PlayerMoveC2SPacket with modified position values. If the + * input packet if of a type that can't hold position data, it will be + * upgraded to PositionAndOnGround or Full as needed. + */ + public static PlayerMoveC2SPacket modifyPosition(PlayerMoveC2SPacket packet, + double x, double y, double z) + { + if(packet instanceof LookAndOnGround) + return new Full(x, y, z, packet.getYaw(0), packet.getPitch(0), + packet.isOnGround(), packet.horizontalCollision()); + + if(packet instanceof OnGroundOnly) + return new PositionAndOnGround(x, y, z, packet.isOnGround(), + packet.horizontalCollision()); + + if(packet instanceof Full) + return new Full(x, y, z, packet.getYaw(0), packet.getPitch(0), + packet.isOnGround(), packet.horizontalCollision()); + + return new PositionAndOnGround(x, y, z, packet.isOnGround(), + packet.horizontalCollision()); + } + + /** + * Creates a new PlayerMoveC2SPacket with modified rotation values. If the + * input packet is of a type that can't hold rotation data, it will be + * upgraded to LookAndOnGround or Full as needed. + */ + public static PlayerMoveC2SPacket modifyRotation(PlayerMoveC2SPacket packet, + float yaw, float pitch) + { + if(packet instanceof PositionAndOnGround) + return new Full(packet.getX(0), packet.getY(0), packet.getZ(0), yaw, + pitch, packet.isOnGround(), packet.horizontalCollision()); + + if(packet instanceof OnGroundOnly) + return new LookAndOnGround(yaw, pitch, packet.isOnGround(), + packet.horizontalCollision()); + + if(packet instanceof Full) + return new Full(packet.getX(0), packet.getY(0), packet.getZ(0), yaw, + pitch, packet.isOnGround(), packet.horizontalCollision()); + + return new LookAndOnGround(yaw, pitch, packet.isOnGround(), + packet.horizontalCollision()); + } + + /** + * Creates a new PlayerMoveC2SPacket with a modified onGround flag. + */ + public static PlayerMoveC2SPacket modifyOnGround(PlayerMoveC2SPacket packet, + boolean onGround) + { + if(packet instanceof Full) + return new Full(packet.getX(0), packet.getY(0), packet.getZ(0), + packet.getYaw(0), packet.getPitch(0), onGround, + packet.horizontalCollision()); + + if(packet instanceof PositionAndOnGround) + return new PositionAndOnGround(packet.getX(0), packet.getY(0), + packet.getZ(0), onGround, packet.horizontalCollision()); + + if(packet instanceof LookAndOnGround) + return new LookAndOnGround(packet.getYaw(0), packet.getPitch(0), + onGround, packet.horizontalCollision()); + + return new OnGroundOnly(onGround, packet.horizontalCollision()); + } + + /** + * Creates a new PlayerMoveC2SPacket with a modified horizontal collision + * flag. + */ + public static PlayerMoveC2SPacket modifyHorizontalCollision( + PlayerMoveC2SPacket packet, boolean horizontalCollision) + { + if(packet instanceof Full) + return new Full(packet.getX(0), packet.getY(0), packet.getZ(0), + packet.getYaw(0), packet.getPitch(0), packet.isOnGround(), + horizontalCollision); + + if(packet instanceof PositionAndOnGround) + return new PositionAndOnGround(packet.getX(0), packet.getY(0), + packet.getZ(0), packet.isOnGround(), horizontalCollision); + + if(packet instanceof LookAndOnGround) + return new LookAndOnGround(packet.getYaw(0), packet.getPitch(0), + packet.isOnGround(), horizontalCollision); + + return new OnGroundOnly(packet.isOnGround(), horizontalCollision); + } +}