-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
InventoryCommand.java
157 lines (113 loc) · 5.42 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
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 { COPY, MOVE, SWAP, 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.asElement().toString()));
else if (!scriptEntry.hasObject("originEntity") &&
!scriptEntry.hasObject("originLocation") &&
arg.matchesPrefix("origin, o, source, s")) {
// 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, 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("originEntity") &&
!scriptEntry.hasObject("originLocation"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "ORIGIN");
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) {
BlockState blockState = originLocation.getBlock().getState();
if (blockState instanceof InventoryHolder) {
origin = new dInventory(((InventoryHolder) blockState).getInventory());
}
}
else if (originEntity != null) {
LivingEntity entity = originEntity.getLivingEntity();
if (entity instanceof Player) {
origin = new dInventory(((Player) entity).getInventory());
}
}
if (destinationLocation != null) {
BlockState blockState = destinationLocation.getBlock().getState();
if (blockState instanceof InventoryHolder) {
destination = new dInventory(((InventoryHolder) blockState).getInventory());
}
}
else if (destinationEntity != null) {
LivingEntity entity = destinationEntity.getLivingEntity();
if (entity instanceof Player) {
destination = new dInventory(((Player) entity).getInventory());
}
}
switch (action) {
// Turn destination's contents into a copy of origin's
case COPY:
origin.replace(destination);
// Copy origin's contents to destination, then empty origin
case MOVE:
origin.replace(destination);
origin.clear();
// Swap the contents of the two inventories
case SWAP:
dInventory temp = destination;
origin.replace(destination);
temp.replace(origin);
// Clear the content of the destination inventory
case CLEAR:
destination.clear();
}
}
}