Skip to content

Commit

Permalink
ban command: TimeTag input option and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 30, 2021
1 parent 493e880 commit 670f8d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.BanList;
Expand All @@ -19,25 +20,27 @@ public class BanCommand extends AbstractCommand {

public BanCommand() {
setName("ban");
setSyntax("ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (duration:<duration>) (source:<text>)");
setSyntax("ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (expire:<time>) (source:<text>)");
setRequiredArguments(1, 5);
isProcedural = false;
}

// <--[command]
// @Name Ban
// @Syntax ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (duration:<duration>) (source:<text>)
// @Syntax ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (expire:<time>) (source:<text>)
// @Required 1
// @Maximum 5
// @Short Ban or un-ban players or ip addresses.
// @Group server
//
// @Description
// Add or remove player or ip address bans from the server. Banning a player will also kick them from the server.
//
// You may optionally specify both a list of players and list of addresses.
// Options are:
//
// Additional options are:
// reason: Sets the ban reason. Defaults to "Banned.".
// duration: Sets the duration of the temporary ban. This will be a permanent ban if not specified.
// expire: Sets the expire time of the temporary ban, as a TimeTag or a DurationTag. This will be a permanent ban if not specified.
// source: Sets the source of the ban. Defaults to "(Unknown)".
//
// @Tags
Expand All @@ -64,7 +67,7 @@ public BanCommand() {
//
// @Usage
// Use to ban a list of players for 10 minutes with a reason.
// - ban <[player]>|<[someplayer]> "reason:Didn't grow enough potatoes." duration:10m
// - ban <[player]>|<[someplayer]> "reason:Didn't grow enough potatoes." expire:10m
//
// @Usage
// Use to ban a player with a source.
Expand All @@ -76,7 +79,7 @@ public BanCommand() {
//
// @Usage
// Use to temporarily ip ban all online players.
// - ban addresses:<server.online_players.parse[ip]> duration:5m
// - ban addresses:<server.online_players.parse[ip]> expire:5m
//
// @Usage
// Use to unban a list of players.
Expand All @@ -94,37 +97,44 @@ public enum Actions {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("action") && (arg.matchesPrefix("action")
|| arg.matchesEnum(Actions.values()))) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("addresses") && arg.matchesPrefix("addresses", "address")) {
if (!scriptEntry.hasObject("addresses")
&& arg.matchesPrefix("addresses", "address")) {
scriptEntry.addObject("addresses", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("targets") && (arg.matchesPrefix("targets", "target")
|| arg.matchesArgumentList(PlayerTag.class))) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("reason") && arg.matchesPrefix("reason")) {
else if (!scriptEntry.hasObject("reason")
&& arg.matchesPrefix("reason")) {
scriptEntry.addObject("reason", arg.asElement());
}
else if (!scriptEntry.hasObject("duration") && (arg.matchesPrefix("duration", "time", "d", "expiration")
|| arg.matchesArgumentType(DurationTag.class))) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("expire", "duration", "time", "d", "expiration")) {
if (arg.matchesArgumentType(TimeTag.class)) {
scriptEntry.addObject("expire", arg.asType(TimeTag.class));
}
else {
scriptEntry.addObject("expire", new TimeTag(TimeTag.now().millis() + arg.asType(DurationTag.class).getMillis()));
}
}
else if (!scriptEntry.hasObject("source") && arg.matchesPrefix("source")) {
else if (!scriptEntry.hasObject("source")
&& arg.matchesPrefix("source")) {
scriptEntry.addObject("source", arg.asElement());
}
else if (!scriptEntry.hasObject("action")
&& arg.limitToOnlyPrefix("action")
&& arg.matchesEnum(Actions.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("targets")
&& arg.limitToOnlyPrefix("targets")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("action", new ElementTag("add"))
.defaultObject("reason", new ElementTag("Banned."))
.defaultObject("source", new ElementTag("(Unknown)"));
if (Actions.valueOf(scriptEntry.getObject("action").toString().toUpperCase()) == null) {
throw new IllegalArgumentException("Invalid action specified.");
}
if ((!scriptEntry.hasObject("targets") || ((List<PlayerTag>) scriptEntry.getObject("targets")).isEmpty())
&& (!scriptEntry.hasObject("addresses") || ((List<ElementTag>) scriptEntry.getObject("addresses")).isEmpty())) {
throw new IllegalArgumentException("Must specify a valid target or address!");
Expand All @@ -137,14 +147,11 @@ public void execute(ScriptEntry scriptEntry) {
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
ListTag addresses = scriptEntry.getObjectTag("addresses");
ElementTag reason = scriptEntry.getElement("reason");
DurationTag duration = scriptEntry.getObjectTag("duration");
TimeTag expire = scriptEntry.getObjectTag("expire");
ElementTag source = scriptEntry.getElement("source");
Date expiration = null;
if (duration != null && duration.getTicks() != 0) {
expiration = new Date(new DurationTag(System.currentTimeMillis() / 50 + duration.getTicks()).getTicks() * 50);
}
Date expiration = expire == null ? null : new Date(expire.millis());
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, db("targets", targets), addresses, reason, duration, source);
Debug.report(scriptEntry, getName(), action, db("targets", targets), addresses, reason, expire, source);
}
Actions banAction = Actions.valueOf(action.toString().toUpperCase());
switch (banAction) {
Expand All @@ -165,7 +172,6 @@ public void execute(ScriptEntry scriptEntry) {
}
}
break;

case REMOVE:
if (targets != null) {
for (PlayerTag player : targets) {
Expand Down
Expand Up @@ -166,7 +166,7 @@ else if (LocationTag.matches(chunkText)) {
Debug.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
}
else {
Debug.echoError("Chunk '" + coord + "' was not on the load list!");
Debug.echoDebug(scriptEntry, "Chunk '" + coord + "' was not on the load list, ignoring.");
}
break;
case REMOVEALL:
Expand Down

0 comments on commit 670f8d7

Please sign in to comment.