From 8bd93c4ff81de99f73d24314e0bfd180e6f25255 Mon Sep 17 00:00:00 2001 From: bowser0000 Date: Sat, 20 Nov 2021 22:39:13 -0500 Subject: Add /hotmof command Also make API handler use UTF-8 --- src/main/java/me/Danker/DankersSkyblockMod.java | 10 +- src/main/java/me/Danker/commands/DHelpCommand.java | 1 + src/main/java/me/Danker/commands/HOTMCommand.java | 122 +++++++++++++++++ .../java/me/Danker/commands/HOTMTreeCommand.java | 151 +++++++++++++++++++++ .../java/me/Danker/commands/InventoryCommand.java | 2 - .../java/me/Danker/containers/GuiChestDynamic.java | 45 ++++++ src/main/java/me/Danker/handlers/APIHandler.java | 3 +- .../resources/assets/dsm/textures/generic_63.png | Bin 0 -> 870 bytes 8 files changed, 329 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/Danker/commands/HOTMCommand.java create mode 100644 src/main/java/me/Danker/commands/HOTMTreeCommand.java create mode 100644 src/main/java/me/Danker/containers/GuiChestDynamic.java create mode 100644 src/main/resources/assets/dsm/textures/generic_63.png (limited to 'src') diff --git a/src/main/java/me/Danker/DankersSkyblockMod.java b/src/main/java/me/Danker/DankersSkyblockMod.java index 1998f41..2730868 100644 --- a/src/main/java/me/Danker/DankersSkyblockMod.java +++ b/src/main/java/me/Danker/DankersSkyblockMod.java @@ -197,6 +197,8 @@ public class DankersSkyblockMod { ClientCommandHandler.instance.registerCommand(new FarmLengthCommand()); ClientCommandHandler.instance.registerCommand(new GetkeyCommand()); ClientCommandHandler.instance.registerCommand(new GuildOfCommand()); + ClientCommandHandler.instance.registerCommand(new HOTMCommand()); + ClientCommandHandler.instance.registerCommand(new HOTMTreeCommand()); ClientCommandHandler.instance.registerCommand(new ImportFishingCommand()); ClientCommandHandler.instance.registerCommand(new InventoryCommand()); ClientCommandHandler.instance.registerCommand(new LobbyBankCommand()); @@ -447,6 +449,9 @@ public class DankersSkyblockMod { case "inventory": mc.displayGuiScreen(InventoryCommand.chest); break; + case "hotminventory": + mc.displayGuiScreen(HOTMTreeCommand.chest); + break; } } guiToOpen = null; @@ -467,16 +472,17 @@ public class DankersSkyblockMod { @SubscribeEvent public void onGuiMouseInputPre(GuiScreenEvent.MouseInputEvent.Pre event) { - if (!Utils.inSkyblock) return; if (Mouse.getEventButton() != 0 && Mouse.getEventButton() != 1 && Mouse.getEventButton() != 2) return; // Left click, middle click or right click if (!Mouse.getEventButtonState()) return; - if (event.gui == InventoryCommand.chest) { + if (event.gui == InventoryCommand.chest || event.gui == HOTMTreeCommand.chest) { event.setCanceled(true); return; } + if (!Utils.inSkyblock) return; + if (event.gui instanceof GuiChest) { Container containerChest = ((GuiChest) event.gui).inventorySlots; if (containerChest instanceof ContainerChest) { diff --git a/src/main/java/me/Danker/commands/DHelpCommand.java b/src/main/java/me/Danker/commands/DHelpCommand.java index c82eb0e..0c81af5 100644 --- a/src/main/java/me/Danker/commands/DHelpCommand.java +++ b/src/main/java/me/Danker/commands/DHelpCommand.java @@ -61,6 +61,7 @@ public class DHelpCommand extends CommandBase { EnumChatFormatting.GOLD + ReloadConfigCommand.usage(arg0) + EnumChatFormatting.AQUA + " - Reloads Danker's Skyblock Mod config.\n" + EnumChatFormatting.GOLD + ReloadRepoCommand.usage(arg0) + EnumChatFormatting.AQUA + " - Reloads Danker's Skyblock Mod repository.\n" + EnumChatFormatting.GOLD + FarmLengthCommand.usage(arg0) + EnumChatFormatting.AQUA + " - Sets coords to be used for end of farm alert.\n" + + EnumChatFormatting.GOLD + HOTMCommand.usage(arg0) + EnumChatFormatting.AQUA + " - Uses API to find total powder and HotM tree of a person. If no name is provided, it checks yours.\n" + EnumChatFormatting.GREEN + " Open Maddox Menu" + EnumChatFormatting.AQUA + " - M by default.\n" + EnumChatFormatting.GREEN + " Start/Stop Skill Tracker" + EnumChatFormatting.AQUA + " - Numpad 5 by default.\n")); } diff --git a/src/main/java/me/Danker/commands/HOTMCommand.java b/src/main/java/me/Danker/commands/HOTMCommand.java new file mode 100644 index 0000000..bfb10f5 --- /dev/null +++ b/src/main/java/me/Danker/commands/HOTMCommand.java @@ -0,0 +1,122 @@ +package me.Danker.commands; + +import com.google.gson.JsonObject; +import me.Danker.DankersSkyblockMod; +import me.Danker.handlers.APIHandler; +import me.Danker.handlers.ConfigHandler; +import me.Danker.utils.Utils; +import net.minecraft.command.CommandBase; +import net.minecraft.command.CommandException; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.event.ClickEvent; +import net.minecraft.util.BlockPos; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.EnumChatFormatting; + +import java.text.NumberFormat; +import java.util.List; +import java.util.Locale; + +public class HOTMCommand extends CommandBase { + + @Override + public String getCommandName() { + return "hotmof"; + } + + @Override + public String getCommandUsage(ICommandSender arg0) { + return "/" + getCommandName() + " [name]"; + } + + public static String usage(ICommandSender arg0) { + return new HOTMCommand().getCommandUsage(arg0); + } + + @Override + public int getRequiredPermissionLevel() { + return 0; + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { + if (args.length == 1) { + return Utils.getMatchingPlayers(args[0]); + } + return null; + } + + @Override + public void processCommand(ICommandSender arg0, String[] arg1) throws CommandException { + // MULTI THREAD DRIFTING + new Thread(() -> { + EntityPlayer player = (EntityPlayer) arg0; + + // Check key + String key = ConfigHandler.getString("api", "APIKey"); + if (key.equals("")) { + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "API key not set. Use /setkey.")); + return; + } + + // Get UUID for Hypixel API requests + String username; + String uuid; + if (arg1.length == 0) { + username = player.getName(); + uuid = player.getUniqueID().toString().replaceAll("[\\-]", ""); + player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Checking HotM of " + DankersSkyblockMod.SECONDARY_COLOUR + username)); + } else { + username = arg1[0]; + player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Checking HotM of " + DankersSkyblockMod.SECONDARY_COLOUR + username)); + uuid = APIHandler.getUUID(username); + } + + // Find stats of latest profile + String latestProfile = APIHandler.getLatestProfileID(uuid, key); + if (latestProfile == null) return; + + String profileURL = "https://api.hypixel.net/skyblock/profile?profile=" + latestProfile + "&key=" + key; + System.out.println("Fetching profile..."); + JsonObject profileResponse = APIHandler.getResponse(profileURL, true); + if (!profileResponse.get("success").getAsBoolean()) { + String reason = profileResponse.get("cause").getAsString(); + player.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Failed with reason: " + reason)); + return; + } + + System.out.println("Fetching mining stats..."); + JsonObject miningCore = profileResponse.get("profile").getAsJsonObject().get("members").getAsJsonObject().get(uuid).getAsJsonObject().get("mining_core").getAsJsonObject(); + int mithril = miningCore.get("powder_mithril").getAsInt() + miningCore.get("powder_spent_mithril").getAsInt(); + int gemstone = miningCore.get("powder_gemstone").getAsInt() + miningCore.get("powder_spent_gemstone").getAsInt(); + String ability = Node.valueOf(miningCore.get("selected_pickaxe_ability").getAsString()).name; + + ChatComponentText tree = new ChatComponentText(EnumChatFormatting.GREEN + "" + EnumChatFormatting.BOLD + "[CLICK]"); + tree.setChatStyle(tree.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/hotmtree " + username + " " + latestProfile))); + + NumberFormat nf = NumberFormat.getIntegerInstance(Locale.US); + player.addChatMessage(new ChatComponentText(DankersSkyblockMod.DELIMITER_COLOUR + "" + EnumChatFormatting.BOLD + "-------------------\n" + + EnumChatFormatting.AQUA + username + "'s HotM:\n" + + DankersSkyblockMod.TYPE_COLOUR + "Mithril Powder: " + EnumChatFormatting.DARK_GREEN + nf.format(mithril) + "\n" + + DankersSkyblockMod.TYPE_COLOUR + "Gemstone Powder: " + EnumChatFormatting.LIGHT_PURPLE + nf.format(gemstone) + "\n" + + DankersSkyblockMod.TYPE_COLOUR + "Pickaxe Ability: " + DankersSkyblockMod.VALUE_COLOUR + ability + "\n" + + DankersSkyblockMod.TYPE_COLOUR + "HotM Tree: ").appendSibling(tree) + .appendSibling(new ChatComponentText("\n" + DankersSkyblockMod.DELIMITER_COLOUR + EnumChatFormatting.BOLD + "-------------------"))); + }).start(); + } + + enum Node { + mining_speed_boost("Mining Speed Boost"), + pickobulus("*Pickobulus"), + vein_seeker("Vein Seeker"), + maniac_miner("*Maniac Miner"); + + public String name; + + Node(String name) { + this.name = name; + } + } + +} diff --git a/src/main/java/me/Danker/commands/HOTMTreeCommand.java b/src/main/java/me/Danker/commands/HOTMTreeCommand.java new file mode 100644 index 0000000..90b82d3 --- /dev/null +++ b/src/main/java/me/Danker/commands/HOTMTreeCommand.java @@ -0,0 +1,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 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)); + } + } + } + +} diff --git a/src/main/java/me/Danker/commands/InventoryCommand.java b/src/main/java/me/Danker/commands/InventoryCommand.java index c1f5cbf..8fb4d5b 100644 --- a/src/main/java/me/Danker/commands/InventoryCommand.java +++ b/src/main/java/me/Danker/commands/InventoryCommand.java @@ -153,8 +153,6 @@ public class InventoryCommand extends CommandBase { chest = new GuiChest(player.inventory, inventory); DankersSkyblockMod.guiToOpen = "inventory"; - - // mc.displayGuiScreen(new GuiChest(player.inventory, inventory)); }).start(); } diff --git a/src/main/java/me/Danker/containers/GuiChestDynamic.java b/src/main/java/me/Danker/containers/GuiChestDynamic.java new file mode 100644 index 0000000..600abbc --- /dev/null +++ b/src/main/java/me/Danker/containers/GuiChestDynamic.java @@ -0,0 +1,45 @@ +package me.Danker.containers; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.inventory.ContainerChest; +import net.minecraft.inventory.IInventory; +import net.minecraft.util.ResourceLocation; + +// Hopefully this is dyanmic, only tested with 63 slots +public class GuiChestDynamic extends GuiContainer { + private final ResourceLocation CHEST_GUI_TEXTURE; + private IInventory upperChestInventory; + private IInventory lowerChestInventory; + private int inventoryRows; + + public GuiChestDynamic(IInventory upperInv, IInventory lowerInv, ResourceLocation texture) + { + super(new ContainerChest(upperInv, lowerInv, Minecraft.getMinecraft().thePlayer)); + this.upperChestInventory = upperInv; + this.lowerChestInventory = lowerInv; + this.allowUserInput = false; + this.inventoryRows = lowerInv.getSizeInventory() / 9; + int i = this.inventoryRows * 37; + int j = i - this.inventoryRows * 18; + this.ySize = j + this.inventoryRows * 18; + this.CHEST_GUI_TEXTURE = texture; + } + + protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) + { + this.fontRendererObj.drawString(this.lowerChestInventory.getDisplayName().getUnformattedText(), 8, 6, 4210752); + this.fontRendererObj.drawString(this.upperChestInventory.getDisplayName().getUnformattedText(), 8, this.ySize - this.inventoryRows * 16 - 1, 4210752); + } + + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) + { + GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); + this.mc.getTextureManager().bindTexture(CHEST_GUI_TEXTURE); + int i = (this.width - this.xSize) / 2; + int j = (this.height - this.ySize) / 2; + this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.inventoryRows * 18 + 17); + this.drawTexturedModalRect(i, j + this.inventoryRows * 18 + 17, 0, this.inventoryRows * 18 + 18, this.xSize, this.inventoryRows * 16); + } +} diff --git a/src/main/java/me/Danker/handlers/APIHandler.java b/src/main/java/me/Danker/handlers/APIHandler.java index 4417973..736a7d7 100644 --- a/src/main/java/me/Danker/handlers/APIHandler.java +++ b/src/main/java/me/Danker/handlers/APIHandler.java @@ -15,6 +15,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Scanner; public class APIHandler { @@ -28,7 +29,7 @@ public class APIHandler { conn.setRequestProperty("User-Agent", "Dsm/1.0"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); String input; StringBuilder response = new StringBuilder(); diff --git a/src/main/resources/assets/dsm/textures/generic_63.png b/src/main/resources/assets/dsm/textures/generic_63.png new file mode 100644 index 0000000..0ccafd7 Binary files /dev/null and b/src/main/resources/assets/dsm/textures/generic_63.png differ -- cgit