/
HeroesHero.java
198 lines (159 loc) · 5.39 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
package net.gnomeffinway.depenizen.objects.heroes;
import com.herocraftonline.heroes.characters.Hero;
import com.herocraftonline.heroes.characters.classes.HeroClass;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.gnomeffinway.depenizen.Depenizen;
import org.bukkit.entity.Player;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HeroesHero implements dObject {
/////////////////////
// PATTERNS
/////////////////
final static Pattern npc_pattern = Pattern.compile("(hero@)?npc-(\\d+)", Pattern.CASE_INSENSITIVE);
/////////////////////
// OBJECT FETCHER
/////////////////
@Fetchable("hero")
public static HeroesHero valueOf(String string) {
if (string == null) return null;
string = string.replace("hero@", "");
if (dPlayer.matches(string))
return new HeroesHero(dPlayer.valueOf(string));
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()) {
hero = Depenizen.heroes.getCharacterManager().getHero(player.getPlayerEntity());
}
}
public HeroesHero(dNPC npc) {
denizenObj = npc;
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
hero = Depenizen.heroes.getCharacterManager().getHero((Player) npc.getEntity());
}
}
public HeroesHero(Hero hero) {
this.hero = hero;
if (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;
}
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) {
dB.echoError("Specified Hero is null! Are they online/spawned?");
return null;
}
// <--[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 Heroes
// -->
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) {
dB.echoError("'" + attribute.getContext(1) + "' is not a valid Heroes class!");
return Element.NULL.getAttribute(attribute.fulfill(1));
}
}
return new Element(hero.getLevel()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <hero@hero.primary_class>
// @returns HeroesClass
// @description
// Returns the primary class for the hero.
// @plugin Heroes
// -->
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 Heroes
// -->
if (attribute.startsWith("secondary_class")) {
return new HeroesClass(hero.getHeroClass()).getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute);
}
}