diff --git a/src/main/java/me/night0721/lilase/Main.java b/src/main/java/me/night0721/lilase/Main.java index 5160e42..2041f38 100644 --- a/src/main/java/me/night0721/lilase/Main.java +++ b/src/main/java/me/night0721/lilase/Main.java @@ -33,7 +33,8 @@ public class Main { static int tickAmount; static long lastAction; - public static States clickState; + public static States clickState = States.NONE; + private AuctionHouse auctionHouse; static Minecraft mc = Minecraft.getMinecraft(); static KeyBinding[] keyBindings = new KeyBinding[3]; static ArrayList interactables = new ArrayList<>(Arrays.asList(Blocks.acacia_door, Blocks.anvil, Blocks.beacon, Blocks.bed, Blocks.birch_door, Blocks.brewing_stand, Blocks.command_block, Blocks.crafting_table, Blocks.chest, Blocks.dark_oak_door, Blocks.daylight_detector, Blocks.daylight_detector_inverted, Blocks.dispenser, Blocks.dropper, Blocks.enchanting_table, Blocks.ender_chest, Blocks.furnace, Blocks.hopper, Blocks.jungle_door, Blocks.lever, Blocks.noteblock, Blocks.powered_comparator, Blocks.unpowered_comparator, Blocks.powered_repeater, Blocks.unpowered_repeater, Blocks.standing_sign, Blocks.wall_sign, Blocks.trapdoor, Blocks.trapped_chest, Blocks.wooden_button, Blocks.stone_button, Blocks.oak_door, Blocks.skull)); @@ -41,7 +42,7 @@ public class Main { @Mod.EventHandler public void init(FMLInitializationEvent event) { MinecraftForge.EVENT_BUS.register(this); - + auctionHouse = new AuctionHouse(); keyBindings[0] = new KeyBinding("Ghost Block Bind", Keyboard.KEY_G, "Lilase"); keyBindings[1] = new KeyBinding("Hub", Keyboard.KEY_DIVIDE, "Lilase"); keyBindings[2] = new KeyBinding("Auction House", Keyboard.KEY_END, "Lilase"); @@ -66,7 +67,7 @@ public class Main { mc.thePlayer.sendChatMessage("/hub"); } if (keyBindings[2].isPressed()) { - new AuctionHouse(); + auctionHouse.toggleAuction(); } if (tickAmount % 20 == 0) { Utils.checkForDungeon(); @@ -76,12 +77,20 @@ public class Main { case CLICK: if (System.currentTimeMillis() - lastAction < 500) return; Minecraft.getMinecraft().playerController.windowClick(Minecraft.getMinecraft().thePlayer.openContainer.windowId, 31, 0, 0, Minecraft.getMinecraft().thePlayer); - break; - - case OPEN: lastAction = System.currentTimeMillis(); + clickState = States.CONFIRM; + break; + case CONFIRM: + if (System.currentTimeMillis() - lastAction < 500) return; + Minecraft.getMinecraft().playerController.windowClick(Minecraft.getMinecraft().thePlayer.openContainer.windowId, 11, 0, 0, Minecraft.getMinecraft().thePlayer); + clickState = States.NONE; + break; + case OPEN: AuctionHouse.sendAuction(); + lastAction = System.currentTimeMillis(); clickState = States.CLICK; + case NONE: + break; } } diff --git a/src/main/java/me/night0721/lilase/utils/DiscordWebhook.java b/src/main/java/me/night0721/lilase/utils/DiscordWebhook.java new file mode 100644 index 0000000..642b145 --- /dev/null +++ b/src/main/java/me/night0721/lilase/utils/DiscordWebhook.java @@ -0,0 +1,391 @@ +package me.night0721.lilase.utils; + +import javax.net.ssl.HttpsURLConnection; +import java.awt.Color; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.reflect.Array; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Class used to execute Discord Webhooks with low effort + */ +public class DiscordWebhook { + + private final String url; + private String content; + private String username; + private String avatarUrl; + private boolean tts; + private List embeds = new ArrayList<>(); + + /** + * Constructs a new DiscordWebhook instance + * + * @param url The webhook URL obtained in Discord + */ + public DiscordWebhook(String url) { + this.url = url; + } + + public void setContent(String content) { + this.content = content; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setAvatarUrl(String avatarUrl) { + this.avatarUrl = avatarUrl; + } + + public void setTts(boolean tts) { + this.tts = tts; + } + + public void addEmbed(EmbedObject embed) { + this.embeds.add(embed); + } + + public void execute() throws IOException { + if (this.content == null && this.embeds.isEmpty()) { + throw new IllegalArgumentException("Set content or add at least one EmbedObject"); + } + + JSONObject json = new JSONObject(); + + json.put("content", this.content); + json.put("username", this.username); + json.put("avatar_url", this.avatarUrl); + json.put("tts", this.tts); + + if (!this.embeds.isEmpty()) { + List embedObjects = new ArrayList<>(); + + for (EmbedObject embed : this.embeds) { + JSONObject jsonEmbed = new JSONObject(); + + jsonEmbed.put("title", embed.getTitle()); + jsonEmbed.put("description", embed.getDescription()); + jsonEmbed.put("url", embed.getUrl()); + + if (embed.getColor() != null) { + Color color = embed.getColor(); + int rgb = color.getRed(); + rgb = (rgb << 8) + color.getGreen(); + rgb = (rgb << 8) + color.getBlue(); + + jsonEmbed.put("color", rgb); + } + + EmbedObject.Footer footer = embed.getFooter(); + EmbedObject.Image image = embed.getImage(); + EmbedObject.Thumbnail thumbnail = embed.getThumbnail(); + EmbedObject.Author author = embed.getAuthor(); + List fields = embed.getFields(); + + if (footer != null) { + JSONObject jsonFooter = new JSONObject(); + + jsonFooter.put("text", footer.getText()); + jsonFooter.put("icon_url", footer.getIconUrl()); + jsonEmbed.put("footer", jsonFooter); + } + + if (image != null) { + JSONObject jsonImage = new JSONObject(); + + jsonImage.put("url", image.getUrl()); + jsonEmbed.put("image", jsonImage); + } + + if (thumbnail != null) { + JSONObject jsonThumbnail = new JSONObject(); + + jsonThumbnail.put("url", thumbnail.getUrl()); + jsonEmbed.put("thumbnail", jsonThumbnail); + } + + if (author != null) { + JSONObject jsonAuthor = new JSONObject(); + + jsonAuthor.put("name", author.getName()); + jsonAuthor.put("url", author.getUrl()); + jsonAuthor.put("icon_url", author.getIconUrl()); + jsonEmbed.put("author", jsonAuthor); + } + + List jsonFields = new ArrayList<>(); + for (EmbedObject.Field field : fields) { + JSONObject jsonField = new JSONObject(); + + jsonField.put("name", field.getName()); + jsonField.put("value", field.getValue()); + jsonField.put("inline", field.isInline()); + + jsonFields.add(jsonField); + } + + jsonEmbed.put("fields", jsonFields.toArray()); + embedObjects.add(jsonEmbed); + } + + json.put("embeds", embedObjects.toArray()); + } + + URL url = new URL(this.url); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.addRequestProperty("Content-Type", "application/json"); + connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_"); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + + OutputStream stream = connection.getOutputStream(); + stream.write(json.toString().getBytes()); + stream.flush(); + stream.close(); + + connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream + connection.disconnect(); + } + + public static class EmbedObject { + private String title; + private String description; + private String url; + private Color color; + + private Footer footer; + private Thumbnail thumbnail; + private Image image; + private Author author; + private List fields = new ArrayList<>(); + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public String getUrl() { + return url; + } + + public Color getColor() { + return color; + } + + public Footer getFooter() { + return footer; + } + + public Thumbnail getThumbnail() { + return thumbnail; + } + + public Image getImage() { + return image; + } + + public Author getAuthor() { + return author; + } + + public List getFields() { + return fields; + } + + public EmbedObject setTitle(String title) { + this.title = title; + return this; + } + + public EmbedObject setDescription(String description) { + this.description = description; + return this; + } + + public EmbedObject setUrl(String url) { + this.url = url; + return this; + } + + public EmbedObject setColor(Color color) { + this.color = color; + return this; + } + + public EmbedObject setFooter(String text, String icon) { + this.footer = new Footer(text, icon); + return this; + } + + public EmbedObject setThumbnail(String url) { + this.thumbnail = new Thumbnail(url); + return this; + } + + public EmbedObject setImage(String url) { + this.image = new Image(url); + return this; + } + + public EmbedObject setAuthor(String name, String url, String icon) { + this.author = new Author(name, url, icon); + return this; + } + + public EmbedObject addField(String name, String value, boolean inline) { + this.fields.add(new Field(name, value, inline)); + return this; + } + + private class Footer { + private String text; + private String iconUrl; + + private Footer(String text, String iconUrl) { + this.text = text; + this.iconUrl = iconUrl; + } + + private String getText() { + return text; + } + + private String getIconUrl() { + return iconUrl; + } + } + + private class Thumbnail { + private String url; + + private Thumbnail(String url) { + this.url = url; + } + + private String getUrl() { + return url; + } + } + + private class Image { + private String url; + + private Image(String url) { + this.url = url; + } + + private String getUrl() { + return url; + } + } + + private class Author { + private String name; + private String url; + private String iconUrl; + + private Author(String name, String url, String iconUrl) { + this.name = name; + this.url = url; + this.iconUrl = iconUrl; + } + + private String getName() { + return name; + } + + private String getUrl() { + return url; + } + + private String getIconUrl() { + return iconUrl; + } + } + + private class Field { + private String name; + private String value; + private boolean inline; + + private Field(String name, String value, boolean inline) { + this.name = name; + this.value = value; + this.inline = inline; + } + + private String getName() { + return name; + } + + private String getValue() { + return value; + } + + private boolean isInline() { + return inline; + } + } + } + + private class JSONObject { + + private final HashMap map = new HashMap<>(); + + void put(String key, Object value) { + if (value != null) { + map.put(key, value); + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + Set> entrySet = map.entrySet(); + builder.append("{"); + + int i = 0; + for (Map.Entry entry : entrySet) { + Object val = entry.getValue(); + builder.append(quote(entry.getKey())).append(":"); + + if (val instanceof String) { + builder.append(quote(String.valueOf(val))); + } else if (val instanceof Integer) { + builder.append(Integer.valueOf(String.valueOf(val))); + } else if (val instanceof Boolean) { + builder.append(val); + } else if (val instanceof JSONObject) { + builder.append(val.toString()); + } else if (val.getClass().isArray()) { + builder.append("["); + int len = Array.getLength(val); + for (int j = 0; j < len; j++) { + builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : ""); + } + builder.append("]"); + } + + builder.append(++i == entrySet.size() ? "}" : ","); + } + + return builder.toString(); + } + + private String quote(String string) { + return "\"" + string + "\""; + } + } + +} \ No newline at end of file diff --git a/src/main/java/me/night0721/lilase/utils/ah/AuctionHouse.java b/src/main/java/me/night0721/lilase/utils/ah/AuctionHouse.java index 6206b03..f48313d 100644 --- a/src/main/java/me/night0721/lilase/utils/ah/AuctionHouse.java +++ b/src/main/java/me/night0721/lilase/utils/ah/AuctionHouse.java @@ -1,8 +1,9 @@ package me.night0721.lilase.utils.ah; import me.night0721.lilase.utils.Utils; -import net.minecraft.client.Minecraft; -import okhttp3.*; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -11,7 +12,6 @@ import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.OutputStream; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Arrays; @@ -27,6 +27,8 @@ public class AuctionHouse { private final OkHttpClient client; private static String uuid; private static String message_toSend; + private Thread thread; + private Boolean open = false; private final List items = new ArrayList<>(); private final List posted = new ArrayList<>(); @@ -35,9 +37,8 @@ public class AuctionHouse { // items.add(new Item("Livid Dagger", ItemType.WEAPON, 8000000, ItemTier.LEGENDARY)); // items.add(new Item("Aspect of the Void", ItemType.WEAPON, 8000000, ItemTier.EPIC)); // items.add(new Item("Bal", ItemType.MISC, 10000000, ItemTier.EPIC)); - items.add(new Item(" ", ItemType.MISC, 1000, ItemTier.UNCOMMON)); - Utils.sendMessage("AuctionHouse is now running"); - new Thread(() -> { + items.add(new Item(" ", ItemType.ANY, 1000, ItemTier.ANY)); + thread = new Thread(() -> { while (true) { try { getItem(); @@ -46,7 +47,7 @@ public class AuctionHouse { e.printStackTrace(); } } - }).start(); + }); } private JSONObject getHypixelData(String player) throws IOException, JSONException { @@ -99,12 +100,16 @@ public class AuctionHouse { if (!auction.getString("item_name").toLowerCase().contains(item.query.toLowerCase())) break; if (!auction.getString("item_lore").contains(lore)) break; - if (!auction.getString("tier").equals(item.tier.name())) break; + if (item.tier != ItemTier.ANY) { + if (!auction.getString("tier").equals(item.tier.name())) break; + } if (auction.getInt("starting_bid") > item.price) break; - if (!auction.getString("category").equals(type.lowercase)) break; + if (type != ItemType.ANY) { + if (!auction.getString("category").equals(type.lowercase)) break; + } if (!auction.getBoolean("bin")) break; - if (!posted.contains(auction.getString("item_uuid"))) { - posted.add(auction.getString("item_uuid")); + if (!posted.contains(auction.getString("uuid"))) { + posted.add(auction.getString("uuid")); NumberFormat format = NumberFormat.getInstance(Locale.US); JSONObject profile = getHypixelData(auction.getString("auctioneer")); Pattern pattern = Pattern.compile("ยง[0-9a-z]", Pattern.MULTILINE); @@ -150,7 +155,6 @@ public class AuctionHouse { embed.put("color", 0x003153); embed.put("title", "Item Is On Low Price"); embed.put("url", "https://www.brandonfowler.me/skyblockah/?uuid=" + auction.getString("uuid")); - embed.put("timestamp", System.currentTimeMillis() / 1000); embed.put("description", updated); embed.put("author", author); embed.put("footer", footer); @@ -162,8 +166,6 @@ public class AuctionHouse { uuid = auction.getString("uuid"); message_toSend = "Auction House: " + auction.getString("item_name") + " is sale for " + format.format(auction.getInt("starting_bid")) + "!"; clickState = States.OPEN; - Minecraft.getMinecraft().playerController.windowClick(Minecraft.getMinecraft().thePlayer.openContainer.windowId, 31, 0, 0, Minecraft.getMinecraft().thePlayer); - return; } } @@ -229,19 +231,54 @@ public class AuctionHouse { connection.setRequestMethod("POST"); OutputStream stream = connection.getOutputStream(); - stream.write(data.toString().getBytes(StandardCharsets.UTF_8)); + stream.write(data.toString().getBytes()); stream.flush(); stream.close(); - - connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream + connection.getInputStream().close(); connection.disconnect(); - - - // Request request = new Request.Builder() - // .url(DISCORD_WEBHOOK) - // .post(RequestBody.create(data.toString(), MediaType.get("application/json"))) - // .addHeader("Content-Type", "application/json") - //.build(); - //client.newCall(request).execute(); } -} \ No newline at end of file + + public void toggleAuction() { + if (open) { + Utils.sendMessage("Stopped Auction House"); + thread.stop(); + open = false; + } else { + Utils.sendMessage("Started Auction House"); + thread.run(); + open = true; + } + } +} +enum ItemTier { + ANY, + COMMON, + UNCOMMON, + RARE, + EPIC, + LEGENDARY, + MYTHIC, + DIVINE, + SPECIAL, + VERY_SPECIAL, +} + +enum ItemType { + ANY("any"), + WEAPON("weapon"), + ARMOR("armor"), + ACCESSORIES("accessories"), + CONSUMABLES("consumables"), + BLOCKS("blocks"), + MISC("misc"),; + + public final String lowercase; + + public String getLowercase() { + return lowercase; + } + + ItemType(String lowercase) { + this.lowercase = lowercase; + } +} diff --git a/src/main/java/me/night0721/lilase/utils/ah/ItemTier.java b/src/main/java/me/night0721/lilase/utils/ah/ItemTier.java deleted file mode 100644 index f13fe71..0000000 --- a/src/main/java/me/night0721/lilase/utils/ah/ItemTier.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.night0721.lilase.utils.ah; - -public enum ItemTier { - COMMON, - UNCOMMON, - RARE, - EPIC, - LEGENDARY, - MYTHIC, - DIVINE, - SPECIAL, - VERY_SPECIAL, -} diff --git a/src/main/java/me/night0721/lilase/utils/ah/ItemType.java b/src/main/java/me/night0721/lilase/utils/ah/ItemType.java deleted file mode 100644 index ae4d63c..0000000 --- a/src/main/java/me/night0721/lilase/utils/ah/ItemType.java +++ /dev/null @@ -1,20 +0,0 @@ -package me.night0721.lilase.utils.ah; - -public enum ItemType { - WEAPON("weapon"), - ARMOR("armor"), - ACCESSORIES("accessories"), - CONSUMABLES("consumables"), - BLOCKS("blocks"), - MISC("misc"),; - - public final String lowercase; - - public String getLowercase() { - return lowercase; - } - - ItemType(String lowercase) { - this.lowercase = lowercase; - } -} diff --git a/src/main/java/me/night0721/lilase/utils/ah/States.java b/src/main/java/me/night0721/lilase/utils/ah/States.java index 6825260..a986950 100644 --- a/src/main/java/me/night0721/lilase/utils/ah/States.java +++ b/src/main/java/me/night0721/lilase/utils/ah/States.java @@ -1,7 +1,9 @@ package me.night0721.lilase.utils.ah; public enum States { - OPEN, - //CLOSE, - CLICK -} \ No newline at end of file + OPEN, + NONE, + //CLOSE, + CLICK, + CONFIRM +}