RuneHive-Game
Loading...
Searching...
No Matches
NpcForceChatParser.java
Go to the documentation of this file.
1package com.runehive.util.parser.impl;
2
3import com.google.gson.JsonObject;
4import com.runehive.game.world.entity.mob.npc.Npc;
5import com.runehive.game.world.position.Position;
6import com.runehive.util.Utility;
7import com.runehive.util.parser.GsonParser;
8import org.jire.runehiveps.OldToNew;
9
10import java.util.HashMap;
11import java.util.Map;
12
13
14/**
15 * Parses through the npc spawn file and creates {@link Npc}s on startup.
16 *
17 * @author Daniel | Obey
18 */
19public class NpcForceChatParser extends GsonParser {
20
21 /**
22 * The map containing all the forced messages.
23 */
24 public static final Map<Position, ForcedMessage> FORCED_MESSAGES = new HashMap<>();
25
26 /**
27 * Constructs a new <code>NpcForceChatParser</code>.
28 */
30 super("def/npc/npc_force_chat", false);
31 }
32
33 @Override
34 protected void parse(JsonObject data) {
35 int id = data.get("id").getAsInt();
36 int newId = OldToNew.get(id);
37 if (newId != -1) {
38 id = newId;
39 }
40
41 final Position position = builder.fromJson(data.get("position"), Position.class);
42 final int interval = data.get("interval").getAsInt();
43
44 final MessageType type = MessageType.valueOf(data.get("type").getAsString());
45
46 String[] messages = new String[]{};
47
48 if (data.has("messages")) {
49 messages = builder.fromJson(data.get("messages"), String[].class);
50 }
51
52 FORCED_MESSAGES.put(position, new ForcedMessage(id, interval, messages, type));
53 }
54
55 /**
56 * The forced message class.
57 */
58 public static class ForcedMessage {
59 /**
60 * The npc id.
61 */
62 private final int id;
63
64 /**
65 * The interval at which the message will be performed.
66 */
67 private final int interval;
68
69 /**
70 * The array of messages the npc will perform.
71 */
72 private final String[] messages;
73
74 /**
75 * The message type.
76 */
77 private final MessageType type;
78
79 /**
80 * The next message.
81 */
82 private int next = 0;
83
84 /**
85 * Constructs a new <code>ForcedMessage</code>.
86 *
87 * @param id The npc id.
88 * @param interval The interval at which the npc will perform the message.
89 * @param messages The messages the npc will be forced to perform.
90 * @param type The type of message.
91 */
92 public ForcedMessage(int id, int interval, String[] messages, MessageType type) {
93 this.id = id;
94 this.interval = interval;
95 this.messages = messages;
96 this.type = type;
97 }
98
99 public int getId() { return id; }
100
101 public int getInterval() {
102 return interval;
103 }
104
105 public String[] getMessages() {
106 return messages;
107 }
108
110 return type;
111 }
112
113 public String nextMessage() {
114 switch (type) {
115 case NORMAL:
116 if (next >= messages.length) {
117 next = 0;
118 }
119 return messages[next++];
120 case RANDOM:
121 return messages[Utility.random(messages.length)];
122 default:
123 throw new IllegalArgumentException("Unhandled type: " + type + ".");
124 }
125 }
126 }
127
128 /**
129 * The enum of message types.
130 */
131 private enum MessageType {
134 }
135}
Represents a single tile on the game world.
Definition Position.java:14
Handles miscellaneous methods.
Definition Utility.java:27
static int random(int bound)
Definition Utility.java:239
GsonParser(String path)
Creates a new GsonParser.
transient Gson builder
The Gson object.
ForcedMessage(int id, int interval, String[] messages, MessageType type)
Constructs a new ForcedMessage.
final String[] messages
The array of messages the npc will perform.
final int interval
The interval at which the message will be performed.
void parse(JsonObject data)
The method allows a user to modify the data as its being parsed.
static final Map< Position, ForcedMessage > FORCED_MESSAGES
The map containing all the forced messages.
NpcForceChatParser()
Constructs a new NpcForceChatParser.