Skip to content

Commit

Permalink
add more options to the weather command
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 8, 2019
1 parent f3b32c7 commit 568d767
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
Expand Up @@ -216,7 +216,7 @@ public void registerCommands() {
AutoNoCitizensCommand.registerFor("VULNERABLE");
}
registerCoreMember(WalkCommand.class, "WALK", "walk (<entity>|...) [<location>/stop] (speed:<#>) (auto_range) (radius:<#.#>) (lookat:<location>)", 1);
registerCoreMember(WeatherCommand.class, "WEATHER", "weather [{global}/player] [sunny/storm/thunder] (<world>)", 1);
registerCoreMember(WeatherCommand.class, "WEATHER", "weather [{global}/player] [sunny/storm/thunder/reset] (<world>) (reset:<duration>)", 1);
registerCoreMember(WorldBorderCommand.class, "WORLDBORDER", "worldborder [<world>/<player>|...] (center:<location>) (size:<#.#>) (current_size:<#.#>) (damage:<#.#>) (damagebuffer:<#.#>) (warningdistance:<#>) (warningtime:<duration>) (duration:<duration>) (reset)", 2);
registerCoreMember(ZapCommand.class, "ZAP", "zap (<script>) [<step>] (<duration>)", 0);

Expand Down
Expand Up @@ -30,7 +30,7 @@ public class TimeCommand extends AbstractCommand {
// If no world is specified, defaults to the NPCs world. If no NPC is available,
// defaults to the player's world. If no player is available, an error will be thrown.
//
// If 'player is specified', it will change their personal time.
// If 'player' is specified, this will change their personal time.
// This is separate from the global time, and does not affect other players.
// When that player logs off, their time will be reset to the global time.
// Additionally, you may instead specify 'reset' to return the player's time back to global time.
Expand Down Expand Up @@ -109,7 +109,7 @@ else if (!scriptEntry.hasObject("world")

// Check to make sure required arguments have been filled

if ((!scriptEntry.hasObject("value") && !scriptEntry.hasObject("reset"))) {
if (!scriptEntry.hasObject("value") && !scriptEntry.hasObject("reset")) {
throw new InvalidArgumentsException("Must specify a value!");
}

Expand Down
@@ -1,10 +1,12 @@
package com.denizenscript.denizen.scripts.commands.world;

import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
Expand All @@ -13,20 +15,28 @@
import org.bukkit.WeatherType;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.UUID;

public class WeatherCommand extends AbstractCommand {

// <--[command]
// @Name Weather
// @Syntax weather [{global}/player] [sunny/storm/thunder] (<world>)
// @Syntax weather [{global}/player] [sunny/storm/thunder/reset] (<world>) (reset:<duration>)
// @Required 1
// @Short Changes the current weather in the minecraft world.
// @Group world
//
// @Description
// Changes the weather in the specified world.
// You can also set weather for the attached player, where that player will experience personal
// weather that is different from the global weather.
// Logging off will reset personal weather.
// By default, changes the weather in the specified world.
//
// If you specify 'player', this will change the weather in that player's view.
// This is separate from the global weather, and does not affect other players.
// When that player logs off, their weather will be reset to the global weather.
// Additionally, you may instead specify 'reset' to return the player's weather back to global weather.
// If you specify a custom weather, optionally specify 'reset:<duration>'
// to set a time after which the player's weather will reset (if not manually changed again before then).
// Note that 'thunder' is no different from 'storm' when used on a single player.
//
// @Tags
// <BiomeTag.downfall_type>
Expand All @@ -37,22 +47,30 @@ public class WeatherCommand extends AbstractCommand {
// <WorldTag.thunder_duration>
//
// @Usage
// Makes the weather sunny
// Use to makes the weather sunny
// - weather sunny
//
// @Usage
// Makes the weather storm in world "cookies"
// Use to makes the weather storm in world "cookies"
// - weather storm cookies
//
// @Usage
// Make the weather storm for the attached player.
// Use to make the weather storm for the attached player.
// - weather player storm
//
// @Usage
// Use to make the player see a storm for 2 minutes.
// - weather player storm reset:2m
//
// @Usage
// Use to let the player see the global weather again.
// - weather player reset
//
// -->

private enum Type {GLOBAL, PLAYER}

private enum Value {SUNNY, STORM, THUNDER}
private enum Value {SUNNY, STORM, THUNDER, RESET}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
Expand All @@ -71,12 +89,17 @@ else if (!scriptEntry.hasObject("value")
&& arg.matchesEnum(Value.values())) {
scriptEntry.addObject("value", arg.asElement());
}
else if (!scriptEntry.hasObject("value")
&& arg.matchesPrefix("reset")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("reset_after", arg.asType(DurationTag.class));
}
else {
arg.reportUnhandled();
}
}

if ((!scriptEntry.hasObject("value"))) {
if (!scriptEntry.hasObject("value")) {
throw new InvalidArgumentsException("Must specify a value!");
}

Expand All @@ -90,17 +113,21 @@ else if (!scriptEntry.hasObject("value")
Bukkit.getWorlds().get(0));
}

public HashMap<UUID, Integer> resetTasks = new HashMap<>();

@Override
public void execute(ScriptEntry scriptEntry) {
Value value = Value.valueOf(((ElementTag) scriptEntry.getObject("value")).asString().toUpperCase());
WorldTag world = scriptEntry.getObjectTag("world");
Type type = (Type) scriptEntry.getObject("type");
DurationTag resetAfter = scriptEntry.getObjectTag("reset_after");

if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("type", type.name()) +
(type.name().equalsIgnoreCase("player") ? ArgumentHelper.debugObj("player", Utilities.getEntryPlayer(scriptEntry)) : "") +
(type.name().equalsIgnoreCase("global") ? ArgumentHelper.debugObj("world", world) : "") +
ArgumentHelper.debugObj("value", value));
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("type", type.name())
+ (type.name().equalsIgnoreCase("player") ? ArgumentHelper.debugObj("player", Utilities.getEntryPlayer(scriptEntry)) : "")
+ (type.name().equalsIgnoreCase("global") ? ArgumentHelper.debugObj("world", world) : "")
+ (resetAfter != null ? resetAfter.debug() : "")
+ ArgumentHelper.debugObj("value", value));
}

if (type.equals(Type.GLOBAL)) {
Expand All @@ -116,16 +143,36 @@ public void execute(ScriptEntry scriptEntry) {
// Note: setThundering always creates a storm
world.getWorld().setThundering(true);
break;
case RESET:
Debug.echoError("Cannot RESET global weather!");
break;
}
}
else {
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
Integer existingTask = resetTasks.get(player.getUniqueId());
if (existingTask != null) {
Bukkit.getScheduler().cancelTask(existingTask);
resetTasks.remove(player.getUniqueId());
}
if (value == Value.SUNNY) {
player.setPlayerWeather(WeatherType.CLEAR);
}
else {
else if (value == Value.STORM || value == Value.THUNDER) {
player.setPlayerWeather(WeatherType.DOWNFALL);
}
else if (value == Value.RESET) {
player.resetPlayerWeather();
}
if (resetAfter != null) {
int newTask = Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
player.resetPlayerWeather();
}
});
resetTasks.put(player.getUniqueId(), newTask);
}
}
}
}

0 comments on commit 568d767

Please sign in to comment.