network: fix item writing, gui: stack dropping

This commit is contained in:
Bixilon 2022-02-21 11:01:53 +01:00
parent bc191c50a7
commit 04c0108257
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 25 additions and 5 deletions

View File

@ -13,8 +13,10 @@
package de.bixilon.minosoft.data.player
import de.bixilon.kutil.cast.CastUtil.nullCast
import de.bixilon.kutil.collections.CollectionUtil.synchronizedBiMapOf
import de.bixilon.kutil.collections.CollectionUtil.synchronizedMapOf
import de.bixilon.kutil.collections.map.LockMap
import de.bixilon.kutil.collections.map.bi.SynchronizedBiMap
import de.bixilon.kutil.math.MMath.clamp
import de.bixilon.kutil.math.MMath.floor
import de.bixilon.kutil.primitive.BooleanUtil.decide
@ -83,7 +85,7 @@ class LocalPlayerEntity(
val baseAbilities = Abilities()
val inventory = PlayerInventory(connection)
val containers: MutableMap<Int, Container> = synchronizedMapOf(
val containers: SynchronizedBiMap<Int, Container> = synchronizedBiMapOf(
ProtocolDefinition.PLAYER_CONTAINER_ID to inventory,
)
var selectedHotbarSlot: Int = 0

View File

@ -147,6 +147,11 @@ open class Container(
revision++
}
fun createAction(): Int {
return 0 // ToDo
}
override fun iterator(): Iterator<Map.Entry<Int, ItemStack>> {
return slots.toSynchronizedMap().iterator()
}

View File

@ -31,6 +31,7 @@ import de.bixilon.minosoft.gui.rendering.gui.elements.VerticalAlignments.Compani
import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.ColorElement
import de.bixilon.minosoft.gui.rendering.gui.elements.primitive.ImageElement
import de.bixilon.minosoft.gui.rendering.gui.elements.text.TextElement
import de.bixilon.minosoft.gui.rendering.gui.input.ModifierKeys
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.gui.popper.text.TextPopper
@ -145,11 +146,21 @@ class ItemElement(
return true
}
val container = container ?: return false
val containerId = renderWindow.connection.player.containers.getKey(container) ?: return false
val controlDown = guiRenderer.isKeyDown(ModifierKeys.CONTROL)
val shiftDown = guiRenderer.isKeyDown(ModifierKeys.SHIFT)
// ToDo
when (key) {
KeyCodes.KEY_Q -> {
stack?.item?.decreaseCount()
renderWindow.connection.sendPacket(ContainerClickC2SP(0, container.serverRevision, slotId, InventoryActions.DROP_ITEM, 0, mapOf(), stack))
val action: InventoryActions
if (controlDown) {
stack?.item?.count = 0
action = InventoryActions.DROP_STACK
} else {
stack?.item?.decreaseCount()
action = InventoryActions.DROP_ITEM
}
renderWindow.connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, slotId, action, container.createAction(), mapOf(slotId to stack), stack))
}
}
return true

View File

@ -50,10 +50,12 @@ class PlayOutByteBuffer(val connection: PlayConnection) : OutByteBuffer() {
writeNBT(itemStack.getNBT())
return
}
writeBoolean(itemStack != null)
if (itemStack == null || !itemStack._valid) {
val valid = itemStack?._valid == true
writeBoolean(valid)
if (!valid) {
return
}
itemStack!!
writeVarInt(connection.registries.itemRegistry.getId(itemStack.item.item))
writeByte(itemStack.item.count)
writeNBT(itemStack.getNBT())