summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/motion/VanillaFlight.java
blob: ae00d3db8ad7424fc1e7a95ee5bd8b692a568733 (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
package ftbsc.bscv.modules.motion;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;

import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.modules.QuickModule;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class VanillaFlight extends QuickModule implements ICommons {

   public final ForgeConfigSpec.ConfigValue<Boolean> force;
   public final ForgeConfigSpec.ConfigValue<Double>  speed;
   public final ForgeConfigSpec.ConfigValue<Boolean> antikick;
   public final ForgeConfigSpec.ConfigValue<Double>  antikick_magnitude;
   public final ForgeConfigSpec.ConfigValue<Integer> antikick_cycle;

   private final float minDescent = 0.03125f;
   private final int maxTicks = 80;
   private int tick = 0;

   public VanillaFlight(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
      super("VanillaFlight", Group.MOTION, UNBOUND, builder, dispatcher);

      this.force = this.option(
         "force", "force enable flight on user", false,
         BoolArgumentType.bool(), Boolean.class,
         builder, dispatcher
      );

      this.speed = this.option(
         "speed", "flight speed to set", 0.05,
         DoubleArgumentType.doubleArg(0.0), Double.class,
         builder, dispatcher
      );

      this.antikick = this.option(
         "antikick", "prevent vanilla flight kick by descending", false,
         BoolArgumentType.bool(), Boolean.class,
         builder, dispatcher
      );

      this.antikick_magnitude = this.option(
         "magnitude", "magnitude of antikick push", 1.0,
         DoubleArgumentType.doubleArg(0.0), Double.class,
         builder, dispatcher
      );

      this.antikick_cycle = this.option(
         "cycle", "how often to run antikick routine", 0,
         IntegerArgumentType.integer(0, 79), Integer.class,
         builder, dispatcher
      );
   }

   private boolean couldFlyBefore = false;
   private float flyingSpeedBefore = 0.05f;

   @SubscribeEvent
   public void onTick(TickEvent.ClientTickEvent event) {
      ClientPlayerEntity player = MC.player;
      if (player == null) return;

      player.abilities.mayfly = true;
      player.abilities.setFlyingSpeed(this.speed.get().floatValue());
      if (this.force.get()) {
         player.abilities.flying = true;
      }
      if (this.antikick.get()) {
         if (this.tick != 0 && this.tick % (maxTicks - this.antikick_cycle.get()) == 0) {
            player.push(0.0, -(this.antikick_magnitude.get() * minDescent), 0.0);
            this.tick = 0;
         } else if (this.tick == 0) {
            player.push(0.0, this.antikick_magnitude.get() * minDescent, 0.0);
            this.tick++;
         } else {
            this.tick++;
         }
      }
   }

   @Override
   protected void onEnabled() {
      if (MC.player != null) {
         this.couldFlyBefore = MC.player.abilities.mayfly;
         this.flyingSpeedBefore = MC.player.abilities.getFlyingSpeed();
         BoSCoVicino.log(String.format("Flying speed before = %f", this.flyingSpeedBefore));
      }
   }

   @Override
   protected void onDisabled() {
      ClientPlayerEntity player = MC.player;
      if (player != null) {
         player.abilities.mayfly = this.couldFlyBefore;
         player.abilities.setFlyingSpeed(this.flyingSpeedBefore);
         if (this.force.get()) {
            player.abilities.flying = false;
         }
      }
   }
}