diff --git a/src/main/java/systems/brn/server_storage/lib/Util.java b/src/main/java/systems/brn/server_storage/lib/Util.java index 22678d5..ab35363 100644 --- a/src/main/java/systems/brn/server_storage/lib/Util.java +++ b/src/main/java/systems/brn/server_storage/lib/Util.java @@ -1,13 +1,18 @@ package systems.brn.server_storage.lib; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.LoreComponent; import net.minecraft.inventory.Inventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.text.RawFilteredPair; +import net.minecraft.text.Style; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class Util { @@ -53,4 +58,40 @@ public class Util { return pages; } + + public static ItemStack addCountToLore(final int count, ItemStack stack) { + if (count > 0) { + ItemStack newStack = stack.copy(); + LoreComponent lore = stack.get(DataComponentTypes.LORE); + Text countLine = Text.literal("Amount: " + count).setStyle(Style.EMPTY.withColor(Formatting.GOLD).withItalic(true)); + + assert lore != null; + List newLines = new ArrayList<>(lore.lines()); + newLines.addFirst(countLine); + LoreComponent newLore = new LoreComponent(newLines); + + newStack.set(DataComponentTypes.LORE, newLore); + return newStack; + } else { + return stack; + } + } + + public static ItemStack removeCountFromLore(ItemStack stack) { + LoreComponent oldLore = stack.get(DataComponentTypes.LORE); + + if (oldLore != null) { + ItemStack newStack = stack.copy(); + List filteredLines = oldLore.lines().stream() + .filter(line -> !line.getString().matches("Amount: \\d+")) + .collect(Collectors.toList()); + + LoreComponent newLore = new LoreComponent(filteredLines); + + newStack.set(DataComponentTypes.LORE, newLore); + return newStack; + } else { + return stack; + } + } } diff --git a/src/main/java/systems/brn/server_storage/screens/StorageScreen.java b/src/main/java/systems/brn/server_storage/screens/StorageScreen.java index f861edf..c06fd57 100644 --- a/src/main/java/systems/brn/server_storage/screens/StorageScreen.java +++ b/src/main/java/systems/brn/server_storage/screens/StorageScreen.java @@ -13,6 +13,8 @@ import net.minecraft.world.World; import systems.brn.server_storage.lib.PagedGui; import static systems.brn.server_storage.lib.StorageOperations.*; +import static systems.brn.server_storage.lib.Util.addCountToLore; +import static systems.brn.server_storage.lib.Util.removeCountFromLore; public class StorageScreen extends PagedGui { private Inventory inventory; @@ -48,7 +50,8 @@ public class StorageScreen extends PagedGui { protected DisplayElement getElement(int id) { if (this.inventory.size() > id) { ItemStack itemStack = this.inventory.getStack(id); - GuiElementBuilder guiElement = new GuiElementBuilder(itemStack); + ItemStack newStack = addCountToLore(itemStack.getCount(), itemStack); + GuiElementBuilder guiElement = new GuiElementBuilder(newStack); return DisplayElement.of(guiElement); } return DisplayElement.empty(); @@ -59,8 +62,9 @@ public class StorageScreen extends PagedGui { GuiElementInterface clickedElement = this.getSlot(index); if (clickedElement != null) { ItemStack clickedItem = clickedElement.getItemStack(); - if (tryRemoveItemStackFromChests(world, pos, clickedItem)) { - this.player.getInventory().insertStack(clickedItem); + ItemStack noLoreStack = removeCountFromLore(clickedItem); + if (tryRemoveItemStackFromChests(world, pos, noLoreStack)) { + this.player.getInventory().insertStack(noLoreStack); } } this.updateDisplay();