RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
NpcForceChatParser.java
1package com.osroyale.util.parser.impl;
2
3import com.google.gson.JsonObject;
4import com.osroyale.game.world.entity.mob.npc.Npc;
5import com.osroyale.game.world.position.Position;
6import com.osroyale.util.Utility;
7import com.osroyale.util.parser.GsonParser;
8import org.jire.tarnishps.OldToNew;
9
10import java.util.HashMap;
11import java.util.Map;
12
13
56
57public class NpcForceChatParser extends GsonParser {
58
62 public static final Map<Position, ForcedMessage> FORCED_MESSAGES = new HashMap<>();
63
68 super("def/npc/npc_force_chat", false);
69 }
70
71 @Override
72 protected void parse(JsonObject data) {
73 int id = data.get("id").getAsInt();
74 int newId = OldToNew.get(id);
75 if (newId != -1) {
76 id = newId;
77 }
78
79 final Position position = builder.fromJson(data.get("position"), Position.class);
80 final int interval = data.get("interval").getAsInt();
81
82 final MessageType type = MessageType.valueOf(data.get("type").getAsString());
83
84 String[] messages = new String[]{};
85
86 if (data.has("messages")) {
87 messages = builder.fromJson(data.get("messages"), String[].class);
88 }
89
90 FORCED_MESSAGES.put(position, new ForcedMessage(id, interval, messages, type));
91 }
92
96 public static class ForcedMessage {
100 private final int id;
101
105 private final int interval;
106
110 private final String[] messages;
111
115 private final MessageType type;
116
120 private int next = 0;
121
130 public ForcedMessage(int id, int interval, String[] messages, MessageType type) {
131 this.id = id;
132 this.interval = interval;
133 this.messages = messages;
134 this.type = type;
135 }
136
137 public int getId() { return id; }
138
139 public int getInterval() {
140 return interval;
141 }
142
143 public String[] getMessages() {
144 return messages;
145 }
146
147 public MessageType getType() {
148 return type;
149 }
150
151 public String nextMessage() {
152 switch (type) {
153 case NORMAL:
154 if (next >= messages.length) {
155 next = 0;
156 }
157 return messages[next++];
158 case RANDOM:
159 return messages[Utility.random(messages.length)];
160 default:
161 throw new IllegalArgumentException("Unhandled type: " + type + ".");
162 }
163 }
164 }
165
169 private enum MessageType {
170 RANDOM,
171 NORMAL
172 }
173}
ForcedMessage(int id, int interval, String[] messages, MessageType type)
static final Map< Position, ForcedMessage > FORCED_MESSAGES