network: improve pre flattening item meta writing

This commit is contained in:
Moritz Zwerger 2024-01-12 21:40:56 +01:00
parent 78b8daabf8
commit c8150e5fb5
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 33 additions and 16 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -134,7 +134,7 @@ when (os) {
Architectures.ARM -> {
lwjglNatives += "-arm64"
zstdNatives += "-amd64"
// TODO: javafx for Windows on arm is not yet supported: https://github.com/luben/zstd-jni/issues/277
// TODO: javafx for Windows on arm is not yet supported
}
*/

View File

@ -22,4 +22,9 @@ interface DurableItem : ItemWithMeta {
override fun setMeta(stack: ItemStack, meta: Int) {
stack.durability.durability = maxDurability - meta // in <1.13 its damage not durability
}
override fun getMeta(id: Int, stack: ItemStack): Int {
val durability = stack._durability ?: return 0
return maxDurability - durability.durability
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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,8 +14,12 @@
package de.bixilon.minosoft.data.registries.item.items.legacy
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.registries.registries.registry.MetaTypes
interface ItemWithMeta {
fun setMeta(stack: ItemStack, meta: Int)
fun getMeta(id: Int, stack: ItemStack): Int {
return id and MetaTypes.ITEM.mask
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -19,9 +19,9 @@ enum class MetaTypes(val bits: Int) {
ITEM(16),
;
private val metaMask = (1 shl bits) - 1
val mask = (1 shl bits) - 1
fun modify(id: Int, meta: Int): Int {
return (id shl bits) or (meta and metaMask)
return (id shl bits) or (meta and mask)
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 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.
*
@ -18,6 +18,8 @@ import de.bixilon.minosoft.data.chat.signature.ChatSignatureProperties
import de.bixilon.minosoft.data.chat.signature.LastSeenMessageList
import de.bixilon.minosoft.data.chat.signature.lastSeen.MessageSignatureData
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.registries.item.items.legacy.ItemWithMeta
import de.bixilon.minosoft.data.registries.registries.registry.MetaTypes
import de.bixilon.minosoft.protocol.PlayerPublicKey
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
@ -48,17 +50,22 @@ class PlayOutByteBuffer(val connection: PlayConnection) : OutByteBuffer() {
}
}
fun writeLegacyItemStack(stack: ItemStack?) {
if (stack == null || !stack._valid) {
writeShort(-1)
return
}
val item = stack.item.item
val id = connection.registries.item.getId(item)
writeShort(id shr MetaTypes.ITEM.bits)
writeByte(stack.item.count)
writeShort(if (item is ItemWithMeta) item.getMeta(id, stack) else 0)
writeNBT(stack.getNBT())
}
fun writeItemStack(stack: ItemStack?) {
if (versionId < ProtocolVersions.V_1_13_2_PRE1) {
if (stack == null || !stack._valid) {
writeShort(-1)
return
}
writeShort(connection.registries.item.getId(stack.item.item))
writeByte(stack.item.count)
writeShort(stack._durability?.durability ?: 0) // ToDo: This is meta in general and not just durability
writeNBT(stack.getNBT())
return
return writeLegacyItemStack(stack)
}
val valid = stack != null && stack._valid
writeBoolean(valid)
@ -80,6 +87,7 @@ class PlayOutByteBuffer(val connection: PlayConnection) : OutByteBuffer() {
}
fun writeNBT(nbt: Any?) {
if (nbt is Map<*, *> && nbt.isEmpty()) return writeNBT(null)
return writeNBT(nbt, versionId < ProtocolVersions.V_14W28B, versionId < V_23W31A)
}