summaryrefslogtreecommitdiff
path: root/src/main/java/co/fantabos/bscv/Module.java
blob: 620ece2b54b57f709d42f67ed8cc6d9712d6edc4 (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
package co.fantabos.bscv;

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

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

import static co.fantabos.bscv.BoSCoVicino.log;

public abstract class Module {
   public enum Group {
      CORE,
      BUILD,
      DEFENSE,
      VISION,
   }

   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 <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 -> {
                        conf.set(ctx.getArgument(name, clazz));
                        conf.save();
                        log(String.format("> %s -> %s", name, 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 enabled", this.name));
      BoSCoVicino.LOGGER.info(String.format("%s enabled", this.name));
   }

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