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

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.IModule;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.common.ForgeConfigSpec;

import static ftbsc.bscv.Boscovicino.log;

public abstract class AbstractModule implements IModule, ICommons {
   protected ForgeConfigSpec.ConfigValue<Boolean> enabled;

   @Override
   public String getName() {
      return this.getClass().getSimpleName();
   }

   @Override
   public String getGroup() {
      String[] pkg = this.getClass().getPackage().getName().split("\\.");
      return pkg[pkg.length - 1].toUpperCase();
   }

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

   public void enable() {
      if (!this.enabled.get()) {
         this.enabled.set(true);
         this.enabled.save();
      }
      IModule.super.enable();
      log(String.format("%s ON", this.getName()));
   }

   public void disable() {
      IModule.super.disable();
      if (this.enabled.get()) {
         this.enabled.set(false);
         this.enabled.save();
      }
      log(String.format("%s OFF", this.getName()));
   }

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