Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ org.gradle.parallel=true
# Fabric Properties
# check these on https://fabricmc.net/develop

minecraft_version=1.21.8
yarn_mappings=1.21.8+build.1
loader_version=0.17.2
minecraft_version=1.21.10
yarn_mappings=1.21.10+build.2
loader_version=0.17.3
loom_version=1.11-SNAPSHOT

# Mod Properties
mod_version=1.1.4+1.21.8
mod_version=1.1.5+1.21.10
maven_group=borknbeans.lightweightinventorysorting
archives_base_name=lightweight-inventory-sorting

# Dependencies
fabric_version=0.132.0+1.21.8
fabric_version=0.136.0+1.21.10
modmenu_version=15.0.0-beta.3
cloth_version=19.0.147
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemGroups;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

import java.util.*;
Expand All @@ -27,11 +28,12 @@ public void onInitializeClient() {
}

private void registerKeyBindings() {
KeyBinding.Category titleCategory = KeyBinding.Category.create(Identifier.of("category.lightweight-inventory-sorting.title"));
sortKeyBind = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.lightweight-inventory-sorting.sort",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"category.lightweight-inventory-sorting.title"
titleCategory
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import borknbeans.lightweightinventorysorting.LightweightInventorySortingClient;
import borknbeans.lightweightinventorysorting.config.Config;
import borknbeans.lightweightinventorysorting.sorting.SortButton;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -47,20 +50,22 @@ private void onRender(DrawContext context, int mouseX, int mouseY, float delta,
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyCode, scanCode)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean keyPressed(KeyInput keyInput) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyInput)) {
sortButton.onPress(new MouseInput(0,0));
return true;
}

return super.keyPressed(keyCode, scanCode, modifiers);
return super.keyPressed(keyInput);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(button)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean mouseClicked(Click click, boolean doubleClick) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(click)) {
sortButton.simulateClick(click); // Simulate a click
return true;
}

return super.mouseClicked(mouseX, mouseY, button);
return super.mouseClicked(click, doubleClick);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package borknbeans.lightweightinventorysorting.mixin.client;

import borknbeans.lightweightinventorysorting.LightweightInventorySortingClient;
import borknbeans.lightweightinventorysorting.config.Config;
import borknbeans.lightweightinventorysorting.sorting.SortButton;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.HorseScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.HorseScreenHandler;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(HorseScreen.class)
public abstract class HorseScreenMixin extends HandledScreen<HorseScreenHandler> {
@Unique
private SortButton sortButton;

public HorseScreenMixin(HorseScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
}

@Override
public void init() {
super.init();

// Initialize button
int x = this.x + this.backgroundWidth - 20 + Config.xOffsetContainer;
int y = this.y + 4 + Config.yOffsetContainer;
int size = Config.buttonSize.getButtonSize();
sortButton = new SortButton(x, y, size, size, Text.literal("S"), 0, getScreenHandler().slots.size() - 37);

// Add button to the screen
this.addDrawableChild(sortButton);
}

@Inject(method = "render", at = @At("RETURN"))
private void onRender(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (sortButton != null) {
sortButton.render(context, mouseX, mouseY, delta);
}
}

@Override
public boolean keyPressed(KeyInput keyInput) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyInput)) {
sortButton.onPress(new MouseInput(0,0));
return true;
}

return super.keyPressed(keyInput);
}

@Override
public boolean mouseClicked(Click click, boolean doubleClick) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(click)) {
sortButton.simulateClick(click); // Simulate a click
return true;
}

return super.mouseClicked(click, doubleClick);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
import borknbeans.lightweightinventorysorting.LightweightInventorySortingClient;
import borknbeans.lightweightinventorysorting.config.Config;
import borknbeans.lightweightinventorysorting.sorting.SortButton;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -43,24 +47,28 @@ private void onRender(DrawContext context, int mouseX, int mouseY, float delta,
}
}

// This override is NOT an ideal solution as it could lead to conflicts with other mods
// Updated: uses KeyInput instead of (int, int, int)
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyCode, scanCode)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean keyPressed(KeyInput keyInput) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyInput)) {
sortButton.onPress(new MouseInput(0,0)); // Simulate a click
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
return super.keyPressed(keyInput);
}

// Updated: uses Click + boolean doubleClick
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(button)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean mouseClicked(Click click, boolean doubleClick) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(click)) {
sortButton.simulateClick(click); // Simulate a click
return true;
}

return super.mouseClicked(mouseX, mouseY, button);
return super.mouseClicked(click, doubleClick);
}

@Unique
private void setButtonCoordinates() {
sortButton.setX(this.x + this.backgroundWidth - 20 + Config.xOffsetInventory);
sortButton.setY(this.height / 2 - 15 + Config.yOffsetInventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import borknbeans.lightweightinventorysorting.LightweightInventorySortingClient;
import borknbeans.lightweightinventorysorting.config.Config;
import borknbeans.lightweightinventorysorting.sorting.SortButton;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.screen.ingame.ShulkerBoxScreen;
import net.minecraft.client.input.KeyInput;
import net.minecraft.client.input.MouseInput;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.ShulkerBoxScreenHandler;
import net.minecraft.text.Text;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;


@Mixin(ShulkerBoxScreen.class)
public abstract class ShulkerBoxScreenMixin extends HandledScreen<ShulkerBoxScreenHandler> {
@Unique
Expand Down Expand Up @@ -45,21 +51,24 @@ private void onRender(DrawContext context, int mouseX, int mouseY, float delta,
}
}

// Updated to new signature
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyCode, scanCode)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean keyPressed(KeyInput keyInput) {
if (LightweightInventorySortingClient.sortKeyBind.matchesKey(keyInput)) {
sortButton.onPress(new MouseInput(0,0));
return true;
}

return super.keyPressed(keyCode, scanCode, modifiers);
return super.keyPressed(keyInput);
}

// Updated to new signature
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(button)) {
sortButton.onClick(0f, 0f); // Simulate a click
public boolean mouseClicked(Click click, boolean doubleClick) {
if (LightweightInventorySortingClient.sortKeyBind.matchesMouse(click)) {
sortButton.simulateClick(click); // Simulate a click
}

return super.mouseClicked(mouseX, mouseY, button);
return super.mouseClicked(click, doubleClick);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import borknbeans.lightweightinventorysorting.config.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.SlotActionType;
Expand All @@ -28,6 +27,15 @@ public ClickOperation(MinecraftClient client, int syncId, int targetSlot, ItemSt
this.expectedEndingMouseStack = expectedEndingMouseStack;
}

// Custom exception to signal that a click was intentionally skipped
public static class SkippedUnsafeClickException extends Exception {
public final int slot;
public SkippedUnsafeClickException(int slot) {
super("Skipped unsafe click on slot " + slot);
this.slot = slot;
}
}

public void execute() throws Exception {
if (client.player == null) {
throw new Exception("Player is null");
Expand All @@ -43,6 +51,12 @@ public void execute() throws Exception {
throw new Exception("[Target: " + targetSlot + "] Starting target stack is not what we expected: (ACTUAL)" + getItemStackString(startingTargetStack) + " != (EXPECTED)" + getItemStackString(expectedStartingTargetStack));
}

// Safety guard:
if (!canSafelyClick(startingMouseStack)) {
throw new SkippedUnsafeClickException(targetSlot);
}


click();

Exception error = null;
Expand Down Expand Up @@ -88,4 +102,27 @@ private void postClickVerification() throws Exception{
private String getItemStackString(ItemStack stack) {
return String.format("%dx %s", stack.getCount(), stack.getItem().getName().getString());
}

private boolean canSafelyClick(ItemStack mouseStack) {
if (client.player == null) return false;

var handler = client.player.currentScreenHandler;
if (targetSlot < 0 || targetSlot >= handler.slots.size()) return false;

var slot = handler.getSlot(targetSlot);
if (slot == null || !slot.isEnabled()) return false;

// Equipment / non-insertable slots will often reject generic items
// Check both directions: whether we can place *into* this slot OR pick up from it.
if (!slot.canTakeItems(client.player) && !slot.hasStack()) return false;

// If holding something, make sure this slot can accept it.
if (!mouseStack.isEmpty() && !slot.canInsert(mouseStack)) return false;

// If this slot has special restrictions (e.g. saddle/armor), skip it.
if (slot.getMaxItemCount() <= 0) return false;

return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package borknbeans.lightweightinventorysorting.sorting;

import com.mojang.blaze3d.pipeline.RenderPipeline;

import borknbeans.lightweightinventorysorting.LightweightInventorySorting;
import borknbeans.lightweightinventorysorting.config.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.gui.Click;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.gui.widget.PressableWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class SortButton extends ClickableWidget {
public class SortButton extends PressableWidget {

private Identifier buttonTexture;
private Identifier buttonHoverTexture;
private final Identifier buttonTexture;
private final Identifier buttonHoverTexture;

private int sortStartIndex, sortEndIndex;
private final int sortStartIndex, sortEndIndex;

public SortButton(int x, int y, int width, int height, Text message, int startIndex, int endIndex) {
super(x, y, width, height, message);
Expand All @@ -45,7 +43,7 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
}

@Override
public void onClick(double mouseX, double mouseY) {
public void onPress(net.minecraft.client.input.AbstractInput input) {
MinecraftClient client = MinecraftClient.getInstance();

if (client.player != null) {
Expand All @@ -55,5 +53,11 @@ public void onClick(double mouseX, double mouseY) {
LightweightInventorySorting.LOGGER.error("Player is not available.");
}
}

public void simulateClick(Click click){
// For simplicity, ignore coordinates and button type, just trigger onPress
// Convert Click → AbstractInput for onPress
this.onPress(click.buttonInfo());
}

}
Loading