feat: crude commands, mod loading and config

This commit is contained in:
dev@ftbsc 2023-01-26 22:20:24 +01:00
parent 84ec30f4c1
commit ced8ae36ee
2 changed files with 53 additions and 4 deletions

View file

@ -3,7 +3,9 @@ package co.fantabos.bscv;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.InterModComms;
@ -19,6 +21,8 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
// The value here should match an entry in the META-INF/mods.toml file // The value here should match an entry in the META-INF/mods.toml file
@ -29,6 +33,8 @@ public class BoSCoVicino {
public static Minecraft minecraft; public static Minecraft minecraft;
public static List<Mod> mods;
public BoSCoVicino() { public BoSCoVicino() {
// Register the setup method for modloading // Register the setup method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
@ -39,10 +45,22 @@ public class BoSCoVicino {
// Register the doClientStuff method for modloading // Register the doClientStuff method for modloading
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
ModLoadingContext.get().registerConfig(Type.CLIENT, Configuration.SPEC, "bscv.toml");
// Store minecraft instance
BoSCoVicino.minecraft = Minecraft.getInstance(); BoSCoVicino.minecraft = Minecraft.getInstance();
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
// load and register mods
BoSCoVicino.mods = new ArrayList<Mod>();
BoSCoVicino.mods.add((Mod) new Fullbright(builder));
ForgeConfigSpec 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 // Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(this);
} }
@ -69,6 +87,14 @@ public class BoSCoVicino {
map(m->m.getMessageSupplier().get()). map(m->m.getMessageSupplier().get()).
collect(Collectors.toList())); collect(Collectors.toList()));
} }
@SubscribeEvent
public void onRegisterCommand(RegisterCommandsEvent event) {
for (Mod mod : BoSCoVicino.mods) {
mod.registerCommands(event.getDispatcher());
}
}
// You can use SubscribeEvent and let the Event Bus discover methods to call // You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent @SubscribeEvent
public void onServerStarting(FMLServerStartingEvent event) { public void onServerStarting(FMLServerStartingEvent event) {

View file

@ -1,5 +1,10 @@
package co.fantabos.bscv; 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.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
@ -15,14 +20,32 @@ public abstract class Mod {
public final Group group; public final Group group;
public final ForgeConfigSpec.ConfigValue<Boolean> enabled; public final ForgeConfigSpec.ConfigValue<Boolean> enabled;
Mod(String name, Group group, ForgeConfigSpec.Builder builder) { protected Mod(String name, Group group, ForgeConfigSpec.Builder builder) {
this.name = name; this.name = name;
this.group = group; this.group = group;
builder.push(this.name.toLowerCase()); builder.push(this.name.toLowerCase());
this.enabled = builder this.enabled = builder
.comment(String.format("Enable %s", this.name)) .comment(String.format("Enables %s", this.name))
.define(this.name.toLowerCase(), false); .define("enabled", false);
}
protected void registerCommand(CommandDispatcher<CommandSource> dispatcher) {
dispatcher.register(
Commands.literal(this.name.toLowerCase())
.then(
Commands.literal("toggle")
.executes(ctx -> {
this.toggle();
return 1;
})
)
);
}
public void toggle() {
if (this.enabled.get()) this.disable();
else this.enable();
} }
public void enable() { public void enable() {