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

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

import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;

import static ftbsc.bscv.BoSCoVicino.log;

public abstract class Module {
   public enum Group {
      SELF,
      HUD,
      BUILD,
      DEFENSE,
      VISION,
      MOTION,
   }

   public final String name;
   public final Group  group;
   public final ForgeConfigSpec.ConfigValue<Boolean> enabled;

   protected Module(String name, Group group, ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
      this.name  = name;
      this.group = group;

      builder.push(this.name.toLowerCase());
      this.enabled = builder
         .comment(String.format("Enables %s", this.name))
         .define("enabled", false);

      // TODO: can this be made an util or moved somewhere else?
      dispatcher.register(
         Commands.literal(this.name.toLowerCase())
            .then(
               Commands.literal("toggle")
                  .then(
                     Commands.argument("state", BoolArgumentType.bool())
                        .executes( ctx -> {
                           boolean value = ctx.getArgument("state", Boolean.class);
                           if (this.enabled.get() ^ value) { // must change
                              if (value) { this.enable(); }
                              else { this.disable(); }
                           }
                           return 1;
                        })
                  )
                  .executes(ctx -> {
                     this.toggle();
                     return 1;
                  })
            )
            .executes(ctx -> {
               log(String.format("%s [%s]", this.name, this.enabled.get() ? "ON" : "OFF"));
               // TODO: print all mod options!
               return 1;
            })
      );
   }

   public Module done(ForgeConfigSpec.Builder builder) {
      builder.pop();
      return this;
   }

   protected void onEnabled()  {}
   protected void onDisabled() {}

   public final void toggle() {
      if (this.enabled.get()) this.disable();
      else this.enable();
   }

   public final void enable() {
      MinecraftForge.EVENT_BUS.register(this);
      this.enabled.set(true);
      // this.enabled.save();
      this.onEnabled();
      log(String.format("%s ON", this.name));
   }

   public final void disable() {
      MinecraftForge.EVENT_BUS.unregister(this);
      this.enabled.set(false);
      // this.enabled.save();
      this.onDisabled();
      log(String.format("%s OFF", this.name));
   }
}