Skip to content

Commit

Permalink
feat: Add /balm export config command for exporting config docs
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Jun 4, 2024
1 parent 62d5b76 commit d904393
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public static ArrayList<ConfigProperty> 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));
Expand All @@ -40,6 +45,10 @@ public static ArrayList<ConfigProperty> 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)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSourceStack> 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;
})));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> exampleStringList = Arrays.asList("Hello", "World");
@Synced
@ExpectedType(ResourceLocation.class)
@Comment("This is an example resource location set property")
public Set<ResourceLocation> exampleResourceLocationSet = Set.of(new ResourceLocation("dirt"), new ResourceLocation("diamond"));
@ExpectedType(Integer.class)
@Comment("This is an example int list property")
public List<Integer> exampleIntList = Arrays.asList(12, 24);
@ExpectedType(ExampleEnum.class)
@Comment("This is an example enum list property")
public List<ExampleEnum> exampleEnumList = Arrays.asList(ExampleEnum.Hello, ExampleEnum.World);

@Comment("This is an example category")
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit d904393

Please sign in to comment.