diff options
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/gui/utils')
4 files changed, 101 insertions, 5 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java b/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java index 2910d0f..25c4cb7 100644 --- a/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java +++ b/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java @@ -1,11 +1,75 @@ package dev.isxander.yacl3.gui.utils; +import com.mojang.blaze3d.platform.NativeImage; import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; import net.minecraft.locale.Language; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; +import net.minecraft.resources.ResourceLocation; + +import java.util.function.Consumer; public class GuiUtils { + public static void drawSpecial(GuiGraphics graphics, Consumer<MultiBufferSource> consumer) { + //? if >=1.21.2 { + graphics.drawSpecial(consumer); + //?} else { + /*MultiBufferSource.BufferSource bufferSource = graphics.bufferSource(); + consumer.accept(bufferSource); + bufferSource.endBatch(); + *///?} + } + + public static void blitGuiTex(GuiGraphics graphics, ResourceLocation texture, int x, int y, float u, float v, int textureWidth, int textureHeight, int width, int height) { + graphics.blit( + //? if >=1.21.2 + RenderType::guiTextured, + texture, + x, y, + u, v, + textureWidth, textureHeight, + width, height + ); + } + + public static void blitGuiTexColor(GuiGraphics graphics, ResourceLocation texture, int x, int y, float u, float v, int textureWidth, int textureHeight, int width, int height, int color) { + //? if <1.21.2 { + /*float a = (color >> 24 & 255) / 255.0F; + float r = (color >> 16 & 255) / 255.0F; + float g = (color >> 8 & 255) / 255.0F; + float b = (color & 255) / 255.0F; + graphics.setColor(r, g, b, a); + *///?} + graphics.blit( + //? if >=1.21.2 + RenderType::guiTextured, + texture, + x, y, + u, v, + textureWidth, textureHeight, + width, height + //? if >=1.21.2 + ,color + ); + //? if <1.21.2 + /*graphics.setColor(1.0F, 1.0F, 1.0F, 1.0F);*/ + } + + //? if >1.20.1 { + public static void blitSprite(GuiGraphics graphics, ResourceLocation sprite, int x, int y, int width, int height) { + graphics.blitSprite( + //? if >=1.21.2 + RenderType::guiTextured, + sprite, + x, y, + width, height + ); + } + //?} + public static MutableComponent translatableFallback(String key, Component fallback) { if (Language.getInstance().has(key)) return Component.translatable(key); @@ -29,4 +93,21 @@ public class GuiUtils { return string; } + + + public static void setPixelARGB(NativeImage nativeImage, int x, int y, int argb) { + // In 1.21.2+, you set the pixel color in ARGB format, where it internally converts to ABGR. + // Before this, you need to directly set the pixel color in ABGR format. + + //? if >=1.21.2 { + nativeImage.setPixel(x, y, argb); + //?} else { + /*int a = (argb >> 24) & 0xFF; + int r = (argb >> 16) & 0xFF; + int g = (argb >> 8) & 0xFF; + int b = argb & 0xFF; + int abgr = (a << 24) | (b << 16) | (g << 8) | r; + nativeImage.setPixelRGBA(x, y, abgr); // method name is misleading. It's actually ABGR. + *///?} + } } diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java b/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java index bb6c664..dc769bc 100644 --- a/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java +++ b/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java @@ -46,7 +46,7 @@ public final class ItemRegistryHelper { try { ResourceLocation itemIdentifier = YACLPlatform.parseRl(identifier.toLowerCase()); if (BuiltInRegistries.ITEM.containsKey(itemIdentifier)) { - return BuiltInRegistries.ITEM.get(itemIdentifier); + return MiscUtil.getFromRegistry(BuiltInRegistries.ITEM, itemIdentifier); } } catch (ResourceLocationException ignored) { } @@ -80,14 +80,15 @@ public final class ItemRegistryHelper { if (sep == -1) { filterPredicate = identifier -> identifier.getPath().contains(value) - || BuiltInRegistries.ITEM.get(identifier).getDescription().getString().toLowerCase().contains(value.toLowerCase()); + || MiscUtil.getFromRegistry(BuiltInRegistries.ITEM, identifier) + /*? if >=1.21.2 {*/ .getName() /*?} else {*/ /*.getDescription() *//*?}*/ + .getString().toLowerCase().contains(value.toLowerCase()); } else { String namespace = value.substring(0, sep); String path = value.substring(sep + 1); filterPredicate = identifier -> identifier.getNamespace().equals(namespace) && identifier.getPath().startsWith(path); } - return BuiltInRegistries.ITEM.holders() - .map(holder -> holder.key().location()) + return BuiltInRegistries.ITEM.keySet().stream() .filter(filterPredicate) /* Sort items as follows based on the given "value" string's path: diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/MiscUtil.java b/src/main/java/dev/isxander/yacl3/gui/utils/MiscUtil.java new file mode 100644 index 0000000..354b38e --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/gui/utils/MiscUtil.java @@ -0,0 +1,14 @@ +package dev.isxander.yacl3.gui.utils; + +import net.minecraft.core.Registry; +import net.minecraft.resources.ResourceLocation; + +public class MiscUtil { + public static <T> T getFromRegistry(Registry<T> registry, ResourceLocation identifier) { + //? if >=1.21.2 { + return registry.getValue(identifier); + //?} else { + /*return registry.get(identifier); + *///?} + } +} diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/YACLRenderHelper.java b/src/main/java/dev/isxander/yacl3/gui/utils/YACLRenderHelper.java index aadc249..23e97c3 100644 --- a/src/main/java/dev/isxander/yacl3/gui/utils/YACLRenderHelper.java +++ b/src/main/java/dev/isxander/yacl3/gui/utils/YACLRenderHelper.java @@ -19,7 +19,7 @@ public class YACLRenderHelper { public static void renderButtonTexture(GuiGraphics graphics, int x, int y, int width, int height, boolean enabled, boolean focused) { /*? if >1.20.1 {*/ - graphics.blitSprite(SPRITES.get(enabled, focused), x, y, width, height); + GuiUtils.blitSprite(graphics, SPRITES.get(enabled, focused), x, y, width, height); /*?} else {*/ /*int textureV; if (enabled) { |
