Skip to content

Commit 0197465

Browse files
committed
Simple sort by Name
1 parent e9d877f commit 0197465

File tree

7 files changed

+152
-45
lines changed

7 files changed

+152
-45
lines changed
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
package net.anvian.smartinventory;
22

3-
import net.fabricmc.api.ModInitializer;
4-
3+
import net.anvian.smartinventory.event.KeyInput;
4+
import net.fabricmc.api.ClientModInitializer;
5+
import net.minecraft.client.MinecraftClient;
6+
import net.minecraft.client.gui.screen.Screen;
7+
import net.minecraft.client.network.ClientPlayerEntity;
8+
import net.minecraft.screen.ScreenHandler;
59
import org.slf4j.Logger;
610
import org.slf4j.LoggerFactory;
711

8-
public class SmartInventory implements ModInitializer {
9-
// This logger is used to write text to the console and the log file.
10-
// It is considered best practice to use your mod id as the logger's name.
11-
// That way, it's clear which mod wrote info, warnings, and errors.
12-
public static final Logger LOGGER = LoggerFactory.getLogger("smartinventory");
12+
public class SmartInventory implements ClientModInitializer {
13+
public static final String MOD_ID = "smartinventory";
14+
public static final String MOD_NAME = "Smart Inventory";
15+
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);
16+
17+
18+
@Override
19+
public void onInitializeClient() {
20+
LOGGER.info("Hello from " + MOD_NAME + "!");
21+
22+
KeyInput.register();
23+
}
24+
25+
public static MinecraftClient getClient() {
26+
return MinecraftClient.getInstance();
27+
}
28+
29+
public static ClientPlayerEntity getPlayer() {
30+
return MinecraftClient.getInstance().player;
31+
}
1332

14-
@Override
15-
public void onInitialize() {
16-
// This code runs as soon as Minecraft is in a mod-load-ready state.
17-
// However, some things (like resources) may still be uninitialized.
18-
// Proceed with mild caution.
33+
public static Screen getScreen() {
34+
return MinecraftClient.getInstance().currentScreen;
35+
}
1936

20-
LOGGER.info("Hello Fabric world!");
21-
}
37+
public static ScreenHandler getScreenHandler() {
38+
assert MinecraftClient.getInstance().player != null;
39+
return MinecraftClient.getInstance().player.currentScreenHandler;
40+
}
2241
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.anvian.smartinventory.event;
2+
3+
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
4+
import net.minecraft.client.option.KeyBinding;
5+
import net.minecraft.client.util.InputUtil;
6+
import org.lwjgl.glfw.GLFW;
7+
8+
public class KeyInput {
9+
public static KeyBinding keyBinding;
10+
11+
public static void register() {
12+
keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
13+
"Sort Inventory Key",
14+
InputUtil.Type.KEYSYM,
15+
GLFW.GLFW_KEY_B,
16+
"Smart Inventory"
17+
));
18+
}
19+
}

src/main/java/net/anvian/smartinventory/mixin/ExampleMixin.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.anvian.smartinventory.mixin;
2+
3+
import net.anvian.smartinventory.SmartInventory;
4+
import net.anvian.smartinventory.event.KeyInput;
5+
import net.anvian.smartinventory.sort.SortInventory;
6+
import net.minecraft.client.gui.screen.ingame.HandledScreen;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
11+
12+
@Mixin(HandledScreen.class)
13+
public class MixinKeyInputHandler {
14+
@Inject(method = "keyPressed", at = @At("HEAD"))
15+
private void onKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
16+
if (keyCode == KeyInput.keyBinding.getDefaultKey().getCode()) {
17+
SortInventory.sort(SmartInventory.getClient());
18+
}
19+
}
20+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.anvian.smartinventory.sort;
2+
3+
import net.minecraft.client.MinecraftClient;
4+
import net.minecraft.entity.player.PlayerEntity;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.text.Text;
7+
import net.minecraft.util.collection.DefaultedList;
8+
9+
import java.util.Comparator;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
public class SortInventory {
16+
private static final int HOTBAR_SIZE = 9;
17+
18+
public static void sort(MinecraftClient client) {
19+
PlayerEntity player = client.player;
20+
if (player == null) return;
21+
22+
DefaultedList<ItemStack> inventory = player.getInventory().main;
23+
Map<String, ItemStack> groupedItems = groupItems(inventory);
24+
DefaultedList<ItemStack> sortedInventory = sortAndFilterInventory(groupedItems);
25+
26+
updatePlayerInventory(inventory, sortedInventory);
27+
player.sendMessage(Text.of("Inventario ordenado!"), false);
28+
}
29+
30+
private static Map<String, ItemStack> groupItems(DefaultedList<ItemStack> inventory) {
31+
Map<String, ItemStack> groupedItems = new HashMap<>();
32+
for (int i = HOTBAR_SIZE; i < inventory.size(); i++) {
33+
ItemStack stack = inventory.get(i);
34+
if (!stack.isEmpty()) {
35+
String key = stack.getItem().toString();
36+
groupedItems.merge(key, stack, SortInventory::mergeStacks);
37+
}
38+
}
39+
return groupedItems;
40+
}
41+
42+
private static ItemStack mergeStacks(ItemStack existing, ItemStack incoming) {
43+
int transferAmount = Math.min(incoming.getCount(), existing.getMaxCount() - existing.getCount());
44+
existing.increment(transferAmount);
45+
incoming.decrement(transferAmount);
46+
return existing.getCount() == 0 ? incoming : existing;
47+
}
48+
49+
private static DefaultedList<ItemStack> sortAndFilterInventory(Map<String, ItemStack> groupedItems) {
50+
List<ItemStack> sortedList = groupedItems.values().stream()
51+
.sorted(Comparator.comparing(stack -> stack.getItem().getName(stack).getString()))
52+
.collect(Collectors.toList());
53+
54+
return DefaultedList.copyOf(ItemStack.EMPTY, sortedList.toArray(new ItemStack[0]));
55+
}
56+
57+
private static void updatePlayerInventory(DefaultedList<ItemStack> inventory, DefaultedList<ItemStack> sortedInventory) {
58+
for (int i = HOTBAR_SIZE; i < inventory.size(); i++) {
59+
inventory.set(i, i - HOTBAR_SIZE < sortedInventory.size() ? sortedInventory.get(i - HOTBAR_SIZE) : ItemStack.EMPTY);
60+
}
61+
}
62+
}

src/main/resources/fabric.mod.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
"id": "smartinventory",
44
"version": "${version}",
55
"name": "SmartInventory",
6-
"description": "This is an example description! Tell everyone what your mod is about!",
6+
"description": "A simple inventory management mod for Minecraft",
77
"authors": [
8-
"Me!"
8+
"anvian"
99
],
1010
"contact": {
11-
"homepage": "https://fabricmc.net/",
12-
"sources": "https://github.com/FabricMC/fabric-example-mod"
11+
"homepage": "",
12+
"sources": "https://github.com/anviaan/SmartInventory",
13+
"issues": "https://github.com/anviaan/SmartInventory/issues"
1314
},
14-
"license": "CC0-1.0",
15+
"license": "GPL-3.0",
1516
"icon": "assets/smartinventory/icon.png",
1617
"environment": "*",
1718
"entrypoints": {
18-
"main": [
19+
"client": [
1920
"net.anvian.smartinventory.SmartInventory"
2021
]
2122
},
@@ -24,7 +25,7 @@
2425
],
2526
"depends": {
2627
"fabricloader": ">=0.15.11",
27-
"minecraft": "~1.21",
28+
"minecraft": ">=1.21",
2829
"java": ">=21",
2930
"fabric-api": "*"
3031
},
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"required": true,
3-
"package": "net.anvian.smartinventory.mixin",
4-
"compatibilityLevel": "JAVA_21",
5-
"mixins": [
6-
"ExampleMixin"
7-
],
8-
"injectors": {
9-
"defaultRequire": 1
10-
}
2+
"required": true,
3+
"package": "net.anvian.smartinventory.mixin",
4+
"compatibilityLevel": "JAVA_21",
5+
"client": [
6+
"MixinKeyInputHandler"
7+
],
8+
"mixins": [],
9+
"injectors": {
10+
"defaultRequire": 1
11+
}
1112
}

0 commit comments

Comments
 (0)