55 private static final Logger logger = LogManager.getLogger(ItemDefParser.class);
57 private ItemDefParser() {
58 super(generateTables());
61 public static void main(String[] args)
throws InterruptedException {
62 ItemDefParser parser =
new ItemDefParser();
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();
72 while (iterator.hasNext()) {
73 Definition definition = iterator.next();
77 protected void parseDocument(Document document) {
78 Elements infobox = document.select(
".wikitable.infobox");
79 Elements equipment = document.select(
".wikitable.smallpadding");
82 EquipmentType type = EquipmentType.NOT_WIELDABLE;
83 boolean twoHanded = false;
86 for (Element info : infobox) {
87 JsonObject object = new JsonObject();
88 Elements attributes = infobox.select(
"a");
90 String destroyMessage =
"Drop";
92 boolean tradeable = false;
94 for (Element attribute : attributes) {
95 String attr = attribute.attr(
"title");
99 tradeable = attribute.parent().nextElementSibling().text().equalsIgnoreCase(
"Yes");
103 destroyMessage = attribute.parent().nextElementSibling().text();
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.]",
"");
112 if (!alchemy.isEmpty())
113 weight = Double.parseDouble(alchemy);
117 } catch (Exception ignored) {
118 logger.warn(
"Exception while parsing item [id=" + definition.id +
", name=" + definition.name +
"]", ignored);
123 if (!equipment.isEmpty()) {
124 bonuses = new int[Equipment.SIZE];
126 Elements equipmentInfo = equipment.select(
"td");
128 if (equipmentInfo.size() > idx) {
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(
"%",
""));
135 } while (ind < Equipment.SIZE);
138 for (Element attribute : equipment.select(
"img")) {
139 switch (attribute.attr(
"data-image-key")) {
140 case
"Head_slot.png":
141 type = EquipmentType.HELM;
143 case
"Cape_slot.png":
144 type = EquipmentType.CAPE;
146 case
"Weapon_slot.png":
147 type = EquipmentType.WEAPON;
149 case
"Neck_slot.png":
150 type = EquipmentType.AMULET;
152 case
"Ammo_slot.png":
153 type = EquipmentType.ARROWS;
155 case
"Shield_slot.png":
156 type = EquipmentType.SHIELD;
158 case
"Torso_slot.png":
159 type = EquipmentType.BODY;
161 case
"Legs_slot.png":
162 type = EquipmentType.LEGS;
164 case
"Gloves_slot.png":
165 type = EquipmentType.GLOVES;
167 case
"Boots_slot.png":
168 type = EquipmentType.BOOTS;
170 case
"Ring_slot.png":
171 type = EquipmentType.RING;
174 type = EquipmentType.WEAPON;
180 } catch (Exception ignored) {
182 type = EquipmentType.NOT_WIELDABLE;
183 logger.warn(
"Exception while parsing item equipment data [id=" + definition.id +
", name=" + definition.name +
"]", ignored);
186 object.addProperty(
"name", info.select(
"caption").text());
188 if (!destroyMessage.equals(
"Drop"))
189 object.addProperty(
"destroy-message", destroyMessage);
192 object.addProperty(
"tradable", true);
195 object.addProperty(
"two-handed", true);
198 object.addProperty(
"weight", weight);
200 if (type != EquipmentType.NOT_WIELDABLE)
201 object.addProperty(
"equipment-type", type.name());
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]);
212 if (!table.contains(object)) {
223 protected void finish() {
224 JsonArray array =
new JsonArray();
226 array.addAll(table.getTable());
228 writeToJson(
"wiki_item_dump", array);
231 private static String format(String name) {
233 name = name.replace(
" ",
"_");
237 private static List<Definition> parseDefinitions() {
238 Set<String> added =
new HashSet<>();
239 List<Definition> definitions =
new LinkedList<>();
242 protected void parse(JsonObject data) {
243 Definition definition =
new Definition();
245 definition.id = data.get(
"id").getAsInt();
246 definition.name = data.get(
"name").getAsString();
248 if (added.contains(definition.name))
251 added.add(definition.name);
252 definitions.add(definition);
259 public static final class Definition {