aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/Danker/commands/HOTMTreeCommand.java
blob: 90b82d3bce1463d8168e348db626fbb546d121b7 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package me.Danker.commands;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.Danker.DankersSkyblockMod;
import me.Danker.containers.GuiChestDynamic;
import me.Danker.handlers.APIHandler;
import me.Danker.utils.Utils;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.*;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Constants;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

public class HOTMTreeCommand extends CommandBase {

    public static GuiChestDynamic chest = null;

    @Override
    public String getCommandName() {
        return "hotmtree";
    }

    @Override
    public String getCommandUsage(ICommandSender arg0) {
        return null;
    }

    @Override
    public int getRequiredPermissionLevel() {
        return 0;
    }

    @Override
    public void processCommand(ICommandSender arg0, String[] arg1) throws CommandException {
        // MULTI THREAD DRIFTING
        new Thread(() -> {
            EntityPlayer player = (EntityPlayer) arg0;

            player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Checking HotM tree of " + DankersSkyblockMod.SECONDARY_COLOUR + arg1[0]));

            System.out.println("Fetching profile...");
            String profileURL = "https://sky.shiiyu.moe/api/v2/profile/" + arg1[0];
            JsonObject profileResponse = APIHandler.getResponse(profileURL, true);
            if (profileResponse.has("error")) {
                String reason = profileResponse.get("error").getAsString();
                player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Failed with reason: " + reason));
                return;
            }

            System.out.println("Fetching HotM tree...");
            JsonArray tree = profileResponse.get("profiles").getAsJsonObject().get(arg1[1]).getAsJsonObject().get("items").getAsJsonObject().get("hotm").getAsJsonArray();

            IInventory inventory = new InventoryBasic(arg1[0] + "'s HotM Tree:", true, 63);

            for (JsonElement e : tree) {
                JsonObject node = e.getAsJsonObject();

                if (!node.has("tag")) continue;

                ItemStack item = new ItemStack(Item.getItemById(node.get("id").getAsInt()), node.get("Count").getAsInt(), node.get("Damage").getAsInt());

                NBTTagCompound nbt = new NBTTagCompound();
                try {
                    nbt = JsonToNBT.getTagFromJson(node.get("tag").toString());
                    removeDoubleQuotes(nbt);
                    nbt.getCompoundTag("display").getTagList("Lore", Constants.NBT.TAG_STRING).removeTag(0);
                } catch (NBTException ex) {
                    ex.printStackTrace();
                    continue;
                }

                if (node.get("glowing").getAsBoolean()) {
                    nbt.setTag("HideFlags", new NBTTagShort((short) 1));
                }

                if (node.has("texture_path")) {
                    String path = node.get("texture_path").getAsString();
                    NBTTagCompound skullOwner = new NBTTagCompound();
                    NBTTagCompound properties = new NBTTagCompound();
                    NBTTagList textures = new NBTTagList();
                    NBTTagCompound value = new NBTTagCompound();
                    String texture = "{\"textures\":{\"SKIN\":{\"url\":\"http://textures.minecraft.net/texture/" + path.substring(path.lastIndexOf("/") + 1) + "\"}}}";
                    value.setTag("Value", new NBTTagString(Base64.getEncoder().encodeToString(texture.getBytes())));
                    textures.appendTag(value);
                    properties.setTag("textures", textures);
                    skullOwner.setTag("Properties", properties);
                    skullOwner.setTag("Id", new NBTTagString(UUID.randomUUID().toString()));
                    nbt.setTag("SkullOwner", skullOwner);
                }

                item.setTagCompound(nbt);

                if (node.get("glowing").getAsBoolean()) {
                    item.addEnchantment(Enchantment.protection, 1);
                }
                item.setStackDisplayName(Utils.removeBold(item.getDisplayName()));

                inventory.setInventorySlotContents(node.get("position").getAsInt() - 1, item);
            }

            chest = new GuiChestDynamic(player.inventory, inventory, new ResourceLocation("dsm", "textures/generic_63.png"));
            DankersSkyblockMod.guiToOpen = "hotminventory";
        }).start();
    }

    // https://bitbucket.org/hrznstudio/mo-legacy-edition/src/4cc47b2a792cc2ef19eb7e5db0169706ea2e48dd/src/main/java/matteroverdrive/util/MOJsonHelper.java#lines-164:179
    public static void removeDoubleQuotes(NBTTagCompound tagCompound) {
        List<String> cachedKeyList = new ArrayList<>();
        cachedKeyList.addAll(tagCompound.getKeySet());
        for (String key : cachedKeyList) {
            NBTBase base = tagCompound.getTag(key);
            tagCompound.removeTag(key);

            key = key.replace("\"", "");
            if (base instanceof NBTTagCompound) {
                removeDoubleQuotes((NBTTagCompound) base);
            } else if (base instanceof NBTTagList) {
                removeDoubleQuotes((NBTTagList) base);
            }
            tagCompound.setTag(key, base);
        }
    }

    // https://bitbucket.org/hrznstudio/mo-legacy-edition/src/4cc47b2a792cc2ef19eb7e5db0169706ea2e48dd/src/main/java/matteroverdrive/util/MOJsonHelper.java#lines-181:189
    public static void removeDoubleQuotes(NBTTagList tagList) {
        for (int i = 0; i < tagList.tagCount(); i++) {
            if (tagList.get(i) instanceof NBTTagCompound) {
                removeDoubleQuotes((NBTTagCompound) tagList.get(i));
            } else if (tagList.get(i) instanceof NBTTagList) {
                removeDoubleQuotes((NBTTagList) tagList.get(i));
            }
        }
    }

}