Commit 9dffc2f4 by Jonathan Thomas

- Refactor of TextureLoader class, to utilize different functions for UI and entity icon loading

- New method to retrieve and use an Entity Renderer to return the texture path associated with it
- Large refactor of entity texture folders and file names to match the Minecraft texture paths.
parent 32293b1e
Pipeline #11934 passed with stage
in 20 seconds
......@@ -9,6 +9,7 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.font.TextRenderer.TextLayerType;
import net.minecraft.client.render.*;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
......@@ -22,11 +23,14 @@ import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
/**
* The {@code ClientInit} class initializes this mod in the client and defines all hooks into the
* render pipeline to draw chat bubbles, text, and entity icons.
*/
public class ClientInit implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
protected static TextureLoader textures = new TextureLoader();;
......@@ -60,13 +64,13 @@ public class ClientInit implements ClientModInitializer {
float z = 0.01F;
// Draw UI text background
RenderSystem.setShaderTexture(0, textures.Get("ui", "text-top"));
RenderSystem.setShaderTexture(0, textures.GetUI("text-top"));
drawTexturePart(matrices, buffer, x, y, z, width, 40);
RenderSystem.setShaderTexture(0, textures.Get("ui", "text-middle"));
RenderSystem.setShaderTexture(0, textures.GetUI("text-middle"));
drawTexturePart(matrices, buffer, x, y + 40, z, width, height);
RenderSystem.setShaderTexture(0, textures.Get("ui", "text-bottom"));
RenderSystem.setShaderTexture(0, textures.GetUI("text-bottom"));
drawTexturePart(matrices, buffer, x, y + 40 + height, z, width, 5);
RenderSystem.disableBlend();
......@@ -84,7 +88,7 @@ public class ClientInit implements ClientModInitializer {
private void drawIcon(String ui_icon_name, MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
// Draw button icon
Identifier button_texture = textures.Get("ui", ui_icon_name);
Identifier button_texture = textures.GetUI(ui_icon_name);
RenderSystem.setShaderTexture(0, button_texture);
RenderSystem.enableDepthTest();
RenderSystem.enableBlend();
......@@ -107,9 +111,12 @@ public class ClientInit implements ClientModInitializer {
}
private void drawEntityIcon(MatrixStack matrices, Entity entity, float x, float y, float width, float height) {
// Get entity renderer
EntityRenderer renderer = EntityRendererAccessor.getEntityRenderer(entity);
String entity_icon_path = renderer.getTexture(entity).getPath();
// Draw face icon
String entity_name = entity.getType().getUntranslatedName().toLowerCase(Locale.ROOT);
Identifier entity_id = textures.Get("entity", entity_name);
Identifier entity_id = textures.GetEntity(entity_icon_path);
if (entity_id == null) {
return;
}
......
package com.owlmaddie;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.entity.EntityRenderDispatcher;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.entity.Entity;
/**
* The {@code EntityRendererAccessor} class returns the EntityRenderer class for a specific Entity.
* This is needed to get the texture path associated with the entity (for rendering our icons).
*/
public class EntityRendererAccessor {
public static EntityRenderer<?> getEntityRenderer(Entity entity) {
MinecraftClient minecraftClient = MinecraftClient.getInstance();
EntityRenderDispatcher renderDispatcher = minecraftClient.getEntityRenderDispatcher();
return renderDispatcher.getRenderer(entity);
}
}
\ No newline at end of file
......@@ -8,15 +8,19 @@ import org.slf4j.LoggerFactory;
import java.util.Optional;
/**
* The {@code TextureLoader} class registers and returns texture identifiers for resources
* contained for this mod. UI and Entity icons.
*/
public class TextureLoader {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
public TextureLoader() {
}
public Identifier Get(String folder, String name) {
public Identifier GetUI(String name) {
// Attempt to load texture resource
String texture_path = "textures/" + folder + "/" + name + ".png";
String texture_path = "textures/ui/" + name + ".png";
Identifier textureId = new Identifier("mobgpt", texture_path);
Optional<Resource> resource = MinecraftClient.getInstance().getResourceManager().getResource(textureId);
......@@ -26,8 +30,25 @@ public class TextureLoader {
return textureId;
} else {
// Resource not found
//LOGGER.info(texture_path + " was not found in mobgpt");
LOGGER.info(texture_path + " was not found");
return null;
}
}
public Identifier GetEntity(String texturePath) {
Identifier textureId = new Identifier("mobgpt", texturePath);
Optional<Resource> resource = MinecraftClient.getInstance().getResourceManager().getResource(textureId);
if (resource.isPresent()) {
// Texture found, bind it and return the Identifier
MinecraftClient.getInstance().getTextureManager().bindTexture(textureId);
return textureId;
} else {
// Texture not found, log a message and return the "not_found" texture Identifier
LOGGER.info(texturePath + " was not found");
Identifier notFoundTextureId = new Identifier("mobgpt", "textures/entity/not_found.png");
MinecraftClient.getInstance().getTextureManager().bindTexture(notFoundTextureId);
return notFoundTextureId;
}
}
}
\ No newline at end of file
......@@ -23,7 +23,7 @@ import java.util.Locale;
import java.util.UUID;
/**
* The {@code ModInit} class initializes this mod and defines all the server message
* The {@code ModInit} class initializes this mod on the server and defines all the server message
* identifiers. It also listens for messages from the client, and has code to send
* messages to the client.
*/
......
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