RuneHive-Game
Loading...
Searching...
No Matches
NpcDefinition.java
Go to the documentation of this file.
1package com.runehive.game.world.entity.mob.npc.definition;
2
3import com.google.gson.JsonObject;
4import com.runehive.Config;
5import com.runehive.fs.cache.decoder.CacheNpcDefinition;
6import com.runehive.game.world.entity.combat.CombatType;
7import com.runehive.game.world.entity.combat.projectile.CombatProjectile;
8import com.runehive.game.world.entity.mob.player.Player;
9import com.runehive.util.parser.GsonParser;
10import org.jire.runehiveps.OldToNew;
11import org.jire.runehiveps.defs.MonsterDefLoader;
12
13import java.util.*;
14
15import static com.runehive.game.world.entity.combat.CombatConstants.*;
16
17/**
18 * Contains the npc definitions.
19 *
20 * @author Michael | Chex
21 */
22public class NpcDefinition {
23
24 /** The array of npc definitions. */
26
27 private final int id;
28 private final String name;
29 private int combatLevel;
30 private int size;
31 private int stand;
32 private int walk;
33 private int turn180;
34 private int turn90CW;
35 private int turn90CCW;
36 private int attackAnim;
37 private int blockAnim;
38 private int deathAnim;
39 private int deathTimer;
40 private boolean attackable;
41 private boolean aggressive;
42 private boolean retaliate;
43 private boolean poisonous;
44 private boolean poisonImmunity;
45 private boolean venomImmunity;
46 private boolean flying;
47 private int[] skills;
48 private int[] bonuses;
49 private int attackDelay;
50 private int attackRadius;
51 private int respawnTime;
53 private Player player;
54
55 public NpcDefinition(int id, String name) {
56 this.id = id;
57 this.name = name;
58 this.combatLevel = 0;
59 this.size = 1;
60 this.stand = -1;
61 this.walk = -1;
62 this.turn180 = -1;
63 this.turn90CW = -1;
64 this.turn90CCW = -1;
65 this.attackAnim = -1;
66 this.blockAnim = -1;
67 this.deathAnim = -1;
68 this.deathTimer = 3;
69 this.flying = false;
70 this.attackable = false;
71 this.aggressive = false;
72 this.retaliate = false;
73 this.poisonous = false;
74 this.poisonImmunity = false;
75 this.venomImmunity = false;
76 this.skills = EMPTY_SKILLS;
77 this.bonuses = EMPTY_BONUSES;
78 this.attackDelay = 4;
79 this.attackRadius = 1;
80 this.respawnTime = 30;
81 }
82
83 public static void main(String[] args) {
84 GsonParser parser = createParser();
85 parser.deserialize();
86 }
87
88 public static GsonParser createParser() {
89 return new GsonParser("def/npc/npc_definitions", false) {
90
91 @Override
92 protected void parse(JsonObject data) {
93 int id = data.get("id").getAsInt();
94 boolean convertId = true;
95 if (data.has("convert-id")) {
96 convertId = data.get("convert-id").getAsBoolean();
97 }
98 if (convertId) {
99 int newId = OldToNew.get(id);
100 if (newId != -1) {
101 id = newId;
102 }
103 }
104
105 final CacheNpcDefinition cacheDef = CacheNpcDefinition.lookup(id);
106 final String cacheDefName = cacheDef.name;
107 final NpcDefinition definition = new NpcDefinition(id, cacheDefName == null ? "" : cacheDefName);
108 definition.size = cacheDef.size;
109 definition.combatLevel = cacheDef.combatLevel;
110 definition.stand = cacheDef.standingAnimation;
111 definition.walk = cacheDef.walkingAnimation;
112 definition.turn180 = cacheDef.halfTurnAnimation;
113 definition.turn90CW = cacheDef.quarterClockwiseTurnAnimation;
114 definition.turn90CCW = cacheDef.quarterAnticlockwiseTurnAnimation;
115
116 definition.deathTimer = 3;
117 if (data.has("death-timer")) {
118 definition.deathTimer = data.get("death-timer").getAsInt();
119 }
120
121 if (data.has("attack-animation")) {
122 definition.attackAnim = data.get("attack-animation").getAsInt();
123 }
124
125 if (data.has("block-animation")) {
126 definition.blockAnim = data.get("block-animation").getAsInt();
127 }
128
129 if (data.has("death-animation")) {
130 definition.deathAnim = data.get("death-animation").getAsInt();
131 }
132
133 if (data.has("attackable")) {
134 definition.attackable = data.get("attackable").getAsBoolean();
135 }
136
137 if (data.has("aggressive")) {
138 definition.aggressive = data.get("aggressive").getAsBoolean();
139 }
140
141 definition.retaliate = definition.isAttackable();
142 if (data.has("retaliate")) {
143 definition.retaliate = data.get("retaliate").getAsBoolean();
144 }
145
146 if (data.has("poisonous")) {
147 definition.poisonous = data.get("poisonous").getAsBoolean();
148 }
149
150 if (data.has("flying")) {
151 definition.flying = data.get("flying").getAsBoolean();
152 }
153
154 if (data.has("poison-immunity")) {
155 definition.poisonImmunity = data.get("poison-immunity").getAsBoolean();
156 }
157
158 if (data.has("venom-immunity")) {
159 definition.venomImmunity = data.get("venom-immunity").getAsBoolean();
160 }
161
162 if (data.has("attack-cooldown")) {
163 definition.attackDelay = data.get("attack-cooldown").getAsInt();
164 }
165
166 if (data.has("attack-radius")) {
167 definition.attackRadius = data.get("attack-radius").getAsInt();
168 }
169
170 if (data.has("combat-type") && data.has("projectile-key")) {
171 CombatType type = CombatType.valueOf(data.get("combat-type").getAsString());
172 String key = data.get("projectile-key").getAsString();
173 definition.combatAttackData = new CombatAttackData(type, key);
174 }
175
176 if (data.has("respawn")) {
177 definition.respawnTime = data.get("respawn").getAsInt();
178 }
179
180 for (int index = 0; index < SKILL_FIELD_NAMES.length; index++) {
181 String skillName = SKILL_FIELD_NAMES[index];
182 if (data.has(skillName)) {
183 if (definition.skills == EMPTY_SKILLS) {
184 definition.skills = new int[EMPTY_SKILLS.length];
185 }
186 definition.skills[index] = data.get(skillName).getAsInt();
187 }
188 }
189
190 for (int index = 0; index < BONUS_CONFIG_FIELD_NAMES.length; index++) {
191 String bonusName = BONUS_CONFIG_FIELD_NAMES[index];
192 if (data.has(bonusName)) {
193 if (definition.bonuses == EMPTY_BONUSES) {
194 definition.bonuses = new int[EMPTY_BONUSES.length];
195 }
196 definition.bonuses[index] = data.get(bonusName).getAsInt();
197 }
198 }
199
201 }
202
203 @Override
204 protected void onEnd() {
205 MonsterDefLoader.load();
206 }
207
208 };
209 }
210
211 /** Gets a npc definition from the definition array. */
212 public static NpcDefinition get(int id) {
215
216 if (id < 0)
217 id = 0;
218
219 if (DEFINITIONS[id] == null)
220 return new NpcDefinition(id, "null");
221
222 return DEFINITIONS[id];
223 }
224
225 public static NpcDefinition get(String name) {
227 if (definition != null && definition.getName().equals(name)) {
228 return definition;
229 }
230 }
231 return null;
232 }
233
234 public static String getCombatLevelRange(String name) {
235 List<Integer> levels = new ArrayList<>();
236
238 if (definition != null && definition.getName().contains(name)) {
239 levels.add(definition.getCombatLevel());
240 }
241 }
242
243 if (levels.isEmpty()) {
244 return "Unknown";
245 }
246
247 Collections.sort(levels);
248
249 return levels.get(0) + "-" + levels.get(levels.size() - 1);
250 }
251
252 /** @return the id. */
253 public int getId() {
254 return id;
255 }
256
257 /** @return the name. */
258 public String getName() {
259 return name;
260 }
261
262 /** @return the sze. */
263 public int getSize() {
264 return size;
265 }
266
267 /** @return the combat level. */
268 public int getCombatLevel() {
269 return combatLevel;
270 }
271
272 /** @return the stand anim. */
273 public int getStand() {
274 return stand;
275 }
276
277 /** @return the walk anim. */
278 public int getWalk() {
279 return walk;
280 }
281
282 /** @return the turn 180 anim. */
283 public int getTurn180() {
284 return turn180;
285 }
286
287 /** @return the turn 90 CW anim. */
288 public int getTurn90CW() {
289 return turn90CW;
290 }
291
292 /** @return the turn 90 CCW anim. */
293 public int getTurn90CCW() {
294 return turn90CCW;
295 }
296
297 /** @return the attack anim. */
298 public int getAttackAnimation() {
299 return attackAnim;
300 }
301
302 /** @return the block anim. */
303 public int getBlockAnimation() {
304 return blockAnim;
305 }
306
307 /** @return the death anim. */
308 public int getDeathAnimation() {
309 return deathAnim;
310 }
311
312 public int getDeathTimer() {
313 return deathTimer;
314 }
315
316 /** @return the attackable flag. */
317 public boolean isAttackable() {
318 return attackable;
319 }
320
321 /** @return the aggressive flag. */
322 public boolean isAggressive() {
323 return aggressive;
324 }
325
326 /** @return the retaliate flag. */
327 public boolean isRetaliate() {
328 return retaliate;
329 }
330
331 /** @return the poisonous flag. */
332 public boolean isPoisonous() {
333 return poisonous;
334 }
335
336 /** @return the poison immunity flag. */
337 public boolean hasPoisonImmunity() {
338 return poisonImmunity;
339 }
340
341 /** @return the venom immunity flag. */
342 public boolean hasVenomImmunity() {
343 return venomImmunity;
344 }
345
346 public boolean isFlying() {
347 return flying;
348 }
349
350 /** @return the attack delay. */
351 public int getAttackDelay() {
352 return attackDelay;
353 }
354
355 /** @return the attack radius. */
356 public int getAttackRadius() {
357 return attackRadius;
358 }
359
360 /** @return the respawn time in ticks. */
361 public int getRespawnTime() {
362 return respawnTime;
363 }
364
365 /** @return the skill level array. */
366 public int[] getSkills() {
367 return skills;
368 }
369
370 /** @return the equipment bonus array. */
371 public int[] getBonuses() {
372 return bonuses;
373 }
374
375 public void setAggressive(boolean aggressive) {
376 this.aggressive = aggressive;
377 }
378
379 public void setRetaliate(boolean retaliate) {
380 this.retaliate = retaliate;
381 }
382
383 public void setRespawnTime(int respawnTime) {
384 this.respawnTime = respawnTime;
385 }
386
387 public void setCombatLevel(int combatLevel) {
388 this.combatLevel = combatLevel;
389 }
390
391 public void setSize(int size) {
392 this.size = size;
393 }
394
395 public void setStand(int stand) {
396 this.stand = stand;
397 }
398
399 public void setWalk(int walk) {
400 this.walk = walk;
401 }
402
403 public void setTurn180(int turn180) {
404 this.turn180 = turn180;
405 }
406
407 public void setTurn90CW(int turn90CW) {
408 this.turn90CW = turn90CW;
409 }
410
411 public void setTurn90CCW(int turn90CCW) {
412 this.turn90CCW = turn90CCW;
413 }
414
415 public int getAttackAnim() {
416 return attackAnim;
417 }
418
419 public void setAttackAnim(int attackAnim) {
420 this.attackAnim = attackAnim;
421 }
422
423 public int getBlockAnim() {
424 return blockAnim;
425 }
426
427 public void setBlockAnim(int blockAnim) {
428 this.blockAnim = blockAnim;
429 }
430
431 public int getDeathAnim() {
432 return deathAnim;
433 }
434
435 public void setDeathAnim(int deathAnim) {
436 this.deathAnim = deathAnim;
437 }
438
439 public void setDeathTimer(int deathTimer) {
440 this.deathTimer = deathTimer;
441 }
442
443 public void setAttackable(boolean attackable) {
444 this.attackable = attackable;
445 }
446
447 public void setPoisonous(boolean poisonous) {
448 this.poisonous = poisonous;
449 }
450
451 public boolean isPoisonImmunity() {
452 return poisonImmunity;
453 }
454
455 public void setPoisonImmunity(boolean poisonImmunity) {
456 this.poisonImmunity = poisonImmunity;
457 }
458
459 public boolean isVenomImmunity() {
460 return venomImmunity;
461 }
462
463 public void setVenomImmunity(boolean venomImmunity) {
464 this.venomImmunity = venomImmunity;
465 }
466
467 public void setFlying(boolean flying) {
468 this.flying = flying;
469 }
470
471 public void setSkills(int[] skills) {
472 this.skills = skills;
473 }
474
475 public void setBonuses(int[] bonuses) {
476 this.bonuses = bonuses;
477 }
478
479 public void setAttackDelay(int attackDelay) {
480 this.attackDelay = attackDelay;
481 }
482
483 public void setAttackRadius(int attackRadius) {
484 this.attackRadius = attackRadius;
485 }
486
488 this.combatAttackData = combatAttackData;
489 }
490
491 public Player getPlayer() {
492 return player;
493 }
494
495 public void setPlayer(Player player) {
496 this.player = player;
497 }
498
499 public Optional<CombatAttackData> getCombatAttackData() {
500 return Optional.ofNullable(combatAttackData);
501 }
502
503 @Override
504 public int hashCode() {
505 return Objects.hash(id, name, size);
506 }
507
508 @Override
509 public boolean equals(Object obj) {
510 return obj == this || (obj instanceof NpcDefinition && ((NpcDefinition) obj).id == id);
511 }
512
513 public static final class CombatAttackData {
514
515 public final CombatType type;
516
517 public final String key;
518
520 this.type = type;
521 this.key = key;
522 }
523
527 }
528}
The class that contains setting-related constants for the server.
Definition Config.java:24
static final int NPC_DEFINITION_LIMIT
The limit of the npc identification.
Definition Config.java:178
static CacheNpcDefinition lookup(int npcId)
void setCombatAttackData(CombatAttackData combatAttackData)
static final NpcDefinition[] DEFINITIONS
The array of npc definitions.
This class represents a character controlled by a player.
Definition Player.java:125
This class provides an easy to use google gson parser specifically designed for parsing JSON files.
final void deserialize()
The method that deserializes the file information.