-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
BanCommand.java
187 lines (178 loc) · 8.09 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.denizenscript.denizen.scripts.commands.server;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
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.scripts.ScriptEntry;
import com.denizenscript.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 BanCommand() {
setName("ban");
setSyntax("ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (duration:<duration>) (source:<text>)");
setRequiredArguments(1, 5);
isProcedural = false;
}
// <--[command]
// @Name Ban
// @Syntax ban ({add}/remove) [<player>|.../addresses:<address>|...] (reason:<text>) (duration:<duration>) (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:
// 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.
// source: Sets the source of the ban. Defaults to "(Unknown)".
//
// @Tags
// <PlayerTag.is_banned>
// <PlayerTag.ban_reason>
// <PlayerTag.ban_expiration_time>
// <PlayerTag.ban_created_time>
// <PlayerTag.ban_source>
// <server.is_banned[<address>]>
// <server.ban_info[<address>].expiration_time>
// <server.ban_info[<address>].reason>
// <server.ban_info[<address>].created_time>
// <server.ban_info[<address>].source>
// <server.banned_addresses>
// <server.banned_players>
//
// @Usage
// Use to ban a player.
// - ban <[player]>
//
// @Usage
// Use to ban a list of players with a reason.
// - ban <[player]>|<[someplayer]> "reason:Didn't grow enough potatoes."
//
// @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
//
// @Usage
// Use to ban a player with a source.
// - ban <[aplayer]> "reason:Grew too many potatoes." source:<player.name>
//
// @Usage
// Use to ban an ip address.
// - ban addresses:127.0.0.1
//
// @Usage
// Use to temporarily ip ban all online players.
// - ban addresses:<server.online_players.parse[ip]> duration:5m
//
// @Usage
// Use to unban a list of players.
// - ban remove <[player]>|<[someplayer]>
//
// @Usage
// Use to unban an ip address.
// - ban remove addresses:127.0.0.1
// -->
public enum Actions {
ADD, REMOVE
}
@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")) {
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")) {
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("source") && arg.matchesPrefix("source")) {
scriptEntry.addObject("source", arg.asElement());
}
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!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
ListTag addresses = scriptEntry.getObjectTag("addresses");
ElementTag reason = scriptEntry.getElement("reason");
DurationTag duration = scriptEntry.getObjectTag("duration");
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);
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, (targets != null ? db("targets", targets) : ""), addresses, reason, duration, source);
}
Actions banAction = Actions.valueOf(action.toString().toUpperCase());
switch (banAction) {
case ADD:
if (targets != null) {
for (PlayerTag 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 (PlayerTag 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;
}
}
}