diff options
author | alemi <me@alemi.dev> | 2023-02-13 18:26:28 +0100 |
---|---|---|
committer | alemi <me@alemi.dev> | 2023-02-13 18:26:28 +0100 |
commit | 8cbe8e9a5ffc5e82bf59433a99d38bbe2aeaf24b (patch) | |
tree | 859974955b4e0eb9d32fc0a91f39598305fb124b /src/main/java | |
parent | 4869fcb698a46a528331bd8d6348b4c18dab490b (diff) |
feat: finished basic freecam module
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ftbsc/bscv/module/self/Freecam.java | 91 | ||||
-rw-r--r-- | src/main/java/ftbsc/bscv/tools/Keyboard.java | 19 |
2 files changed, 100 insertions, 10 deletions
diff --git a/src/main/java/ftbsc/bscv/module/self/Freecam.java b/src/main/java/ftbsc/bscv/module/self/Freecam.java index b3eff57..2161ce9 100644 --- a/src/main/java/ftbsc/bscv/module/self/Freecam.java +++ b/src/main/java/ftbsc/bscv/module/self/Freecam.java @@ -2,10 +2,15 @@ package ftbsc.bscv.module.self; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; +import com.mojang.brigadier.arguments.DoubleArgumentType; import ftbsc.bscv.BoSCoVicino; import ftbsc.bscv.events.PacketEvent; +import ftbsc.bscv.ICommons; import ftbsc.bscv.module.QuickModule; +import ftbsc.bscv.tools.Keyboard; +import net.minecraft.client.entity.player.RemoteClientPlayerEntity; +import net.minecraft.client.network.play.NetworkPlayerInfo; import net.minecraft.command.CommandSource; import net.minecraft.network.play.client.CPlayerPacket; import net.minecraft.util.math.vector.Vector3d; @@ -14,9 +19,16 @@ import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; -public class Freecam extends QuickModule { +public class Freecam extends QuickModule implements ICommons { public final ForgeConfigSpec.ConfigValue<Boolean> log; + public final ForgeConfigSpec.ConfigValue<Double> speed; + public final ForgeConfigSpec.ConfigValue<Boolean> drift; + + private Vector3d prev_pos = new Vector3d(0.0, 0.0, 0.0); + private float prev_speed = 0.05f; + private GameType prev_gamemode = GameType.SURVIVAL; + private MockPlayer mock_player; public Freecam(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { super("Freecam", Group.SELF, UNBOUND, builder, dispatcher); @@ -26,13 +38,23 @@ public class Freecam extends QuickModule { BoolArgumentType.bool(), Boolean.class, builder, dispatcher ); - } - private Vector3d prev_pos = new Vector3d(0.0, 0.0, 0.0); + this.speed = this.option( + "speed", "flight speed in freecam", 0.05, + DoubleArgumentType.doubleArg(0.0), Double.class, + builder, dispatcher + ); + + this.drift = this.option( + "drift", "allow inertia drift in freecam", true, + BoolArgumentType.bool(), Boolean.class, + builder, dispatcher + ); + } @SubscribeEvent public void onPacket(PacketEvent event) { - if (BoSCoVicino.minecraft.player == null) return; + if (MC.player == null) return; if (event.outgoing && event.packet instanceof CPlayerPacket) { // TODO must ignore more packets than just this if (this.log.get()) { BoSCoVicino.log(String.format("[X] %s", event.packet.getClass().getName())); @@ -41,20 +63,69 @@ public class Freecam extends QuickModule { } } + @SubscribeEvent + protected void onTick(TickEvent.ClientTickEvent event) { + if (MC.player == null) return; + MC.player.abilities.setFlyingSpeed(this.speed.get().floatValue()); + if (!this.drift.get() && !Keyboard.isStrafing()) { + MC.player.setDeltaMovement(Vector3d.ZERO); + } + } + @Override protected void onEnabled() { - if (BoSCoVicino.minecraft.player != null) { - this.prev_pos = BoSCoVicino.minecraft.player.position(); - BoSCoVicino.minecraft.gameMode.setLocalMode(GameType.SPECTATOR); + if (MC.player == null) { + BoSCoVicino.log("[!] Can only enable freecam while in-game"); + this.disable(); + } + + this.prev_speed = MC.player.abilities.getFlyingSpeed(); + this.prev_pos = MC.player.position(); + this.prev_gamemode = MC.gameMode.getPlayerMode(); + MC.gameMode.setLocalMode(GameType.SPECTATOR); + MC.player.noCulling = true; + if (MC.getConnection() != null) { + NetworkPlayerInfo info = MC.getConnection().getPlayerInfo( + MC.player.getGameProfile().getId() + ); + info.gameMode = GameType.SPECTATOR; // ACCESSTRANSFORMER: public net.minecraft.client.network.play.NetworkPlayerInfo field_178866_b } + + this.mock_player = new MockPlayer(); + this.mock_player.setPosAndOldPos(this.prev_pos.x, this.prev_pos.y, this.prev_pos.z); + this.mock_player.setYBodyRot(MC.player.yBodyRot); + MC.level.addPlayer(-666, this.mock_player); } @Override protected void onDisabled() { - if (BoSCoVicino.minecraft.player != null) { - BoSCoVicino.minecraft.gameMode.setLocalMode(GameType.SURVIVAL); - BoSCoVicino.minecraft.player.setPos(this.prev_pos.x, this.prev_pos.y, this.prev_pos.z); + if (MC.player == null) return; + MC.gameMode.setLocalMode(this.prev_gamemode); + MC.player.noCulling = false; + MC.player.abilities.setFlyingSpeed(this.prev_speed); + if (MC.getConnection() != null) { + NetworkPlayerInfo info = MC.getConnection().getPlayerInfo( + MC.player.getGameProfile().getId() + ); + info.gameMode = this.prev_gamemode; // ACCESSTRANSFORMER: public net.minecraft.client.network.play.NetworkPlayerInfo field_178866_b } + MC.player.setPos(this.prev_pos.x, this.prev_pos.y, this.prev_pos.z); + MC.player.setDeltaMovement(Vector3d.ZERO); + MC.level.removeEntity(this.mock_player.getId()); + this.mock_player = null; // get rid of reference } + private class MockPlayer extends RemoteClientPlayerEntity { + public MockPlayer() { + super(MC.level, MC.player.getGameProfile()); + this.setId(-666); // TODO hax + } + + @Override + public boolean isSpectator() { return false; } + + @Override + public boolean isCreative() { return false; } + + } } diff --git a/src/main/java/ftbsc/bscv/tools/Keyboard.java b/src/main/java/ftbsc/bscv/tools/Keyboard.java new file mode 100644 index 0000000..29a5a03 --- /dev/null +++ b/src/main/java/ftbsc/bscv/tools/Keyboard.java @@ -0,0 +1,19 @@ +package ftbsc.bscv.tools; + +import ftbsc.bscv.ICommons; + +public class Keyboard implements ICommons { + + public static boolean isStrafing() { + return MC.options.keyUp.isDown() + || MC.options.keyDown.isDown() + || MC.options.keyLeft.isDown() + || MC.options.keyRight.isDown(); + } + + public static boolean isMoving() { + return isStrafing() + || MC.options.keyJump.isDown() + || MC.options.keySprint.isDown(); + } +} |