|
| 1 | +package de.skyrising.mc.scanner |
| 2 | + |
| 3 | +import kotlinx.serialization.* |
| 4 | +import kotlinx.serialization.json.* |
| 5 | + |
| 6 | +interface Needle |
| 7 | + |
| 8 | +private val BLOCK_STATE_MAP = readBlockStateMap() |
| 9 | +private val ITEM_MAP = readItemMap() |
| 10 | + |
| 11 | +data class Identifier(val namespace: String, val path: String) : Comparable<Identifier> { |
| 12 | + constructor(id: String) : this(getNamespace(id), getPath(id)) |
| 13 | + |
| 14 | + override fun compareTo(other: Identifier): Int { |
| 15 | + val namespaceCompare = namespace.compareTo(other.namespace) |
| 16 | + if (namespaceCompare != 0) return namespaceCompare |
| 17 | + return path.compareTo(other.path) |
| 18 | + } |
| 19 | + |
| 20 | + override fun toString() = "$namespace:$path" |
| 21 | + |
| 22 | + companion object { |
| 23 | + fun getNamespace(id: String) = id.substringBefore(':', "minecraft") |
| 24 | + fun getPath(id: String) = id.substringAfter(':') |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +data class BlockState(val id: Identifier, val properties: Map<String, String> = emptyMap()) : Needle, Comparable<BlockState> { |
| 29 | + fun unflatten(): List<BlockIdMask> { |
| 30 | + val list = mutableListOf<BlockIdMask>() |
| 31 | + var id: Int? = null |
| 32 | + var mask = 0 |
| 33 | + for (i in BLOCK_STATE_MAP.indices) { |
| 34 | + val mapped = BLOCK_STATE_MAP[i] ?: continue |
| 35 | + if (!mapped.matches(this)) continue |
| 36 | + val currentId = i shr 4 |
| 37 | + if (id != null && currentId != id) { |
| 38 | + list.add(BlockIdMask(id, mask, this)) |
| 39 | + mask = 0 |
| 40 | + } |
| 41 | + id = currentId |
| 42 | + mask = mask or (1 shl (i and 0xf)) |
| 43 | + } |
| 44 | + if (id != null) list.add(BlockIdMask(id, mask, this)) |
| 45 | + return list |
| 46 | + } |
| 47 | + |
| 48 | + fun matches(predicate: BlockState): Boolean { |
| 49 | + if (id != predicate.id) return false |
| 50 | + for (e in predicate.properties.entries) { |
| 51 | + if (!properties.containsKey(e.key) || properties[e.key] != e.value) return false |
| 52 | + } |
| 53 | + return true |
| 54 | + } |
| 55 | + |
| 56 | + override fun compareTo(other: BlockState): Int { |
| 57 | + val idComp = id.compareTo(other.id) |
| 58 | + if (idComp != 0) return idComp |
| 59 | + if (properties == other.properties) return 0 |
| 60 | + return properties.hashCode().compareTo(other.properties.hashCode()) or 1 |
| 61 | + } |
| 62 | + |
| 63 | + fun format(): String { |
| 64 | + if (properties.isEmpty()) return id.toString() |
| 65 | + val sb = StringBuilder(id.toString()).append('[') |
| 66 | + var first = true |
| 67 | + for (e in properties.entries) { |
| 68 | + if (!first) sb.append(',') |
| 69 | + first = false |
| 70 | + sb.append(e.key).append('=').append(e.value) |
| 71 | + } |
| 72 | + return sb.append(']').toString() |
| 73 | + } |
| 74 | + |
| 75 | + override fun toString() = "BlockState(${format()})" |
| 76 | + |
| 77 | + companion object { |
| 78 | + fun parse(desc: String): BlockState { |
| 79 | + if (!desc.contains('[')) return BlockState(Identifier(desc)) |
| 80 | + val bracketIndex = desc.indexOf('[') |
| 81 | + val closingBracketIndex = desc.indexOf(']', bracketIndex + 1) |
| 82 | + if (closingBracketIndex != desc.lastIndex) throw IllegalArgumentException("Expected closing ]") |
| 83 | + val id = Identifier(desc.substring(0, bracketIndex)) |
| 84 | + val properties = LinkedHashMap<String, String>() |
| 85 | + for (kvPair in desc.substring(bracketIndex + 1, closingBracketIndex).split(',')) { |
| 86 | + val equalsIndex = kvPair.indexOf('=') |
| 87 | + if (equalsIndex < 0) throw IllegalArgumentException("Invalid key-value pair") |
| 88 | + properties[kvPair.substring(0, equalsIndex)] = kvPair.substring(equalsIndex + 1) |
| 89 | + } |
| 90 | + return BlockState(id, properties) |
| 91 | + } |
| 92 | + |
| 93 | + fun from(nbt: CompoundTag): BlockState { |
| 94 | + val id = Identifier(nbt.getString("Name")) |
| 95 | + if (!nbt.has("Properties", Tag.COMPOUND)) return BlockState(id) |
| 96 | + val properties = LinkedHashMap<String, String>() |
| 97 | + for (e in nbt.getCompound("Properties").entries) { |
| 98 | + properties[e.key] = (e.value as StringTag).value |
| 99 | + } |
| 100 | + return BlockState(id, properties) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +data class BlockIdMask(val id: Int, val metaMask: Int, val blockState: BlockState? = null) : Needle { |
| 106 | + fun matches(id: Int, meta: Int) = this.id == id && (1 shl meta) and metaMask != 0 |
| 107 | + |
| 108 | + infix fun or(other: BlockIdMask): BlockIdMask { |
| 109 | + if (other.id != id) throw IllegalArgumentException("Cannot combine different ids") |
| 110 | + return BlockIdMask(id, metaMask or other.metaMask) |
| 111 | + } |
| 112 | + |
| 113 | + override fun toString(): String { |
| 114 | + if (blockState == null) return "BlockIdMask(%d:0x%04x)".format(id, metaMask) |
| 115 | + return "BlockIdMask(%d:0x%04x %s)".format(id, metaMask, blockState.format()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +data class ItemType(val id: Identifier, val damage: Int = -1, val flattened: Boolean = damage < 0) : Needle, Comparable<ItemType> { |
| 120 | + fun flatten(): ItemType { |
| 121 | + if (this.flattened) return this |
| 122 | + var flattened = ITEM_MAP[this] |
| 123 | + if (flattened == null) flattened = ITEM_MAP[ItemType(id, 0)] |
| 124 | + if (flattened == null) return ItemType(id, damage, true) |
| 125 | + return ItemType(flattened, -1, true) |
| 126 | + } |
| 127 | + |
| 128 | + fun unflatten(): List<ItemType> { |
| 129 | + if (!flattened) return emptyList() |
| 130 | + val list = mutableListOf<ItemType>() |
| 131 | + for (e in ITEM_MAP.entries) { |
| 132 | + if (e.value == this.id && e.key.id != this.id) { |
| 133 | + list.add(e.key) |
| 134 | + } |
| 135 | + } |
| 136 | + return list |
| 137 | + } |
| 138 | + |
| 139 | + override fun toString(): String { |
| 140 | + return "ItemType(${format()})" |
| 141 | + } |
| 142 | + |
| 143 | + fun format() = if (damage < 0 || (flattened && damage == 0)) "$id" else "$id.$damage" |
| 144 | + |
| 145 | + override fun compareTo(other: ItemType): Int { |
| 146 | + val idComp = id.compareTo(other.id) |
| 147 | + if (idComp != 0) return idComp |
| 148 | + return damage.compareTo(other.damage) |
| 149 | + } |
| 150 | + |
| 151 | + companion object { |
| 152 | + fun parse(str: String): ItemType { |
| 153 | + if (!str.contains('.')) return ItemType(Identifier(str)) |
| 154 | + return ItemType(Identifier(str.substringBefore('.')), str.substringAfter('.').toInt()) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +private fun getFlatteningMap(name: String): JsonObject = Json.decodeFromString(BlockIdMask::class.java.getResourceAsStream("/flattening/$name.json")!!.reader().readText()) |
| 160 | + |
| 161 | +private fun readBlockStateMap(): Array<BlockState?> { |
| 162 | + val jsonMap = getFlatteningMap("block_states") |
| 163 | + val map = Array<BlockState?>(256 * 16) { null } |
| 164 | + for (e in jsonMap.entries) { |
| 165 | + val id = if (e.key.contains(':')) { |
| 166 | + e.key.substringBefore(':').toInt() shl 4 or e.key.substringAfter(':').toInt() |
| 167 | + } else { |
| 168 | + e.key.toInt() shl 4 |
| 169 | + } |
| 170 | + map[id] = BlockState.parse(e.value.jsonPrimitive.content) |
| 171 | + } |
| 172 | + for (i in map.indices) { |
| 173 | + if (map[i] != null) continue |
| 174 | + map[i] = map[i and 0xff0] |
| 175 | + } |
| 176 | + return map |
| 177 | +} |
| 178 | + |
| 179 | +private fun readItemMap(): Map<ItemType, Identifier> { |
| 180 | + val jsonMap = getFlatteningMap("items") |
| 181 | + val map = LinkedHashMap<ItemType, Identifier>() |
| 182 | + for (e in jsonMap.entries) { |
| 183 | + map[ItemType.parse(e.key)] = Identifier(e.value.jsonPrimitive.content) |
| 184 | + } |
| 185 | + return map |
| 186 | +} |
0 commit comments