From 7abdb69b8779c63aadd0473a788baf29446e929a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 21 Feb 2021 12:13:06 -0500 Subject: [PATCH] Move terminal stack requesting logic to TerminalScreen --- .../block/terminal/TerminalBlockEntity.kt | 27 ----------------- .../network/block/terminal/TerminalScreen.kt | 30 +++++++++++++++---- .../block/terminal/TerminalScreenHandler.kt | 30 +------------------ 3 files changed, 26 insertions(+), 61 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt index 3d71587..e17af71 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalBlockEntity.kt @@ -56,7 +56,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), private var observers = 0 val cachedNetItems = ItemStackCollections.intMap() -// var cachedSortedNetItems = listOf() var netItemObserver: WeakReference? = null @@ -130,12 +129,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), cachedNetItems.mergeInt(stack, amount) { a, b -> a + b } } } -// // todo: is the map necessary or is just the sorted list enough? -// cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map { -// val stack = it.key.copy() -// stack.count = it.intValue -// stack -// } } private fun beginInsertions() { @@ -259,31 +252,11 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL), override fun toClientTag(tag: CompoundTag): CompoundTag { tag.put("InternalBuffer", internalBuffer.toTag()) -// val list = ListTag() -// tag.put("CachedNetItems", list) -// for ((stack, amount) in cachedNetItems) { -// val entryTag = stack.toTag(CompoundTag()) -// entryTag.putInt("NetAmount", amount) -// list.add(entryTag) -// } return tag } override fun fromClientTag(tag: CompoundTag) { internalBuffer.fromTag(tag.getCompound("InternalBuffer")) -// val list = tag.getList("CachedNetItems", 10) -// cachedNetItems.clear() -// for (entryTag in list) { -// val stack = ItemStack.fromTag(entryTag as CompoundTag) -// val netAmount = entryTag.getInt("NetAmount") -// cachedNetItems[stack] = netAmount -// } -// netItemObserver?.get()?.netItemsChanged() -// cachedSortedNetItems = cachedNetItems.object2IntEntrySet().sortedByDescending { it.intValue }.map { -// val stack = it.key.copy() -// stack.count = it.intValue -// stack -// } } interface NetItemObserver { diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreen.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreen.kt index 6dd5ee6..e245942 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreen.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreen.kt @@ -17,12 +17,14 @@ import net.minecraft.text.LiteralText import net.minecraft.text.Text import net.minecraft.util.Identifier import net.shadowfacts.phycon.PhysicalConnectivity +import net.shadowfacts.phycon.networking.C2STerminalRequestItem import net.shadowfacts.phycon.networking.C2STerminalUpdateDisplayedItems import net.shadowfacts.phycon.util.SortMode import org.lwjgl.glfw.GLFW import java.lang.NumberFormatException import kotlin.math.ceil import kotlin.math.floor +import kotlin.math.min /** * @author shadowfacts @@ -229,10 +231,22 @@ class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory, // don't unfocus the search box on mouse click searchBox.setSelected(true) - if (type == SlotActionType.PICKUP && clickData == 0 && slot != null && handler.isNetworkSlot(slot.id) && !slot.stack.isEmpty) { - dialogStack = slot.stack - showingAmountDialog = true - searchBox.setSelected(false) + if (slot != null && !slot.stack.isEmpty && handler.isNetworkSlot(slot.id)) { + val stack = slot.stack + + if (type == SlotActionType.QUICK_MOVE) { + // shift click, request full stack + requestItem(stack, min(stack.count, stack.maxCount)) + } else if (type == SlotActionType.PICKUP) { + if (clickData == 1) { + // right click, request half stack + requestItem(stack, ceil(min(stack.count, stack.maxCount) / 2f).toInt()) + } else { + dialogStack = stack + showingAmountDialog = true + searchBox.setSelected(false) + } + } } } @@ -329,7 +343,13 @@ class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory, private fun doDialogRequest() { showingAmountDialog = false - handler.requestItem(client!!.player!!, dialogStack, amountBox.intValue) + requestItem(dialogStack, amountBox.intValue) + } + + private fun requestItem(stack: ItemStack, amount: Int) { + val netHandler = MinecraftClient.getInstance().player!!.networkHandler + val packet = C2STerminalRequestItem(handler.terminal, stack, amount) + netHandler.sendPacket(packet) } private var TextFieldWidget.intValue: Int diff --git a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreenHandler.kt index 33756e7..e361c85 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/network/block/terminal/TerminalScreenHandler.kt @@ -137,28 +137,7 @@ class TerminalScreenHandler(syncId: Int, val playerInv: PlayerInventory, val ter } override fun onSlotClick(slotId: Int, clickData: Int, actionType: SlotActionType, player: PlayerEntity): ItemStack { - if (isNetworkSlot(slotId)) { - // the slot clicked was one of the network stacks - if (actionType == SlotActionType.QUICK_MOVE) { - val stack = slots[slotId].stack - if (!stack.isEmpty && player.world.isClient) { - requestItem(player, stack, min(stack.count, stack.maxCount)) - - } - } else if (actionType == SlotActionType.PICKUP && clickData == 1) { - if (clickData == 1) { - // right click, request half stack - val stack = slots[slotId].stack - if (!stack.isEmpty && player.world.isClient) { - requestItem(player, stack, ceil(min(stack.count, stack.maxCount) / 2f).toInt()) - } - } else { - // todo: left click, show amount dialog - } - } - return ItemStack.EMPTY - } else if (isBufferSlot(slotId)) { - // internal buffer + if (isBufferSlot(slotId)) { // todo: why does this think it's quick_craft sometimes? if ((actionType == SlotActionType.PICKUP || actionType == SlotActionType.QUICK_CRAFT) && !player.inventory.cursorStack.isEmpty) { // placing cursor stack into buffer @@ -169,13 +148,6 @@ class TerminalScreenHandler(syncId: Int, val playerInv: PlayerInventory, val ter return super.onSlotClick(slotId, clickData, actionType, player) } - fun requestItem(player: PlayerEntity, stack: ItemStack, amount: Int) { - if (!player.world.isClient) return - val handler = (player as ClientPlayerEntity).networkHandler - val packet = C2STerminalRequestItem(terminal, stack, amount) - handler.sendPacket(packet) - } - override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack { if (isNetworkSlot(slotId)) { return ItemStack.EMPTY;