/
AgeCommand.java
140 lines (118 loc) · 5.36 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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.ArgumentHelper;
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 {
// <--[command]
// @Name Age
// @Syntax age [<entity>|...] (adult/baby/<age>) (lock)
// @Required 1
// @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
// ...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.getProcessedArgs()) {
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.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)) {
scriptEntry.addObject("age", arg.asElement());
}
else if (!scriptEntry.hasObject("lock")
&& arg.matches("lock")) {
scriptEntry.addObject("lock", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("No valid entities specified.");
}
// Use default age if one is not specified
scriptEntry.defaultObject("age", new ElementTag(1));
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) {
// Get objects
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");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (lock ? ArgumentHelper.debugObj("lock", lock) : "") +
(ageType != null ? ArgumentHelper.debugObj("agetype", ageType)
: ArgumentHelper.debugObj("age", age)) +
ArgumentHelper.debugObj("entities", entities.toString()));
}
// Go through all the entities and set their ages
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
// Check if entity specified can be described by 'EntityAge'
if (EntityAge.describes(entity)) {
EntityAge property = EntityAge.getFrom(entity);
// Adjust 'ageType'
if (ageType != null) {
if (ageType.equals(AgeType.BABY)) {
property.setBaby(true);
}
else {
property.setBaby(false);
}
}
else {
property.setAge(age);
}
// Adjust 'locked'
property.setLock(lock);
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), entity.identify() + " is not ageable!");
}
}
}
}
}