package com.mojang.realmsclient.util; import com.google.common.collect.Maps; import com.mojang.blaze3d.platform.NativeImage; import com.mojang.logging.LogUtils; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Base64; import java.util.Map; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.DynamicTexture; import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite; import net.minecraft.resources.Identifier; import org.jspecify.annotations.Nullable; import org.lwjgl.system.MemoryUtil; import org.slf4j.Logger; public class RealmsTextureManager { private static final Map TEXTURES = Maps.newHashMap(); private static final Logger LOGGER = LogUtils.getLogger(); private static final Identifier TEMPLATE_ICON_LOCATION = Identifier.withDefaultNamespace("textures/gui/presets/isles.png"); public static Identifier worldTemplate(String string, @Nullable String string1) { return string1 == null ? TEMPLATE_ICON_LOCATION : getTexture(string, string1); } private static Identifier getTexture(String string, String string1) { RealmsTextureManager.RealmsTexture realmsTexture = TEXTURES.get(string); if (realmsTexture != null && realmsTexture.image().equals(string1)) { return realmsTexture.textureId; } else { NativeImage nativeImage = loadImage(string1); if (nativeImage == null) { Identifier identifier1 = MissingTextureAtlasSprite.getLocation(); TEXTURES.put(string, new RealmsTextureManager.RealmsTexture(string1, identifier1)); return identifier1; } else { Identifier identifier = Identifier.fromNamespaceAndPath("realms", "dynamic/" + string); Minecraft.getInstance().getTextureManager().register(identifier, new DynamicTexture(identifier::toString, nativeImage)); TEXTURES.put(string, new RealmsTextureManager.RealmsTexture(string1, identifier)); return identifier; } } } @Nullable private static NativeImage loadImage(String string) { byte[] b = Base64.getDecoder().decode(string); ByteBuffer byteBuffer = MemoryUtil.memAlloc(b.length); try { return NativeImage.read(byteBuffer.put(b).flip()); } catch (IOException iOException) { LOGGER.warn("Failed to load world image: {}", string, iOException); } finally { MemoryUtil.memFree(byteBuffer); } return null; } public static record RealmsTexture(String image, Identifier textureId) { } }