/
DropCommand.java
91 lines (72 loc) · 3.28 KB
/
DropCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
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.arguments.dItem;
import net.aufdemrand.denizen.arguments.dLocation;
import net.aufdemrand.denizen.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ExperienceOrb;
/**
* Drops items or experience in a location.
*
* @author Jeremy Schroeder
*/
public class DropCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize necessary fields
dItem item = null;
Integer qty = null;
dLocation location = null;
Boolean exp = false;
// Set some defaults
if (scriptEntry.getPlayer() != null)
location = new dLocation(scriptEntry.getPlayer().getLocation());
if (location == null && scriptEntry.getNPC() != null)
location = new dLocation(scriptEntry.getNPC().getLocation());
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesItem(arg)) {
item = aH.getItemFrom(arg);
} else if (aH.matchesArg("XP, EXP", arg)) {
exp = true;
} else if (aH.matchesQuantity(arg)) {
qty = aH.getIntegerFrom(arg);
} else if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
if (item == null && !exp) throw new InvalidArgumentsException(Messages.ERROR_INVALID_ITEM);
if (location == null) throw new InvalidArgumentsException(Messages.ERROR_MISSING_LOCATION);
// Stash objects
scriptEntry.addObject("location", location);
scriptEntry.addObject("item", item);
scriptEntry.addObject("exp", exp);
scriptEntry.addObject("qty", qty);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
dLocation location = (dLocation) scriptEntry.getObject("location");
Integer qty = (Integer) scriptEntry.getObject("qty");
Boolean exp = (Boolean) scriptEntry.getObject("exp");
dItem item = (dItem) scriptEntry.getObject("item");
// Set quantity if not specified
if (qty != null && item != null)
item.getItemStack().setAmount(qty);
else qty = 1;
// Report to dB
dB.report(getName(),
location.debug()
+ (item != null ? item.debug()
: aH.debugObj("Exp", String.valueOf(qty))));
if (exp)
((ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB))
.setExperience(qty);
else
location.getWorld().dropItemNaturally(location, item.getItemStack());
}
}