Add count via lore

This commit is contained in:
Bruno Rybársky 2024-05-25 08:50:16 +02:00
parent 747f2fb30c
commit 820f73a12d
2 changed files with 48 additions and 3 deletions

@ -1,13 +1,18 @@
package systems.brn.server_storage.lib; 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.inventory.Inventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.text.RawFilteredPair; import net.minecraft.text.RawFilteredPair;
import net.minecraft.text.Style;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class Util { public class Util {
@ -53,4 +58,40 @@ public class Util {
return pages; 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<Text> 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<Text> 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;
}
}
} }

@ -13,6 +13,8 @@ import net.minecraft.world.World;
import systems.brn.server_storage.lib.PagedGui; import systems.brn.server_storage.lib.PagedGui;
import static systems.brn.server_storage.lib.StorageOperations.*; 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 { public class StorageScreen extends PagedGui {
private Inventory inventory; private Inventory inventory;
@ -48,7 +50,8 @@ public class StorageScreen extends PagedGui {
protected DisplayElement getElement(int id) { protected DisplayElement getElement(int id) {
if (this.inventory.size() > id) { if (this.inventory.size() > id) {
ItemStack itemStack = this.inventory.getStack(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.of(guiElement);
} }
return DisplayElement.empty(); return DisplayElement.empty();
@ -59,8 +62,9 @@ public class StorageScreen extends PagedGui {
GuiElementInterface clickedElement = this.getSlot(index); GuiElementInterface clickedElement = this.getSlot(index);
if (clickedElement != null) { if (clickedElement != null) {
ItemStack clickedItem = clickedElement.getItemStack(); ItemStack clickedItem = clickedElement.getItemStack();
if (tryRemoveItemStackFromChests(world, pos, clickedItem)) { ItemStack noLoreStack = removeCountFromLore(clickedItem);
this.player.getInventory().insertStack(clickedItem); if (tryRemoveItemStackFromChests(world, pos, noLoreStack)) {
this.player.getInventory().insertStack(noLoreStack);
} }
} }
this.updateDisplay(); this.updateDisplay();