Skip to content

Commit

Permalink
add custom texture cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Aug 16, 2024
1 parent b084e7b commit 2d6cd82
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ craftedcore 5.3
- add new lang keys for changing config entries
- fix duplicate log entries with no internet connection
- add methods to check caching status (for player profiles)
- add custom texture cache

craftedcore 5.2
================
Expand Down
50 changes: 50 additions & 0 deletions common/src/main/java/tocraft/craftedcore/gui/TextureCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tocraft.craftedcore.gui;

import com.mojang.blaze3d.platform.NativeImage;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import tocraft.craftedcore.CraftedCore;
import tocraft.craftedcore.patched.Identifier;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings("unused")
@Environment(EnvType.CLIENT)
public class TextureCache {
private static final Map<String, ResourceLocation> TEXTURE_CACHE = new ConcurrentHashMap<>();

/**
* Converts a URL to a readable id
*
* @param namespace the namespace in which the id should be saved
* @param type for the id: "textures/entity/skin_123.png", choose: "entity"
* @param prefix for the id: "textures/entity/skin_123.png", choose: "skin". (some number will be auto-generated)
* @param fileType the file type, e.g. "png"
* @param textureURL the URL where the texture is found
* @return the id if the texture could be cached or null, if there was an exception
*/
@Nullable
public static ResourceLocation getTextureId(String namespace, String type, String prefix, String fileType, URL textureURL) {
return TEXTURE_CACHE.computeIfAbsent(String.valueOf(textureURL), key -> {
ResourceLocation id = Identifier.parse(namespace, "textures/" + type + "/" + prefix + key.hashCode() + "." + fileType);
try(InputStream is = textureURL.openStream()) {
NativeImage image = NativeImage.read(new ByteArrayInputStream(is.readAllBytes()));
DynamicTexture dynamicTexture = new DynamicTexture(image);
Minecraft.getInstance().getTextureManager().register(id, dynamicTexture);
} catch (IOException e) {
CraftedCore.LOGGER.error("Caught an exception while reading url: {}", textureURL, e);
return null;
}
return id;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tocraft.craftedcore.platform;

import com.google.gson.*;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tocraft.craftedcore.CraftedCore;
import tocraft.craftedcore.gui.TextureCache;
import tocraft.craftedcore.util.NetUtils;

import java.io.IOException;
Expand Down Expand Up @@ -135,4 +137,20 @@ private static UUID stringToUUID(final String input) throws IllegalArgumentExcep
return UUID.fromString(input.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5"));
}
}

public ResourceLocation getSkinId() {
if (skin != null) {
return TextureCache.getTextureId(CraftedCore.MODID, "entity", "custom_skin_", "png", skin);
} else {
return null;
}
}

public ResourceLocation getCapeId() {
if (cape != null) {
return TextureCache.getTextureId(CraftedCore.MODID, "entity", "custom_cape_", "png", cape);
} else {
return null;
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx4G
# Base Versions
minecraft=1.21
archives_base_name=craftedcore
mod_version=5.2
mod_version=5.3
artifact_type=release
maven_group=dev.tocraft
# Loader Versions
Expand Down

0 comments on commit 2d6cd82

Please sign in to comment.