/
AgeCommand.java
123 lines (114 loc) · 4.85 KB
/
AgeCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.properties.entity.EntityAge;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.List;
public class AgeCommand extends AbstractCommand {
public AgeCommand() {
setName("age");
setSyntax("age [<entity>|...] (adult/baby/<age>) (lock)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Age
// @Syntax age [<entity>|...] (adult/baby/<age>) (lock)
// @Required 1
// @Maximum 3
// @Short Sets the ages of a list of entities, optionally locking them in those ages.
// @Group entity
//
// @Description
// Some living entity types are 'ageable' which can affect an entities ability to breed, or whether they appear as a baby or an adult.
// Using the 'age' command allows modification of an entity's age.
// Specify an entity and either 'baby', 'adult', or an integer age to set the age of an entity.
// Using the 'lock' argument will keep the entity from increasing its age automatically.
// NPCs which use ageable entity types can also be specified.
//
// @Tags
// <EntityTag.age>
//
// @Usage
// Use to make an ageable entity a permanant baby.
// - age <[some_entity]> baby lock
//
// @Usage
// ...or a mature adult.
// - age <[some_entity]> adult lock
//
// @Usage
// Use to make a baby entity an adult.
// - age <[some_npc]> adult
//
// @Usage
// Use to mature some animals so that they are old enough to breed.
// - age <player.location.find_entities.within[20]> adult
// -->
private enum AgeType {ADULT, BABY}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("agetype")
&& arg.matchesEnum(AgeType.values())) {
scriptEntry.addObject("agetype", AgeType.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("age")
&& arg.matchesInteger()) {
scriptEntry.addObject("age", arg.asElement());
}
else if (!scriptEntry.hasObject("lock")
&& arg.matches("lock")) {
scriptEntry.addObject("lock", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("No valid entities specified.");
}
scriptEntry.defaultObject("age", new ElementTag(1));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
AgeType ageType = (AgeType) scriptEntry.getObject("agetype");
int age = scriptEntry.getElement("age").asInt();
boolean lock = scriptEntry.hasObject("lock");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (lock ? db("lock", true) : ""), (ageType != null ? db("agetype", ageType) : db("age", age)), db("entities", entities));
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
if (EntityAge.describes(entity)) {
EntityAge property = EntityAge.getFrom(entity);
if (ageType != null) {
if (ageType.equals(AgeType.BABY)) {
property.setAge(-24000);
}
else {
property.setAge(0);
}
}
else {
property.setAge(age);
}
property.setLock(lock);
}
else {
Debug.echoError(scriptEntry, entity.identify() + " is not ageable!");
}
}
}
}
}