/
FeedCommand.java
134 lines (119 loc) · 5.64 KB
/
FeedCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.npc.traits.HungerTrait;
import com.denizenscript.denizen.objects.dNPC;
import com.denizenscript.denizen.objects.dPlayer;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class FeedCommand extends AbstractCommand {
// <--[command]
// @Name Feed
// @Syntax feed (amount:<#>) (target:<entity>)
// @Required 0
// @Short Feed the player or npc.
// @Group entity
//
// @Description
// Feeds the player or npc specified. By default targets the player attached to the script queue and feeds
// a full amount. Accepts the 'amount:' argument, which is in half bar increments, for a total of 20 food
// points. Also accepts the 'target:<entity>' argument to specify the entity which will be fed the amount.
// NOTE: This command is outdated and bound to be updated.
//
// @Tags
// <p@player.food_level>
// <p@player.food_level.formatted>
//
// @Usage
// Use to feed the player for 5 foodpoints or 2.5 bars.
// - feed amount:5
//
// @Usage
// Use to feed an npc with id 5 for 10 foodpoints or 5 bars.
// - feed amount:10 target:n@5
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)
&& !scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", arg.asElement());
}
else if (arg.matchesArgumentType(dPlayer.class)
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")) {
scriptEntry.addObject("targetplayer", arg.asType(dPlayer.class));
}
else if (Depends.citizens != null && arg.matchesArgumentType(dNPC.class)
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")) {
scriptEntry.addObject("targetnpc", arg.asType(dNPC.class));
}
// Backwards compatibility
else if (arg.matches("NPC")
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")
&& Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("targetnpc", Utilities.getEntryNPC(scriptEntry));
}
else if (arg.matches("PLAYER")
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")
&& Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("targetplayer", Utilities.getEntryPlayer(scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("targetplayer") &&
!scriptEntry.hasObject("targetnpc")) {
if (Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.addObject("targetplayer", Utilities.getEntryPlayer(scriptEntry));
}
else if (Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("targetnpc", Utilities.getEntryNPC(scriptEntry));
}
else {
throw new InvalidArgumentsException("Must specify a player!");
}
}
if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", new ElementTag(9999)); // TODO: 9999?
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
dPlayer player = (dPlayer) scriptEntry.getObject("targetplayer");
dNPC npc = (dNPC) scriptEntry.getObject("targetnpc");
ElementTag amount = scriptEntry.getElement("amount");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(player == null ? "" : player.debug())
+ (npc == null ? "" : npc.debug())
+ amount.debug());
}
if (npc != null) {
if (!npc.getCitizen().hasTrait(HungerTrait.class)) {
Debug.echoError(scriptEntry.getResidingQueue(), "This NPC does not have the HungerTrait enabled! Use /trait hunger");
return;
}
npc.getCitizen().getTrait(HungerTrait.class).feed(amount.asInt());
}
else if (player != null) {
if (95999 - player.getPlayerEntity().getFoodLevel() < amount.asInt()) // Setting hunger too high = error
{
amount = new ElementTag(95999 - player.getPlayerEntity().getFoodLevel());
}
player.getPlayerEntity().setFoodLevel(player.getPlayerEntity().getFoodLevel() + amount.asInt());
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "No target?"); // Mostly just here to quiet code analyzers.
}
}
}