Skip to content

Commit

Permalink
🐛 Fixed throwing items when clicking on buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiviacz1337 committed Dec 28, 2023
1 parent 3a9e6db commit 08e6746
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -46,15 +46,15 @@ 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);
this.screen.children().stream().filter(w -> w instanceof WidgetBase).filter(w -> ((WidgetBase) w).isSettingsChild()).forEach(w -> ((WidgetBase) w).setVisible(true));
this.screen.playUIClickSound();
return true;
}
else if(isHovered)
else if(isMouseOver(pMouseX, pMouseY))
{
this.isWidgetActive = false;
if(!TravelersBackpackConfig.disableCrafting) this.screen.craftingWidget.setVisible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text> component = new ArrayList<>();
Expand Down Expand Up @@ -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)
{
Expand All @@ -105,7 +107,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int pButton)
}
}

if(isHovered)
if(isMouseOver(mouseX, mouseY))
{
setWidgetStatus(!this.isWidgetActive);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 08e6746

Please sign in to comment.