RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ItemDefParser.java
1package com.osroyale.util.tools.wiki.impl;
2
3import com.google.gson.JsonArray;
4import com.google.gson.JsonObject;
5import com.osroyale.game.world.items.containers.equipment.Equipment;
6import com.osroyale.game.world.items.containers.equipment.EquipmentType;
7import com.osroyale.util.Utility;
8import com.osroyale.util.parser.GsonParser;
9import com.osroyale.util.tools.wiki.parser.WikiTable;
10import com.osroyale.util.tools.wiki.parser.WikiTableParser;
11import org.apache.logging.log4j.LogManager;
12import org.apache.logging.log4j.Logger;
13import org.jsoup.nodes.Document;
14import org.jsoup.nodes.Element;
15import org.jsoup.select.Elements;
16
17import java.util.*;
18
19import static com.osroyale.game.world.entity.combat.CombatConstants.BONUS_CONFIG_FIELD_NAMES;
20
52
53public class ItemDefParser extends WikiTableParser {
54
55 private static final Logger logger = LogManager.getLogger(ItemDefParser.class);
56
57 private ItemDefParser() {
58 super(generateTables());
59 }
60
61 public static void main(String[] args) throws InterruptedException {
62 ItemDefParser parser = new ItemDefParser();
63 parser.begin();
64 }
65
66 private static LinkedList<WikiTable> generateTables() {
67 List<Definition> definitions = parseDefinitions();
68 LinkedList<WikiTable> tables = new LinkedList<>();
69 String link = "https://oldschoolrunescape.fandom.com/wiki/Category:Items";
70 Iterator<Definition> iterator = definitions.iterator();
71
72 while (iterator.hasNext()) {
73 Definition definition = iterator.next();
74 iterator.remove();
75 tables.add(new WikiTable(link + format(definition.name)) {
76 @Override
77 protected void parseDocument(Document document) {
78 Elements infobox = document.select(".wikitable.infobox");
79 Elements equipment = document.select(".wikitable.smallpadding");
80 int idx = 0;
81
82 EquipmentType type = EquipmentType.NOT_WIELDABLE;
83 boolean twoHanded = false;
84 int[] bonuses = null;
85
86 for (Element info : infobox) {
87 JsonObject object = new JsonObject();
88 Elements attributes = infobox.select("a");
89
90 String destroyMessage = "Drop";
91 double weight = 0;
92 boolean tradeable = false;
93
94 for (Element attribute : attributes) {
95 String attr = attribute.attr("title");
96 try {
97 switch (attr) {
98 case "Tradeable": {
99 tradeable = attribute.parent().nextElementSibling().text().equalsIgnoreCase("Yes");
100 break;
101 }
102 case "Destroy": {
103 destroyMessage = attribute.parent().nextElementSibling().text();
104 break;
105 }
106 case "Weight": {
107 Element element = attribute.parent().nextElementSibling();
108 String alchemy = element.text().replaceAll("[^-\\d.]", "");
109 if (!alchemy.matches("-?[0-9]*\\.?[0-9]*")) {
110 alchemy = element.children().last().nextSibling().toString().replaceAll("[^-\\d.]", "");
111 }
112 if (!alchemy.isEmpty())
113 weight = Double.parseDouble(alchemy);
114 break;
115 }
116 }
117 } catch (Exception ignored) {
118 logger.warn("Exception while parsing item [id=" + definition.id + ", name=" + definition.name + "]", ignored);
119 }
120 }
121
122 try {
123 if (!equipment.isEmpty()) {
124 bonuses = new int[Equipment.SIZE];
125
126 Elements equipmentInfo = equipment.select("td");
127 int ind = 0;
128 if (equipmentInfo.size() > idx) {
129 do {
130 Element attribute = equipmentInfo.get(idx++);
131 if (attribute.attr("style").equals("text-align: center; width: 35px;")
132 || attribute.attr("style").equals("text-align: center; width: 30px;")) {
133 bonuses[ind++ % Equipment.SIZE] = Integer.parseInt(attribute.text().replace("%", ""));
134 }
135 } while (ind < Equipment.SIZE);
136 }
137
138 for (Element attribute : equipment.select("img")) {
139 switch (attribute.attr("data-image-key")) {
140 case "Head_slot.png":
141 type = EquipmentType.HELM; // TODO
142 break;
143 case "Cape_slot.png":
144 type = EquipmentType.CAPE;
145 break;
146 case "Weapon_slot.png":
147 type = EquipmentType.WEAPON;
148 break;
149 case "Neck_slot.png":
150 type = EquipmentType.AMULET;
151 break;
152 case "Ammo_slot.png":
153 type = EquipmentType.ARROWS;
154 break;
155 case "Shield_slot.png":
156 type = EquipmentType.SHIELD;
157 break;
158 case "Torso_slot.png":
159 type = EquipmentType.BODY; // TODO
160 break;
161 case "Legs_slot.png":
162 type = EquipmentType.LEGS;
163 break;
164 case "Gloves_slot.png":
165 type = EquipmentType.GLOVES;
166 break;
167 case "Boots_slot.png":
168 type = EquipmentType.BOOTS;
169 break;
170 case "Ring_slot.png":
171 type = EquipmentType.RING;
172 break;
173 case "2h_slot.png":
174 type = EquipmentType.WEAPON;
175 twoHanded = true;
176 break;
177 }
178 }
179 }
180 } catch (Exception ignored) {
181 bonuses = null;
182 type = EquipmentType.NOT_WIELDABLE;
183 logger.warn("Exception while parsing item equipment data [id=" + definition.id + ", name=" + definition.name + "]", ignored);
184 }
185
186 object.addProperty("name", info.select("caption").text());
187
188 if (!destroyMessage.equals("Drop"))
189 object.addProperty("destroy-message", destroyMessage);
190
191 if (tradeable)
192 object.addProperty("tradable", true);
193
194 if (twoHanded)
195 object.addProperty("two-handed", true);
196
197 if (weight != 0)
198 object.addProperty("weight", weight);
199
200 if (type != EquipmentType.NOT_WIELDABLE)
201 object.addProperty("equipment-type", type.name());
202
203 if (bonuses != null) {
204 for (int index = 0; index < BONUS_CONFIG_FIELD_NAMES.length; index++) {
205 String bonusName = BONUS_CONFIG_FIELD_NAMES[index];
206 if (bonuses[index] != 0) {
207 object.addProperty(bonusName, bonuses[index]);
208 }
209 }
210 }
211
212 if (!table.contains(object)) {
213 table.add(object);
214 }
215 }
216 }
217 });
218 }
219 return tables;
220 }
221
222 @Override
223 protected void finish() {
224 JsonArray array = new JsonArray();
225 for (WikiTable table : tables) {
226 array.addAll(table.getTable());
227 }
228 writeToJson("wiki_item_dump", array);
229 }
230
231 private static String format(String name) {
232 name = Utility.capitalizeSentence(name);
233 name = name.replace(" ", "_");
234 return name;
235 }
236
237 private static List<Definition> parseDefinitions() {
238 Set<String> added = new HashSet<>();
239 List<Definition> definitions = new LinkedList<>();
240 new GsonParser("wiki/wiki_item_dump") {
241 @Override
242 protected void parse(JsonObject data) {
243 Definition definition = new Definition();
244
245 definition.id = data.get("id").getAsInt();
246 definition.name = data.get("name").getAsString();
247
248 if (added.contains(definition.name))
249 return;
250
251 added.add(definition.name);
252 definitions.add(definition);
253 }
254 }.run();
255 added.clear();
256 return definitions;
257 }
258
259 public static final class Definition {
260 public int id;
261 public String name;
262 }
263
264}
static String capitalizeSentence(final String string)
Definition Utility.java:136