-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
InventoryCommand.java
170 lines (132 loc) · 6.09 KB
/
InventoryCommand.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
package net.aufdemrand.denizen.scripts.commands.item;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.block.BlockState;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
/**
* Lets you store and edit inventories.
*
* @author David Cernat
*/
public class InventoryCommand extends AbstractCommand {
private enum Action { OPEN, COPY, MOVE, SWAP, ADD, REMOVE, KEEP, EXCLUDE, FILL, CLEAR }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values()))
// add Action
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
else if (!scriptEntry.hasObject("originEntity") &&
!scriptEntry.hasObject("originLocation") &&
arg.matchesPrefix("origin, o, source, s, items, i, from, f")) {
// Is entity
if (arg.matchesArgumentType(dEntity.class))
scriptEntry.addObject("originEntity", arg.asType(dEntity.class).setPrefix("entity"));
// Is location
else if (arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("originLocation", arg.asType(dLocation.class).setPrefix("location"));
}
else if (!scriptEntry.hasObject("destinationEntity") &&
!scriptEntry.hasObject("destinationLocation") &&
arg.matchesPrefix("destination, d, target, to, t")) {
// Is entity
if (arg.matchesArgumentType(dEntity.class))
scriptEntry.addObject("destinationEntity", arg.asType(dEntity.class).setPrefix("entity"));
// Is location
else if (arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("destinationLocation", arg.asType(dLocation.class).setPrefix("location"));
}
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an Inventory action!");
if (!scriptEntry.hasObject("destinationEntity") &&
!scriptEntry.hasObject("destinationLocation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "DESTINATION");
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Action action = (Action) scriptEntry.getObject("action");
dEntity originEntity = (dEntity) scriptEntry.getObject("originEntity");
dLocation originLocation = (dLocation) scriptEntry.getObject("originLocation");
dEntity destinationEntity = (dEntity) scriptEntry.getObject("destinationEntity");
dLocation destinationLocation = (dLocation) scriptEntry.getObject("destinationLocation");
dInventory origin = null;
dInventory destination = null;
if (originLocation != null) {
origin = new dInventory(originLocation.getBlock().getState());
}
else if (originEntity != null) {
origin = new dInventory(originEntity.getLivingEntity());
}
if (destinationLocation != null) {
destination = new dInventory(destinationLocation.getBlock().getState());
}
else if (destinationEntity != null) {
destination = new dInventory(destinationEntity.getLivingEntity());
}
switch (action) {
// Make the attached player open the destination inventory
case OPEN:
scriptEntry.getPlayer().getPlayerEntity().openInventory(destination.getInventory());
return;
// Turn destination's contents into a copy of origin's
case COPY:
origin.replace(destination);
return;
// Copy origin's contents to destination, then empty origin
case MOVE:
origin.replace(destination);
origin.clear();
return;
// Swap the contents of the two inventories
case SWAP:
dInventory temp = new dInventory(destination.getInventoryType())
.add(destination.getContents());
origin.replace(destination);
temp.replace(origin);
return;
// Add origin's contents to destination
case ADD:
destination.add(origin.getContents());
return;
// Remove origin's contents from destination
case REMOVE:
destination.remove(origin.getContents());
return;
// Keep only items from the origin's contents in the
// destination
case KEEP:
destination.keep(origin.getContents());
return;
// Exclude all items from the origin's contents in the
// destination
case EXCLUDE:
destination.exclude(origin.getContents());
return;
// Add origin's contents over and over to destination
// until it is full
case FILL:
destination.fill(origin.getContents());
return;
// Clear the content of the destination inventory
case CLEAR:
destination.clear();
return;
default:
return;
}
}
}