/
FishCommand.java
88 lines (68 loc) · 3.51 KB
/
FishCommand.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
package net.aufdemrand.denizen.scripts.commands.npc;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.interfaces.FishingHelper;
import net.aufdemrand.denizen.npc.traits.FishingTrait;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class FishCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (!scriptEntry.hasObject("catch")
&& arg.matchesPrefix("catch")
&& arg.matchesEnum(FishingHelper.CatchType.values())) {
scriptEntry.addObject("catch", arg.asElement());
}
else if (!scriptEntry.hasObject("stop")
&& arg.matches("stop")) {
scriptEntry.addObject("stop", Element.TRUE);
}
else if (!scriptEntry.hasObject("percent")
&& arg.matchesPrefix("catchpercent", "percent", "chance", "c")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("percent", arg.asElement());
}
}
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("stop")) {
throw new InvalidArgumentsException("Must specify a valid location!");
}
scriptEntry.defaultObject("catch", new Element("NONE"))
.defaultObject("stop", Element.FALSE)
.defaultObject("percent", new Element(65));
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() || !((BukkitScriptEntryData) scriptEntry.entryData).getNPC().isSpawned()) {
throw new InvalidArgumentsException("This command requires a linked and spawned NPC!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
dLocation location = scriptEntry.getdObject("location");
Element catchtype = scriptEntry.getElement("catch");
Element stop = scriptEntry.getElement("stop");
Element percent = scriptEntry.getElement("percent");
dNPC npc = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC();
FishingTrait trait = npc.getFishingTrait();
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), location.debug() + catchtype.debug() + percent.debug() + stop.debug());
}
if (stop.asBoolean()) {
trait.stopFishing();
return;
}
npc.getEquipmentTrait().set(0, new ItemStack(Material.FISHING_ROD));
trait.setCatchPercent(percent.asInt());
trait.setCatchType(FishingHelper.CatchType.valueOf(catchtype.asString().toUpperCase()));
trait.startFishing(location);
}
}