Move terminal stack requesting logic to TerminalScreen

This commit is contained in:
Shadowfacts 2021-02-21 12:13:06 -05:00
parent f9befe9549
commit 7abdb69b87
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 26 additions and 61 deletions

View File

@ -56,7 +56,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL),
private var observers = 0 private var observers = 0
val cachedNetItems = ItemStackCollections.intMap() val cachedNetItems = ItemStackCollections.intMap()
// var cachedSortedNetItems = listOf<ItemStack>()
var netItemObserver: WeakReference<NetItemObserver>? = null var netItemObserver: WeakReference<NetItemObserver>? = null
@ -130,12 +129,6 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL),
cachedNetItems.mergeInt(stack, amount) { a, b -> a + b } 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() { private fun beginInsertions() {
@ -259,31 +252,11 @@ class TerminalBlockEntity: DeviceBlockEntity(PhyBlockEntities.TERMINAL),
override fun toClientTag(tag: CompoundTag): CompoundTag { override fun toClientTag(tag: CompoundTag): CompoundTag {
tag.put("InternalBuffer", internalBuffer.toTag()) 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 return tag
} }
override fun fromClientTag(tag: CompoundTag) { override fun fromClientTag(tag: CompoundTag) {
internalBuffer.fromTag(tag.getCompound("InternalBuffer")) 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 { interface NetItemObserver {

View File

@ -17,12 +17,14 @@ import net.minecraft.text.LiteralText
import net.minecraft.text.Text import net.minecraft.text.Text
import net.minecraft.util.Identifier import net.minecraft.util.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.networking.C2STerminalRequestItem
import net.shadowfacts.phycon.networking.C2STerminalUpdateDisplayedItems import net.shadowfacts.phycon.networking.C2STerminalUpdateDisplayedItems
import net.shadowfacts.phycon.util.SortMode import net.shadowfacts.phycon.util.SortMode
import org.lwjgl.glfw.GLFW import org.lwjgl.glfw.GLFW
import java.lang.NumberFormatException import java.lang.NumberFormatException
import kotlin.math.ceil import kotlin.math.ceil
import kotlin.math.floor import kotlin.math.floor
import kotlin.math.min
/** /**
* @author shadowfacts * @author shadowfacts
@ -229,10 +231,22 @@ class TerminalScreen(handler: TerminalScreenHandler, playerInv: PlayerInventory,
// don't unfocus the search box on mouse click // don't unfocus the search box on mouse click
searchBox.setSelected(true) searchBox.setSelected(true)
if (type == SlotActionType.PICKUP && clickData == 0 && slot != null && handler.isNetworkSlot(slot.id) && !slot.stack.isEmpty) { if (slot != null && !slot.stack.isEmpty && handler.isNetworkSlot(slot.id)) {
dialogStack = slot.stack val stack = slot.stack
showingAmountDialog = true
searchBox.setSelected(false) 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() { private fun doDialogRequest() {
showingAmountDialog = false 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 private var TextFieldWidget.intValue: Int

View File

@ -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 { override fun onSlotClick(slotId: Int, clickData: Int, actionType: SlotActionType, player: PlayerEntity): ItemStack {
if (isNetworkSlot(slotId)) { if (isBufferSlot(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
// todo: why does this think it's quick_craft sometimes? // todo: why does this think it's quick_craft sometimes?
if ((actionType == SlotActionType.PICKUP || actionType == SlotActionType.QUICK_CRAFT) && !player.inventory.cursorStack.isEmpty) { if ((actionType == SlotActionType.PICKUP || actionType == SlotActionType.QUICK_CRAFT) && !player.inventory.cursorStack.isEmpty) {
// placing cursor stack into buffer // 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) 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 { override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack {
if (isNetworkSlot(slotId)) { if (isNetworkSlot(slotId)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;