From 82b368bdd1def7363c2acade011caaaa9cc9d5ab Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 10 Oct 2017 20:06:43 -0600 Subject: [PATCH 1/4] Use shared config reloading; register CommandTree --- src/main/java/cofh/cofhworld/CoFHWorld.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/cofh/cofhworld/CoFHWorld.java b/src/main/java/cofh/cofhworld/CoFHWorld.java index 6a75e46..188023d 100644 --- a/src/main/java/cofh/cofhworld/CoFHWorld.java +++ b/src/main/java/cofh/cofhworld/CoFHWorld.java @@ -1,14 +1,17 @@ package cofh.cofhworld; +import cofh.cofhworld.command.CommandCoFHWorld; import cofh.cofhworld.init.FeatureParser; import cofh.cofhworld.init.WorldHandler; import cofh.cofhworld.init.WorldProps; +import net.minecraft.world.World; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.event.FMLServerStartingEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -57,14 +60,15 @@ public void preInit(FMLPreInitializationEvent event) { @EventHandler public void loadComplete(FMLLoadCompleteEvent event) { - try { - FeatureParser.parseGenerationFiles(); - } catch (Throwable t) { - t.printStackTrace(); - } + WorldHandler.reloadConfig(); config.save(); log.info(MOD_NAME + ": Load Complete."); } + @Mod.EventHandler + public void onServerStarted(FMLServerStartingEvent event) + { + event.registerServerCommand(new CommandCoFHWorld()); + } } From f2cb8129889cde5f6f58ff737b371f17d69912f9 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 10 Oct 2017 20:08:05 -0600 Subject: [PATCH 2/4] Add cofhworld command tree and associated i18n resources --- .../cofhworld/command/CommandCoFHWorld.java | 73 +++++++++++++++++++ .../assets/cofhworld/lang/en_US.lang | 6 ++ 2 files changed, 79 insertions(+) create mode 100644 src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java create mode 100644 src/main/resources/assets/cofhworld/lang/en_US.lang diff --git a/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java b/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java new file mode 100644 index 0000000..936c3c0 --- /dev/null +++ b/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java @@ -0,0 +1,73 @@ +package cofh.cofhworld.command; + +import cofh.cofhworld.feature.IFeatureGenerator; +import cofh.cofhworld.init.WorldHandler; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.server.command.CommandTreeBase; + +public class CommandCoFHWorld extends CommandTreeBase { + + @Override + public String getName() { + return "cofhworld"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "cofhworld.usage"; + } + + public CommandCoFHWorld() { + addSubcommand(new CommandReload()); + addSubcommand(new CommandList()); + } + + // Command to reload all feature definitions + public static class CommandReload extends CommandBase { + @Override + public String getName() { + return "reload"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "reload.usage"; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + if (WorldHandler.reloadConfig()) { + notifyCommandListener(sender, this, "reload.successful"); + } else { + notifyCommandListener(sender, this, "reload.failed"); + } + } + } + + // Command to list all feature definitions + public static class CommandList extends CommandBase { + + @Override + public String getName() { + return "list"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "list.usage"; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { + StringBuilder b = new StringBuilder(); + b.append("\n"); + for (IFeatureGenerator feature: WorldHandler.getFeatures()) { + b.append("* " + feature.getFeatureName() + "\n"); + } + notifyCommandListener(sender, this, "list", b.toString()); + } + } +} diff --git a/src/main/resources/assets/cofhworld/lang/en_US.lang b/src/main/resources/assets/cofhworld/lang/en_US.lang new file mode 100644 index 0000000..53c0627 --- /dev/null +++ b/src/main/resources/assets/cofhworld/lang/en_US.lang @@ -0,0 +1,6 @@ + +reload.usage=Reloads CoFHWorld configuration +reload.successful=Configuration reloaded. +reload.failed=Failed to reload config; check your logs. +list.usage=Lists all CoFHWorld features +list=Available CoFHWorld features: %s From 278b43e611f5fc91c98fd162d14554f02969716a Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 10 Oct 2017 21:49:56 -0600 Subject: [PATCH 3/4] Make sure all i18n keys are globally unique --- .../java/cofh/cofhworld/command/CommandCoFHWorld.java | 10 +++++----- src/main/resources/assets/cofhworld/lang/en_US.lang | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java b/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java index 936c3c0..6ae3b01 100644 --- a/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java +++ b/src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java @@ -34,15 +34,15 @@ public String getName() { @Override public String getUsage(ICommandSender sender) { - return "reload.usage"; + return "cofhworld.reload.usage"; } @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if (WorldHandler.reloadConfig()) { - notifyCommandListener(sender, this, "reload.successful"); + notifyCommandListener(sender, this, "cofhworld.reload.successful"); } else { - notifyCommandListener(sender, this, "reload.failed"); + notifyCommandListener(sender, this, "cofhworld.reload.failed"); } } } @@ -57,7 +57,7 @@ public String getName() { @Override public String getUsage(ICommandSender sender) { - return "list.usage"; + return "cofhworld.list.usage"; } @Override @@ -67,7 +67,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args for (IFeatureGenerator feature: WorldHandler.getFeatures()) { b.append("* " + feature.getFeatureName() + "\n"); } - notifyCommandListener(sender, this, "list", b.toString()); + notifyCommandListener(sender, this, "cofhworld.list", b.toString()); } } } diff --git a/src/main/resources/assets/cofhworld/lang/en_US.lang b/src/main/resources/assets/cofhworld/lang/en_US.lang index 53c0627..a834af6 100644 --- a/src/main/resources/assets/cofhworld/lang/en_US.lang +++ b/src/main/resources/assets/cofhworld/lang/en_US.lang @@ -1,6 +1,6 @@ -reload.usage=Reloads CoFHWorld configuration -reload.successful=Configuration reloaded. -reload.failed=Failed to reload config; check your logs. -list.usage=Lists all CoFHWorld features -list=Available CoFHWorld features: %s +cofhworld.reload.usage=Reloads CoFHWorld configuration +cofhworld.reload.successful=Configuration reloaded. +cofhworld.reload.failed=Failed to reload config; check your logs. +cofhworld.list.usage=Lists all CoFHWorld features +cofhworld.list=Available CoFHWorld features: %s From 46885f751857a17f0c81e1babc994ae726c7901c Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 11 Oct 2017 17:31:15 -0600 Subject: [PATCH 4/4] Add config reload & feature iteration --- .../cofh/cofhworld/init/WorldHandler.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/cofh/cofhworld/init/WorldHandler.java b/src/main/java/cofh/cofhworld/init/WorldHandler.java index e22ab95..3d4ef7f 100644 --- a/src/main/java/cofh/cofhworld/init/WorldHandler.java +++ b/src/main/java/cofh/cofhworld/init/WorldHandler.java @@ -98,6 +98,21 @@ private WorldHandler() { } + public static boolean reloadConfig() { + // Reset all features so that config will reload properly + features.clear(); + featureNames.clear(); + + // Parse all the generation files into features + try { + FeatureParser.parseGenerationFiles(); + return true; + } catch (Throwable t) { + t.printStackTrace(); + return false; + } + } + public static boolean registerFeature(IFeatureGenerator feature) { String featureName = feature.getFeatureName(); @@ -129,6 +144,19 @@ public static boolean removeFeature(IFeatureGenerator feature) { return true; } + public static IFeatureGenerator findFeature(String name) { + for (IFeatureGenerator feature : features) { + if (feature.getFeatureName().equals(name)) { + return feature; + } + } + return null; + } + + public static List getFeatures() { + return features; + } + /* EVENT HANDLING */ @SubscribeEvent public void handlePopulateChunkEvent(PopulateChunkEvent.Pre event) {