/
BanCommand.java
137 lines (117 loc) · 5.93 KB
/
BanCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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("addresses") && arg.matchesPrefix("addresses", "address")) {
scriptEntry.addObject("addresses", arg.asType(dList.class));
}
else if (!scriptEntry.hasObject("targets") && (arg.matchesPrefix("targets", "target")
|| arg.matchesArgumentList(dPlayer.class))) {
scriptEntry.addObject("targets", arg.asType(dList.class).filter(dPlayer.class, scriptEntry));
}
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 if (!scriptEntry.hasObject("source") && arg.matchesPrefix("source")) {
scriptEntry.addObject("source", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("action", new Element("add"))
.defaultObject("reason", new Element("Banned."))
.defaultObject("source", new Element("(Unknown)"));
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())
&& (!scriptEntry.hasObject("addresses") || ((List<Element>) scriptEntry.getObject("addresses")).isEmpty())) {
throw new IllegalArgumentException("Must specify a valid target or address!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element action = scriptEntry.getElement("action");
List<dPlayer> targets = (List<dPlayer>) scriptEntry.getObject("targets");
dList addresses = (dList) scriptEntry.getObject("addresses");
Element reason = scriptEntry.getElement("reason");
Duration duration = scriptEntry.getdObject("duration");
Element source = scriptEntry.getElement("source");
Date expiration = null;
if (duration != null && duration.getTicks() != 0) {
expiration = new Date(new Duration(System.currentTimeMillis() / 50 + duration.getTicks()).getTicks() * 50);
}
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
action.debug() +
(targets != null ? aH.debugObj("targets", targets) : "") +
(addresses != null ? addresses.debug() : "") +
reason.debug() +
(duration != null ? duration.debug() : "") +
source.debug());
}
Actions banAction = Actions.valueOf(action.toString().toUpperCase());
switch (banAction) {
case ADD:
if (targets != null) {
for (dPlayer player : targets) {
if (player.isValid()) {
Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), reason.toString(), expiration, source.toString());
if (player.isOnline()) {
player.getPlayerEntity().kickPlayer(reason.toString());
}
}
}
}
if (addresses != null) {
for (String address : addresses) {
Bukkit.getBanList(BanList.Type.IP).addBan(address, reason.toString(), expiration, source.toString());
}
}
break;
case REMOVE:
if (targets != null) {
for (dPlayer player : targets) {
if (player.isValid()) {
if (player.getOfflinePlayer().isBanned()) {
Bukkit.getBanList(BanList.Type.NAME).pardon(player.getName());
}
}
}
}
if (addresses != null) {
for (String address : addresses) {
Bukkit.getBanList(BanList.Type.IP).pardon(address);
}
}
break;
}
}
}