improve ItemStack nbt initialisation performance

This commit is contained in:
Moritz Zwerger 2023-11-11 15:51:19 +01:00
parent 8eeb079a32
commit 8ae3886f5b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 46 additions and 44 deletions

View File

@ -20,6 +20,8 @@ import de.bixilon.kutil.observer.DataObserver.Companion.observed
import de.bixilon.minosoft.data.Rarities
import de.bixilon.minosoft.data.container.SlotEdit
import de.bixilon.minosoft.data.container.stack.property.*
import de.bixilon.minosoft.data.container.stack.property.DisplayProperty.Companion.updateDisplayNbt
import de.bixilon.minosoft.data.container.stack.property.EnchantingProperty.Companion.updateEnchantingNbt
import de.bixilon.minosoft.data.registries.item.items.Item
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@ -166,13 +168,13 @@ class ItemStack {
return
}
// ToDo: This force creates an instance of every property
if (!this.display.updateNbt(nbt)) {
if (!this.updateDisplayNbt(nbt)) {
_display = null
}
if (!this.durability.updateNbt(nbt)) {
_durability = null
}
if (!this.enchanting.updateNbt(nbt)) {
if (!this.updateEnchantingNbt(nbt)) {
_enchanting = null
}
if (!this.hide.updateNbt(nbt)) {

View File

@ -50,21 +50,6 @@ class DisplayProperty(
return _customDisplayName == null && lore.isEmpty() && _dyeColor == null
}
override fun updateNbt(nbt: MutableJsonObject): Boolean {
val display = nbt.remove(DISPLAY_TAG)?.nullCast<JsonObject>() ?: return false
display[DISPLAY_MAME_TAG]?.let { _customDisplayName = ChatComponent.of(it, translator = stack.holder?.connection?.language) }
display[DISPLAY_LORE_TAG]?.listCast<String>()?.let {
for (line in it) {
this.lore += ChatComponent.of(line, translator = stack.holder?.connection?.language)
}
}
display[DISPLAY_COLOR_TAG]?.toInt()?.asRGBColor()?.let { this._dyeColor = it }
return !isDefault()
}
override fun hashCode(): Int {
return Objects.hashCode(lore, _customDisplayName, _dyeColor)
}
@ -92,5 +77,21 @@ class DisplayProperty(
private const val DISPLAY_MAME_TAG = "Name"
private const val DISPLAY_LORE_TAG = "Lore"
private const val DISPLAY_COLOR_TAG = "color"
fun ItemStack.updateDisplayNbt(nbt: MutableJsonObject): Boolean {
val display = nbt.remove(DISPLAY_TAG)?.nullCast<JsonObject>() ?: return false
display[DISPLAY_MAME_TAG]?.let { this.display._customDisplayName = ChatComponent.of(it, translator = this.holder?.connection?.language) }
display[DISPLAY_LORE_TAG]?.listCast<String>()?.let {
for (line in it) {
this.display.lore += ChatComponent.of(line, translator = this.holder?.connection?.language)
}
}
display[DISPLAY_COLOR_TAG]?.toInt()?.asRGBColor()?.let { this.display._dyeColor = it }
if (_display == null) return false
return !this.display.isDefault()
}
}
}

View File

@ -59,7 +59,7 @@ class DurabilityProperty(
return !_unbreakable && (stack.item.item is DurableItem && _durability == stack.item.item.maxDurability)
}
override fun updateNbt(nbt: MutableJsonObject): Boolean {
fun updateNbt(nbt: MutableJsonObject): Boolean {
nbt.remove(UNBREAKABLE_TAG)?.toBoolean()?.let { this._unbreakable = it }
nbt.remove(DAMAGE_TAG)?.toInt()?.let { if (stack.item.item is DurableItem) this._durability = stack.item.item.maxDurability - it }

View File

@ -61,26 +61,6 @@ class EnchantingProperty(
return _repairCost == 0 && enchantments.isEmpty()
}
override fun updateNbt(nbt: MutableJsonObject): Boolean {
nbt.remove(REPAIR_COST_TAG)?.toInt()?.let { _repairCost = it }
nbt.remove(*ENCHANTMENTS_TAG)?.listCast<JsonObject>()?.let {
val registry = stack.holder?.connection?.registries?.enchantment ?: return@let
for (tag in it) {
val enchantmentName = tag[ENCHANTMENT_ID_TAG]
val enchantment = registry[enchantmentName] ?: throw IllegalArgumentException("Unknown enchantment: $enchantmentName")
val level = tag[ENCHANTMENT_LEVEL_TAG]?.toInt() ?: 1
if (level <= 0) {
continue
}
enchantments[enchantment] = level
}
}
return !isDefault()
}
override fun hashCode(): Int {
return Objects.hashCode(enchantments, _repairCost)
}
@ -111,5 +91,27 @@ class EnchantingProperty(
private const val ENCHANTMENT_ID_TAG = "id"
private const val ENCHANTMENT_LEVEL_TAG = "lvl"
fun ItemStack.updateEnchantingNbt(nbt: MutableJsonObject): Boolean {
nbt.remove(REPAIR_COST_TAG)?.toInt()?.let { enchanting._repairCost = it }
nbt.remove(*ENCHANTMENTS_TAG)?.listCast<JsonObject>()?.let {
val registry = holder?.connection?.registries?.enchantment ?: return@let
for (tag in it) {
val enchantmentName = tag[ENCHANTMENT_ID_TAG]
val enchantment = registry[enchantmentName] ?: throw IllegalArgumentException("Unknown enchantment: $enchantmentName")
val level = tag[ENCHANTMENT_LEVEL_TAG]?.toInt() ?: 1
if (level <= 0) {
continue
}
this.enchanting.enchantments[enchantment] = level
}
}
if (_enchanting == null) return false
return !enchanting.isDefault()
}
}
}

View File

@ -72,7 +72,7 @@ class HideProperty(
return _hideFlags == 0
}
override fun updateNbt(nbt: MutableJsonObject): Boolean {
fun updateNbt(nbt: MutableJsonObject): Boolean {
nbt.remove(HIDE_FLAGS_TAG)?.toInt()?.let { this._hideFlags = it }
return !isDefault()
}

View File

@ -29,7 +29,7 @@ class NbtProperty(
return nbt.isEmpty()
}
override fun updateNbt(nbt: MutableJsonObject): Boolean {
fun updateNbt(nbt: MutableJsonObject): Boolean {
this.nbt.putAll(nbt)
return !isDefault()
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -14,13 +14,10 @@
package de.bixilon.minosoft.data.container.stack.property
import de.bixilon.kutil.json.JsonObject
import de.bixilon.kutil.json.MutableJsonObject
interface Property {
fun isDefault(): Boolean
fun updateNbt(nbt: MutableJsonObject): Boolean = false
fun getNBT(): JsonObject = emptyMap()
}