-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
KickCommand.java
88 lines (70 loc) · 2.86 KB
/
KickCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.List;
public class KickCommand extends AbstractCommand {
// <--[command]
// @Name kick
// @Syntax kick [<player>|...] (reason:<text>)
// @Required 1
// @Short Kicks a player from the server.
// @Group player
//
// @Description
// Kick a player or a list of players from the server and optionally specify a reason.
// If no reason is specified it defaults to "Kicked."
//
// @Tags
// None
//
// @Usage
// Use to kick the player with the default reason.
// - kick <player>
//
// @Usage
// Use to kick the player with a reason.
// - kick <player> "reason:Because I can."
//
// @Usage
// Use to kick another player with a reason.
// - kick <[player]> "reason:Because I can."
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("reason")) {
scriptEntry.addObject("reason", arg.asElement());
}
else if (arg.matchesPrefix("targets", "target", "players")
|| arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
}
scriptEntry.defaultObject("reason", new ElementTag("Kicked."));
if (!scriptEntry.hasObject("targets")) {
throw new InvalidArgumentsException("Must specify target(s).");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag reason = scriptEntry.getElement("reason");
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
ArgumentHelper.debugObj("targets", targets) +
reason.debug());
}
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
player.getPlayerEntity().kickPlayer(reason.toString());
}
}
}
}