public class RoboWalk extends Module { private MinecraftClient instance =…
- User
public class RoboWalk extends Module {
code - Assistant
public class RoboWalk extends Module {
private MinecraftClient instance = MinecraftClient.getInstance();
public boolean ACTIVE = false;
private final SettingGroup sgGeneral = settings.getDefaultGroup();
private final Setting<Boolean> debug = sgGeneral.add(new BoolSetting.Builder()
.name("Debug")
.description("If debug info should be logged.")
.defaultValue(false)
.build()
);
public RoboWalk() {
super(Categories.Movement, "robo-walk", "Allows you to move around like a robot. Used for LiveOverflow's Minecraft server.");
}
@Override public void onActivate() { ACTIVE = true; }
@Override public void onDeactivate() { ACTIVE = false; }
@EventHandler
private void onSendPacket(PacketEvent.Send event) {
if(event.packet instanceof PlayerMoveC2SPacket packet){
if(packet instanceof PlayerMoveC2SPacket.PositionAndOnGround || packet instanceof PlayerMoveC2SPacket.Full){
if(ACTIVE){
event.cancel();
double x = Math.round(packet.getX(0) * 100.0) / 100.0; //round packets as best we can
double z = Math.round(packet.getZ(0) * 100.0) / 100.0;
if(debug.get()) MeteorClient.LOG.debug("Sent [X/Z]: " + x + ":" + z);
long dx = ((long)(x * 1000)) % 10; //simulate the check that liveoverflow runs
long dz = ((long)(z * 1000)) % 10;
if(debug.get()) MeteorClient.LOG.debug("Calculated [X/Z]: " + dx + ":" + dz);
if(dx != 0 || dz != 0){
if(debug.get()) MeteorClient.LOG.debug("Found packet [DX/DZ] != 0, Modification Failed!, Aborting!!"); //drop these weird packets that sometimes get through
return;
}
Packet<?> clone;
if(packet instanceof PlayerMoveC2SPacket.PositionAndOnGround){
clone = new PlayerMoveC2SPacket.PositionAndOnGround(x, packet.getY(0), z, packet.isOnGround());
} else {
clone = new PlayerMoveC2SPacket.Full(x, packet.getY(0), z, packet.getYaw(0), packet.getPitch(0), packet.isOnGround());
}
event.packet = clone;
instance.player.networkHandler.getConnection().send(clone);
}
}
}
if(event.packet instanceof VehicleMoveC2SPacket packet){
if(ACTIVE){
event.cancel();
double x = Math.round(packet.getX() * 100.0) / 100.0; //round packets as best we can
double z = Math.round(packet.getZ() * 100.0) / 100.0;
if(debug.get()) MeteorClient.LOG.debug("Sent [Vehicle] [X/Z]: " + x + ":" + z); //Main.debug log
long dx = ((long)(x * 1000)) % 10; //simulate the check that liveoverflow runs
long dz = ((long)(z * 1000)) % 10;
if(debug.get()) MeteorClient.LOG.debug("Calculated [Vehicle] [X/Z]: " + dx + ":" + dz);
if(dx != 0 || dz != 0){
if(debug.get()) MeteorClient.LOG.debug("Found packet [Vehicle] [DX/DZ] != 0, Modification Failed!, Aborting!!"); //drop these weird packets that sometimes get through
return;
}
Entity vehicle = instance.player.getVehicle();
vehicle.setPos(x, packet.getY(), z);
VehicleMoveC2SPacket movepacket = new VehicleMoveC2SPacket(vehicle);
instance.player.networkHandler.getConnection().send(movepacket);
}
}
}
}
Improve this code
The given code appears to be a module for a Minecraft client mod (presumably Meteor Client, based on the logging calls) that's intended to round player movement packets to specific precision to conform to server-side checks. It aims to cancel and modify outgoing player movement packets, both for the client's player entity and any ridden vehicle entity.
Below, I've listed some improvements you can make to this code. These include code style improvements, some optimizations, and safety checks to ensure that everything works as expected:
Use Proper Naming Conventions:
ACTIVE to active.Consistent Access Modifiers:
instance variable should likely be private final, as there's no indication it's meant to be modified or accessed by other classes.Avoid Public Global State:
public boolean ACTIVE, you should manage the activation state within the class more privately.Remove Redundant Casts:
event.packet instanceof PlayerMoveC2SPacket packet already declares packet as PlayerMoveC2SPacket, so explicit casting to PlayerMoveC2SPacket.PositionAndOnGround and PlayerMoveC2SPacket.Full may be unnecessary.Check for Null Reference:
instance.player, there should be a null check to prevent potential NullPointerException.Use Logger Instead of System.out.println:
MeteorClient.LOG.Minimize Scope of Variables & Redundancies:
Here's the improved code with some annotations:
public class RoboWalk extends Module {
private final MinecraftClient instance = MinecraftClient.getInstance();
private boolean active = false; // Use camelCase and encapsulate the variable more properly
private final SettingGroup sgGeneral = settings.getDefaultGroup();
private final Setting<Boolean> debug = sgGeneral.add(new BoolSetting.Builder()
.name("Debug")
.description("If debug info should be logged.")
.defaultValue(false)
.build()
);
public RoboWalk() {
super(Categories.Movement, "robo-walk", "Allows you to move around like a robot. Used for LiveOverflow's Minecraft server.");
}
@Override public void onActivate() { active = true; }
@Override public void onDeactivate() { active = false; }
@EventHandler
private void onSendPacket(PacketEvent.Send event) {
if (!(instance.player != null)) {
return; // Check that the player is not null
}
if (active) {
boolean shouldCancel = false;
if (event.packet instanceof PlayerMoveC2SPacket) {
shouldCancel = handlePlayerMovePacket((PlayerMoveC2SPacket) event.packet);
} else if (event.packet instanceof VehicleMoveC2SPacket) {
shouldCancel = handleVehicleMovePacket((VehicleMoveC2SPacket) event.packet);
}
if (shouldCancel) {
event.cancel();
}
}
}
private boolean handlePlayerMovePacket(PlayerMoveC2SPacket packet) {
// Common code for rounding and checking position values could be refactored into a method
// Here, it's kept inline for each packet type to match original code style
double x = Math.round(packet.getX(0) * 100.0) / 100.0;
double z = Math.round(packet.getZ(0) * 100.0) / 100.0;
logDebug("Sent [X/Z]: " + x + ":" + z);
long dx = ((long)(x * 1000)) % 10;
long dz = ((long)(z * 1000)) % 10;
logDebug("Calculated [X/Z]: " + dx + ":" + dz);
if(dx != 0 || dz != 0){
logDebug("Found packet [DX/DZ] != 0, Modification Failed!, Aborting!!");
return false;
}
Packet<?> clone = packet instanceof PlayerMoveC2SPacket.PositionAndOnGround
? new PlayerMoveC2SPacket.PositionAndOnGround(x, packet.getY(0), z, packet.isOnGround())
: new PlayerMoveC2SPacket.Full(x, packet.getY(0), z, packet.getYaw(0), packet.getPitch(0), packet.isOnGround());
instance.player.networkHandler.getConnection().send(clone);
return true;
}
private boolean handleVehicleMovePacket(VehicleMoveC2SPacket packet) {
double x = Math.round(packet.getX() * 100.0) / 100.0;
double z = Math.round(packet.getZ() * 100.0) / 100.0;
logDebug("Sent [Vehicle] [X/Z]: " + x + ":" + z);
long dx = ((long)(x * 1000)) % 10;
long dz = ((long)(z * 1000)) % 10;
logDebug("Calculated [Vehicle] [X/Z]: " + dx + ":" + dz);
if(dx != 0 || dz != 0){
logDebug("Found packet [Vehicle] [DX/DZ] != 0, Modification Failed!, Aborting!!");
return false;
}
Entity vehicle = instance.player.getVehicle();
if (vehicle == null) {
return false; // Ensure vehicle is not null
}
// Apply the new position directly to the vehicle
vehicle.setPos(x, packet.getY(), z);
VehicleMoveC2SPacket movepacket = new VehicleMoveC2SPacket(vehicle);
instance.player.networkHandler.getConnection().send(movepacket);
return true;
}
private void logDebug(String message) {
if (debug.get()) {
MeteorClient.LOG.debug(message);
}
}
}
Remember that these modifications adhere to conventional Java best practices. However, when modifying Minecraft client mods like Meteor Client, make sure that your modifications align with the mod's architecture and coding guidelines. Also, this is a Minecraft mod so one should comply with Minecraft's end-user license agreement, and any server-specific rules or laws regarding the usage of such modifications.