aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/self/Freecam.java
blob: 2811178f72b28e9247dddb7d5f43d23fd78661f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ftbsc.bscv.modules.self;

import com.google.auto.service.AutoService;
import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.QuickModule;
import ftbsc.bscv.patches.PacketPatch.PacketEvent;
import ftbsc.bscv.tools.Keyboard;
import ftbsc.bscv.tools.Setting;
import net.minecraft.client.entity.player.RemoteClientPlayerEntity;
import net.minecraft.client.network.play.NetworkPlayerInfo;
import net.minecraft.network.play.client.CPlayerPacket;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.GameType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

@AutoService(ILoadable.class)
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;

   @Override
   protected int getDefaultKey() {
      return UNBOUND;
   }

   public Freecam() {
      super();

      this.log = Setting.Bool.builder()
         .name("log")
         .comment("log canceled packets")
         .fallback(false)
         .build(this);

      this.speed = Setting.Decimal.builder()
         .name("speed")
         .comment("flight speed in freecam")
         .fallback(0.05)
         .build(this);

      this.drift = Setting.Bool.builder()
         .name("drift")
         .comment("allow inertia drift in freecam")
         .fallback(true)
         .build(this);
   }

   @SubscribeEvent
   public void onPacket(PacketEvent.Outgoing event) {
      if (MC.player == null) return;
      if (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()));
         }
         event.setCanceled(true);
      }
   }

   @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.isMoving()) {
         MC.player.setDeltaMovement(Vector3d.ZERO);
      }
   }

   @Override
   public void enable() {
      if (MC.player == null) {
         Boscovicino.log("[!] Can only enable freecam while in-game");
         return;
      }

      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);
      super.enable();
   }

   @Override
   public void disable() {
      super.disable();
      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; }

   }
}