| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.hash.Hashing;
|
| 4 | import com.mojang.blaze3d.platform.NativeImage;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 8 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.util.Util;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class FaviconTexture implements AutoCloseable {
|
| 15 | private static final Identifier MISSING_LOCATION = Identifier.withDefaultNamespace("textures/misc/unknown_server.png");
|
| 16 | private static final int WIDTH = 64;
|
| 17 | private static final int HEIGHT = 64;
|
| 18 | private final TextureManager textureManager;
|
| 19 | private final Identifier textureLocation;
|
| 20 | private @Nullable DynamicTexture texture;
|
| 21 | private boolean closed;
|
| 22 |
|
| 23 | private FaviconTexture(final TextureManager textureManager, final Identifier textureLocation) {
|
| 24 | this.textureManager = textureManager;
|
| 25 | this.textureLocation = textureLocation;
|
| 26 | }
|
| 27 |
|
| 28 | public static FaviconTexture forWorld(final TextureManager textureManager, final String levelId) {
|
| 29 | return new FaviconTexture(
|
| 30 | textureManager,
|
| 31 | Identifier.withDefaultNamespace(
|
| 32 | "worlds/" + Util.sanitizeName(levelId, Identifier::validPathChar) + "/" + Hashing.sha1().hashUnencodedChars(levelId) + "/icon"
|
| 33 | )
|
| 34 | );
|
| 35 | }
|
| 36 |
|
| 37 | public static FaviconTexture forServer(final TextureManager textureManager, final String address) {
|
| 38 | return new FaviconTexture(textureManager, Identifier.withDefaultNamespace("servers/" + Hashing.sha1().hashUnencodedChars(address) + "/icon"));
|
| 39 | }
|
| 40 |
|
| 41 | public void upload(final NativeImage image) {
|
| 42 | if (image.getWidth() == 64 && image.getHeight() == 64) {
|
| 43 | try {
|
| 44 | this.checkOpen();
|
| 45 | if (this.texture == null) {
|
| 46 | this.texture = new DynamicTexture(() -> "Favicon " + this.textureLocation, image);
|
| 47 | } else {
|
| 48 | this.texture.setPixels(image);
|
| 49 | this.texture.upload();
|
| 50 | }
|
| 51 |
|
| 52 | this.textureManager.register(this.textureLocation, this.texture);
|
| 53 | } catch (Throwable t) {
|
| 54 | image.close();
|
| 55 | this.clear();
|
| 56 | throw t;
|
| 57 | }
|
| 58 | } else {
|
| 59 | image.close();
|
| 60 | throw new IllegalArgumentException("Icon must be 64x64, but was " + image.getWidth() + "x" + image.getHeight());
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | public void clear() {
|
| 65 | this.checkOpen();
|
| 66 | if (this.texture != null) {
|
| 67 | this.textureManager.release(this.textureLocation);
|
| 68 | this.texture.close();
|
| 69 | this.texture = null;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public Identifier textureLocation() {
|
| 74 | return this.texture != null ? this.textureLocation : MISSING_LOCATION;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void close() {
|
| 79 | this.clear();
|
| 80 | this.closed = true;
|
| 81 | }
|
| 82 |
|
| 83 | public boolean isClosed() {
|
| 84 | return this.closed;
|
| 85 | }
|
| 86 |
|
| 87 | private void checkOpen() {
|
| 88 | if (this.closed) {
|
| 89 | throw new IllegalStateException("Icon already closed");
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|