rework some container sections

This commit is contained in:
Bixilon 2022-11-18 10:24:38 +01:00
parent 3ca82f202f
commit 660e41db88
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
11 changed files with 87 additions and 60 deletions

View File

@ -57,7 +57,7 @@ class FastMoveContainerActionTest {
container.invokeAction(FastMoveContainerAction(0))
assertNull(container.floatingItem)
assertEquals(container.slots, slotsOf(62 to ItemStack(AppleTestO.item, 9)))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 1, 0, 0, slotsOf(62 to null, 0 to ItemStack(AppleTestO.item, count = 9)), null))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 1, 0, 0, slotsOf(0 to null, 62 to ItemStack(AppleTestO.item, count = 9)), null))
}
fun fullHotbarChestToHotbar() {
@ -116,11 +116,7 @@ class FastMoveContainerActionTest {
container.invokeAction(FastMoveContainerAction(30))
assertNull(container.floatingItem)
assertNull(container[0])
assertNull(container[1])
assertNull(container[2])
assertNull(container[30])
assertEquals(container[3], ItemStack(EggTestO.item, 12))
assertEquals(container.slots, slotsOf(3 to ItemStack(EggTestO.item, 12)))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 1, 0, 0, slotsOf(30 to null, 3 to ItemStack(AppleTestO.item, count = 8)), null))
}
@ -133,11 +129,9 @@ class FastMoveContainerActionTest {
container.invokeAction(FastMoveContainerAction(30))
assertNull(container.floatingItem)
assertNull(container[0])
assertEquals(container[1], ItemStack(CoalTest0.item, 12))
assertNull(container[2])
assertEquals(container.slots, slotsOf(1 to ItemStack(CoalTest0.item, 12)))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 0, 1, 0, 0, slotsOf(30 to null, 1 to ItemStack(AppleTestO.item, count = 8)), null))
connection.assertOnlyPacket(ContainerClickC2SP(9, container.serverRevision, 30, 1, 0, 0, slotsOf(30 to null, 1 to ItemStack(CoalTest0.item, count = 12)), null))
}
// TODO: revert, full container

View File

@ -87,7 +87,15 @@ open class Container(
open fun getSlotType(slotId: Int): SlotType? = DefaultSlotType
open fun getSlotSwap(slot: SlotSwapContainerAction.SwapTargets): Int? = null
open fun getSection(slotId: Int): Int? = null
open fun getSection(slotId: Int): Int? {
for ((index, section) in sections.withIndex()) {
if (slotId in section) {
return index
}
}
return null
}
operator fun get(slotId: Int): ItemStack? {
try {

View File

@ -32,4 +32,8 @@ object ContainerUtil {
return map
}
fun section(offset: Int, count: Int): IntRange {
return offset until offset + count
}
}

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.container
import de.bixilon.minosoft.data.container.ContainerUtil.section
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.container.types.PlayerInventory
import de.bixilon.minosoft.data.registries.other.containers.ContainerType
@ -24,7 +25,7 @@ abstract class InventorySynchronizedContainer(
type: ContainerType,
title: ChatComponent? = null,
protected var synchronizedSlots: IntRange,
protected var inventorySlots: IntRange = PlayerInventory.MAIN_SLOTS_START until (PlayerInventory.MAIN_SLOTS_START + PlayerInventory.MAIN_SLOTS),
protected var inventorySlots: IntRange = section(PlayerInventory.MAIN_SLOTS_START, PlayerInventory.MAIN_SLOTS),
) : Container(connection, type, title) {
private val playerInventory = connection.player.inventory

View File

@ -18,6 +18,7 @@ import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.c2s.play.container.ContainerClickC2SP
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
import it.unimi.dsi.fastutil.ints.IntArrayList
class FastMoveContainerAction(
val slot: Int,
@ -32,13 +33,18 @@ class FastMoveContainerAction(
val sourceSection = container.getSection(slot) ?: Int.MAX_VALUE
// loop over all sections and get the lowest slot in the lowest section that fits best
val targets = Int2ObjectOpenHashMap<ItemStack?>()
val targets: MutableList<IntArrayList> = mutableListOf()
for ((index, section) in container.sections.withIndex()) {
if (index == sourceSection) {
// we don't want to swap into the same section, that is just useless
// ToDo: Is this vanilla behavior?
continue
}
if (section.isEmpty()) {
continue
}
val list = IntArrayList()
targets += list
for (slot in section) {
val content = container.slots[slot]
if (content != null && !source.matches(content)) { // only check slots that are not empty
@ -49,18 +55,19 @@ class FastMoveContainerAction(
// this item is not allowed in this slot (e.g. blocks in armor slot)
continue
}
targets[slot] = content
list += slot
}
}
val changes: Int2ObjectOpenHashMap<ItemStack> = Int2ObjectOpenHashMap()
for ((slot, content) in targets.toSortedMap()) {
sections@ for (list in targets) {
for (slot in list.intIterator()) {
val content = container.slots[slot]
if (content == null) {
changes[slot] = source
changes[this.slot] = null
container._set(slot, source)
container._set(this.slot, null)
break
break@sections
}
val countToPut = source.item._count - (source.item.item.maxStackSize - content.item._count)
source.item._count -= countToPut
@ -71,6 +78,7 @@ class FastMoveContainerAction(
break
}
}
}
connection.sendPacket(ContainerClickC2SP(containerId, container.serverRevision, this.slot, 1, 0, container.createAction(this), changes, null))
} finally {

View File

@ -18,7 +18,6 @@ import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.registries.fluid.lava.LavaFluid
import de.bixilon.minosoft.data.registries.item.items.bucket.BucketItem
@Deprecated("ToDo")
object FuelSlotType : SlotType {
override fun canPut(container: Container, slot: Int, stack: ItemStack): Boolean {

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.container.types
import de.bixilon.minosoft.data.container.ContainerUtil.section
import de.bixilon.minosoft.data.container.InventorySynchronizedContainer
import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction
import de.bixilon.minosoft.data.container.slots.DefaultSlotType
@ -26,8 +27,8 @@ import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class CraftingContainer(connection: PlayConnection, type: ContainerType, title: ChatComponent?) : InventorySynchronizedContainer(connection, type, title, (CRAFTING_SLOTS + 1)..(CRAFTING_SLOTS + PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> = arrayOf(0 until 1, 1 until 1 + CRAFTING_SLOTS, 1 + CRAFTING_SLOTS until 1 + CRAFTING_SLOTS + PlayerInventory.MAIN_SLOTS)
class CraftingContainer(connection: PlayConnection, type: ContainerType, title: ChatComponent?) : InventorySynchronizedContainer(connection, type, title, section(CRAFTING_SLOTS + 1, PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> get() = SECTIONS
override fun getSlotType(slotId: Int): SlotType? {
if (slotId == 0) {
@ -64,6 +65,12 @@ class CraftingContainer(connection: PlayConnection, type: ContainerType, title:
override val RESOURCE_LOCATION: ResourceLocation = "minecraft:crafting".toResourceLocation()
override val ALIASES: Set<ResourceLocation> = setOf("minecraft:crafting_table".toResourceLocation())
const val CRAFTING_SLOTS = 3 * 3
val SECTIONS: Array<IntRange> = arrayOf(
// crafting slots are not shift clickable, no section
section(CRAFTING_SLOTS + 1 + PlayerInventory.PASSIVE_SLOTS, PlayerInventory.HOTBAR_SLOTS),
section(CRAFTING_SLOTS + 1, PlayerInventory.PASSIVE_SLOTS),
)
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): CraftingContainer {
return CraftingContainer(connection, type, title)

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.container.types
import de.bixilon.minosoft.data.container.Container
import de.bixilon.minosoft.data.container.ContainerUtil.section
import de.bixilon.minosoft.data.container.InventorySynchronizedContainer
import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction
import de.bixilon.minosoft.data.container.slots.DefaultSlotType
@ -30,8 +31,8 @@ import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class EnchantingContainer(connection: PlayConnection, type: ContainerType, title: ChatComponent?) : InventorySynchronizedContainer(connection, type, title, ENCHANTING_SLOTS until (ENCHANTING_SLOTS + PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> = arrayOf(0 until ENCHANTING_SLOTS, ENCHANTING_SLOTS until ENCHANTING_SLOTS + PlayerInventory.MAIN_SLOTS)
class EnchantingContainer(connection: PlayConnection, type: ContainerType, title: ChatComponent?) : InventorySynchronizedContainer(connection, type, title, section(ENCHANTING_SLOTS, PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> get() = SECTIONS
val costs = IntArray(ENCHANTING_OPTIONS) { -1 }
val enchantments: Array<Enchantment?> = arrayOfNulls(ENCHANTING_OPTIONS)
var enchantmentLevels = IntArray(ENCHANTING_OPTIONS) { -1 }
@ -47,16 +48,6 @@ class EnchantingContainer(connection: PlayConnection, type: ContainerType, title
}
}
override fun getSection(slotId: Int): Int? {
if (slotId in 0..1) {
return 0
}
if (slotId in ENCHANTING_SLOTS..ENCHANTING_SLOTS + PlayerInventory.MAIN_SLOTS) {
return 1
}
return null
}
override fun getSlotSwap(slot: SlotSwapContainerAction.SwapTargets): Int? {
if (slot == SlotSwapContainerAction.SwapTargets.OFFHAND) {
return null // ToDo: It is possible to press F in vanilla, but there is no slot for it
@ -87,6 +78,13 @@ class EnchantingContainer(connection: PlayConnection, type: ContainerType, title
const val ENCHANTING_SLOTS = 2
const val ENCHANTING_OPTIONS = 3
private val SECTIONS: Array<IntRange> = arrayOf(
section(0, ENCHANTING_SLOTS),
section(ENCHANTING_SLOTS + PlayerInventory.PASSIVE_SLOTS, PlayerInventory.HOTBAR_SLOTS),
section(ENCHANTING_SLOTS, PlayerInventory.PASSIVE_SLOTS),
)
override fun build(connection: PlayConnection, type: ContainerType, title: ChatComponent?): EnchantingContainer {
return EnchantingContainer(connection, type, title)
}

View File

@ -155,6 +155,7 @@ class PlayerInventory(connection: PlayConnection) : Container(connection = conne
const val MAIN_SLOTS_PER_ROW = 9
const val MAIN_ROWS = 4
const val PASSIVE_SLOTS = MAIN_SLOTS_PER_ROW * (MAIN_ROWS - 1)
const val MAIN_SLOTS = MAIN_SLOTS_PER_ROW * MAIN_ROWS
const val MAIN_SLOTS_START = ARMOR_OFFSET + 4

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.data.container.types.generic
import de.bixilon.minosoft.data.container.ContainerUtil.section
import de.bixilon.minosoft.data.container.InventorySynchronizedContainer
import de.bixilon.minosoft.data.container.click.SlotSwapContainerAction
import de.bixilon.minosoft.data.container.slots.DefaultSlotType
@ -27,8 +28,12 @@ abstract class GenericContainer(
connection: PlayConnection,
type: ContainerType,
title: ChatComponent?,
) : InventorySynchronizedContainer(connection, type, title, (rows * SLOTS_PER_ROW) until (rows * SLOTS_PER_ROW + PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> = arrayOf(0 until rows * SLOTS_PER_ROW, rows * SLOTS_PER_ROW + 1 until rows * SLOTS_PER_ROW + PlayerInventory.MAIN_SLOTS)
) : InventorySynchronizedContainer(connection, type, title, section(rows * SLOTS_PER_ROW, PlayerInventory.MAIN_SLOTS)) {
override val sections: Array<IntRange> = arrayOf(
section(0, rows * SLOTS_PER_ROW),
section(rows * SLOTS_PER_ROW + PlayerInventory.PASSIVE_SLOTS, PlayerInventory.HOTBAR_SLOTS),
section(rows * SLOTS_PER_ROW, PlayerInventory.PASSIVE_SLOTS),
)
override fun getSlotType(slotId: Int): SlotType? {
if (slotId in 0 until rows * SLOTS_PER_ROW + PlayerInventory.MAIN_SLOTS) {
@ -37,16 +42,6 @@ abstract class GenericContainer(
return null
}
override fun getSection(slotId: Int): Int? {
if (slotId in 0 until rows * SLOTS_PER_ROW) {
return 0
}
if (slotId in rows * SLOTS_PER_ROW until rows * SLOTS_PER_ROW + PlayerInventory.MAIN_SLOTS) {
return 1
}
return null
}
override fun getSlotSwap(slot: SlotSwapContainerAction.SwapTargets): Int? {
if (slot == SlotSwapContainerAction.SwapTargets.OFFHAND) {
return null // ToDo: It is possible to press F in vanilla, but there is no slot for it

View File

@ -37,10 +37,12 @@ abstract class SmeltingContainer(connection: PlayConnection, type: ContainerType
var maxFuel: Int = 0
private set
override val sections: Array<IntRange> get() = SECTIONS
override fun getSlotType(slotId: Int): SlotType? {
if (slotId == 0) {
return DefaultSlotType // ToDo: only smeltable items
return DefaultSlotType // ToDo: only smeltable items (check recipes)
}
if (slotId == 1) {
return FuelSlotType
@ -58,12 +60,15 @@ abstract class SmeltingContainer(connection: PlayConnection, type: ContainerType
if (slotId == 2) {
return 0
}
if (slotId == 0 || slotId == 1) {
if (slotId == 1) {
return 1
}
if (slotId in SMELTING_SLOTS until SMELTING_SLOTS + PlayerInventory.MAIN_SLOTS) {
if (slotId == 0) {
return 2
}
if (slotId in SMELTING_SLOTS until SMELTING_SLOTS + PlayerInventory.MAIN_SLOTS) {
return 3
}
return null
}
@ -86,5 +91,12 @@ abstract class SmeltingContainer(connection: PlayConnection, type: ContainerType
companion object {
const val SMELTING_SLOTS = 3
val SECTIONS: Array<IntRange> = arrayOf(
2..2,
1..1,
0..0,
SMELTING_SLOTS..SMELTING_SLOTS + PlayerInventory.MAIN_SLOTS,
)
}
}