aboutsummaryrefslogblamecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/modules/AcceptablePrices.java
blob: 624a47ff5ce527195cf3b0dd9522273267a7d931 (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);
        return ret;
    }

    @Override
    public AcceptablePrices fromTag(NbtCompound tag) {
        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(Identifier.of(item_id));
                int price = a.getInt("Price");
                ret.put(item, price);
            }
        } catch (NullPointerException e) {
            this.prices = new HashMap<>();
            return this;
        }
        this.prices = ret;
        return this;
    }
}