/
MoneyCommand.java
147 lines (139 loc) · 5.78 KB
/
MoneyCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.depends.Depends;
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 com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.milkbowl.vault.economy.Economy;
import java.util.Arrays;
import java.util.List;
public class MoneyCommand extends AbstractCommand {
public MoneyCommand() {
setName("money");
setSyntax("money [give/take/set] (quantity:<#.#>) (players:<player>|...)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Money
// @Syntax money [give/take/set] (quantity:<#.#>) (players:<player>|...)
// @Required 1
// @Maximum 3
// @Short Manage a player's money.
// @Group player
// @Plugin Vault
//
// @Description
// Give money to, take money from, and set the balance of a player.
// If no quantity is specified it defaults to '1'. You can specify a list of
// players to give to or take from. If no player(s) are specified defaults to the attached player.
// NOTE: This requires an economy plugin. May work for offline players depending on economy plugin.
//
// @Tags
// <PlayerTag.money>
//
// @Usage
// Use to give 1 money to the player.
// - money give
//
// @Usage
// Use to take 10 money from a player.
// - money take quantity:10 from:<[player]>
//
// @Usage
// Use to give all players on the server 100 money.
// - money give quantity:100 to:<server.players>
//
// @Usage
// Use to set the money of all online players to 250.
// - money set quantity:250 players:<server.online_players>
// -->
enum Action {
GIVE,
TAKE,
SET
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
if (Depends.economy == null) {
Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
return;
}
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("quantity")
&& arg.matchesPrefix("quantity", "qty", "q")
&& arg.matchesFloat()) {
if (arg.matchesPrefix("q", "qty")) {
Deprecations.qtyTags.warn(scriptEntry);
}
scriptEntry.addObject("quantity", arg.asElement());
}
else if (!scriptEntry.hasObject("players")
&& arg.matchesPrefix("to", "from", "players", "player") &&
arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("quantity", new ElementTag(1));
if (!scriptEntry.hasObject("players")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("This command must have a player attached!");
}
else {
scriptEntry.addObject("players",
Arrays.asList(Utilities.getEntryPlayer(scriptEntry)));
}
}
else if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify a valid action!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
ElementTag quantity = scriptEntry.getElement("quantity");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("Player(s)", players) + action.debug() + quantity.debug());
}
Economy eco = Depends.economy;
double amt = quantity.asDouble();
switch (Action.valueOf(action.asString().toUpperCase())) {
case GIVE:
for (PlayerTag player : players) {
eco.depositPlayer(player.getOfflinePlayer(), amt);
}
break;
case TAKE:
for (PlayerTag player : players) {
eco.withdrawPlayer(player.getOfflinePlayer(), amt);
}
break;
case SET:
for (PlayerTag player : players) {
double balance = eco.getBalance(player.getOfflinePlayer());
if (amt > balance) {
eco.depositPlayer(player.getOfflinePlayer(), amt - balance);
}
else {
eco.withdrawPlayer(player.getOfflinePlayer(), balance - amt);
}
}
break;
}
}
}