Skip to content

Commit

Permalink
Add utils for modifying PlayerMoveC2SPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Dec 11, 2024
1 parent 49851e2 commit 6e221a2
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/main/java/net/wurstclient/hacks/AntiHungerHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down Expand Up @@ -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)
Expand All @@ -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));
}
}
112 changes: 112 additions & 0 deletions src/main/java/net/wurstclient/util/PacketUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 6e221a2

Please sign in to comment.