RuneHive-Game
Loading...
Searching...
No Matches
ItemDefLoader.kt
Go to the documentation of this file.
1package org.jire.runehiveps.defs
2
3import com.google.gson.Gson
4import com.google.gson.GsonBuilder
5import com.runehive.game.world.entity.combat.ranged.RangedAmmunition
6import com.runehive.game.world.entity.combat.ranged.RangedWeaponDefinition
7import com.runehive.game.world.entity.combat.ranged.RangedWeaponType
8import com.runehive.game.world.entity.combat.weapon.WeaponInterface
9import com.runehive.game.world.items.ItemDefinition
10import com.runehive.game.world.items.containers.equipment.EquipmentType
11import it.unimi.dsi.fastutil.ints.Int2ObjectMap
12import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
13import java.io.File
14
15/**
16 * @author Jire
17 */
18object ItemDefLoader {
19
20 @JvmField
21 val map: Int2ObjectMap<ItemDef> = Int2ObjectOpenHashMap()
22
23 @JvmStatic
24 @JvmOverloads
25 fun load(
26 gson: Gson = GsonBuilder()
27 .setPrettyPrinting()
28 .create()
29 ) {
30 for (file in File("data/def/items-json/").listFiles()!!) {
31 if (file.extension != "json") continue
32 val id = file.nameWithoutExtension.toIntOrNull() ?: continue
33 val itemDef = gson.fromJson(file.bufferedReader(), ItemDef::class.java)
34
35 map.put(id, itemDef)
36
37 val oldDef = ItemDefinition.DEFINITIONS[id]
38 if (oldDef == null) {
39 ItemDefinition.DEFINITIONS[id] = ItemDefinition(id, itemDef.name).apply {
40 updateFrom(itemDef)
41 }
42 } else oldDef.updateFrom(itemDef)
43 }
44 }
45
46 private fun ItemDefinition.updateFrom(itemDef: ItemDef) {
47 val name = itemDef.name
48
49 val noted = itemDef.linkedIdItem != 0 && itemDef.linkedIdNoted == 0
50 isStackable = itemDef.stackable || noted
51 isTradeable = itemDef.tradeable
52 if (noted) unnotedId = itemDef.linkedIdItem
53 else {
54 if (itemDef.linkedIdNoted != 0)
55 notedId = itemDef.linkedIdNoted
56 }
57
58 base_value = itemDef.cost
59 lowAlch = itemDef.lowAlch
60 highAlch = itemDef.highAlch
61
62 weight = itemDef.weight
63
64 val equipment = itemDef.equipment
65 if (equipment != null) {
66 val equipmentSlot = equipment.slot
67 isTwoHanded = equipmentSlot == "2h"
68
69 bonuses = equipment.toBonusArray()
70
71 val req = equipment.requirements
72 if (req != null)
73 requirements = req.toArray()
74
75 if (equipmentType == null || equipmentType == EquipmentType.NOT_WIELDABLE) {
76 if (isTwoHanded) this.equipmentType = EquipmentType.WEAPON
77 else if (name.startsWith("hood", true)
78 || name.endsWith("hood", true)
79 ) {
80 equipmentType = EquipmentType.FACE
81 } else if (name.contains("full helm", true)
82 || name == "Masori mask"
83 || name == "Masori mask (f)"
84 || name == "Gas mask"
85 ) {
86 equipmentType = EquipmentType.HELM
87 } else if (name.contains("bloodbark helm", true)) {
88 equipmentType = EquipmentType.HAT
89 } else if (name.contains("med helm", true)) {
90 equipmentType = EquipmentType.FACE
91 } else if(name.contains("dragon hunter lance", true)) {
92 equipmentType = EquipmentType.WEAPON
93 stand_animation = 813
94 walk_animation = 1205
95 run_animation = 2563
96 weaponInterface = WeaponInterface.HUNTER_LANCE
97 } else if(name.contains("sled", true)) {
98 equipmentType = EquipmentType.WEAPON
99 stand_animation = 1461
100 walk_animation = 1468
101 run_animation = 1467
102 } else if(name.contains("Tumeken's shadow", true)) {
103 equipmentType = EquipmentType.WEAPON
104 stand_animation = 9494
105 walk_animation = 1703
106 run_animation = 1707
107 weaponInterface = WeaponInterface.SHADOW
108 } else if (name.contains("Inquisitor's mace", true)) {
109 weaponInterface = WeaponInterface.INQUISITOR_MACE
110 equipmentType = EquipmentType.WEAPON
111 } else if (name.contains("helm", true)) {
112 equipmentType =
113 if (name.startsWith("dharok", true)) {
114 EquipmentType.FACE
115 } else {
116 EquipmentType.HELM
117 }
118 } else if (name.contains("mask", true)) {
119 equipmentType = EquipmentType.MASK
120 } else {
121 val newEquipmentType = EquipmentType.forNewName(equipmentSlot)
122 if (newEquipmentType != null)
123 equipmentType = newEquipmentType
124 }
125 }
126 }
127
128 val weapon = itemDef.weapon
129 if (weapon != null) {
130 if (weaponInterface == null) {
131 weaponInterface = WeaponInterface.forNewName(weapon.weaponType)
132 }
133
134 var replaceRangedDef = rangedDefinition.isEmpty
135 if (!replaceRangedDef) {
136 val def = rangedDefinition.get()
137 replaceRangedDef = def.allowed == null || def.allowed.firstOrNull() == null
138 }
139 if (replaceRangedDef) {
140 val type = RangedWeaponType.THROWN
141 val allowed = RangedAmmunition.forItemId(id)
142 if (allowed != null) {
143 setRangedDefinition(RangedWeaponDefinition(type, allowed))
144 }
145 }
146 }
147 }
148
149 @JvmStatic
150 fun main(args: Array<String>) = load()
151
152}