Commit 7e853d9e by Jonathan Thomas

- Writing textures.json files in gradle (for iterating over all known mod textures)

- Removing old mod textures, and static loading
- Dynamic loading of textures now working
parent 5f047612
Pipeline #11634 passed with stage
in 21 seconds
import com.google.gson.Gson
import com.google.gson.GsonBuilder
plugins {
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish'
......@@ -89,3 +92,47 @@ publishing {
// 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
......@@ -56,18 +56,7 @@ import com.google.gson.Gson;
public class ExampleModClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
private static String funnyGreeting = "Greetings!"; // Default greeting. This will be overwritten by ChatGPT response.
private static final Identifier PIG = new Identifier("mobgpt", "textures/pig.png");
private static final Identifier COW = new Identifier("mobgpt", "textures/cow.png");
private static final Identifier WOLF = new Identifier("mobgpt", "textures/wolf.png");
private static final Identifier CHICKEN = new Identifier("mobgpt", "textures/chicken.png");
private static final Identifier ARROW1 = new Identifier("mobgpt", "textures/arrow1.png");
private static final Identifier ARROW2 = new Identifier("mobgpt", "textures/arrow2.png");
private static final Identifier TEXT_TOP = new Identifier("mobgpt", "textures/text-top.png");
private static final Identifier TEXT_MIDDLE = new Identifier("mobgpt", "textures/text-middle.png");
private static final Identifier TEXT_BOTTOM = new Identifier("mobgpt", "textures/text-bottom.png");
private static final Identifier KEYBOARD = new Identifier("mobgpt", "textures/keyboard.png");
private static final Identifier DOTDOT = new Identifier("mobgpt", "textures/dotdot.png");
protected static TextureLoader textures = new TextureLoader();;
@Override
public void onInitializeClient() {
......@@ -195,16 +184,15 @@ public class ExampleModClient implements ClientModInitializer {
float z = 0.01F;
// Draw UI text background
RenderSystem.setShaderTexture(0, TEXT_TOP);
RenderSystem.setShaderTexture(0, textures.Get("text-top"));
drawTexturePart(matrices, buffer, x, y, z, width, 40);
RenderSystem.setShaderTexture(0, TEXT_MIDDLE);
RenderSystem.setShaderTexture(0, textures.Get("text-middle"));
drawTexturePart(matrices, buffer, x, y + 40, z, width, height);
RenderSystem.setShaderTexture(0, TEXT_BOTTOM);
RenderSystem.setShaderTexture(0, textures.Get("text-bottom"));
drawTexturePart(matrices, buffer, x, y + 40 + height, z, width, 5);
RenderSystem.disableBlend();
RenderSystem.disableDepthTest();
}
......@@ -220,20 +208,13 @@ public class ExampleModClient implements ClientModInitializer {
private void drawEntityIcon(MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
// Draw face icon
switch (entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT)) {
case "pig":
RenderSystem.setShaderTexture(0, PIG);
break;
case "chicken":
RenderSystem.setShaderTexture(0, CHICKEN);
break;
case "wolf":
RenderSystem.setShaderTexture(0, WOLF);
break;
case "cow":
RenderSystem.setShaderTexture(0, COW);
break;
String entity_name = entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT);
Identifier entity_id = textures.Get(entity_name);
if (entity_id == null) {
return;
}
RenderSystem.setShaderTexture(0, entity_id);
RenderSystem.enableDepthTest();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
......@@ -256,14 +237,6 @@ public class ExampleModClient implements ClientModInitializer {
private void drawTextAboveEntities(WorldRenderContext context) {
MinecraftClient.getInstance().getTextureManager().bindTexture(PIG);
MinecraftClient.getInstance().getTextureManager().bindTexture(CHICKEN);
MinecraftClient.getInstance().getTextureManager().bindTexture(WOLF);
MinecraftClient.getInstance().getTextureManager().bindTexture(COW);
MinecraftClient.getInstance().getTextureManager().bindTexture(ARROW1);
MinecraftClient.getInstance().getTextureManager().bindTexture(ARROW2);
Camera camera = context.camera();
Entity cameraEntity = camera.getFocusedEntity();
if (cameraEntity == null) return;
......
package com.owlmaddie;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Identifier;
import java.util.Map;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.io.InputStream;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TextureLoader {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
private static final String ASSETS_PATH = "/assets/mobgpt/";
private static final String ENTITY_TEXTURE_PATH = "textures/entity/";
private static final String UI_TEXTURE_PATH = "textures/ui/";
private static final Map<String, Identifier> TEXTURE_MAP = new HashMap<>();
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) {
Identifier textureId = TEXTURE_MAP.get(name);
if (textureId != null) {
MinecraftClient.getInstance().getTextureManager().bindTexture(textureId);
return textureId;
} else {
// You can bind and return a default texture here if you have one
// textureManager.bindTexture(DEFAULT_TEXTURE_ID);
// 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;
}
}
}
\ No newline at end of file
[
"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