From c6f6453c739280129f2f25526a4b4b9b48a5112b Mon Sep 17 00:00:00 2001 From: Bixilon Date: Thu, 12 Jan 2023 22:24:08 +0100 Subject: [PATCH] fix some container bugs + tests --- .../data/container/ContainerTestUtil.kt | 7 +++++ .../click/FastMoveContainerActionTest.kt | 31 ++++++++++++++++++- .../data/container/sections/HotbarSection.kt | 5 ++- .../sections/PassiveInventorySection.kt | 5 ++- .../data/container/types/PlayerInventory.kt | 14 ++------- .../netty/pipeline/ClientPacketHandler.kt | 5 +-- .../s2c/play/container/CloseContainerS2CP.kt | 9 ++++-- 7 files changed, 53 insertions(+), 23 deletions(-) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt index b2e886d30..69bb6111b 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/ContainerTestUtil.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.data.container +import de.bixilon.minosoft.data.container.types.PlayerInventory import de.bixilon.minosoft.data.container.types.generic.Generic9x3Container import de.bixilon.minosoft.data.container.types.processing.smelting.FurnaceContainer import de.bixilon.minosoft.data.registries.containers.ContainerFactory @@ -36,6 +37,12 @@ object ContainerTestUtil { reference() } + fun createInventory(connection: PlayConnection = createConnection()): Container { + val inventory = PlayerInventory(connection) + connection.player.containers[0] = inventory + return inventory + } + fun createContainer(connection: PlayConnection = createConnection()): Container { val container = Container(connection, this.container) connection.player.containers[9] = container diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/click/FastMoveContainerActionTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/click/FastMoveContainerActionTest.kt index 4f399b951..8bbfac392 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/container/click/FastMoveContainerActionTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/container/click/FastMoveContainerActionTest.kt @@ -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. * @@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.container.click import de.bixilon.minosoft.data.container.ContainerTestUtil.createChest import de.bixilon.minosoft.data.container.ContainerTestUtil.createFurnace +import de.bixilon.minosoft.data.container.ContainerTestUtil.createInventory import de.bixilon.minosoft.data.container.ContainerUtil.slotsOf import de.bixilon.minosoft.data.container.stack.ItemStack import de.bixilon.minosoft.data.registries.items.AppleTestO @@ -139,5 +140,33 @@ class FastMoveContainerActionTest { connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 30, 1, 0, 0, slotsOf(30 to null, 1 to ItemStack(CoalTest0.item, count = 12)), null)) } + + fun playerPassiveToHotbar() { + val connection = createConnection() + val container = createInventory(connection) + container[9] = ItemStack(AppleTestO.item) + container.invokeAction(FastMoveContainerAction(9)) + assertNull(container.floatingItem) + assertEquals(container.slots, slotsOf(36 to ItemStack(AppleTestO.item))) + } + + fun craftingToPassive() { + val connection = createConnection() + val container = createInventory(connection) + container[1] = ItemStack(AppleTestO.item) + container.invokeAction(FastMoveContainerAction(1)) + assertNull(container.floatingItem) + assertEquals(container.slots, slotsOf(9 to ItemStack(AppleTestO.item))) + } + + fun hotbarToPassive() { + val connection = createConnection() + val container = createInventory(connection) + container[36] = ItemStack(AppleTestO.item) + container.invokeAction(FastMoveContainerAction(36)) + assertNull(container.floatingItem) + assertEquals(container.slots, slotsOf(9 to ItemStack(AppleTestO.item))) + } + // TODO: revert, full container } diff --git a/src/main/java/de/bixilon/minosoft/data/container/sections/HotbarSection.kt b/src/main/java/de/bixilon/minosoft/data/container/sections/HotbarSection.kt index 5b2152d88..b4cfc242d 100644 --- a/src/main/java/de/bixilon/minosoft/data/container/sections/HotbarSection.kt +++ b/src/main/java/de/bixilon/minosoft/data/container/sections/HotbarSection.kt @@ -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. * @@ -15,7 +15,6 @@ package de.bixilon.minosoft.data.container.sections import de.bixilon.minosoft.data.container.types.PlayerInventory -class HotbarSection(val offset: Int) : RangeSection(offset, PlayerInventory.HOTBAR_SLOTS) { - override val fillReversed: Boolean get() = true +class HotbarSection(val offset: Int, override val fillReversed: Boolean = true) : RangeSection(offset, PlayerInventory.HOTBAR_SLOTS) { override val count: Int get() = PlayerInventory.HOTBAR_SLOTS } diff --git a/src/main/java/de/bixilon/minosoft/data/container/sections/PassiveInventorySection.kt b/src/main/java/de/bixilon/minosoft/data/container/sections/PassiveInventorySection.kt index 83cc88fa2..c5b2750a5 100644 --- a/src/main/java/de/bixilon/minosoft/data/container/sections/PassiveInventorySection.kt +++ b/src/main/java/de/bixilon/minosoft/data/container/sections/PassiveInventorySection.kt @@ -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. * @@ -15,7 +15,6 @@ package de.bixilon.minosoft.data.container.sections import de.bixilon.minosoft.data.container.types.PlayerInventory -class PassiveInventorySection(val offset: Int) : RangeSection(offset, PlayerInventory.PASSIVE_SLOTS) { +class PassiveInventorySection(val offset: Int, override val fillReversed: Boolean = true) : RangeSection(offset, PlayerInventory.PASSIVE_SLOTS) { override val count: Int get() = PlayerInventory.PASSIVE_SLOTS - override val fillReversed: Boolean get() = true } diff --git a/src/main/java/de/bixilon/minosoft/data/container/types/PlayerInventory.kt b/src/main/java/de/bixilon/minosoft/data/container/types/PlayerInventory.kt index 66a7eecb7..477eb5a2f 100644 --- a/src/main/java/de/bixilon/minosoft/data/container/types/PlayerInventory.kt +++ b/src/main/java/de/bixilon/minosoft/data/container/types/PlayerInventory.kt @@ -115,16 +115,6 @@ class PlayerInventory(connection: PlayConnection) : Container(connection = conne return HOTBAR_OFFSET + slot.ordinal } - override fun getSection(slotId: Int): Int? { - return when (slotId) { - in 0..4 -> null // crafting - in ARMOR_OFFSET..ARMOR_OFFSET + 4 -> 0 // armor - in MAIN_SLOTS_START until HOTBAR_OFFSET -> 1 // inventory - in HOTBAR_OFFSET..HOTBAR_OFFSET + HOTBAR_SLOTS -> 2 // hotbar - else -> null // offhand, else - } - } - val EquipmentSlots.slot: Int get() = when (this) { EquipmentSlots.HEAD -> ARMOR_OFFSET + 0 @@ -169,8 +159,8 @@ class PlayerInventory(connection: PlayConnection) : Container(connection = conne private val SECTIONS = arrayOf( RangeSection(ARMOR_OFFSET, 4), - PassiveInventorySection(ARMOR_OFFSET + 5), - HotbarSection(HOTBAR_OFFSET), + PassiveInventorySection(MAIN_SLOTS_START, false), + HotbarSection(HOTBAR_OFFSET, false), ) override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): PlayerInventory { diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/pipeline/ClientPacketHandler.kt b/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/pipeline/ClientPacketHandler.kt index 3af8b298f..5d3a7821c 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/pipeline/ClientPacketHandler.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/network/network/client/netty/pipeline/ClientPacketHandler.kt @@ -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. * @@ -80,7 +80,8 @@ class ClientPacketHandler( private fun handle(packet: S2CPacket) { val event = PacketReceiveEvent(connection, packet) - if (connection.fire(event)) { + Thread.sleep(100) + if (connection.events.fire(event)) { return } when (packet) { diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/container/CloseContainerS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/container/CloseContainerS2CP.kt index c3a8cc5e2..ed8784aac 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/container/CloseContainerS2CP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/container/CloseContainerS2CP.kt @@ -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. * @@ -12,6 +12,7 @@ */ package de.bixilon.minosoft.protocol.packets.s2c.play.container +import de.bixilon.minosoft.data.container.types.PlayerInventory import de.bixilon.minosoft.modding.event.events.container.ContainerCloseEvent import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.packets.factory.LoadPacket @@ -26,9 +27,13 @@ class CloseContainerS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { val containerId: Int = buffer.readUnsignedByte() override fun handle(connection: PlayConnection) { + if (containerId == PlayerInventory.CONTAINER_ID) { + // server can not close inventory + return + } val container = connection.player.containers[containerId] ?: return container.onClose() - connection.fire(ContainerCloseEvent(connection, containerId, container)) + connection.events.fire(ContainerCloseEvent(connection, containerId, container)) } override fun log(reducedLog: Boolean) {