From b561e36dd10d77dc577da8809bbf0150cab39b5e Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Wed, 21 Aug 2024 11:37:51 +0300 Subject: [PATCH] Updated wildcard icon --- .../web/local/client/ImageGenerator.java | 114 ++++++++++-------- .../web/local/client/KubeJSClientWeb.java | 44 ++++++- .../assets/kubejs/textures/misc/star.png | Bin 1112 -> 0 bytes .../kubejs/textures/misc/star.png.mcmeta | 5 - .../assets/kubejs/textures/misc/wildcard.png | Bin 0 -> 212 bytes 5 files changed, 104 insertions(+), 59 deletions(-) delete mode 100644 src/main/resources/assets/kubejs/textures/misc/star.png delete mode 100644 src/main/resources/assets/kubejs/textures/misc/star.png.mcmeta create mode 100644 src/main/resources/assets/kubejs/textures/misc/wildcard.png diff --git a/src/main/java/dev/latvian/mods/kubejs/web/local/client/ImageGenerator.java b/src/main/java/dev/latvian/mods/kubejs/web/local/client/ImageGenerator.java index f7a6d4438..5c386d880 100644 --- a/src/main/java/dev/latvian/mods/kubejs/web/local/client/ImageGenerator.java +++ b/src/main/java/dev/latvian/mods/kubejs/web/local/client/ImageGenerator.java @@ -22,6 +22,7 @@ import dev.latvian.mods.kubejs.util.CachedComponentObject; import dev.latvian.mods.kubejs.util.Cast; import dev.latvian.mods.kubejs.web.KJSHTTPRequest; +import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -75,7 +76,7 @@ public class ImageGenerator { new Vector3f(0.625F, 0.625F, 0.625F) ); - public static final ResourceLocation STAR_TEXTURE = KubeJS.id("textures/misc/star.png"); + public static final ResourceLocation WILDCARD_TEXTURE = KubeJS.id("textures/misc/wildcard.png"); private record RenderImage(Minecraft mc, GuiGraphics graphics, int size) { } @@ -94,14 +95,34 @@ public boolean equals(Object o) { public static final Int2ObjectMap FB_CACHE = new Int2ObjectArrayMap<>(); - private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, String dir, @Nullable UUID cacheUUID, Consumer render) { + public static TextureTarget getCanvas(int size) { + var target = FB_CACHE.get(size); + + if (target == null) { + target = new TextureTarget(size, size, true, Minecraft.ON_OSX); + target.setClearColor(0.54F, 0.54F, 0.54F, 0F); + FB_CACHE.put(size, target); + } + + return target; + } + + private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, String dir, @Nullable ByteBuf cacheBuf, boolean wildcard, Consumer render) { int size = Integer.parseInt(req.variable("size")); if (size < 1 || size > 1024) { return HTTPStatus.BAD_REQUEST.text("Invalid size, must be [1, 1024]"); } - var cacheUUIDStr = cacheUUID == null || req.query().containsKey("uncached") ? null : UUIDWrapper.toString(cacheUUID); + if (req.query().containsKey("uncached")) { + cacheBuf = null; + } + + if (cacheBuf != null) { + cacheBuf.writeBoolean(wildcard); + } + + var cacheUUIDStr = cacheBuf == null ? null : UUIDWrapper.toString(UUID.nameUUIDFromBytes(cacheBuf.array())); var cachePath = cacheUUIDStr == null ? null : KubeJSPaths.dir(KubeJSPaths.LOCAL.resolve("cache/web/img/" + dir + "/" + cacheUUIDStr.substring(0, 2))).resolve(cacheUUIDStr + "_" + size + ".png"); if (cachePath != null && Files.exists(cachePath)) { @@ -109,12 +130,7 @@ private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, Str } var bytes = req.supplyInMainThread(() -> { - var target = FB_CACHE.get(size); - - if (target == null) { - target = new TextureTarget(size, size, true, Minecraft.ON_OSX); - FB_CACHE.put(size, target); - } + var target = getCanvas(size); var mc = Minecraft.getInstance(); var bufferSource = mc.renderBuffers().bufferSource(); @@ -131,6 +147,13 @@ private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, Str GuiGraphics graphics = new GuiGraphics(mc, bufferSource); render.accept(new RenderImage(mc, graphics, size)); + + if (wildcard) { + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + graphics.blit(WILDCARD_TEXTURE, 0, 0, 300, 0F, 0F, 16, 16, 16, 16); + } + graphics.flush(); target.bindRead(); @@ -139,6 +162,20 @@ private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, Str try (var image = new NativeImage(size, size, false)) { image.downloadTexture(0, false); image.flipY(); + + for (int y = 0; y < size; y++) { + for (int x = 0; x < size; x++) { + int color = image.getPixelRGBA(x, y); + int a = (color >> 24) & 0xFF; + + if (a == 0) { + image.setPixelRGBA(x, y, 0); + } else if (a < 255) { + image.setPixelRGBA(x, y, (color & 0xFFFFFF) | 0xFF000000); + } + } + } + return image.asByteArray(); } catch (Exception ex) { ex.printStackTrace(); @@ -162,14 +199,18 @@ private static HTTPResponse renderCanvas(KJSHTTPRequest req, int canvasSize, Str return HTTPResponse.ok().content(bytes, "image/png").header("X-KubeJS-Cache-Key", cacheUUIDStr); } - private static HTTPResponse renderAnimated(KJSHTTPRequest req, String dir, @Nullable UUID cacheUUID, List responses) throws Exception { + private static HTTPResponse renderAnimated(KJSHTTPRequest req, String dir, @Nullable ByteBuf cacheBuf, List responses) throws Exception { int size = Integer.parseInt(req.variable("size")); if (size < 1 || size > 1024) { return HTTPStatus.BAD_REQUEST.text("Invalid size, must be [1, 1024]"); } - var cacheUUIDStr = cacheUUID == null || req.query().containsKey("uncached") ? null : UUIDWrapper.toString(cacheUUID); + if (req.query().containsKey("uncached")) { + cacheBuf = null; + } + + var cacheUUIDStr = cacheBuf == null ? null : UUIDWrapper.toString(UUID.nameUUIDFromBytes(cacheBuf.array())); var cachePath = cacheUUIDStr == null ? null : KubeJSPaths.dir(KubeJSPaths.LOCAL.resolve("cache/web/img/" + dir + "/" + cacheUUIDStr.substring(0, 2))).resolve(cacheUUIDStr + "_" + size + ".gif"); if (cachePath != null && Files.exists(cachePath)) { @@ -180,9 +221,8 @@ private static HTTPResponse renderAnimated(KJSHTTPRequest req, String dir, @Null var encoder = new AnimatedGifEncoder(); encoder.start(outputStream); encoder.setSize(size, size); - var backgroundColor = new Color(43, 51, 51, 255); - encoder.setBackground(backgroundColor); - encoder.setTransparent(backgroundColor, true); + encoder.setBackground(Color.BLUE); + encoder.setTransparent(Color.BLUE, false); encoder.setRepeat(0); encoder.setDelay(1000); @@ -220,36 +260,29 @@ private static HTTPResponse renderAnimated(KJSHTTPRequest req, String dir, @Null public static HTTPResponse item(KJSHTTPRequest req) throws Exception { var stack = BuiltInRegistries.ITEM.get(req.id()).getDefaultInstance(); stack.applyComponents(req.components(req.registries().nbt())); - return renderItem(req, stack, req.query().containsKey("star")); + return renderItem(req, stack, req.query().containsKey("wildcard")); } - private static HTTPResponse renderItem(KJSHTTPRequest req, ItemStack stack, boolean star) throws Exception { + public static HTTPResponse renderItem(KJSHTTPRequest req, ItemStack stack, boolean wildcard) { if (stack.isEmpty()) { return HTTPStatus.NOT_FOUND; } var buf = new FriendlyByteBuf(Unpooled.buffer()); CachedComponentObject.writeCacheKey(buf, stack.getItem(), DataComponentWrapper.visualPatch(stack.getComponentsPatch())); - buf.writeBoolean(star); - return renderCanvas(req, 16, "item", UUID.nameUUIDFromBytes(buf.array()), render -> { + return renderCanvas(req, 16, "item", buf, wildcard, render -> { render.graphics.renderFakeItem(stack, 0, 0, 0); render.graphics.renderItemDecorations(render.mc.font, stack, 0, 0); - - if (star) { - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - render.graphics.blit(STAR_TEXTURE, 0, 0, 300, 0F, 0F, 6, 6, 6, 6); - } }); } public static HTTPResponse block(KJSHTTPRequest req) throws Exception { var state = BlockWrapper.withProperties(BuiltInRegistries.BLOCK.get(req.id()).defaultBlockState(), req.query()); - return renderBlock(req, state, req.query().containsKey("star")); + return renderBlock(req, state, req.query().containsKey("wildcard")); } - private static HTTPResponse renderBlock(KJSHTTPRequest req, BlockState state, boolean star) throws Exception { + public static HTTPResponse renderBlock(KJSHTTPRequest req, BlockState state, boolean wildcard) { if (state.isEmpty()) { return HTTPStatus.NOT_FOUND; } @@ -270,9 +303,7 @@ private static HTTPResponse renderBlock(KJSHTTPRequest req, BlockState state, bo } } - buf.writeBoolean(star); - - return renderCanvas(req, 16, "block", UUID.nameUUIDFromBytes(buf.array()), render -> { + return renderCanvas(req, 16, "block", buf, wildcard, render -> { var model = render.mc.getBlockRenderer().getBlockModel(state); var pose = render.graphics.pose(); pose.pushPose(); @@ -310,22 +341,16 @@ private static HTTPResponse renderBlock(KJSHTTPRequest req, BlockState state, bo } render.graphics.pose().popPose(); - - if (star) { - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - render.graphics.blit(STAR_TEXTURE, 0, 0, 300, 0F, 0F, 6, 6, 6, 6); - } }); } public static HTTPResponse fluid(KJSHTTPRequest req) throws Exception { var stack = new FluidStack(BuiltInRegistries.FLUID.get(req.id()), FluidType.BUCKET_VOLUME); stack.applyComponents(req.components(req.registries().nbt())); - return renderFluid(req, stack, req.query().containsKey("star")); + return renderFluid(req, stack, req.query().containsKey("wildcard")); } - public static HTTPResponse renderFluid(KJSHTTPRequest req, FluidStack stack, boolean star) throws Exception { + public static HTTPResponse renderFluid(KJSHTTPRequest req, FluidStack stack, boolean wildcard) { if (stack.isEmpty()) { return HTTPStatus.NOT_FOUND; } @@ -340,9 +365,8 @@ public static HTTPResponse renderFluid(KJSHTTPRequest req, FluidStack stack, boo var buf = new FriendlyByteBuf(Unpooled.buffer()); CachedComponentObject.writeCacheKey(buf, stack.getFluid(), DataComponentWrapper.visualPatch(stack.getComponentsPatch())); - buf.writeBoolean(star); - return renderCanvas(req, 16, "fluid", UUID.nameUUIDFromBytes(buf.array()), render -> { + return renderCanvas(req, 16, "fluid", buf, wildcard, render -> { var s = render.mc.getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(still); RenderSystem.setShaderTexture(0, TextureAtlas.LOCATION_BLOCKS); RenderSystem.setShader(GameRenderer::getPositionTexColorShader); @@ -353,12 +377,6 @@ public static HTTPResponse renderFluid(KJSHTTPRequest req, FluidStack stack, boo builder.addVertex(m, 16F, 16F, 0F).setUv(s.getU1(), s.getV0()).setColor(r, g, b, a); builder.addVertex(m, 16F, 0F, 0F).setUv(s.getU1(), s.getV1()).setColor(r, g, b, a); BufferUploader.drawWithShader(builder.buildOrThrow()); - - if (star) { - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - render.graphics.blit(STAR_TEXTURE, 0, 0, 300, 0F, 0F, 6, 6, 6, 6); - } }); } @@ -377,7 +395,7 @@ public static HTTPResponse itemTag(KJSHTTPRequest req) throws Exception { list.add(renderItem(req, holder.value().getDefaultInstance(), true)); } - return renderAnimated(req, "item_tag", UUID.nameUUIDFromBytes(buf.array()), list); + return renderAnimated(req, "item_tag", buf, list); } public static HTTPResponse blockTag(KJSHTTPRequest req) throws Exception { @@ -402,7 +420,7 @@ public static HTTPResponse blockTag(KJSHTTPRequest req) throws Exception { } } - return renderAnimated(req, "block_tag", UUID.nameUUIDFromBytes(buf.array()), list); + return renderAnimated(req, "block_tag", buf, list); } public static HTTPResponse fluidTag(KJSHTTPRequest req) throws Exception { @@ -420,7 +438,7 @@ public static HTTPResponse fluidTag(KJSHTTPRequest req) throws Exception { list.add(renderFluid(req, new FluidStack(holder, FluidType.BUCKET_VOLUME), true)); } - return renderAnimated(req, "fluid_tag", UUID.nameUUIDFromBytes(buf.array()), list); + return renderAnimated(req, "fluid_tag", buf, list); } private static class ContentGrabber extends HTTPResponseBuilder { diff --git a/src/main/java/dev/latvian/mods/kubejs/web/local/client/KubeJSClientWeb.java b/src/main/java/dev/latvian/mods/kubejs/web/local/client/KubeJSClientWeb.java index afb7a2bfc..c58fe1db2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/web/local/client/KubeJSClientWeb.java +++ b/src/main/java/dev/latvian/mods/kubejs/web/local/client/KubeJSClientWeb.java @@ -52,9 +52,9 @@ public class KubeJSClientWeb { public static void register(LocalWebServerRegistry registry) { KubeJSWeb.addScriptTypeEndpoints(registry, ScriptType.CLIENT, KubeJS.getClientScriptManager()::reload); - registry.get("/api/client/search/items", KubeJSClientWeb::getItemsResponse); - registry.get("/api/client/search/blocks", KubeJSClientWeb::getBlocksResponse); - registry.get("/api/client/search/fluids", KubeJSClientWeb::getFluidsResponse); + registry.get("/api/client/search/items", KubeJSClientWeb::getSearchItems); + registry.get("/api/client/search/blocks", KubeJSClientWeb::getSearchBlocks); + registry.get("/api/client/search/fluids", KubeJSClientWeb::getSearchFluids); registry.get("/api/client/assets/list/", KubeJSClientWeb::getAssetList); registry.get("/api/client/assets/get/{namespace}/", KubeJSClientWeb::getAssetContent); @@ -90,7 +90,7 @@ private static HTTPResponse getScreenshot(KJSHTTPRequest req) { return HTTPResponse.ok().content(bytes, "image/png"); } - private static HTTPResponse getItemsResponse(KJSHTTPRequest req) { + private static HTTPResponse getSearchItems(KJSHTTPRequest req) { return HTTPResponse.ok().content(JsonContent.object(json -> { var jsonOps = Minecraft.getInstance().level == null ? req.registries().json() : Minecraft.getInstance().level.registryAccess().createSerializationContext(JsonOps.INSTANCE); var nbtOps = Minecraft.getInstance().level == null ? req.registries().nbt() : Minecraft.getInstance().level.registryAccess().createSerializationContext(NbtOps.INSTANCE); @@ -126,6 +126,16 @@ private static HTTPResponse getItemsResponse(KJSHTTPRequest req) { } } + var tags = new JsonArray(); + + for (var t : item.value().builtInRegistryHolder().tags().toList()) { + tags.add(t.location().toString()); + } + + if (!tags.isEmpty()) { + o.add("tags", tags); + } + results.add(o); } @@ -134,23 +144,45 @@ private static HTTPResponse getItemsResponse(KJSHTTPRequest req) { })); } - private static HTTPResponse getBlocksResponse(KJSHTTPRequest req) { + private static HTTPResponse getSearchBlocks(KJSHTTPRequest req) { return HTTPResponse.ok().content(JsonContent.array(json -> { for (var block : BuiltInRegistries.BLOCK) { var o = new JsonObject(); o.addProperty("id", block.kjs$getId()); o.addProperty("name", Component.translatable(block.getDescriptionId()).getString()); + + var tags = new JsonArray(); + + for (var t : block.builtInRegistryHolder().tags().toList()) { + tags.add(t.location().toString()); + } + + if (!tags.isEmpty()) { + o.add("tags", tags); + } + json.add(o); } })); } - private static HTTPResponse getFluidsResponse(KJSHTTPRequest req) { + private static HTTPResponse getSearchFluids(KJSHTTPRequest req) { return HTTPResponse.ok().content(JsonContent.array(json -> { for (var fluid : BuiltInRegistries.FLUID) { var o = new JsonObject(); o.addProperty("id", fluid.kjs$getId()); o.addProperty("name", fluid.getFluidType().getDescription().getString()); + + var tags = new JsonArray(); + + for (var t : fluid.builtInRegistryHolder().tags().toList()) { + tags.add(t.location().toString()); + } + + if (!tags.isEmpty()) { + o.add("tags", tags); + } + json.add(o); } })); diff --git a/src/main/resources/assets/kubejs/textures/misc/star.png b/src/main/resources/assets/kubejs/textures/misc/star.png deleted file mode 100644 index 188303defe7799d3b98b193670fe938517301a54..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1112 zcmV-e1gHCnP) zcWw&&02M)Z3aLdT6|`t07#3-1nf?3!dESe2pZW0Kci#7$xv%#Je|VX>_nhZB_k29( zJg2Ftq@<*zq$COe{GaY0===lJ81=G#`~NpIbhwk6r2e9AR(^N-=Nb}qdumI?`GgzP z2CCUV-*C{$$EizGoKN_H8uZUM6m+na+V31sxI=wJb@=xh20FKr`Yr8uir=YK{=J5P zj`dJqI)`WC7}f3HFFbT=6Kux67n%@9{QHH54)syzoTGDbhI-JKQ%GRT-Z*TM%gQa- zQd#_EkxmRyU#I=c#ARy0mscq0KnwLA@_qe;f2nt=R$p#mpz%*rSJHmv;umVjms<#E z_+sj)bEq1=qI!M#3l7+1*A8?18R`(M8_$=&sRPurR4>);>kbZVY->j+g9fQ#>P?u_ zKf>1X|4>{0q`sjJQah*-Y6;a%b)cSS3$Peo&Mu|a!6vrDu$fxnt9Ev^_}W=f<0`09 z-4|fCOu}qiPA#HZydSxMb!i1KFTgf1-&HEq8eQwJTPtMSAF!ZHHjh$$-nUG^#wb^IrJnMBWd^nqXchQ1U2Y+`NO|U>C>){sy?sGo#mlIloIxQ`j`0a^S1@3EhFyp= z6tpGZ?rjPVjaZL3tAgqr_4Wh@me-H5BB;)d-k#vVLe3aN;5Iex?FkBOv!uW%2ATsV z5Ho$o%G;<n`C7yYf)bRG8uoStgO$GmTaWXixIt<2Q?Sj9x^gvl2>ziq zd%J?c%HKx5G?~8GMd~f;F<1^yNmk|a6i}T^v+TCJn?9$&_6z>+dARo>M^Hy0luQUj<5 z>~Y2jbxYj_)y)o_%U6Br5q7z9HT6ZM%$eAY`JVk(3v4H-!$>);)gV$Aj8$(* eNl8gbqwqi7hx<+VMrx7(0000GS38a`yg8YL2|7W;0*Mi(Ad2iZ<%YXO0!3`ur! y<_=3V0vQVJ>*61