mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
ItemStack::equals, ItemStack improvements
This commit is contained in:
parent
559be7236d
commit
e1d2d75813
@ -30,18 +30,33 @@ class ItemStack {
|
||||
|
||||
var _display: DisplayProperty? = null
|
||||
private set
|
||||
val display: DisplayProperty by lazy { _display?.let { return@lazy it }; return@lazy DisplayProperty(this).apply { _display = this } }
|
||||
val display: DisplayProperty
|
||||
get() {
|
||||
_display?.let { return it }; return DisplayProperty(this).apply { _display = this }
|
||||
}
|
||||
var _durability: DurabilityProperty? = null
|
||||
private set
|
||||
val durability: DurabilityProperty by lazy { _durability?.let { return@lazy it }; return@lazy DurabilityProperty(this).apply { this@ItemStack._durability = this } }
|
||||
val durability: DurabilityProperty
|
||||
get() {
|
||||
_durability?.let { return it }; return DurabilityProperty(this).apply { this@ItemStack._durability = this }
|
||||
}
|
||||
var _enchanting: EnchantingProperty? = null
|
||||
val enchanting: EnchantingProperty by lazy { _enchanting?.let { return@lazy it }; return@lazy EnchantingProperty(this).apply { _enchanting = this } }
|
||||
val enchanting: EnchantingProperty
|
||||
get() {
|
||||
_enchanting?.let { return it }; return EnchantingProperty(this).apply { _enchanting = this }
|
||||
}
|
||||
var _hide: HideProperty? = null
|
||||
private set
|
||||
val hide: HideProperty by lazy { _hide?.let { return@lazy it }; return@lazy HideProperty(this).apply { _hide = this } }
|
||||
val hide: HideProperty
|
||||
get() {
|
||||
_hide?.let { return it }; return HideProperty(this).apply { _hide = this }
|
||||
}
|
||||
var _nbt: NbtProperty? = null
|
||||
private set
|
||||
val nbt: NbtProperty by lazy { _nbt?.let { return@lazy it }; return@lazy NbtProperty(this).apply { _nbt = this } }
|
||||
val nbt: NbtProperty
|
||||
get() {
|
||||
_nbt?.let { return it }; return NbtProperty(this).apply { _nbt = this }
|
||||
}
|
||||
|
||||
var revision by watched(0L)
|
||||
|
||||
@ -80,7 +95,7 @@ class ItemStack {
|
||||
if (item._count <= 0) {
|
||||
return false
|
||||
}
|
||||
if (!durability._valid) {
|
||||
if (_durability?._valid == false) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@ -150,11 +165,21 @@ class ItemStack {
|
||||
return
|
||||
}
|
||||
// ToDo: This force creates an instance of every property
|
||||
display.updateNbt(nbt)
|
||||
durability.updateNbt(nbt)
|
||||
enchanting.updateNbt(nbt)
|
||||
hide.updateNbt(nbt)
|
||||
this.nbt.updateNbt(nbt)
|
||||
if (!this.display.updateNbt(nbt)) {
|
||||
_display = null
|
||||
}
|
||||
if (!this.durability.updateNbt(nbt)) {
|
||||
_durability = null
|
||||
}
|
||||
if (!this.enchanting.updateNbt(nbt)) {
|
||||
_enchanting = null
|
||||
}
|
||||
if (!this.hide.updateNbt(nbt)) {
|
||||
_hide = null
|
||||
}
|
||||
if (!this.nbt.updateNbt(nbt)) {
|
||||
_nbt = null
|
||||
}
|
||||
}
|
||||
|
||||
fun getNBT(): JsonObject {
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
package de.bixilon.minosoft.data.inventory.stack.property
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import de.bixilon.kutil.cast.CastUtil.nullCast
|
||||
import de.bixilon.kutil.json.JsonObject
|
||||
import de.bixilon.kutil.json.MutableJsonObject
|
||||
@ -43,8 +44,12 @@ class DisplayProperty(
|
||||
this::lore.observeList(this) { stack.holder?.container?.let { it.revision++ } }
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject) {
|
||||
val display = nbt.remove(DISPLAY_TAG)?.nullCast<JsonObject>() ?: return
|
||||
override fun isDefault(): Boolean {
|
||||
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 {
|
||||
@ -54,6 +59,22 @@ class DisplayProperty(
|
||||
}
|
||||
|
||||
display[DISPLAY_COLOR_TAG]?.toInt()?.asRGBColor()?.let { this._dyeColor = it }
|
||||
|
||||
return !isDefault()
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(lore, _customDisplayName, _dyeColor)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is DisplayProperty) {
|
||||
return false
|
||||
}
|
||||
if (other.hashCode() != hashCode()) {
|
||||
return false
|
||||
}
|
||||
return lore == other.lore && _customDisplayName == other._customDisplayName && _dyeColor == other._dyeColor
|
||||
}
|
||||
|
||||
fun copy(
|
||||
|
@ -13,8 +13,10 @@
|
||||
|
||||
package de.bixilon.minosoft.data.inventory.stack.property
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import de.bixilon.kutil.json.MutableJsonObject
|
||||
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
|
||||
import de.bixilon.kutil.primitive.IntUtil.toInt
|
||||
import de.bixilon.minosoft.data.inventory.InventoryDelegate
|
||||
import de.bixilon.minosoft.data.inventory.stack.ItemStack
|
||||
|
||||
@ -52,8 +54,29 @@ class DurabilityProperty(
|
||||
return true
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject) {
|
||||
override fun isDefault(): Boolean {
|
||||
return !_unbreakable && _durability == stack.item.item.maxDurability
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject): Boolean {
|
||||
nbt.remove(UNBREAKABLE_TAG)?.toBoolean()?.let { this._unbreakable = it }
|
||||
nbt.remove(DAMAGE_TAG)?.toInt()?.let { this._durability = stack.item.item.maxDurability - it }
|
||||
|
||||
return !isDefault()
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(_unbreakable, _durability)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is DurabilityProperty) {
|
||||
return false
|
||||
}
|
||||
if (other.hashCode() != hashCode()) {
|
||||
return false
|
||||
}
|
||||
return _durability == other._durability && _unbreakable == other._unbreakable
|
||||
}
|
||||
|
||||
fun copy(
|
||||
@ -66,5 +89,6 @@ class DurabilityProperty(
|
||||
|
||||
companion object {
|
||||
private const val UNBREAKABLE_TAG = "unbreakable"
|
||||
private const val DAMAGE_TAG = "Damage"
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
package de.bixilon.minosoft.data.inventory.stack.property
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import de.bixilon.kutil.json.JsonObject
|
||||
import de.bixilon.kutil.json.MutableJsonObject
|
||||
import de.bixilon.kutil.primitive.IntUtil.toInt
|
||||
@ -56,7 +57,11 @@ class EnchantingProperty(
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject) {
|
||||
override fun isDefault(): Boolean {
|
||||
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 {
|
||||
@ -69,6 +74,22 @@ class EnchantingProperty(
|
||||
enchantments[enchantment] = level
|
||||
}
|
||||
}
|
||||
|
||||
return !isDefault()
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(enchantments, _repairCost)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is EnchantingProperty) {
|
||||
return false
|
||||
}
|
||||
if (other.hashCode() != hashCode()) {
|
||||
return false
|
||||
}
|
||||
return enchantments == other.enchantments && _repairCost == other._repairCost
|
||||
}
|
||||
|
||||
fun copy(
|
||||
|
@ -68,8 +68,24 @@ class HideProperty(
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject) {
|
||||
override fun isDefault(): Boolean {
|
||||
return _hideFlags == 0
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject): Boolean {
|
||||
nbt.remove(HIDE_FLAGS_TAG)?.toInt()?.let { this._hideFlags = it }
|
||||
return !isDefault()
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return _hideFlags
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is HideProperty) {
|
||||
return false
|
||||
}
|
||||
return _hideFlags == other._hideFlags
|
||||
}
|
||||
|
||||
fun copy(
|
||||
|
@ -19,4 +19,7 @@ import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
|
||||
class HolderProperty(
|
||||
val connection: PlayConnection? = null,
|
||||
var container: Container? = null,
|
||||
) : Property
|
||||
) : Property {
|
||||
|
||||
override fun isDefault(): Boolean = false
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
package de.bixilon.minosoft.data.inventory.stack.property
|
||||
|
||||
import com.google.common.base.Objects
|
||||
import de.bixilon.minosoft.data.inventory.InventoryDelegate
|
||||
import de.bixilon.minosoft.data.inventory.stack.ItemStack
|
||||
import de.bixilon.minosoft.data.registries.items.Item
|
||||
@ -37,4 +38,20 @@ class ItemProperty(
|
||||
_count += 1
|
||||
stack.commit()
|
||||
}
|
||||
|
||||
override fun isDefault(): Boolean = false
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return Objects.hashCode(item, _count)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is ItemProperty) {
|
||||
return false
|
||||
}
|
||||
if (other.hashCode() != hashCode()) {
|
||||
return false
|
||||
}
|
||||
return item == other.item && _count == other._count
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,29 @@ class NbtProperty(
|
||||
val nbt: MutableJsonObject = mutableMapOf(),
|
||||
) : Property {
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return nbt.hashCode()
|
||||
}
|
||||
|
||||
override fun isDefault(): Boolean {
|
||||
return nbt.isEmpty()
|
||||
}
|
||||
|
||||
override fun updateNbt(nbt: MutableJsonObject): Boolean {
|
||||
this.nbt.putAll(nbt)
|
||||
return isDefault()
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is NbtProperty) {
|
||||
return false
|
||||
}
|
||||
if (other.hashCode() != hashCode()) {
|
||||
return false
|
||||
}
|
||||
return nbt == other.nbt
|
||||
}
|
||||
|
||||
fun copy(
|
||||
stack: ItemStack,
|
||||
nbt: MutableJsonObject = this.nbt.toMutableMap(),
|
||||
|
@ -18,7 +18,9 @@ import de.bixilon.kutil.json.MutableJsonObject
|
||||
|
||||
interface Property {
|
||||
|
||||
fun updateNbt(nbt: MutableJsonObject) {}
|
||||
fun isDefault(): Boolean
|
||||
|
||||
fun updateNbt(nbt: MutableJsonObject): Boolean = false
|
||||
|
||||
fun getNBT(): JsonObject = mapOf()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user