diff --git a/common/src/main/java/net/blay09/mods/balm/api/config/ConfigJsonExport.java b/common/src/main/java/net/blay09/mods/balm/api/config/ConfigJsonExport.java index 3ca043a3..c4c44da4 100644 --- a/common/src/main/java/net/blay09/mods/balm/api/config/ConfigJsonExport.java +++ b/common/src/main/java/net/blay09/mods/balm/api/config/ConfigJsonExport.java @@ -28,7 +28,12 @@ public static ArrayList export(Class configDataClass) throws if (isProperty(field)) { final var name = field.getName(); final var type = field.getType().getSimpleName(); - final var description = field.getAnnotation(Comment.class).value(); + final var commentAnnotation = field.getAnnotation(Comment.class); + if (commentAnnotation == null) { + throw new IllegalArgumentException("Missing @Comment annotation on field: " + field); + } + + final var description = commentAnnotation.value(); final var defaultValue = field.get(defaults); final var validValues = getValidValues(field); properties.add(new ConfigProperty(name, type, description, defaultValue, validValues)); @@ -40,6 +45,10 @@ public static ArrayList export(Class configDataClass) throws } public static void exportToFile(Class configDataClass, File file) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException { + final var parentFile = file.getParentFile(); + if (!parentFile.exists() && !parentFile.mkdirs()) { + throw new IOException("Failed to create parent directories for file: " + file); + } Files.writeString(file.toPath(), new Gson().toJson(export(configDataClass))); } diff --git a/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java b/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java new file mode 100644 index 00000000..8711e47a --- /dev/null +++ b/common/src/main/java/net/blay09/mods/balm/common/command/BalmCommand.java @@ -0,0 +1,38 @@ +package net.blay09.mods.balm.common.command; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import net.blay09.mods.balm.api.config.ConfigJsonExport; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; + +import java.io.File; + +public class BalmCommand { + + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(Commands.literal("balm") + .then(Commands.literal("export") + .then(Commands.literal("config").then(Commands.argument("class", StringArgumentType.string()).executes(context -> { + final var className = context.getArgument("class", String.class); + try { + final var configDataClass = Class.forName(className); + ConfigJsonExport.exportToFile(configDataClass, new File("exports/config/" + configDataClass.getSimpleName() + ".json")); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException("Invalid config data class: " + className, e); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error exporting config data class: " + className, e); + } + + return 0; + }) + )).then(Commands.literal("icons")).then(Commands.argument("mod", StringArgumentType.string())).executes(context -> { + context.getSource().sendFailure(Component.literal("Not yet implemented")); + return 0; + }))); + } + +} diff --git a/common/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java b/common/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java index 5284f548..f8893a29 100644 --- a/common/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java +++ b/common/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java @@ -17,19 +17,27 @@ public enum ExampleEnum { @Comment("This is an example boolean property") public boolean exampleBoolean = true; + @Comment("This is an example int property") public int exampleInt = 42; + @Comment("This is an example string property") public String exampleString = "Hello World"; + @Comment("This is an example multiline string property") public String exampleMultilineString = "Hello World"; + @Comment("This is an example enum property") public ExampleEnum exampleEnum = ExampleEnum.Hello; @Synced @ExpectedType(String.class) + @Comment("This is an example string list property") public List exampleStringList = Arrays.asList("Hello", "World"); @Synced @ExpectedType(ResourceLocation.class) + @Comment("This is an example resource location set property") public Set exampleResourceLocationSet = Set.of(new ResourceLocation("dirt"), new ResourceLocation("diamond")); @ExpectedType(Integer.class) + @Comment("This is an example int list property") public List exampleIntList = Arrays.asList(12, 24); @ExpectedType(ExampleEnum.class) + @Comment("This is an example enum list property") public List exampleEnumList = Arrays.asList(ExampleEnum.Hello, ExampleEnum.World); @Comment("This is an example category") @@ -38,6 +46,7 @@ public enum ExampleEnum { public static class ExampleCategory { @Comment("This is an example string inside a category") public String innerField = "I am inside"; + @Comment("This is an example float inside a category") public float exampleFloat = 42.84f; } } diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java index 11f0011a..810ea5ff 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalm.java @@ -6,6 +6,7 @@ import net.blay09.mods.balm.api.entity.BalmEntity; import net.blay09.mods.balm.api.fluid.BalmFluidTankProvider; import net.blay09.mods.balm.api.fluid.FluidTank; +import net.blay09.mods.balm.common.command.BalmCommand; import net.blay09.mods.balm.config.ExampleConfig; import net.blay09.mods.balm.fabric.fluid.BalmFluidStorage; import net.blay09.mods.balm.fabric.provider.FabricBalmProviders; @@ -25,6 +26,7 @@ public void onInitialize() { ((FabricBalmHooks) Balm.getHooks()).initialize(); ((AbstractBalmConfig) Balm.getConfig()).initialize(); ExampleConfig.initialize(); + Balm.getCommands().register(BalmCommand::register); ServerPlayerEvents.COPY_FROM.register((oldPlayer, newPlayer, alive) -> { CompoundTag data = ((BalmEntity) oldPlayer).getFabricBalmData(); diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalm.java b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalm.java index 292e753b..cdce898c 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalm.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalm.java @@ -4,6 +4,7 @@ import net.blay09.mods.balm.api.config.AbstractBalmConfig; import net.blay09.mods.balm.api.energy.EnergyStorage; import net.blay09.mods.balm.api.fluid.FluidTank; +import net.blay09.mods.balm.common.command.BalmCommand; import net.blay09.mods.balm.config.ExampleConfig; import net.blay09.mods.balm.forge.client.ForgeBalmClient; import net.blay09.mods.balm.forge.provider.ForgeBalmProviders; @@ -23,6 +24,7 @@ public class ForgeBalm { public ForgeBalm() { ((AbstractBalmConfig) Balm.getConfig()).initialize(); ExampleConfig.initialize(); + Balm.getCommands().register(BalmCommand::register); ForgeBalmWorldGen.initializeBalmBiomeModifiers(); FMLJavaModLoadingContext.get().getModEventBus().addListener(ForgeBalmClient::onInitializeClient); diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java index f5e7530b..ed36eb76 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalm.java @@ -2,6 +2,7 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.config.AbstractBalmConfig; +import net.blay09.mods.balm.common.command.BalmCommand; import net.blay09.mods.balm.config.ExampleConfig; import net.blay09.mods.balm.neoforge.client.NeoForgeBalmClient; import net.blay09.mods.balm.neoforge.provider.NeoForgeBalmProviders; @@ -20,6 +21,7 @@ public class NeoForgeBalm { public NeoForgeBalm(IEventBus modBus) { ((AbstractBalmConfig) Balm.getConfig()).initialize(); ExampleConfig.initialize(); + Balm.getCommands().register(BalmCommand::register); NeoForgeBalmWorldGen.initializeBalmBiomeModifiers(modBus); modBus.addListener(NeoForgeBalmClient::onInitializeClient);