aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/BoSCoVicino.java
blob: 6c06c42d0fe772223fb9e7a2a23d1a57ed28479f (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package ftbsc.bscv;

import net.minecraft.block.Block;
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.RegistryEvent;
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 com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import ftbsc.bscv.modules.Module;
import ftbsc.bscv.modules.vision.*;
import ftbsc.bscv.modules.motion.*;
import ftbsc.bscv.modules.self.*;
import ftbsc.bscv.modules.hud.*;

// The value here should match an entry in the META-INF/mods.toml file
@Mod("bscv")
public class BoSCoVicino implements ICommons {
   public static String MOD_ID = "bscv";

   // Directly reference a log4j logger.
   public static final Logger LOGGER = LogManager.getLogger();

   public static List<Module> mods;

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

   public static ForgeConfigSpec spec;

   public BoSCoVicino() {
      // Register the setup method for modloading
      FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);

      // load and register mods
      BoSCoVicino.mods = new ArrayList<Module>();

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

      BoSCoVicino.mods.add(new AutoDisconnect(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new ActiveModules(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new VanillaFlight(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new FastInteract(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new InfoDisplay(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new Coordinates(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new EntityList(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new Fullbright(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new AntiHunger(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new PortalGui(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new AutoFish(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new AutoTool(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new Freecam(cfg, dp).done(cfg));
      BoSCoVicino.mods.add(new BoatFly(cfg, dp).done(cfg));

      BoSCoVicino.spec = cfg.build();

      // register config handler
      ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.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 (Module m : BoSCoVicino.mods) {
         if (m.enabled.get()) 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 (Module mod : BoSCoVicino.mods) {
                  if (mod.enabled.get()) {
                     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");
      }
   }
}