/
HeroesHero.java
241 lines (200 loc) · 7.06 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
package net.gnomeffinway.depenizen.objects.heroes;
import com.herocraftonline.heroes.Heroes;
import com.herocraftonline.heroes.characters.Hero;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.gnomeffinway.depenizen.support.Supported;
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));
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 = Supported.get("HEROES").getPlugin();
hero = heroes.getCharacterManager().getHero(player.getPlayerEntity());
}
}
public HeroesHero(dNPC npc) {
denizenObj = npc;
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
Heroes heroes = Supported.get("HEROES").getPlugin();
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;
}
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.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
// -->
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.party.leader>
// @returns dPlayer
// @description
// Returns the leader of the hero's party.
// @plugin Heroes
// -->
if (attribute.startsWith("party.leader")) {
return dPlayer.mirrorBukkitPlayer(hero.getParty().getLeader().getPlayer())
.getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <hero@hero.party.members>
// @returns dList(dPlayer)
// @description
// Returns a list of players currently in the hero's party.
// @plugin Heroes
// -->
if (attribute.startsWith("party.members")) {
dList members = new dList();
for (Hero member : hero.getParty().getMembers()) {
members.add(dPlayer.mirrorBukkitPlayer(member.getPlayer()).identify());
}
return members.getAttribute(attribute.fulfill(2));
}
// <--[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));
}
// <--[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 Heroes
// -->
if (attribute.startsWith("type")) {
return new Element("Hero").getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute);
}
}