summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/Boscovicino.java
blob: d54a3d425a18d3db4ec022af2ddceadd26a5bf76 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ftbsc.bscv;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import ftbsc.bscv.api.IModule;
import ftbsc.bscv.system.Friends;
import ftbsc.bscv.system.ModManager;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.screen.IngameMenuScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig.Type;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;

@Mod("bscv")
public class Boscovicino implements ICommons {
   public static String MOD_ID = "bscv";

   public static final Logger LOGGER = LogManager.getLogger();

   public static ModManager modManager; //todo this should not be static

   private final CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();

   public static ForgeConfigSpec spec;

   private static Friends friends;
   public static Friends friends() { return Boscovicino.friends; }

   public Boscovicino() {
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);

      ForgeConfigSpec.Builder cfg = new ForgeConfigSpec.Builder();
      CommandDispatcher<CommandSource> dp = this.dispatcher;

      Boscovicino.modManager = new ModManager(cfg, dp);
      Boscovicino.modManager.load();

      Boscovicino.modManager.finish();

      Boscovicino.spec = cfg.build();

      ForgeConfigSpec.Builder friendSpec = new ForgeConfigSpec.Builder();
      Boscovicino.friends = new Friends(friendSpec, dp);

      // register config handler
      ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml");
      ModLoadingContext.get().registerConfig(Type.CLIENT, friendSpec.build(), "friends.toml");

      // Register ourselves for server and other game events we are interested in
      MinecraftForge.EVENT_BUS.register(this);
   }

   public static void log(String message) {
      LOGGER.info(message);
      if (MC.player != null) {
         MC.player.displayClientMessage(new StringTextComponent(message), true);
      }
   }

   private void clientSetup(final FMLClientSetupEvent event) {
      LOGGER.info("Initializing modules");

      for (IModule m : modManager.mods) {
         if (m.isEnabled()) m.enable();
      }

      // TEMPORARY! add command to regenerate suggestions
      dispatcher.register(
         Commands.literal("hints")
            .executes(ctx -> {
               ClientPlayerEntity player = MC.player;
               if (player != null) {
                  try {
                     Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
                     commands.setAccessible(true);
                     commands.set(player.connection, this.dispatcher);
                     LOGGER.info("Rebuild HINTS");
                     log("> rebuilt hints");
                     return 1;
                  } catch (NoSuchFieldException e) {
                     log("! no such field error");
                     LOGGER.error("No such field Exception while rebuilding hints");
                     return 0;
                  } catch (IllegalAccessException e) {
                     log("! illegal access error");
                     LOGGER.error("Illegal Access Exception while rebuilding hints");
                     return 0;
                  }
               } else {
                  log("! local player is NULL");
                  LOGGER.error("Local player is NULL");
                  return 0;
               }
            })
      );

      dispatcher.register(
         Commands.literal("toggle-all")
            .executes(ctx -> {
               for (IModule mod : modManager.mods) {
                  if (mod.isEnabled()) {
                     mod.disable();
                     mod.enable();
                  }
               }
               return 1;
            })
      );
   }

   @SubscribeEvent
   public void onClientChatEvent(ClientChatEvent event) {
      if (event.getMessage().startsWith("/")) {
         CommandSource source = MC.player.createCommandSourceStack(); // TODO player could be NULL
         try {
            LOGGER.info(String.format("Running command %s", event.getMessage()));
            this.dispatcher.execute(event.getMessage().substring(1), source);
            MC.gui.getChat().addRecentChat(event.getMessage());
            event.setCanceled(true);
         } catch (CommandSyntaxException e) {
            LOGGER.error(String.format("Syntax error in command : %s", e.toString()));
         }
      }
   }

   @SubscribeEvent
   public void onPauseMenu(InitGuiEvent.Post event) {
      if (event.getGui() instanceof IngameMenuScreen) { // TODO de-jank this, maybe by extending IngameMenuScreen
         IngameMenuScreen screen = (IngameMenuScreen) event.getGui();
         screen.buttons.remove(3);
         screen.buttons.remove(3);
         screen.children.remove(3);
         screen.children.remove(3);
         Button mods_btn = new Button(
            screen.width / 2 + 4,
            screen.height / 4 + 72 + -16, 98, 20,
            new TranslationTextComponent("fml.menu.mods"),
            cb -> {
               MC.setScreen(new net.minecraftforge.fml.client.gui.screen.ModListScreen(screen));
            }
         );
         // need to add it twice for it to work once...
         screen.buttons.add(3, mods_btn);
         screen.buttons.add(3, mods_btn);
         screen.children.add(3, mods_btn);
         screen.children.add(3, mods_btn);
      }
   }

   @SubscribeEvent
   public void onWorldLoad(WorldEvent.Load event) {
      // TEMPORARY! add command to regenerate suggestions
      ClientPlayerEntity player = MC.player;
      if (player != null) {
         try {
            Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
            commands.setAccessible(true);
            commands.set(player.connection, this.dispatcher);
            LOGGER.info("Rebuild HINTS");
            log("> rebuilt hints");
         } catch (NoSuchFieldException e) {
            LOGGER.error("No such field Exception while rebuilding hints");
         } catch (IllegalAccessException e) {
            LOGGER.error("Illegal Access Exception while rebuilding hints");
         }
      } else {
         LOGGER.error("Local player is NULL");
      }
   }
}