-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
GiveCommand.java
211 lines (202 loc) · 9.25 KB
/
GiveCommand.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.denizenscript.denizen.scripts.commands.item;
import com.denizenscript.denizen.scripts.containers.core.ItemScriptHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.utilities.inventory.SlotHelper;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
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 com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.function.Consumer;
public class GiveCommand extends AbstractCommand {
public GiveCommand() {
setName("give");
setSyntax("give [xp/<item>|...] (quantity:<#>) (unlimit_stack_size) (to:<inventory>) (slot:<slot>)");
setRequiredArguments(1, 5);
isProcedural = false;
}
// <--[command]
// @Name Give
// @Syntax give [xp/<item>|...] (quantity:<#>) (unlimit_stack_size) (to:<inventory>) (slot:<slot>)
// @Required 1
// @Maximum 5
// @Short Gives the player an item, xp, or money.
// @Group item
//
// @Description
// Gives the linked player or inventory items, xp.
//
// Optionally specify a slot to put the items into. If the slot is already filled, the next available slot will be used.
// If the inventory is full, the items will be dropped on the ground at the inventory's location.
// For player inventories, only the storage contents are valid - to equip armor or an offhand item, use <@link command equip>.
//
// Specifying "unlimit_stack_size" will allow an item to stack up to 64. This is useful for stacking items
// with a max stack size that is less than 64 (for example, most weapon and armor items have a stack size of 1).
//
// When giving an item, you can specify any valid inventory as a target. If unspecified, the linked player's inventory will be used.
//
// If 'xp' is specified, this will give experience points to the linked player.
//
// To give money to a player, use <@link command money>.
//
// @Tags
// <PlayerTag.money>
//
// @Usage
// Use to give XP to the player.
// - give xp quantity:10
//
// @Usage
// Use to give an item to the player.
// - give iron_sword
//
// @Usage
// Use to give an item and place it in a specific slot if possible.
// - give WATCH slot:5
//
// @Usage
// Use to give an item to some other defined player.
// - give diamond player:<[target]>
// -->
enum Type {ITEM, MONEY, EXP}
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Material material : Material.values()) {
if (material.isItem()) {
addOne.accept(material.name());
}
}
for (String itemScript : ItemScriptHelper.item_scripts.keySet()) {
addOne.accept(itemScript);
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("quantity")
&& arg.matchesPrefix("q", "qty", "quantity")
&& arg.matchesFloat()) {
if (arg.matchesPrefix("q", "qty")) {
Deprecations.qtyTags.warn(scriptEntry);
}
scriptEntry.addObject("quantity", arg.asElement());
scriptEntry.addObject("set_quantity", new ElementTag(true));
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("money", "coins")) {
Deprecations.giveTakeMoney.warn(scriptEntry);
scriptEntry.addObject("type", Type.MONEY);
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("xp", "exp", "experience")) {
scriptEntry.addObject("type", Type.EXP);
}
else if (!scriptEntry.hasObject("unlimit_stack_size")
&& arg.matches("unlimit_stack_size")) {
scriptEntry.addObject("unlimit_stack_size", new ElementTag(true));
}
else if (!scriptEntry.hasObject("items")
&& !scriptEntry.hasObject("type")
&& (arg.matchesArgumentList(ItemTag.class))) {
scriptEntry.addObject("items", arg.asType(ListTag.class).filter(ItemTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("inventory")
&& arg.matchesPrefix("t", "to")
&& arg.matchesArgumentType(InventoryTag.class)) {
scriptEntry.addObject("inventory", arg.asType(InventoryTag.class));
}
else if (!scriptEntry.hasObject("slot")
&& arg.matchesPrefix("slot")) {
scriptEntry.addObject("slot", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("type", Type.ITEM)
.defaultObject("unlimit_stack_size", new ElementTag(false))
.defaultObject("quantity", new ElementTag(1))
.defaultObject("slot", new ElementTag(1));
Type type = (Type) scriptEntry.getObject("type");
if (type == Type.ITEM) {
if (!scriptEntry.hasObject("items")) {
throw new InvalidArgumentsException("Must specify item/items!");
}
if (!scriptEntry.hasObject("inventory")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must specify an inventory to give to!");
}
scriptEntry.addObject("inventory", Utilities.getEntryPlayer(scriptEntry).getInventory());
}
}
else {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("Must link a player to give money or XP!");
}
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag unlimit_stack_size = scriptEntry.getElement("unlimit_stack_size");
InventoryTag inventory = scriptEntry.getObjectTag("inventory");
ElementTag quantity = scriptEntry.getElement("quantity");
Type type = (Type) scriptEntry.getObject("type");
ElementTag slot = scriptEntry.getElement("slot");
Object items_object = scriptEntry.getObject("items");
List<ItemTag> items = null;
if (items_object != null) {
items = (List<ItemTag>) items_object;
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("Type", type.name()), inventory, quantity, unlimit_stack_size, (items != null ? db("Items", items) : ""), slot);
}
switch (type) {
case MONEY:
if (Depends.economy != null) {
Depends.economy.depositPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
}
else {
Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
break;
case EXP:
Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(quantity.asInt());
break;
case ITEM:
boolean set_quantity = scriptEntry.hasObject("set_quantity");
boolean limited = !unlimit_stack_size.asBoolean();
for (ItemTag item : items) {
ItemStack is = item.getItemStack();
if (is.getType() == Material.AIR) {
Debug.echoError("Cannot give air!");
continue;
}
if (set_quantity) {
is.setAmount(quantity.asInt());
}
int slotId = SlotHelper.nameToIndexFor(slot.asString(), inventory.getInventory().getHolder());
if (slotId == -1) {
Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + slot.asString() + "' is not a valid slot!");
return;
}
List<ItemStack> leftovers = inventory.addWithLeftovers(slotId, limited, is);
if (!leftovers.isEmpty()) {
Debug.echoDebug(scriptEntry, "The inventory didn't have enough space, the rest of the items have been placed on the floor.");
for (ItemStack leftoverItem : leftovers) {
inventory.getLocation().getWorld().dropItem(inventory.getLocation(), leftoverItem);
}
}
}
break;
}
}
}