Skip to content

Commit

Permalink
Merge pull request #3 from dizzyd/dss-add-world-commands-redux
Browse files Browse the repository at this point in the history
Redux of world commands
  • Loading branch information
KingLemming authored Oct 11, 2017
2 parents d32c170 + 46885f7 commit c7647ef
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/cofh/cofhworld/CoFHWorld.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());
}
}
73 changes: 73 additions & 0 deletions src/main/java/cofh/cofhworld/command/CommandCoFHWorld.java
Original file line number Diff line number Diff line change
@@ -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 "cofhworld.reload.usage";
}

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if (WorldHandler.reloadConfig()) {
notifyCommandListener(sender, this, "cofhworld.reload.successful");
} else {
notifyCommandListener(sender, this, "cofhworld.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 "cofhworld.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, "cofhworld.list", b.toString());
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/cofh/cofhworld/init/WorldHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<IFeatureGenerator> getFeatures() {
return features;
}

/* EVENT HANDLING */
@SubscribeEvent
public void handlePopulateChunkEvent(PopulateChunkEvent.Pre event) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/assets/cofhworld/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

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

0 comments on commit c7647ef

Please sign in to comment.