fix: load mods early and check config on setup

This commit is contained in:
əlemi 2023-01-29 05:03:01 +01:00
parent 93ceec462e
commit 4b6bab1e8b
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -6,12 +6,19 @@ 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.Color;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponent;
import net.minecraft.util.text.event.HoverEvent;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms;
import net.minecraftforge.fml.ModLoadingContext;
@ -41,7 +48,7 @@ import co.fantabos.bscv.modules.*;
@Mod("bscv")
public class BoSCoVicino {
// Directly reference a log4j logger.
private static final Logger LOGGER = LogManager.getLogger();
public static final Logger LOGGER = LogManager.getLogger();
public static Minecraft minecraft;
@ -51,23 +58,17 @@ public class BoSCoVicino {
public BoSCoVicino() {
// Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::clientSetup);
// Store minecraft instance
BoSCoVicino.minecraft = Minecraft.getInstance();
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
// load and register mods
BoSCoVicino.mods = new ArrayList<Module>();
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
BoSCoVicino.mods.add(new Hud(builder, this.dispatcher));
BoSCoVicino.mods.add(new Fullbright(builder, this.dispatcher));
BoSCoVicino.mods.add(new VanillaFlight(builder, this.dispatcher));
@ -76,9 +77,24 @@ public class BoSCoVicino {
// 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("rebuild_hints")
Commands.literal("hints")
.executes(ctx -> {
ClientPlayerEntity player = BoSCoVicino.minecraft.player;
if (player != null) {
@ -105,43 +121,8 @@ public class BoSCoVicino {
}
})
);
// 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 setup(final FMLCommonSetupEvent event) {
// some preinit code
LOGGER.info("HELLO FROM PREINIT");
LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
}
private void doClientStuff(final FMLClientSetupEvent event) {
// do something that can only be done on the client
LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().options);
}
private void enqueueIMC(final InterModEnqueueEvent event) {
// some example code to dispatch IMC to another mod
InterModComms.sendTo("bscv", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
}
private void processIMC(final InterModProcessEvent event) {
// some example code to receive and process InterModComms from other mods
LOGGER.info("Got IMC {}", event.getIMCStream().
map(m->m.getMessageSupplier().get()).
collect(Collectors.toList()));
}
// @SubscribeEvent
// public void onRegisterCommand(RegisterCommandsEvent event) {
// this.dispatcher.
// }
@SubscribeEvent
public void onClientChatEvent(ClientChatEvent event) {
if (event.getMessage().startsWith("/")) {
@ -157,11 +138,25 @@ public class BoSCoVicino {
}
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) {
// do something when the server starts
LOGGER.info("HELLO from server starting");
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