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

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

import ftbsc.bscv.BoSCoVicino;
import ftbsc.bscv.api.IModule;
import ftbsc.bscv.system.ModManager;
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 AbstractModule implements IModule<AbstractModule.Group> {
   public enum Group {
      SELF,
      HUD,
      BUILD,
      DEFENSE,
      VISION,
      MOTION,
   }

   protected ForgeConfigSpec.ConfigValue<Boolean> enabled;

   @Override
   public ForgeConfigSpec.Builder getConfigBuilder() {
      return BoSCoVicino.modManager.getCfgBuilder();
   }

   @Override
   public CommandDispatcher<CommandSource> getDispatcher() {
      return BoSCoVicino.modManager.getDispatcher();
   }

   public AbstractModule() {
      this.getConfigBuilder().push(this.getName().toLowerCase());
      this.enabled = this.getConfigBuilder()
         .comment(String.format("Enables %s", this.getName()))
         .define("enabled", false);

      // TODO: can this be made an util or moved somewhere else?
      this.getDispatcher().register(
         Commands.literal(this.getName().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.getName(), this.enabled.get() ? "ON" : "OFF"));
               // TODO: print all mod options!
               return 1;
            })
      );

      this.getConfigBuilder().pop();
   }

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

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

   @Override
   public boolean isEnabled() {
      return enabled.get();
   }
}