From 1539579be1a2792205a14cdbf3c91b2f40eb5b95 Mon Sep 17 00:00:00 2001 From: Fortifier42 Date: Thu, 10 Dec 2015 20:21:41 +1100 Subject: [PATCH] Extend ban controls. * add Ban command * add is_banned mechanism for dPlayer * add ban_info tags minor improvement to kick command meta --- .../aufdemrand/denizen/objects/dPlayer.java | 63 +++++++++- .../commands/BukkitCommandRegistry.java | 46 ++++++- .../scripts/commands/player/KickCommand.java | 8 +- .../scripts/commands/server/BanCommand.java | 114 ++++++++++++++++++ 4 files changed, 219 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/aufdemrand/denizen/scripts/commands/server/BanCommand.java diff --git a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java index 4d0c86932b..8cfe8af5ec 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java @@ -1030,9 +1030,13 @@ else if (attribute.startsWith("uuid") && !isOnline()) // @description // returns whether the player is banned. // --> - if (attribute.startsWith("is_banned")) - return new Element(getOfflinePlayer().isBanned()) - .getAttribute(attribute.fulfill(1)); + if (attribute.startsWith("is_banned")) { + BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName()); + if (ban != null && ban.getExpiration().after(new Date())) { + return Element.TRUE.getAttribute(attribute.fulfill(1)); + } + return Element.FALSE.getAttribute(attribute.fulfill(1)); + } // <--[tag] // @attribute @@ -1113,6 +1117,48 @@ else if (attribute.startsWith("uuid") && !isOnline()) return list.getAttribute(attribute.fulfill(1)); } + if (attribute.startsWith("ban_info")) { + attribute.fulfill(1); + BanEntry ban = Bukkit.getBanList(BanList.Type.NAME).getBanEntry(getName()); + if (ban == null) { + return null; + } + else if (ban.getExpiration().before(new Date())) { + return null; + } + // <--[tag] + // @attribute + // returns Duration + // description + // returns the expiration of the player's ban, if they are banned. + // --> + if (attribute.startsWith("expiration")) { + return new Duration(ban.getExpiration().getTime() / 50) + .getAttribute(attribute.fulfill(1)); + } + // <--[tag] + // @attribute + // returns Element + // description + // returns the reason for the player's ban, if they are banned. + // --> + else if (attribute.startsWith("reason")) { + return new Element(ban.getReason()) + .getAttribute(attribute.fulfill(1)); + } + // <--[tag] + // @attribute + // returns Duration + // description + // returns when the player's ban was created, if they are banned. + // --> + else if (attribute.startsWith("created")) { + return new Duration(ban.getCreated().getTime() / 50) + .getAttribute(attribute.fulfill(1)); + } + return null; + } + // <--[tag] // @attribute ]> // @returns Element(Boolean) @@ -2573,6 +2619,17 @@ else if (split.length > 1) { getPlayerEntity().setOp(mechanism.getValue().asBoolean()); } + // <--[mechanism] + // @object dPlayer + // @name is_banned + // @input Element(Boolean) + // @description + // Set whether the player is banned or not. + // --> + if (mechanism.matches("is_banned") && mechanism.requireBoolean()) { + getOfflinePlayer().setBanned(mechanism.getValue().asBoolean()); + } + // Iterate through this object's properties' mechanisms for (Property property : PropertyParser.getProperties(this)) { property.adjust(mechanism); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java index 9d7788a96e..dcc80cb691 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java @@ -5,9 +5,7 @@ import net.aufdemrand.denizen.scripts.commands.item.*; import net.aufdemrand.denizen.scripts.commands.npc.*; import net.aufdemrand.denizen.scripts.commands.player.*; -import net.aufdemrand.denizen.scripts.commands.server.AnnounceCommand; -import net.aufdemrand.denizen.scripts.commands.server.ExecuteCommand; -import net.aufdemrand.denizen.scripts.commands.server.ScoreboardCommand; +import net.aufdemrand.denizen.scripts.commands.server.*; import net.aufdemrand.denizen.scripts.commands.world.*; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.depends.Depends; @@ -353,6 +351,46 @@ public void registerCoreMembers() { registerCoreMember(AttackCommand.class, "ATTACK", "attack (|...) (target:/cancel)", 0); + // <--[command] + // @Name Ban + // Syntax ban ({ADD}/REMOVE) [|...] reason: duration: + // @Required 1 + // @Stable stable + // @Short Ban or un-ban a player or list of players. + // @Author Fortifier42 + // @Group server + // @Description + // Add or remove bans from the server, with optional arguments for a reason and duration of a temporary + // ban. By default will ban the specified player(s), and kick them from the server. You can set a reason for + // which the player(s) will be banned, along with a duration (if you wish to temporarily ban them). If a + // reason is not specified, it will default to "Banned.". If a duration for the ban if not specified, they + // will be banned permanently. + // @Tags + // + // + // + // + + // @Usage + // Use to ban a player. + // - ban p@mcmonkey4eva + + // @Usage + // Use to ban a list of players with a reason. + // - ban p@mcmonkey4eva|p@Morphan1 "reason:Didn't grow enough potatoes." + + // @Usage + // Use to ban a list of players for 10 minutes with a reason. + // - ban p@mcmonkey4eva|p@Morphan1 "reason:Didn't grow enough potatoes." duration:10m + + // @Usage + // Use to unban a list of players. + // - ban remove p@mcmonkey4eva|p@Morphan1 + + // --> + registerCoreMember(BanCommand.class, + "BAN", "ban ({ADD}/REMOVE) [|...] (reason:) (duration:)", 1); + // <--[command] // @Name Break @@ -1548,7 +1586,7 @@ public void registerCoreMembers() { // - kick p@mcmonkey4eva "reason:Because I can." // --> registerCoreMember(KickCommand.class, - "KICK", "kick (|...) (reason:)", 1); + "KICK", "kick [|...] (reason:)", 1); // <--[command] // @Name Leash diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/player/KickCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/player/KickCommand.java index 095ae589cf..0bf9eef520 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/player/KickCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/player/KickCommand.java @@ -1,6 +1,5 @@ package net.aufdemrand.denizen.scripts.commands.player; -import net.aufdemrand.denizen.BukkitScriptEntryData; import net.aufdemrand.denizen.objects.dPlayer; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizencore.exceptions.CommandExecutionException; @@ -11,7 +10,6 @@ import net.aufdemrand.denizencore.scripts.ScriptEntry; import net.aufdemrand.denizencore.scripts.commands.AbstractCommand; -import java.util.Arrays; import java.util.List; public class KickCommand extends AbstractCommand { @@ -44,9 +42,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { Element reason = scriptEntry.getElement("reason"); List targets = (List) scriptEntry.getObject("targets"); - dB.report(scriptEntry, getName(), - (reason != null ? reason.debug() : "") + - aH.debugObj("targets", targets)); + dB.report(scriptEntry, getName() + aH.debugObj("targets", targets) + + reason.debug()); for (dPlayer player : targets) { if (player.isValid() && player.isOnline()) { diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/server/BanCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/server/BanCommand.java new file mode 100644 index 0000000000..cf38f35e0c --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/server/BanCommand.java @@ -0,0 +1,114 @@ +package net.aufdemrand.denizen.scripts.commands.server; + +import net.aufdemrand.denizen.objects.dPlayer; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizencore.exceptions.CommandExecutionException; +import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizencore.objects.Duration; +import net.aufdemrand.denizencore.objects.Element; +import net.aufdemrand.denizencore.objects.aH; +import net.aufdemrand.denizencore.objects.dList; +import net.aufdemrand.denizencore.scripts.ScriptEntry; +import net.aufdemrand.denizencore.scripts.commands.AbstractCommand; +import org.bukkit.BanList; +import org.bukkit.Bukkit; + +import java.util.Date; +import java.util.List; + +public class BanCommand extends AbstractCommand { + + public enum Actions { + ADD, REMOVE + } + + @Override + public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + + if (!scriptEntry.hasObject("action") && (arg.matchesPrefix("action") + || arg.matchesEnum(Actions.values()))) { + scriptEntry.addObject("action", arg.asElement()); + } + + else if (!scriptEntry.hasObject("targets") && (arg.matchesPrefix("targets", "target") + || arg.matchesArgumentList(dPlayer.class))) { + scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class)); + } + + 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(Duration.class))) { + scriptEntry.addObject("duration", arg.asType(Duration.class)); + } + + else { + arg.reportUnhandled(); + } + } + + scriptEntry.defaultObject("action", new Element("add")) + .defaultObject("reason", new Element("Banned.")); + + if (Actions.valueOf(scriptEntry.getObject("action").toString().toUpperCase()) == null) { + throw new IllegalArgumentException("Invalid action specified."); + } + + if (!scriptEntry.hasObject("targets") || ((List) scriptEntry.getObject("targets")).isEmpty()) { + throw new IllegalArgumentException("Must specify a valid target!"); + } + + } + + @Override + public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { + + Element action = scriptEntry.getElement("action"); + List targets = (List) scriptEntry.getObject("targets"); + Element reason = scriptEntry.getElement("reason"); + Duration duration = scriptEntry.getdObject("duration"); + Date expiration = null; + + if (duration != null && duration.getTicks() != 0) { + expiration = new Date(new Duration(System.currentTimeMillis() / 50 + duration.getTicks()).getTicks() * 50); + } + + dB.report(scriptEntry, getName(), + action.debug() + + aH.debugObj("targets", targets) + + reason.debug() + + (duration != null ? duration.debug() : "")); + + Actions banAction = Actions.valueOf(action.toString().toUpperCase()); + + switch (banAction) { + case ADD: + for (dPlayer player : targets) { + if (player.isValid()) { + Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), reason.toString(), expiration, null); + if (player.isOnline()) { + player.getPlayerEntity().kickPlayer(reason.toString()); + } + } + } + break; + + case REMOVE: + for (dPlayer player : targets) { + if (player.isValid()) { + if (player.getOfflinePlayer().isBanned()) { + Bukkit.getBanList(BanList.Type.NAME).pardon(player.getName()); + } + } + } + break; + + } + + } + +}