aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/PriceInfoTooltip.java
blob: 64f6d8761a6b82da1228ca071c98e0ae6e61a196 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package me.xmrvizzy.skyblocker.skyblock.item;


import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.logging.log4j.LogManager;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class PriceInfoTooltip {
    public static JsonObject bazaarPricesJson;
    public static JsonObject auctionPricesJson;

    public static void onInjectTooltip(ItemStack stack, TooltipContext context, List<Text> list) {
        String name = getInternalNameForItem(stack);

        try {
            if(!list.toString().contains("Avg. BIN Price") && auctionPricesJson != null && auctionPricesJson.has(name) ){
                JsonElement getPrice = auctionPricesJson.get(name);
                String price = round(getPrice.getAsDouble(), 2);

                list.add(new LiteralText("Avg. BIN Price: ").formatted(Formatting.GOLD).append(new LiteralText(price + " Coins").formatted(Formatting.DARK_AQUA)));
            }
            else if(!list.toString().contains("Bazaar Price") && bazaarPricesJson != null && bazaarPricesJson.has(name) ){
                JsonObject getItem = bazaarPricesJson.getAsJsonObject(name);
                String buyprice = round(getItem.get("buyPrice").getAsDouble(), 2);
                String sellprice = round(getItem.get("sellPrice").getAsDouble(), 2);
                list.add(new LiteralText("Bazaar buy Price: ").formatted(Formatting.GOLD).append(new LiteralText(buyprice + " Coins").formatted(Formatting.DARK_AQUA)));
                list.add(new LiteralText("Bazaar sell Price: ").formatted(Formatting.GOLD).append(new LiteralText(sellprice + " Coins").formatted(Formatting.DARK_AQUA)));
            }
        }catch(Exception e) {
            MinecraftClient.getInstance().player.sendMessage(new LiteralText(e.toString()), false);
        }

	}
    public static String round(double value, int places) {
        DecimalFormat df = new DecimalFormat("#,##0.00");
        if (places < 0) throw new IllegalArgumentException();

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return df.format(bd);
    }
    public static String getInternalNameForItem(ItemStack stack) {
        if(stack == null) return null;
        NbtCompound tag = stack.getNbt();
        return getInternalnameFromNBT(tag);
    }

    public static String getInternalnameFromNBT(NbtCompound tag) {
        String internalname = null;
        if(tag != null && tag.contains("ExtraAttributes", 10)) {
            NbtCompound  ea = tag.getCompound("ExtraAttributes");

            if(ea.contains("id", 8)) {
                internalname = ea.getString("id").replaceAll(":", "-");
            } else {
                return null;
            }


            if("ENCHANTED_BOOK".equals(internalname)) {
                NbtCompound enchants = ea.getCompound("enchantments");

                for(String enchname : enchants.getKeys()) {
                    internalname = enchname.toUpperCase() + ";" + enchants.getInt(enchname);
                    break;
                }
            }
        }

        return internalname;
    }

    public static void init() {
        new Thread(PriceInfoTooltip::downloadPrices).start();
        new Thread(PriceInfoTooltip::downloadbazaarPrices).start();
    }

    private static void downloadPrices() {
        JsonObject result = null;
        try {
            URL apiAddr = new URL("https://moulberry.codes/auction_averages_lbin/3day.json.gz");
            try (InputStream src = apiAddr.openStream()) {
                try (GZIPInputStream gzipOutput = new GZIPInputStream(src)) {
                    try (InputStreamReader reader = new InputStreamReader(gzipOutput)) {
                        result = new Gson().fromJson(reader, JsonObject.class);
                    }
                }
            }
        }
        catch(IOException e) {
            LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download item prices!", e);
        }
        auctionPricesJson = result;
    }
    private static void downloadbazaarPrices() {
        JsonObject result = null;
        try {
            URL apiAddr = new URL("https://sky.shiiyu.moe/api/v2/bazaar");
            InputStreamReader reader = new InputStreamReader(apiAddr.openStream());
            result = new Gson().fromJson(reader, JsonObject.class);
        }
        catch(IOException e) {
            LogManager.getLogger(PriceInfoTooltip.class.getName()).warn("[Skyblocker] Failed to download item prices!", e);
        }
        bazaarPricesJson = result;
    }
}