diff --git a/gradle.properties b/gradle.properties index c6a4093..9407261 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ yarn_mappings=1.21.3+build.2 loader_version=0.16.9 # Mod Properties -mod_version=0.2 +mod_version=0.6.9 maven_group=anticope archives_base_name=e621-addon diff --git a/src/main/java/anticope/esixtwoone/ImageHUD.java b/src/main/java/anticope/esixtwoone/ImageHUD.java index cbaded5..9938377 100644 --- a/src/main/java/anticope/esixtwoone/ImageHUD.java +++ b/src/main/java/anticope/esixtwoone/ImageHUD.java @@ -25,6 +25,11 @@ import anticope.esixtwoone.sources.Source.Size; import anticope.esixtwoone.sources.Source.SourceType; +import javax.imageio.ImageIO; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + import static meteordevelopment.meteorclient.MeteorClient.mc; import static meteordevelopment.meteorclient.utils.Utils.WHITE; @@ -56,7 +61,11 @@ public class ImageHUD extends HudElement { .defaultValue(100) .min(10) .sliderRange(70, 1000) - .onChanged(o -> updateSize()) + .onChanged(o -> { + if (o != 0) { + updateSize(); + } + }) .build() ); @@ -154,8 +163,10 @@ private void loadImage() { return; } E621Hud.LOG.info(url); - var img = NativeImage.read(Http.get(url).sendInputStream()); - mc.getTextureManager().registerTexture(TEXID, new NativeImageBackedTexture(img)); + var img = ImageIO.read(Http.get(url).sendInputStream()); + var baos = new ByteArrayOutputStream(); + ImageIO.write(img, "png", baos); + mc.getTextureManager().registerTexture(TEXID, new NativeImageBackedTexture(NativeImage.read(new ByteArrayInputStream(baos.toByteArray())))); empty = false; } catch (Exception ex) { E621Hud.LOG.error("Failed to render the image.", ex); diff --git a/src/main/java/anticope/esixtwoone/sources/DanBooru.java b/src/main/java/anticope/esixtwoone/sources/DanBooru.java new file mode 100644 index 0000000..1ac5bbe --- /dev/null +++ b/src/main/java/anticope/esixtwoone/sources/DanBooru.java @@ -0,0 +1,39 @@ +package anticope.esixtwoone.sources; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import meteordevelopment.meteorclient.utils.network.Http; + +public class DanBooru extends Source { + + private final String domain; + private final int lastPage; + + public DanBooru(String domain, int lastPage) { + this.domain = domain; + this.lastPage = lastPage; + } + + @Override + public void reset() {} + + @Override + public String randomImage(String filter, Size size) { + String query = String.format("%s/posts.json?tags=%s&page=%d&format=json&limit=10", domain, filter, random.nextInt(0, lastPage)); + JsonElement result = Http.get(query).sendJson(JsonElement.class); + if (result == null) return null; + + if (result instanceof JsonArray array) { + if (array.get(random.nextInt(0, Math.min(11, array.size()))) instanceof JsonObject post) { + return switch (size) { + case preview -> post.get("preview_file_url").getAsString(); + case sample -> post.get("large_file_url").getAsString(); + case file -> post.get("file_url").getAsString(); + }; + } + } + return null; + } +} diff --git a/src/main/java/anticope/esixtwoone/sources/ESixTwoOne.java b/src/main/java/anticope/esixtwoone/sources/ESixTwoOne.java index 52ed983..77ec91b 100644 --- a/src/main/java/anticope/esixtwoone/sources/ESixTwoOne.java +++ b/src/main/java/anticope/esixtwoone/sources/ESixTwoOne.java @@ -7,6 +7,11 @@ public class ESixTwoOne extends Source { + private final String domain; + + public ESixTwoOne(String domain) { + this.domain = domain; + } private int maxPage = 30; @Override @@ -17,15 +22,14 @@ public void reset() { @Override public String randomImage(String filter, Size size) { int pageNum = random.nextInt(1, maxPage); - JsonObject result = Http.get("https://e621.net/posts.json?limit=320&tags="+filter+"&page="+ pageNum).sendJson(JsonObject.class); + JsonObject result = Http.get(domain + "/posts.json?limit=320&tags="+filter+"&page="+ pageNum).sendJson(JsonObject.class); if (result.get("posts") instanceof JsonArray array) { if(array.size() <= 0) { maxPage = pageNum - 1; return null; } if (array.get(random.nextInt(array.size())) instanceof JsonObject post) { - var url = post.get(size.toString()).getAsJsonObject().get("url").getAsString(); - return url; + return post.get(size.toString()).getAsJsonObject().get("url").getAsString(); } } return null; diff --git a/src/main/java/anticope/esixtwoone/sources/NekosLife.java b/src/main/java/anticope/esixtwoone/sources/NekosLife.java new file mode 100644 index 0000000..cea7ece --- /dev/null +++ b/src/main/java/anticope/esixtwoone/sources/NekosLife.java @@ -0,0 +1,33 @@ +package anticope.esixtwoone.sources; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import meteordevelopment.meteorclient.utils.network.Http; + +public class NekosLife extends Source { + + private final String domain; + + public NekosLife(String domain) { + this.domain = domain; + } + + @Override + public void reset() {} + + @Override + public String randomImage(String filter, Size size) { + String query = String.format("%s/api/v2/img/%s", domain, filter); + JsonElement result = Http.get(query).sendJson(JsonElement.class); + if (result == null) return null; + + if (result instanceof JsonObject object) { + if (object.get("url") != null) { + return object.get("url").getAsString(); + } + } + + return null; + } +} diff --git a/src/main/java/anticope/esixtwoone/sources/Source.java b/src/main/java/anticope/esixtwoone/sources/Source.java index 9f0dcb6..b1f5fb2 100644 --- a/src/main/java/anticope/esixtwoone/sources/Source.java +++ b/src/main/java/anticope/esixtwoone/sources/Source.java @@ -15,8 +15,12 @@ public enum Size { public enum SourceType { e621, + e926, gelbooru, - rule34 + danbooru, + safebooru, + rule34, + nekoslife } protected final Random random = new Random(); @@ -36,10 +40,13 @@ public String getRandomImage(String filter, Size size) { public static Source getSource(SourceType type) { return switch (type) { - case e621 -> new ESixTwoOne(); - case gelbooru -> new GelBooru("https://gelbooru.com/", 700); - case rule34 -> new GelBooru("https://api.rule34.xxx/", 700); - default -> null; + case e621 -> new ESixTwoOne("https://e621.net"); + case e926 -> new ESixTwoOne("https://e926.net"); + case gelbooru -> new GelBooru("https://gelbooru.com", 700); + case danbooru -> new DanBooru("https://danbooru.donmai.us", 700); + case safebooru -> new GelBooru("https://safebooru.org", 700); + case rule34 -> new GelBooru("https://api.rule34.xxx", 700); + case nekoslife -> new NekosLife("https://nekos.life"); }; } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1fde26a..a2932f0 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -8,7 +8,7 @@ "AntiCope" ], "contact": { - "repo": "https://github.com/MeteorDevelopment/meteor-addon-template" + "repo": "https://github.com/AntiCope/meteor-e621-integration" }, "icon": "assets/e621/icon.png", "environment": "client",