summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/Module.java
blob: 3cdc50aa920fc779e0bf5411f207294b2e3ce78f (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ftbsc.bscv.modules;

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

import ftbsc.bscv.BoSCoVicino;
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);

      dispatcher.register(
         Commands.literal(this.name.toLowerCase())
            .then(
               Commands.literal("toggle")
                  .executes(ctx -> {
                     this.toggle();
                     return 1;
                  })
            )
            .executes(ctx -> {
               log(String.format("=[ %s ]%s", this.name, this.enabled.get() ? "+" : ""));
               // TODO: print all mod options!
               return 1;
            })
      );
   }

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


   // TODO can I merge these two option into one? Maybe redo with builder pattern?

   public <T extends Enum<T>> ForgeConfigSpec.EnumValue<T> optionEnum(
      String name,
      String comment,
      T fallback,
      ArgumentType<T> argument,
      Class<T> clazz,
      ForgeConfigSpec.Builder builder,
      CommandDispatcher<CommandSource> dispatcher
   ) {
      ForgeConfigSpec.EnumValue<T> conf = builder
         .comment(comment)
         .defineEnum(name, fallback);

      dispatcher.register(
         Commands.literal(this.name.toLowerCase())
            .then(
               Commands.literal(name)
               .then(
                  Commands.argument(name, argument)
                     .executes( ctx -> {
                        T value = ctx.getArgument(name, clazz);
                        conf.set(value);
                        conf.save();
                        log(String.format("> %s -> %s <", String.join(".", conf.getPath()), conf.get().toString()));
                        return 1;
                     }))
               .executes(ctx -> {
                  log(String.format("> %s: %s <", String.join(".", conf.getPath()), conf.get().toString()));
                  return 1;
               })
            )
      );

      return conf;
   }

   public <T> ForgeConfigSpec.ConfigValue<T> option(
      String name,
      String comment,
      T fallback,
      ArgumentType<T> argument,
      Class<T> clazz,
      ForgeConfigSpec.Builder builder,
      CommandDispatcher<CommandSource> dispatcher
   ) {
      ForgeConfigSpec.ConfigValue<T> conf = builder
         .comment(comment)
         .define(name, fallback);

      dispatcher.register(
         Commands.literal(this.name.toLowerCase())
            .then(
               Commands.literal(name)
               .then(
                  Commands.argument(name, argument)
                     .executes( ctx -> {
                        T value = ctx.getArgument(name, clazz);
                        conf.set(value);
                        conf.save();
                        log(String.format("> %s -> %s <", String.join(".", conf.getPath()), conf.get().toString()));
                        return 1;
                     }))
               .executes(ctx -> {
                  log(String.format("> %s: %s <", name, conf.get().toString()));
                  return 1;
               })
            )
      );

      return conf;
   }

   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));
   }
}