From 08e6746943149260dd0d638db12684afdb870b61 Mon Sep 17 00:00:00 2001 From: Kamil <34790800+Tiviacz1337@users.noreply.github.com> Date: Thu, 28 Dec 2023 18:28:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20throwing=20items=20when?= =?UTF-8?q?=20clicking=20on=20buttons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TravelersBackpackHandledScreen.java | 21 +++++++++++++++++++ .../client/screen/widget/ControlTab.java | 1 - .../client/screen/widget/CraftingWidget.java | 8 +++---- .../client/screen/widget/MemoryWidget.java | 6 ++++-- .../client/screen/widget/SettingsWidget.java | 6 +++--- .../client/screen/widget/SortWidget.java | 8 ++++--- .../client/screen/widget/ToolSlotsWidget.java | 2 +- .../client/screen/widget/WidgetBase.java | 11 ++++++---- 8 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/TravelersBackpackHandledScreen.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/TravelersBackpackHandledScreen.java index 23719d81e..6936b739f 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/TravelersBackpackHandledScreen.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/TravelersBackpackHandledScreen.java @@ -19,6 +19,7 @@ import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Element; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.player.PlayerInventory; @@ -435,6 +436,26 @@ public void drawMemoryOverlay(DrawContext context, int x, int y) context.getMatrices().pop(); } + @Override + public boolean mouseReleased(double pMouseX, double pMouseY, int pButton) + { + int i = this.x; + int j = this.y; + boolean bl = this.isClickOutsideBounds(pMouseX, pMouseY, i, j, pButton); + + if(bl && !this.handler.getCursorStack().isEmpty()) + { + for(Element widget : children()) + { + if(widget instanceof WidgetBase base) + { + if(base.isMouseOver(pMouseX, pMouseY)) return false; + } + } + } + return super.mouseReleased(pMouseX, pMouseY, pButton); + } + @Override protected void onMouseClick(Slot slot, int slotId, int button, SlotActionType actionType) { diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ControlTab.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ControlTab.java index 3b2066197..d6e5748a7 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ControlTab.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ControlTab.java @@ -56,7 +56,6 @@ void drawBackground(DrawContext context, MinecraftClient minecraft, int mouseX, public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { super.render(context, mouseX, mouseY, partialTicks); - isHovered = false; } @Override diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/CraftingWidget.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/CraftingWidget.java index 257607407..50c267685 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/CraftingWidget.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/CraftingWidget.java @@ -26,8 +26,6 @@ public CraftingWidget(TravelersBackpackHandledScreen screen, int x, int y, int w @Override public void render(DrawContext drawContext, int mouseX, int mouseY, float partialTicks) { - isHovered = mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height; - if(zOffset != 0) { drawContext.getMatrices().push(); @@ -78,7 +76,7 @@ protected void drawBackground(DrawContext drawContext, MinecraftClient minecraft @Override public void drawMouseoverTooltip(DrawContext drawContext, int mouseX, int mouseY) { - if(isHovered && showTooltip && isVisible) + if(isMouseOver(mouseX, mouseY) && showTooltip && isVisible) { if(!isWidgetActive()) { @@ -114,7 +112,9 @@ public void setWidgetStatus(boolean status) @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - if(isHovered) + if(this.screen.settingsWidget.isWidgetActive()) return false; + + if(isMouseOver(mouseX, mouseY)) { if(this.isWidgetActive) { diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/MemoryWidget.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/MemoryWidget.java index d81ef8024..d2337de71 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/MemoryWidget.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/MemoryWidget.java @@ -38,7 +38,7 @@ protected void drawBackground(DrawContext context, MinecraftClient minecraft, in @Override public void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) { - if(isHovered && showTooltip) + if(isMouseOver(mouseX, mouseY) && showTooltip) { context.drawTooltip(screen.getTextRenderer(), TextUtils.getTranslatedSplittedText("screen.travelersbackpack.memory", null), mouseX, mouseY); //String[] s = I18n.translate("screen.travelersbackpack.memory").split("\n"); @@ -56,12 +56,14 @@ public void setWidgetStatus(boolean status) @Override public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) { + if(!this.screen.settingsWidget.isWidgetActive()) return false; + if(screen.inventory.getSlotManager().isSelectorActive(SlotManager.UNSORTABLE)) { return false; } - if(isHovered) + if(isMouseOver(pMouseX, pMouseY)) { setWidgetStatus(!this.isWidgetActive); diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SettingsWidget.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SettingsWidget.java index 8e2b45227..b41c4e661 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SettingsWidget.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SettingsWidget.java @@ -30,7 +30,7 @@ protected void drawBackground(DrawContext context, MinecraftClient minecraft, in @Override public void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) { - if(isHovered && showTooltip) + if(isMouseOver(mouseX, mouseY) && showTooltip) { if(isWidgetActive()) { @@ -46,7 +46,7 @@ public void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) @Override public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) { - if(isHovered && !this.isWidgetActive) + if(isMouseOver(pMouseX, pMouseY) && !this.isWidgetActive) { this.isWidgetActive = true; if(!TravelersBackpackConfig.disableCrafting) this.screen.craftingWidget.setVisible(false); @@ -54,7 +54,7 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) this.screen.playUIClickSound(); return true; } - else if(isHovered) + else if(isMouseOver(pMouseX, pMouseY)) { this.isWidgetActive = false; if(!TravelersBackpackConfig.disableCrafting) this.screen.craftingWidget.setVisible(true); diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SortWidget.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SortWidget.java index 65fe5d0ea..e9fed7b6e 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SortWidget.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/SortWidget.java @@ -47,7 +47,7 @@ protected void drawBackground(DrawContext context, MinecraftClient minecraft, in @Override public void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY) { - if(isHovered && showTooltip) + if(isMouseOver(mouseX, mouseY) && showTooltip) { //String[] s = I18n.translate("screen.travelersbackpack.unsortable").split("\n"); //List component = new ArrayList<>(); @@ -78,12 +78,14 @@ public void setWidgetStatus(boolean status) @Override public boolean mouseClicked(double mouseX, double mouseY, int pButton) { + if(!this.screen.settingsWidget.isWidgetActive()) return false; + if(screen.inventory.getSlotManager().isSelectorActive(SlotManager.MEMORY)) { return false; } - if(isHovered && this.isWidgetActive) + if(isMouseOver(mouseX, mouseY) && this.isWidgetActive) { if(mouseX >= x + 1 && mouseY >= y + 15 && mouseX < x + 11 && mouseY < y + 25) { @@ -105,7 +107,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int pButton) } } - if(isHovered) + if(isMouseOver(mouseX, mouseY)) { setWidgetStatus(!this.isWidgetActive); diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ToolSlotsWidget.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ToolSlotsWidget.java index 4c502c2ae..8d82bd6a4 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ToolSlotsWidget.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/ToolSlotsWidget.java @@ -48,7 +48,7 @@ public boolean isSettingsChild() @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - if(isHovered) + if(isMouseOver(mouseX, mouseY)) { boolean showToolSlots = screen.inventory.getSettingsManager().showToolSlots(); screen.inventory.getSettingsManager().set(SettingsManager.TOOL_SLOTS, SettingsManager.SHOW_TOOL_SLOTS, (byte)(showToolSlots ? 0 : 1)); diff --git a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/WidgetBase.java b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/WidgetBase.java index 29ae9faee..b8e1eb091 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/WidgetBase.java +++ b/src/main/java/com/tiviacz/travelersbackpack/client/screen/widget/WidgetBase.java @@ -17,7 +17,6 @@ public abstract class WidgetBase implements Drawable, Element, Selectable protected int zOffset = 0; protected int width; protected int height; - protected boolean isHovered; protected boolean isWidgetActive = false; protected boolean isVisible; protected boolean showTooltip; @@ -34,8 +33,6 @@ public WidgetBase(TravelersBackpackHandledScreen screen, int x, int y, int width @Override public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { - isHovered = mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height; - if(zOffset != 0) { context.getMatrices().push(); @@ -56,10 +53,16 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic abstract void drawMouseoverTooltip(DrawContext context, int mouseX, int mouseY); + @Override + public boolean isMouseOver(double mouseX, double mouseY) + { + return mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height; + } + @Override public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) { - if(isHovered) + if(isMouseOver(pMouseX, pMouseY)) { setWidgetStatus(!this.isWidgetActive); this.screen.playUIClickSound();