diff options
Diffstat (limited to 'src/main/java/ftbsc/bscv/BoSCoVicino.java')
-rw-r--r-- | src/main/java/ftbsc/bscv/BoSCoVicino.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/bscv/BoSCoVicino.java b/src/main/java/ftbsc/bscv/BoSCoVicino.java new file mode 100644 index 0000000..5ea29b1 --- /dev/null +++ b/src/main/java/ftbsc/bscv/BoSCoVicino.java @@ -0,0 +1,181 @@ +package ftbsc.bscv; + +import net.minecraft.block.Block; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.player.ClientPlayerEntity; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraft.util.text.StringTextComponent; +import net.minecraftforge.client.event.ClientChatEvent; +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.module.Module; +import ftbsc.bscv.module.vision.*; +import ftbsc.bscv.module.motion.*; +import ftbsc.bscv.module.self.*; +import ftbsc.bscv.module.hud.*; + +// The value here should match an entry in the META-INF/mods.toml file +@Mod("bscv") +public class BoSCoVicino { + public static String MOD_ID = "bscv"; + + // Directly reference a log4j logger. + public static final Logger LOGGER = LogManager.getLogger(); + + public static Minecraft minecraft; + + 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); + + // Store minecraft instance + BoSCoVicino.minecraft = Minecraft.getInstance(); + + // load and register mods + BoSCoVicino.mods = new ArrayList<Module>(); + + ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder(); + + // TODO also push! + // modules cannot easily pop from their builder, but here we can't easily get + // the module name yet. We should push and pop the builder ourselves and not + // bother the modules themselves. + + BoSCoVicino.mods.add(new Fullbright(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new VanillaFlight(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new FastInteract(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new InfoDisplay(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new Coordinates(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new EntityList(builder, this.dispatcher)); + builder.pop(); + BoSCoVicino.mods.add(new ActiveModules(builder, this.dispatcher)); + builder.pop(); + + BoSCoVicino.spec = builder.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) { + BoSCoVicino.minecraft.gui.getChat().addMessage(new StringTextComponent(message)); + } + + 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 = BoSCoVicino.minecraft.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; + } + }) + ); + } + + @SubscribeEvent + public void onClientChatEvent(ClientChatEvent event) { + if (event.getMessage().startsWith("/")) { + CommandSource source = BoSCoVicino.minecraft.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); + BoSCoVicino.minecraft.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 onWorldLoad(WorldEvent.Load event) { + // TEMPORARY! add command to regenerate suggestions + ClientPlayerEntity player = BoSCoVicino.minecraft.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"); + } + } + + // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD + // Event bus for receiving Registry Events) + @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD) + public static class RegistryEvents { + @SubscribeEvent + public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) { + // register a new block here + LOGGER.info("HELLO from Register Block"); + } + } +} |