/
FeedCommand.java
146 lines (138 loc) · 6.42 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
135
136
137
138
139
140
141
142
143
144
145
146
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizen.npc.traits.HungerTrait;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class FeedCommand extends AbstractCommand {
public FeedCommand() {
setName("feed");
setSyntax("feed (<entity>) (amount:<#>) (saturation:<#.#>)");
setRequiredArguments(0, 3);
isProcedural = false;
}
// <--[command]
// @Name Feed
// @Syntax feed (<entity>) (amount:<#>) (saturation:<#.#>)
// @Required 0
// @Maximum 3
// @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, up to a total of 20 food points.
// The amount may be negative, to cause hunger instead of satiating it.
//
// You can optionally also specify an amount to change the saturation by.
// By default, the saturation change will be the same as the food level change.
// This is also up to a total of 20 points. This value may also be negative.
//
// Also accepts the 'target:<entity>' argument to specify the entity which will be fed the amount.
//
// @Tags
// <PlayerTag.food_level>
// <PlayerTag.formatted_food_level>
// <PlayerTag.saturation>
//
// @Usage
// Use to feed the player for 5 foodpoints (or 2.5 bars).
// - feed amount:5
//
// @Usage
// Use to feed an NPC for 10 foodpoints (or 5 bars).
// - feed <npc> amount:10
//
// @Usage
// Use to feed a player from a definition fully without refilling saturation.
// - feed <[player]> saturation:0
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesInteger()
&& arg.matchesPrefix("amount", "amt", "quantity", "qty", "a", "q")
&& !scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", arg.asElement());
}
else if (arg.matchesInteger()
&& arg.matchesPrefix("saturation", "sat", "s")
&& !scriptEntry.hasObject("saturation")) {
scriptEntry.addObject("saturation", arg.asElement());
}
else if (arg.matchesArgumentType(PlayerTag.class)
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")) {
scriptEntry.addObject("targetplayer", arg.asType(PlayerTag.class));
}
else if (Depends.citizens != null && arg.matchesArgumentType(NPCTag.class)
&& !scriptEntry.hasObject("targetplayer")
&& !scriptEntry.hasObject("targetnpc")) {
scriptEntry.addObject("targetnpc", arg.asType(NPCTag.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!");
}
}
scriptEntry.defaultObject("amount", new ElementTag(20));
scriptEntry.defaultObject("saturation", scriptEntry.getObject("amount"));
}
@Override
public void execute(ScriptEntry scriptEntry) {
PlayerTag player = scriptEntry.getObjectTag("targetplayer");
NPCTag npc = scriptEntry.getObjectTag("targetnpc");
ElementTag amount = scriptEntry.getElement("amount");
ElementTag saturation = scriptEntry.getElement("saturation");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), player, npc, amount, saturation);
}
if (npc != null) {
if (!npc.getCitizen().hasTrait(HungerTrait.class)) {
Debug.echoError(scriptEntry, "This NPC does not have the HungerTrait enabled! Use /trait hunger");
return;
}
npc.getCitizen().getOrAddTrait(HungerTrait.class).feed(amount.asInt());
}
else {
int result = Math.max(0, Math.min(20, player.getPlayerEntity().getFoodLevel() + amount.asInt()));
player.getPlayerEntity().setFoodLevel(result);
float satResult = Math.max(0, Math.min(20, player.getPlayerEntity().getSaturation() + saturation.asFloat()));
player.getPlayerEntity().setSaturation(satResult);
Debug.echoDebug(scriptEntry, "Player food level updated to " + result + " food and " + satResult + " saturation.");
}
}
}