/
ConstantsTrait.java
207 lines (184 loc) · 8.69 KB
/
ConstantsTrait.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.denizen.events.bukkit.ScriptReloadEvent;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.Paginator;
import net.citizensnpcs.util.Messages;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
public class ConstantsTrait extends Trait {
// Saved to C2 saves.yml
@Persist(value = "", collectionType = ConcurrentHashMap.class)
private Map<String, String> constants = new HashMap<>();
// Used internally
private Map<String, String> assignmentConstants = new HashMap<>();
private String assignment = null;
public ConstantsTrait() {
super("constants");
}
/**
* Returns the value of the specified Constant, unique to this NPC. Note:
* Returns tags filled with the currently assigned NPC. See: Denizen TagManager
*
* @param name name of constant, case in-sensitive
* @return value of the constant
*/
public String getConstant(String name) {
getAssignmentConstants();
if (constants.containsKey(CoreUtilities.toLowerCase(name))) // TODO: shouldDebug
{
return TagManager.tag(constants.get(CoreUtilities.toLowerCase(name)),
new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), false, null, true, null));
}
else if (getAssignmentConstants().containsKey(CoreUtilities.toLowerCase(name))) {
return TagManager.tag(assignmentConstants.get(CoreUtilities.toLowerCase(name)),
new BukkitTagContext(null, DenizenAPI.getDenizenNPC(npc), false, null, true, null));
}
return null;
}
/**
* Gets a map of the NPCs constants. Note: Does not include any constants
* inherited by the NPCs Assignment. To grab a comprehensive map of both,
* use {@link #getAllConstants()}.
*
* @return a map of constants, keyed by constant name.
*/
public Map<String, String> getNPCConstants() {
return constants;
}
/**
* Gets a map of the NPCs constants, including those inherited by the Assignment.
* Any constants that are overridden by this NPC are taken into account, so this
* map may differ from the constants found in the Assignment. To get a map of
* constants from an Assignment, use {@link #getAssignmentConstants()}.
*
* @return a map of constants, keyed by constant name.
*/
public Map<String, String> getAllConstants() {
Map<String, String> allConstants = new HashMap<>();
getAssignmentConstants().putAll(allConstants);
getNPCConstants().putAll(allConstants);
return allConstants;
}
/**
* Sets the value of a constant, as identified by the name. This will
* override any constants inherited from the NPCs Assignment.
*
* @param name name of the constant, case in-sensitive
* @param value value of the constant
*/
public void setConstant(String name, String value) {
constants.put(CoreUtilities.toLowerCase(name), value);
}
/**
* Removes an NPC-specific constant, as identified by the name. This will
* not remove any values inherited from an NPCs Assignment, only constants
* unique to this NPC.
*
* @param name name of the constant, case in-sensitive
*/
public void removeConstant(String name) {
constants.remove(CoreUtilities.toLowerCase(name));
}
/**
* Checks if this NPC has any unique constants, beyond what is inherited from
* the NPCs Assignment.
*
* @return true if NPC constants are present
*/
public boolean hasNPCConstants() {
return !constants.isEmpty();
}
public Map<String, String> getAssignmentConstants() {
// Check to make sure NPC has an assignment
if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment()) {
// Check to make sure assignment hasn't changed.. if it has, the assignmentConstants map will be rebuilt
if (assignment != null && assignment.equalsIgnoreCase(npc.getTrait(AssignmentTrait.class).getAssignment().getName())) {
return assignmentConstants;
}
else {
return rebuildAssignmentConstants();
}
}
return assignmentConstants;
}
public Map<String, String> rebuildAssignmentConstants() {
// Builds a map of constants inherited from the NPCs current Assignment
if (!npc.hasTrait(AssignmentTrait.class) || !npc.getTrait(AssignmentTrait.class).hasAssignment()) {
assignmentConstants.clear();
return assignmentConstants;
}
if (npc.getTrait(AssignmentTrait.class).getAssignment() != null) {
assignment = npc.getTrait(AssignmentTrait.class).getAssignment().getName();
assignmentConstants.clear();
}
else {
return assignmentConstants;
}
try {
if (ScriptRegistry.getScriptContainer(assignment).contains("DEFAULT CONSTANTS")) {
for (StringHolder constant : ScriptRegistry.getScriptContainer(assignment)
.getConfigurationSection("DEFAULT CONSTANTS").getKeys(false)) {
assignmentConstants.put(CoreUtilities.toLowerCase(constant.str),
ScriptRegistry.getScriptContainer(assignment)
.getString("DEFAULT CONSTANTS." + constant.str.toUpperCase(), ""));
}
}
}
catch (NullPointerException e) {
Debug.echoError("Constants in assignment script '" + npc.getTrait(AssignmentTrait.class)
.getAssignment().getName() + "' improperly defined, no constants will be set.");
}
return assignmentConstants;
}
/**
* Rebuilds assignment constants on a script reload
*/
@EventHandler
public void onScriptsReload(ScriptReloadEvent event) {
rebuildAssignmentConstants();
}
public void describe(CommandSender sender, int page) throws CommandException {
Paginator paginator = new Paginator().header("Constants for " + npc.getName());
paginator.addLine("<e>NPC-specific constants: " + (hasNPCConstants() ? "" : "None.") + "");
if (hasNPCConstants()) {
paginator.addLine("<e>Key: <a>Name <b>Value");
}
for (Entry<String, String> constant : constants.entrySet()) {
paginator.addLine("<a> " + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
paginator.addLine("");
if (npc.hasTrait(AssignmentTrait.class) && npc.getTrait(AssignmentTrait.class).hasAssignment()) {
getAssignmentConstants();
// List constants inherited from an Assignment.
paginator.addLine("<e>Constants for assignment '" + assignment.toUpperCase() + "':");
paginator.addLine("<e>Key: <a>Name <b>Value");
for (Entry<String, String> constant : getAssignmentConstants().entrySet()) {
// If a constant from the Assignment has been overridden by a NPC constant,
// change formatting to indicate so.
if (constants.containsKey(constant.getKey())) {
paginator.addLine("<m>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<r> <m>" + constant.getValue());
}
else {
paginator.addLine("<a>" + String.valueOf(constant.getKey().charAt(0)).toUpperCase() + constant.getKey().substring(1) + "<b> " + constant.getValue());
}
}
paginator.addLine("");
}
if (!paginator.sendPage(sender, page)) {
throw new CommandException(Messages.COMMAND_PAGE_MISSING, page);
}
}
}