From 195ff51e2f32c1767cb781ee0ca40346a7beb7fa Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 28 Jun 2013 03:07:25 +0300 Subject: [PATCH] Split World command into Time and Weather commands. --- .../scripts/commands/CommandRegistry.java | 9 +- .../scripts/commands/world/TimeCommand.java | 60 +++++++++ .../commands/world/WeatherCommand.java | 79 +++++++++++ .../scripts/commands/world/WorldCommand.java | 124 ------------------ 4 files changed, 145 insertions(+), 127 deletions(-) create mode 100644 src/main/java/net/aufdemrand/denizen/scripts/commands/world/TimeCommand.java create mode 100644 src/main/java/net/aufdemrand/denizen/scripts/commands/world/WeatherCommand.java delete mode 100644 src/main/java/net/aufdemrand/denizen/scripts/commands/world/WorldCommand.java diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index ecf2953bbc..c866ced2f0 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -258,6 +258,9 @@ public void registerCoreMembers() { registerCoreMember(TeleportCommand.class, "TELEPORT", "teleport (npc) [location:x,y,z,world] (target(s):[n@#]|[p@name])", 1); + registerCoreMember(TimeCommand.class, + "TIME", "time [type:{global}|player] [<#>]", 1); + registerCoreMember(TriggerCommand.class, "TRIGGER", "trigger [name:trigger_name] [(toggle:true|false)|(cooldown:#.#)|(radius:#)]", 2); @@ -269,10 +272,10 @@ public void registerCoreMembers() { registerCoreMember(WalkToCommand.class, "WALKTO", "walkto [location:x,y,z,world] (speed:#)", 1); - - registerCoreMember(WorldCommand.class, - "WORLD", "world [type:global|player] (player:player.name) [action:sub_action]", 1); + registerCoreMember(WeatherCommand.class, + "WEATHER", "weather [type:{global}|player] [sunny|storm|thunder]", 1); + registerCoreMember(YamlCommand.class, "YAML", "...", 1); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/TimeCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/TimeCommand.java new file mode 100644 index 0000000000..5ed91d65a4 --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/TimeCommand.java @@ -0,0 +1,60 @@ +package net.aufdemrand.denizen.scripts.commands.world; + +import net.aufdemrand.denizen.exceptions.CommandExecutionException; +import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizen.scripts.ScriptEntry; +import net.aufdemrand.denizen.scripts.commands.AbstractCommand; +import net.aufdemrand.denizen.objects.Duration; +import net.aufdemrand.denizen.objects.Element; +import net.aufdemrand.denizen.objects.aH; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizen.utilities.debugging.dB.Messages; + +/** + * + * Set the time in the world to a number of ticks. + * + */ +public class TimeCommand extends AbstractCommand { + + private enum Type { GLOBAL, PLAYER } + + @Override + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + + if (!scriptEntry.hasObject("type") + && arg.matchesEnum(Type.values())) + // add type + scriptEntry.addObject("type", arg.asElement()); + + + else if (!scriptEntry.hasObject("value") + && arg.matchesArgumentType(Duration.class)) + // add value + scriptEntry.addObject("value", arg.asType(Duration.class)); + } + + // Check to make sure required arguments have been filled + + if ((!scriptEntry.hasObject("value"))) + throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "VALUE"); + } + + @Override + public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { + // Fetch objects + Duration value = (Duration) scriptEntry.getObject("value"); + Element type = (scriptEntry.hasObject("type") ? + (Element) scriptEntry.getObject("type") : new Element("player")); + + // Report to dB + dB.report(getName(), type.debug() + + (type.toString().equalsIgnoreCase("player") ? scriptEntry.getPlayer().debug() : "") + + value.debug()); + + scriptEntry.getPlayer().getPlayerEntity().getWorld().setTime(value.getTicks()); + } + +} diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WeatherCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WeatherCommand.java new file mode 100644 index 0000000000..24a33ad4ca --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WeatherCommand.java @@ -0,0 +1,79 @@ +package net.aufdemrand.denizen.scripts.commands.world; + +import org.bukkit.Bukkit; +import org.bukkit.World; + +import net.aufdemrand.denizen.exceptions.CommandExecutionException; +import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizen.scripts.ScriptEntry; +import net.aufdemrand.denizen.scripts.commands.AbstractCommand; +import net.aufdemrand.denizen.objects.Element; +import net.aufdemrand.denizen.objects.aH; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizen.utilities.debugging.dB.Messages; + +/** + * + * Set the weather in the world. + * + */ +public class WeatherCommand extends AbstractCommand { + + private enum Type { GLOBAL, PLAYER } + private enum Value { SUNNY, STORM, THUNDER } + + @Override + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + + if (!scriptEntry.hasObject("type") + && arg.matchesEnum(Type.values())) + // add type + scriptEntry.addObject("type", arg.asElement()); + + + else if (!scriptEntry.hasObject("value") + && arg.matchesEnum(Value.values())) + // add value + scriptEntry.addObject("value", arg.asElement()); + } + + // Check to make sure required arguments have been filled + + if ((!scriptEntry.hasObject("value"))) + throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "VALUE"); + } + + @Override + public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { + // Fetch objects + Value value = Value.valueOf(((Element) scriptEntry.getObject("value")) + .asString().toUpperCase()); + Element type = (scriptEntry.hasObject("type") ? + (Element) scriptEntry.getObject("type") : new Element("player")); + World world = scriptEntry.getPlayer().getPlayerEntity().getWorld(); + + // Report to dB + dB.report(getName(), type.debug() + + (type.toString().equalsIgnoreCase("player") ? scriptEntry.getPlayer().debug() : "") + + value.toString()); + + switch(value) { + case SUNNY: + world.setStorm(false); + world.setThundering(false); + break; + + case STORM: + world.setStorm(true); + break; + + case THUNDER: + // Note: setThundering always creates a storm + world.setThundering(true); + break; + } + } + +} diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WorldCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WorldCommand.java deleted file mode 100644 index 8aea34397d..0000000000 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/WorldCommand.java +++ /dev/null @@ -1,124 +0,0 @@ -package net.aufdemrand.denizen.scripts.commands.world; - -import net.aufdemrand.denizen.exceptions.CommandExecutionException; -import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; -import net.aufdemrand.denizen.scripts.ScriptEntry; -import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.objects.aH; -import net.aufdemrand.denizen.objects.aH.ArgumentType; -import net.aufdemrand.denizen.utilities.debugging.dB; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -/** - * Various world based commands. - * Such as: weather and times - * - * @author spaceemotion - */ -public class WorldCommand extends AbstractCommand { - - private enum Action { WEATHER, TIME } - - private enum SubAction { - // Time actions - DAY, NIGHT, DUSK, DAWN, - - // Weather actions - THUNDERING, STORM, SUNNY, THUNDERSTORM - } - - private enum Type { GLOBAL, PLAYER } // TODO: Player type will come when Bukkit PR gets accepted - - @Override - public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - Type type = Type.GLOBAL; - Action action = null; - SubAction sub = null; - Player player = scriptEntry.getPlayer().getPlayerEntity(); - - for (String arg : scriptEntry.getArguments()) { - if (aH.matchesArg("player", arg)) { - player = Bukkit.getServer().getPlayer(aH.getStringFrom("player")); - type = Type.PLAYER; - - } else if (aH.matchesValueArg("weather", arg, ArgumentType.String)) { - action = Action.WEATHER; - Bukkit.broadcastMessage("Got inside weather!"); - sub = SubAction.valueOf(aH.getStringFrom(arg).toUpperCase()); - if(sub == null) throw new InvalidArgumentsException("Invalid sub action for WEATHER!"); - - } else if (aH.matchesValueArg("time", arg, ArgumentType.String)) { - action = Action.TIME; - Bukkit.broadcastMessage("Got inside time!"); - sub = SubAction.valueOf(aH.getStringFrom(arg).toUpperCase()); - if(sub == null) throw new InvalidArgumentsException("Invalid sub action for TIME!"); - - } else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg); - } - - if (type == null) - throw new InvalidArgumentsException("Must specify an action! Valid: WEATHER, TIME"); - - - scriptEntry.addObject("type", type); - scriptEntry.addObject("action", action); - scriptEntry.addObject("subaction", sub); - scriptEntry.addObject("player", player); - } - - @Override - public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { - Type type = (Type) scriptEntry.getObject("type"); - Action action = (Action) scriptEntry.getObject("action"); - SubAction sub = (SubAction) scriptEntry.getObject("subaction"); - Player player = (Player) scriptEntry.getObject("player"); - - dB.report(getName(), - aH.debugObj("Type", type.name()) - + aH.debugObj("Action", action.name()) - + aH.debugObj("Sub-Action", sub.name()) - + ((type == Type.PLAYER) ? aH.debugObj("Player", player.getName()) : "")); - - switch(action) { - case WEATHER: - switch(sub) { - case SUNNY: - player.getWorld().setThundering(false); - player.getWorld().setStorm(false); - break; - - case THUNDERSTORM: - player.getWorld().setThundering(true); - - case STORM: - player.getWorld().setStorm(true); - break; - - case THUNDERING: - player.getWorld().setThundering(true); - } - - break; - - case TIME: - switch(sub) { - case DAY: - player.getWorld().setTime(0); - break; - - case NIGHT: - player.getWorld().setTime(13500); - break; - - case DUSK: - player.getWorld().setTime(12500); - break; - - case DAWN: - player.getWorld().setTime(23000); - } - } - } - -}