aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/modules/AcceptablePrices.java
blob: 48096401b3eead527b6102e8d3bc7f050f1a9697 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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(new Identifier(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;
    }
}