aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerObject.java
blob: 1c6e54d436d95d06c8835492400df06563bcdfda (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
package com.thatgravyboat.skyblockhud.tracker;

import com.google.gson.JsonObject;
import com.thatgravyboat.skyblockhud.location.Locations;
import java.util.EnumSet;
import java.util.Locale;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ResourceLocation;

public class TrackerObject {

    private final ItemStack stack;
    private final String internalId;
    private final EnumSet<Locations> locations;
    private final boolean isEntity;
    private int count;

    public TrackerObject(JsonObject jsonObject, EnumSet<Locations> locations) {
        this.stack = decodeToItemStack(jsonObject);
        this.internalId = jsonObject.get("id").getAsString();
        this.isEntity = jsonObject.get("id").getAsString().contains("entity:");
        this.locations = locations;
    }

    public static ItemStack decodeToItemStack(JsonObject jsonObject) {
        jsonObject = jsonObject.getAsJsonObject("displayItem");
        int meta = jsonObject.get("meta").getAsInt();
        ResourceLocation itemid = new ResourceLocation(jsonObject.get("item").getAsString());
        ItemStack stack = new ItemStack(Item.itemRegistry.getObject(itemid), 0, meta);
        if (jsonObject.has("displayName")) stack.setStackDisplayName(jsonObject.get("displayName").getAsString());
        if (jsonObject.has("skullData") && itemid.getResourcePath().equals("skull") && meta == 3) {
            stack.setTagInfo("SkullOwner", getSkullTag(jsonObject.getAsJsonObject("skullData")));
        }
        if (jsonObject.has("enchanted") && jsonObject.get("enchanted").getAsBoolean()) {
            stack.setTagInfo("ench", new NBTTagList());
        }
        if (!jsonObject.get("id").getAsString().contains("entity:")) {
            NBTTagCompound extraAttributes = new NBTTagCompound();
            extraAttributes.setString("id", jsonObject.get("id").getAsString());
            stack.setTagInfo("ExtraAttributes", extraAttributes);
        }
        return stack;
    }

    public static NBTBase getSkullTag(JsonObject skullObject) {
        NBTTagCompound skullOwner = new NBTTagCompound();
        NBTTagCompound properties = new NBTTagCompound();
        NBTTagList textures = new NBTTagList();
        NBTTagCompound value = new NBTTagCompound();

        skullOwner.setString("Id", skullObject.get("id").getAsString());

        value.setString("Value", skullObject.get("texture").getAsString());
        textures.appendTag(value);

        properties.setTag("textures", textures);

        skullOwner.setTag("Properties", properties);
        return skullOwner;
    }

    public void increaseCount(int amount) {
        count += amount;
    }

    public void increaseCount() {
        count++;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public ItemStack getDisplayStack() {
        return stack;
    }

    public EnumSet<Locations> getLocations() {
        return locations;
    }

    public String getInternalId() {
        return internalId.toUpperCase(Locale.ENGLISH);
    }

    public boolean isEntity() {
        return isEntity;
    }
}