aboutsummaryrefslogblamecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/modules/AcceptablePrices.java
blob: 9e96dfe8e0bf5f1bfba2abea3fccef40289ba6fa (plain) (tree)




























































































                                                                                                                                      
package org.chrisoft.trashyaddon.modules;

import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.WidgetScreen;
import meteordevelopment.meteorclient.utils.misc.ICopyable;
import meteordevelopment.meteorclient.utils.misc.ISerializable;
import meteordevelopment.meteorclient.gui.utils.IScreenFactory;
import net.minecraft.item.Item;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;

public class AcceptablePrices implements ICopyable<AcceptablePrices>, ISerializable<AcceptablePrices>, IScreenFactory {
    public static final List<Item> allItems = Stream.concat(AutoTrade.allSellItems.stream(), AutoTrade.allBuyItems.stream()).toList();
    private HashMap<Item, Integer> prices;
    AcceptablePrices(HashMap<Item, Integer> prices) {
        this.prices = prices;
    }
    Integer getMaxPriceForItem(Item i) {
        return prices.get(i);
    }
    void setMaxPriceForItem(Item i, int p) {
        prices.put(i, p);
    }
    void unsetMaxPriceForItem(Item i) {
        prices.remove(i);
    }
    List<Item> allConfiguredItems() {
        return prices.keySet().stream().toList();
    }

    @Override
    public WidgetScreen createScreen(GuiTheme theme) {
        return new AcceptablePricesScreen(theme, this);
    }

    @Override
    public AcceptablePrices set(AcceptablePrices value) {
        this.prices = value.prices;
        return this;
    }

    @Override
    public AcceptablePrices copy() {
        return new AcceptablePrices(new HashMap<>(this.prices));
    }

    @Override
    public NbtCompound toTag() {
        NbtCompound ret = new NbtCompound();
        NbtList l = new NbtList();
        for (Item i : this.prices.keySet()) {
            NbtCompound a = new NbtCompound();
            a.putString("Item", Registries.ITEM.getId(i).toString());
            a.putInt("Price", prices.get(i));
            l.add(a);
        }
        ret.put("Prices", l);
        MeteorClient.LOG.warn("AcceptablePrices NBT result: " + ret.asString());
        return ret;
    }

    @Override
    public AcceptablePrices fromTag(NbtCompound tag) {
        MeteorClient.LOG.warn("AcceptablePrices NBT from: " + tag.asString());
        HashMap<Item, Integer> ret = new HashMap<>();
        try {
            NbtList l = tag.getList("Prices", NbtElement.COMPOUND_TYPE);
            for (int i = 0; i < l.size(); ++i) {
                NbtCompound a = l.getCompound(i);
                String item_id = a.getString("Item");
                Item item = Registries.ITEM.get(new Identifier(item_id));
                int price = a.getInt("Price");
                ret.put(item, price);
            }
        } catch (NullPointerException e) {
            this.prices = new HashMap<>();
            return this;
        }
        MeteorClient.LOG.warn(ret.toString());
        this.prices = ret;
        return this;
    }
}