-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
TakeCommand.java
160 lines (128 loc) · 7.12 KB
/
TakeCommand.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
package net.aufdemrand.denizen.scripts.commands.item;
import java.util.List;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import net.aufdemrand.denizen.utilities.depends.Depends;
/* TAKE [MONEY|ITEMINHAND|#(:#)|MATERIAL_TYPE(:#)] (QTY:#) */
/*
* Arguments: [] - Required, () - Optional
* [MONEY|ITEMINHAND|[#](:#)|[MATERIAL_TYPE](:#)] specifies what to take.
* [MONEY] takes money using your economy.
* [ITEMINHAND] takes from the item the Player has in their hand.
* [#](:#) takes the item with the specified item ID. Optional
* argument (:#) can specify a specific data value.
* [MATERIAL_TYPE](:#) takes the item with the specified
* bukkit MaterialType. Optional argument (:#) can specify
* a specific data value.
* (QTY:#) specifies quantity. If not specified, assumed 'QTY:1'
*
*/
public class TakeCommand extends AbstractCommand{
private enum Type { MONEY, ITEMINHAND, ITEM, INVENTORY }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matches("money, coins"))
scriptEntry.addObject("type", Type.MONEY);
else if (!scriptEntry.hasObject("type")
&& arg.matches("item_in_hand, iteminhand"))
scriptEntry.addObject("type", Type.ITEMINHAND);
else if (!scriptEntry.hasObject("qty")
&& arg.matchesPrefix("q, qty, quantity")
&& arg.matchesPrimitive(aH.PrimitiveType.Double))
scriptEntry.addObject("qty", arg.asElement());
else if (!scriptEntry.hasObject("items")
&& !scriptEntry.hasObject("type")
&& arg.matchesArgumentList(dItem.class))
scriptEntry.addObject("items", dList.valueOf(arg.raw_value.replace("item:", "")).filter(dItem.class));
else if (!scriptEntry.hasObject("inventory")
&& arg.matchesPrefix("f, from")
&& arg.matchesArgumentType(dInventory.class))
scriptEntry.addObject("inventory", arg.asType(dInventory.class));
else if (!scriptEntry.hasObject("type")
&& arg.matches("inventory"))
scriptEntry.addObject("type", Type.INVENTORY);
else if (!scriptEntry.hasObject("inventory")
&& arg.matches("npc"))
scriptEntry.addObject("inventory", new dInventory(scriptEntry.getNPC().getEntity()));
}
scriptEntry.defaultObject("type", Type.ITEM)
.defaultObject("inventory", (scriptEntry.hasPlayer() ? new dInventory(scriptEntry.getPlayer().getPlayerEntity()) : null))
.defaultObject("qty", new Element(1));
if (scriptEntry.getObject("type") == Type.ITEM && scriptEntry.getObject("items") == null)
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ITEMS");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dInventory inventory = (dInventory) scriptEntry.getObject("inventory");
Element qty = scriptEntry.getElement("qty");
Type type = (Type) scriptEntry.getObject("type");
Object items_object = scriptEntry.getObject("items");
List<dItem> items = null;
if (items_object != null)
items = (List<dItem>) items_object;
dB.report(getName(),
aH.debugObj("Type", type.name())
+ qty.debug()
+ inventory.debug()
+ aH.debugObj("Items", items));
switch (type) {
case INVENTORY:
inventory.clear();
break;
case ITEMINHAND:
int inHandAmt = scriptEntry.getPlayer().getPlayerEntity().getItemInHand().getAmount();
int theAmount = qty.asInt();
ItemStack newHandItem = new ItemStack(0);
if (theAmount > inHandAmt) {
dB.echoDebug("...player did not have enough of the item in hand, so Denizen just took as many as it could. To avoid this situation, use an IF <PLAYER.ITEM_IN_HAND.QTY>.");
scriptEntry.getPlayer().getPlayerEntity().setItemInHand(newHandItem);
}
else {
// amount is just right!
if (theAmount == inHandAmt) {
scriptEntry.getPlayer().getPlayerEntity().setItemInHand(newHandItem);
} else {
// amount is less than what's in hand, need to make a new itemstack of what's left...
newHandItem = new ItemStack(scriptEntry.getPlayer().getPlayerEntity().getItemInHand().getType(),
inHandAmt - theAmount, scriptEntry.getPlayer().getPlayerEntity().getItemInHand().getData().getData());
newHandItem.setItemMeta(scriptEntry.getPlayer().getPlayerEntity().getItemInHand().getItemMeta());
scriptEntry.getPlayer().getPlayerEntity().setItemInHand(newHandItem);
scriptEntry.getPlayer().getPlayerEntity().updateInventory();
}
}
break;
case MONEY:
if(Depends.economy != null) {
dB.echoDebug ("...taking " + qty.asDouble() + " money.");
Depends.economy.withdrawPlayer(scriptEntry.getPlayer().getName(), qty.asDouble());
} else {
dB.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
}
break;
case ITEM:
for (dItem item : items) {
ItemStack is = item.getItemStack();
if (is.getItemMeta() instanceof BookMeta)
inventory.removeBook(is);
is.setAmount(qty.asInt());
if (!inventory.getInventory().removeItem(is).isEmpty())
dB.echoError("Inventory does not contain at least " + qty.asInt() + " of " + item.identify() +
"... Taking as much as possible...");
}
break;
}
}
}