From 55e77d9929dbdf228daa767b094a85011b0ea4f7 Mon Sep 17 00:00:00 2001 From: Firenoo <49818773+firenoo@users.noreply.github.com> Date: Sat, 13 May 2023 09:21:07 -0700 Subject: [PATCH] Fix CME from resizing GUI, and add patterns to drops when changing P2P interface type CME caused by iterating over the button list. Clicking on resize button forces the GUI to reset, so the button list changes and causes the CME. P2P interfaces now drop their items if changing their type from the GUI. --- .../client/gui/GuiAdvancedMemoryCard.kt | 27 +++++++++++++------ .../client/gui/widget/WidgetButton.kt | 2 +- .../betterp2p/util/p2p/P2PUtil.kt | 9 ++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt index 6053cf4..12f1803 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/GuiAdvancedMemoryCard.kt @@ -97,7 +97,7 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { ::selectedInfo, ::mode, scrollBar) searchBar = MEGuiTextField(100, 10) resizeButton = object: WidgetButton(this, 0, 0, 32, 32) { - override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int): Boolean { if (super.mousePressed(mc, mouseX, mouseY)) { if (button == 0) { scale = when (scale) { @@ -116,7 +116,9 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { } initGui() super.func_146113_a(mc.soundHandler) + return true } + return false } } @@ -148,18 +150,20 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { hoverText = modeDescriptions[mode.ordinal] } - override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int): Boolean { if (super.mousePressed(mc, mouseX, mouseY)) { mode = when (button) { 0 -> mode.next() 1 -> mode.next(true) - else -> return + else -> return false } hoverText = modeDescriptions[mode.ordinal] setTexCoords((mode.ordinal + 3) * 32.0, 232.0) syncMemoryInfo() super.func_146113_a(mc.soundHandler) + return true } + return false } } @@ -197,17 +201,19 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { } } - override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int): Boolean { if (super.mousePressed(mc, mouseX, mouseY)) { if (button == 1) { // open type selector? openTypeSelector(this, true) - return + return true } else { type = nextType(false) } commitType() + return true } + return false } override fun draw(mc: Minecraft, mouseX: Int, mouseY: Int, partialTicks: Float) { @@ -266,10 +272,12 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { } refreshButton = object: WidgetButton(this, 0, 0, 32, 32) { - override fun mousePressed(mouseX: Int, mouseY: Int, button: Int) { + override fun mousePressed(mouseX: Int, mouseY: Int, button: Int): Boolean { if (super.mousePressed(mc, mouseX, mouseY)) { ModNetwork.channel.sendToServer(C2SRefreshP2PList(type?.index ?: TUNNEL_ANY)) + return true } + return false } } @@ -471,8 +479,11 @@ class GuiAdvancedMemoryCard(msg: S2CListP2P) : GuiScreen(), TextureBound { } col.mouseClicked(mouseX, mouseY, mouseButton) scrollBar.click(mouseX, mouseY) - buttonList.forEach {it as WidgetButton - it.mousePressed(mouseX, mouseY, mouseButton) + for (button in buttonList) { + button as WidgetButton + if (button.mousePressed(mouseX, mouseY, mouseButton)) { + return + } } searchBar.mouseClicked(mouseX, mouseY, mouseButton) if (mouseButton == 1 && searchBar.isMouseIn(mouseX, mouseY)) { diff --git a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt index 516eb10..d7f90e1 100644 --- a/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt +++ b/src/main/java/com/projecturanus/betterp2p/client/gui/widget/WidgetButton.kt @@ -25,7 +25,7 @@ abstract class WidgetButton(val gui: GuiAdvancedMemoryCard, x: Int, y: Int, widt private var texY = 0.0 var hoverText: List = listOf() - abstract fun mousePressed(mouseX: Int, mouseY: Int, button: Int) + abstract fun mousePressed(mouseX: Int, mouseY: Int, button: Int): Boolean open fun draw(mc: Minecraft, mouseX: Int, mouseY: Int, partialTicks: Float) { val tessellator = Tessellator.instance diff --git a/src/main/java/com/projecturanus/betterp2p/util/p2p/P2PUtil.kt b/src/main/java/com/projecturanus/betterp2p/util/p2p/P2PUtil.kt index 19f31b4..046c4f8 100644 --- a/src/main/java/com/projecturanus/betterp2p/util/p2p/P2PUtil.kt +++ b/src/main/java/com/projecturanus/betterp2p/util/p2p/P2PUtil.kt @@ -36,7 +36,7 @@ fun linkP2P(player: EntityPlayer, inputIndex: Long, outputIndex: Long, status: P // TODO Change to exception if (input.javaClass != output.javaClass) { //change output to input - changeP2P(output, BetterP2P.proxy.getP2PFromClass(input.javaClass)!!, player) + output = changeP2P(output, BetterP2P.proxy.getP2PFromClass(input.javaClass)!!, player) ?: return null } if (input == output) { // Network loop @@ -130,6 +130,13 @@ fun changeP2P(tunnel: PartP2PTunnel<*>, newType: TunnelInfo, player: EntityPlaye if (BetterP2P.proxy.getP2PFromClass(tunnel.javaClass) == newType) { return null } + if (tunnel is PartP2PInterface) { + // For output, drop items. + val dropItems = mutableListOf() + val patternsOut = tunnel.interfaceDuality.patterns as AppEngInternalInventory + dropItems.addAll(patternsOut) + Platform.spawnDrops(player.worldObj, tunnel.location.x, tunnel.location.y, tunnel.location.z, dropItems) + } val host = tunnel.host host.removePart(tunnel.side, false) val dir = host.addPart(newType.stack, tunnel.side, player)