/
PlayerPointsCommand.java
127 lines (99 loc) · 4.3 KB
/
PlayerPointsCommand.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
package com.denizenscript.depenizen.bukkit.commands.playerpoints;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.depenizen.bukkit.bridges.PlayerPointsBridge;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.black_ixx.playerpoints.PlayerPoints;
import com.denizenscript.denizencore.utilities.debugging.Debug;
public class PlayerPointsCommand extends AbstractCommand {
// <--[command]
// @Name playerpoints
// @Syntax playerpoints [set/give/take] (amount:<amount>) (target:<player>)
// @Group Depenizen
// @Plugin Depenizen, PlayerPoints
// @Required 2
// @Short Adjusts the amount of points the player has.
// @Description
// Take, give or set the amount of points a player has.
// This is useful for plugins supporting this kind of economy
// which uses the points instead of money as an alternative system.
// This works for offline players.
// @Tags
// <PlayerTag.playerpoints_points>
// @Usage
// Use to give 5 points to the player
// - playerpoints give 5
// @Usage
// Use take 2 points from a target player
// - playerpoints take 2 target:p@Mwthorn
// @Usage
// Use to set 10 points to the player
// - playerpoints set 10
// -->
private enum Action {SET, GIVE, TAKE}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("target")
&& arg.matchesPrefix("target")) {
scriptEntry.addObject("target", arg.asType(PlayerTag.class));
}
else if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Action not specified! (remove/mob/player/misc)");
}
if (!scriptEntry.hasObject("amount")) {
throw new InvalidArgumentsException("Amount not specified!");
}
if (!scriptEntry.hasObject("target")) {
if (Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("target", Utilities.getEntryPlayer(scriptEntry));
}
else {
throw new InvalidArgumentsException("This command does not have a player attached!");
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
PlayerTag target = scriptEntry.getObjectTag("target");
ElementTag action = scriptEntry.getObjectTag("action");
ElementTag amount = scriptEntry.getObjectTag("amount");
// Report to dB
Debug.report(scriptEntry, getName(), action.debug()
+ (target != null ? target.debug() : "")
+ (amount != null ? amount.debug() : ""));
if (target == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Target not found!");
return;
}
if (amount == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Entity not specified!");
return;
}
PlayerPoints plugin = (PlayerPoints) PlayerPointsBridge.instance.plugin;
if (action.asString().equalsIgnoreCase("give")) {
plugin.getAPI().give(target.getOfflinePlayer().getUniqueId(), amount.asInt());
}
else if (action.asString().equalsIgnoreCase("take")) {
plugin.getAPI().take(target.getOfflinePlayer().getUniqueId(), amount.asInt());
}
else if (action.asString().equalsIgnoreCase("set")) {
plugin.getAPI().set(target.getOfflinePlayer().getUniqueId(), amount.asInt());
}
}
}