-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
HeroesHero.java
440 lines (381 loc) · 14.1 KB
/
HeroesHero.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package com.denizenscript.depenizen.bukkit.objects.heroes;
import com.denizenscript.depenizen.bukkit.support.Support;
import com.herocraftonline.heroes.Heroes;
import com.herocraftonline.heroes.characters.Hero;
import com.denizenscript.depenizen.bukkit.support.plugins.HeroesSupport;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dNPC;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.objects.Adjustable;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Fetchable;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HeroesHero implements dObject, Adjustable {
/////////////////////
// PATTERNS
/////////////////
final static Pattern npc_pattern = Pattern.compile("(hero@)?npc-(\\d+)", Pattern.CASE_INSENSITIVE);
/////////////////////
// OBJECT FETCHER
/////////////////
public static HeroesHero valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("hero")
public static HeroesHero valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
string = string.replace("hero@", "");
if (dPlayer.matches(string)) {
return new HeroesHero(dPlayer.valueOf(string));
}
if (Depends.citizens != null) {
Matcher m = npc_pattern.matcher(string);
if (m.matches()) {
NPC npc = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(m.group(2)));
if (npc != null) {
return new HeroesHero(dNPC.mirrorCitizensNPC(npc));
}
}
}
return null;
}
public static boolean matches(String arg) {
if (valueOf(arg) != null) {
return true;
}
return false;
}
/////////////////////
// CONSTRUCTORS
/////////////////
Hero hero = null;
dObject denizenObj = null;
public HeroesHero(dPlayer player) {
denizenObj = player;
if (player.isOnline()) {
Heroes heroes = Support.getPlugin(HeroesSupport.class);
hero = heroes.getCharacterManager().getHero(player.getPlayerEntity());
}
}
public HeroesHero(dNPC npc) {
denizenObj = npc;
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
Heroes heroes = Support.getPlugin(HeroesSupport.class);
hero = heroes.getCharacterManager().getHero((Player) npc.getEntity());
}
}
public HeroesHero(Hero hero) {
this.hero = hero;
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC(hero.getPlayer())) {
denizenObj = dNPC.fromEntity(hero.getPlayer());
}
else {
denizenObj = dPlayer.mirrorBukkitPlayer(hero.getPlayer());
}
}
public Hero getHero() {
return hero;
}
public boolean isPlayer() {
return denizenObj instanceof dPlayer;
}
public boolean isNPC() {
return denizenObj instanceof dNPC;
}
@Override
public String toString() {
return identify();
}
public dObject getDenizenObject() {
return denizenObj;
}
/////////////////////
// dObject Methods
/////////////////
private String prefix = "Hero";
@Override
public String getPrefix() {
return prefix;
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getObjectType() {
return "Hero";
}
@Override
public String identify() {
return "hero@" + (denizenObj instanceof dNPC ?
"npc-" + ((dNPC) denizenObj).getId() :
hero.getPlayer().getUniqueId());
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String getAttribute(Attribute attribute) {
if (hero == null) {
if (!attribute.hasAlternative()) {
dB.echoError("Specified Hero is null! Are they online/spawned?");
}
}
// <--[tag]
// @attribute <hero@hero.delaying_skill>
// @returns Element(Boolean)
// @description
// Returns whether the hero is currently using a delayed skill.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("delaying_skill")) {
return new Element(hero.getDelayedSkill() != null).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.delayed_skill>
// @returns Element
// @description
// Returns the delayed skill the hero is currently using.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("delayed_skill")) {
return new Element(hero.getDelayedSkill().getSkill().getName()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.has_party>
// @returns Element(Boolean)
// @description
// Returns whether the hero is in a party.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("has_party")) {
return new Element(hero.hasParty()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.in_combat[<entity>]>
// @returns Element(Boolean)
// @description
// Returns whether the hero is currently in combat, optionally with a specific entity.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("in_combat")) {
if (attribute.hasContext(1)) {
dEntity entity = dEntity.valueOf(attribute.getContext(1));
if (entity != null && entity.isLivingEntity()) {
return new Element(hero.isInCombatWith(entity.getLivingEntity()))
.getAttribute(attribute.fulfill(1));
}
}
return new Element(hero.isInCombat()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.level[<class>]>
// @returns Element(Number)
// @description
// Returns the level of the hero for the specified class.
// If no class is specified, returns the hero's current highest level.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("level")) {
if (attribute.hasContext(1)) {
try {
return new Element(hero.getLevel(HeroesClass.valueOf(attribute.getContext(1)).getHeroClass()))
.getAttribute(attribute.fulfill(1));
}
catch (Exception e) {
if (!attribute.hasAlternative()) {
dB.echoError("'" + attribute.getContext(1) + "' is not a valid Heroes class!");
}
}
}
return new Element(hero.getLevel()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.list_combatants>
// @returns dList(dEntity)
// @description
// Returns the list of entities the hero is currently in combat with.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("list_combatants")) {
dList list = new dList();
for (Entity entity : hero.getCombatants().keySet()) {
list.add(new dEntity(entity).identify());
}
return list.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.mana>
// @returns Element(Number)
// @description
// Returns the hero's current amount of mana.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("mana")) {
return new Element(hero.getMana()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.mana_regen>
// @returns Element(Number)
// @description
// Returns the hero's current amount of mana regeneration.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("mana_regen")) {
return new Element(hero.getManaRegen()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.max_mana>
// @returns Element(Number)
// @description
// Returns the hero's maximum amount of mana.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("max_mana")) {
return new Element(hero.getMaxMana()).getAttribute(attribute.fulfill(1));
}
else if (attribute.startsWith("party") && hero.hasParty()) {
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <hero@hero.party.leader>
// @returns dPlayer
// @description
// Returns the leader of the hero's party.
// @Plugin DepenizenBukkit, Heroes
// -->
if (attribute.startsWith("leader")) {
return dPlayer.mirrorBukkitPlayer(hero.getParty().getLeader().getPlayer())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.party.members>
// @returns dList(dPlayer)
// @description
// Returns a list of players currently in the hero's party.
// @Plugin DepenizenBukkit, Heroes
// -->
if (attribute.startsWith("members")) {
dList members = new dList();
for (Hero member : hero.getParty().getMembers()) {
members.add(dPlayer.mirrorBukkitPlayer(member.getPlayer()).identify());
}
return members.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <hero@hero.primary_class>
// @returns HeroesClass
// @description
// Returns the primary class for the hero.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("primary_class")) {
return new HeroesClass(hero.getHeroClass()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.secondary_class>
// @returns HeroesClass
// @description
// Returns the secondary class for the hero.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("secondary_class")) {
return new HeroesClass(hero.getSecondClass()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.skills>
// @returns dList
// @description
// Returns a list of skills the hero currently has access to.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("skills")) {
Set<String> skills = new HashSet<String>();
skills.addAll(hero.getSkills().keySet());
skills.addAll(hero.getHeroClass().getSkillNames());
skills.addAll(hero.getSecondClass().getSkillNames());
return new dList(skills).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.type>
// @returns Element
// @description
// Always returns 'Hero' for HeroesHero objects. All objects fetchable by the Object Fetcher will return the
// type of object that is fulfilling this attribute.
// @Plugin DepenizenBukkit, Heroes
// -->
else if (attribute.startsWith("type")) {
return new Element("Hero").getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute);
}
@Override
public void applyProperty(Mechanism mechanism) {
dB.echoError("Cannot apply properties to a hero!");
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object HeroesHero
// @name mana
// @input Element(Integer)
// @description
// Sets the hero's current mana level.
// @tags
// <hero@hero.mana>
// -->
if (mechanism.matches("mana") && mechanism.requireInteger()) {
hero.setMana(mechanism.getValue().asInt());
}
// <--[mechanism]
// @object HeroesHero
// @name primary_class
// @input HeroesClass
// @description
// Sets the hero's primary class.
// @tags
// <hero@hero.primary_class>
// -->
else if (mechanism.matches("primary_class") && mechanism.requireObject(HeroesClass.class)) {
hero.setHeroClass(mechanism.valueAsType(HeroesClass.class).getHeroClass(), false);
}
// <--[mechanism]
// @object HeroesHero
// @name secondary_class
// @input HeroesClass
// @description
// Sets the hero's secondary class.
// @tags
// <hero@hero.secondary_class>
// -->
else if (mechanism.matches("secondary_class") && mechanism.requireObject(HeroesClass.class)) {
hero.setHeroClass(mechanism.valueAsType(HeroesClass.class).getHeroClass(), true);
}
}
}