Skip to content

Commit

Permalink
Extend ban controls.
Browse files Browse the repository at this point in the history
* add Ban command
* add is_banned mechanism for dPlayer
* add ban_info tags
minor improvement to kick command meta
  • Loading branch information
Fortifier42 committed Dec 10, 2015
1 parent fc1c59a commit 1539579
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 12 deletions.
63 changes: 60 additions & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -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 <p@player.is_online>
Expand Down Expand Up @@ -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 <p@player.ban_info.expiration>
// 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 <p@player.ban_info.reason>
// 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 <p@player.ban_info.created>
// 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 <p@player.in_group[<group_name>]>
// @returns Element(Boolean)
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -353,6 +351,46 @@ public void registerCoreMembers() {
registerCoreMember(AttackCommand.class,
"ATTACK", "attack (<entity>|...) (target:<entity>/cancel)", 0);

// <--[command]
// @Name Ban
// Syntax ban ({ADD}/REMOVE) [<player>|...] reason:<text> duration:<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
// <p@player.is_banned>
// <p@player.ban_info.reason>
// <p@player.ban_info.expiration>
// <p@player.ban_info.created>

// @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) [<player>|...] (reason:<text>) (duration:<duration>)", 1);


// <--[command]
// @Name Break
Expand Down Expand Up @@ -1548,7 +1586,7 @@ public void registerCoreMembers() {
// - kick p@mcmonkey4eva "reason:Because I can."
// -->
registerCoreMember(KickCommand.class,
"KICK", "kick (<player>|...) (reason:<text>)", 1);
"KICK", "kick [<player>|...] (reason:<text>)", 1);

// <--[command]
// @Name Leash
Expand Down
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -44,9 +42,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element reason = scriptEntry.getElement("reason");
List<dPlayer> targets = (List<dPlayer>) 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()) {
Expand Down
@@ -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<dPlayer>) 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<dPlayer> targets = (List<dPlayer>) 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;

}

}

}

0 comments on commit 1539579

Please sign in to comment.