Commit 655a6356 by Jonathan Thomas

Removing textures.json files - not needed anymore. Simplify texture loading and error handling.

parent 7e853d9e
Pipeline #11635 passed with stage
in 22 seconds
import com.google.gson.Gson
import com.google.gson.GsonBuilder
plugins { plugins {
id 'fabric-loom' version '1.4-SNAPSHOT' id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish' id 'maven-publish'
...@@ -92,47 +89,3 @@ publishing { ...@@ -92,47 +89,3 @@ publishing {
// retrieving dependencies. // retrieving dependencies.
} }
} }
task generateEntityJson {
doLast {
def resourcesDir = new File('src/main/resources/assets/mobgpt/textures/entity') // adjust the path
def outputJson = new File('src/main/resources/assets/mobgpt/textures/entity/textures.json') // output file
def pngFiles = resourcesDir.listFiles(new FilenameFilter() {
@Override
boolean accept(File dir, String name) {
return name.endsWith('.png');
}
})
def resourcesList = pngFiles.collect { it.name }
// Using GSON to write the JSON
Gson gson = new GsonBuilder().setPrettyPrinting().create()
outputJson.text = gson.toJson(resourcesList)
}
}
task generateUiJson {
doLast {
def resourcesDir = new File('src/main/resources/assets/mobgpt/textures/ui') // adjust the path
def outputJson = new File('src/main/resources/assets/mobgpt/textures/ui/textures.json') // output file
def pngFiles = resourcesDir.listFiles(new FilenameFilter() {
@Override
boolean accept(File dir, String name) {
return name.endsWith('.png');
}
})
def resourcesList = pngFiles.collect { it.name }
// Using GSON to write the JSON
Gson gson = new GsonBuilder().setPrettyPrinting().create()
outputJson.text = gson.toJson(resourcesList)
}
}
// Ensure the task runs before the build
build.dependsOn generateEntityJson
build.dependsOn generateUiJson
...@@ -184,13 +184,13 @@ public class ExampleModClient implements ClientModInitializer { ...@@ -184,13 +184,13 @@ public class ExampleModClient implements ClientModInitializer {
float z = 0.01F; float z = 0.01F;
// Draw UI text background // Draw UI text background
RenderSystem.setShaderTexture(0, textures.Get("text-top")); RenderSystem.setShaderTexture(0, textures.Get("ui", "text-top"));
drawTexturePart(matrices, buffer, x, y, z, width, 40); drawTexturePart(matrices, buffer, x, y, z, width, 40);
RenderSystem.setShaderTexture(0, textures.Get("text-middle")); RenderSystem.setShaderTexture(0, textures.Get("ui", "text-middle"));
drawTexturePart(matrices, buffer, x, y + 40, z, width, height); drawTexturePart(matrices, buffer, x, y + 40, z, width, height);
RenderSystem.setShaderTexture(0, textures.Get("text-bottom")); RenderSystem.setShaderTexture(0, textures.Get("ui", "text-bottom"));
drawTexturePart(matrices, buffer, x, y + 40 + height, z, width, 5); drawTexturePart(matrices, buffer, x, y + 40 + height, z, width, 5);
RenderSystem.disableBlend(); RenderSystem.disableBlend();
...@@ -209,7 +209,7 @@ public class ExampleModClient implements ClientModInitializer { ...@@ -209,7 +209,7 @@ public class ExampleModClient implements ClientModInitializer {
private void drawEntityIcon(MatrixStack matrices, Entity entity, float x, float y, float width, float height) { private void drawEntityIcon(MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
// Draw face icon // Draw face icon
String entity_name = entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT); String entity_name = entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT);
Identifier entity_id = textures.Get(entity_name); Identifier entity_id = textures.Get("entity", entity_name);
if (entity_id == null) { if (entity_id == null) {
return; return;
} }
......
...@@ -4,11 +4,12 @@ import net.minecraft.client.MinecraftClient; ...@@ -4,11 +4,12 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.io.InputStreamReader; import java.util.Optional;
import java.io.InputStream;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.minecraft.resource.Resource;
public class TextureLoader { public class TextureLoader {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
...@@ -18,44 +19,21 @@ public class TextureLoader { ...@@ -18,44 +19,21 @@ public class TextureLoader {
private static final Map<String, Identifier> TEXTURE_MAP = new HashMap<>(); private static final Map<String, Identifier> TEXTURE_MAP = new HashMap<>();
public TextureLoader() { public TextureLoader() {
// Load Entity Textures
InputStream inputStream = TextureLoader.class.getResourceAsStream(ASSETS_PATH + ENTITY_TEXTURE_PATH + "textures.json");
Gson gson = new Gson();
// Assuming your JSON is a list of strings, i.e., filenames.
String[] filenames = gson.fromJson(new InputStreamReader(inputStream), String[].class);
for(String filename : filenames) {
String texturePath = ENTITY_TEXTURE_PATH + filename;
Identifier textureId = new Identifier("mobgpt", texturePath);
TEXTURE_MAP.put(filename.replace(".png", ""), textureId);
}
// Load UI Textures
inputStream = TextureLoader.class.getResourceAsStream(ASSETS_PATH + UI_TEXTURE_PATH + "textures.json");
gson = new Gson();
// Assuming your JSON is a list of strings, i.e., filenames.
filenames = gson.fromJson(new InputStreamReader(inputStream), String[].class);
for(String filename : filenames) {
String texturePath = UI_TEXTURE_PATH + filename;
Identifier textureId = new Identifier("mobgpt", texturePath);
TEXTURE_MAP.put(filename.replace(".png", ""), textureId);
}
} }
public Identifier Get(String name) { public Identifier Get(String folder, String name) {
Identifier textureId = TEXTURE_MAP.get(name); // Attempt to load texture resource
String texture_path = "textures/" + folder + "/" + name + ".png";
Identifier textureId = new Identifier("mobgpt", texture_path);
Optional<Resource> resource = MinecraftClient.getInstance().getResourceManager().getResource(textureId);
if (textureId != null) { if (!resource.isEmpty()) {
// Bind texture, and return Identity
MinecraftClient.getInstance().getTextureManager().bindTexture(textureId); MinecraftClient.getInstance().getTextureManager().bindTexture(textureId);
return textureId; return textureId;
} else { } else {
// You can bind and return a default texture here if you have one // Resource not found
// textureManager.bindTexture(DEFAULT_TEXTURE_ID); LOGGER.info(texture_path + " was not found in mobgpt");
// return DEFAULT_TEXTURE_ID;
// Or just return null or handle the missing texture situation as needed
LOGGER.info(name + " was not found in mobgpt");
return null; return null;
} }
} }
......
[
"bee_angry.png",
"pig.png",
"rabbit_black.png",
"cow.png",
"zombie.png",
"rabbit_brown.png",
"villager.png",
"frog_gray.png",
"skeleton.png",
"rabbit_white_splotched.png",
"wolf_angry.png",
"frog_orange.png",
"wolf.png",
"rabbit_white.png",
"chicken.png",
"shulker.png",
"rabbit_gold.png",
"bee.png",
"creeper.png",
"rabbit_toast.png",
"rabbit_salt.png",
"enderman.png",
"frog_green.png",
"warden.png"
]
\ No newline at end of file
[
"keyboard.png",
"text-middle.png",
"dotdot.png",
"button-chat.png",
"text-top.png",
"arrow1.png",
"arrow2.png",
"text-bottom.png"
]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment