Untitled

 avatar
unknown
java
14 days ago
3.7 kB
10
Indexable
package dev.unnm3d.redistrade.guis;

import org.jetbrains.annotations.NotNull;
import xyz.xenondevs.invui.gui.AbstractGui;
import xyz.xenondevs.invui.gui.SlotElement;
import xyz.xenondevs.invui.inventory.Inventory;
import xyz.xenondevs.invui.item.Item;
import xyz.xenondevs.invui.item.ItemProvider;
import xyz.xenondevs.invui.item.impl.SimpleItem;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class MutableGui extends AbstractGui {

    private final HashMap<Character, Set<ItemPosition>> itemPositionsMap;

    public MutableGui(@NotNull String... structureData) {
        super(sanitize(structureData[0]).length(), structureData.length);
        this.itemPositionsMap = new HashMap<>();

        for (int i = 0; i < structureData.length; i++) {
            final String row = sanitize(structureData[i]);
            for (int j = 0; j < row.length(); j++) {
                char c = row.charAt(j);
                final ItemPosition itemPosition = new ItemPosition(j, i);
                this.itemPositionsMap.compute(c, (key, value) -> {
                    if (value == null) {
                        return new HashSet<>(Set.of(itemPosition));
                    } else {
                        value.add(itemPosition);
                        return value;
                    }
                });
            }
        }
    }

    public MutableGui setIngredient(char c, Item item) {
        Set<ItemPosition> itemPositions = itemPositionsMap.get(c);
        if (itemPositions == null) {
            throw new IllegalArgumentException("Character " + c + " not found in the structure");
        }

        itemPositions.forEach(itemPosition ->
                setSlotElement(itemPosition.x, itemPosition.y,
                        new SlotElement.ItemSlotElement(item)));
        return this;
    }

    public MutableGui setIngredient(char c, ItemProvider itemProvider) {
        Set<ItemPosition> itemPositions = itemPositionsMap.get(c);
        if (itemPositions == null) {
            throw new IllegalArgumentException("Character " + c + " not found in the structure");
        }
        itemPositions.forEach(itemPosition ->
                setSlotElement(itemPosition.x, itemPosition.y,
                        new SlotElement.ItemSlotElement(new SimpleItem(itemProvider))));
        return this;
    }

    public MutableGui setIngredient(char c, Inventory inventory) {
        Set<ItemPosition> itemPositions = itemPositionsMap.get(c);
        if (itemPositions == null) {
            throw new IllegalArgumentException("Character " + c + " not found in the structure");
        }
        int i = 0;
        for (ItemPosition itemPosition : itemPositions) {
            setSlotElement(itemPosition.x, itemPosition.y,
                    new SlotElement.InventorySlotElement(inventory, i++));
        }
        return this;
    }

    public MutableGui setMultipleIngredient(String chars, Item item) {
        chars = sanitize(chars);
        for (char c : chars.toCharArray()) {
            Set<ItemPosition> itemPositions = itemPositionsMap.get(c);
            if (itemPositions == null) {
                throw new IllegalArgumentException("Character " + c + " not found in the structure");
            }
            itemPositions.forEach(itemPosition -> setSlotElement(itemPosition.x, itemPosition.y,
                    new SlotElement.ItemSlotElement(item)));
        }
        return this;
    }


    private static String sanitize(String s) {
        return s.replace(" ", "").replace("\n", "");
    }

    private record ItemPosition(int x, int y) {
    }
}
Leave a Comment