Commit d275adb2 by Jonathan Thomas

Added initial C2S packet and click handler. Refactor of json messages. Clean-up…

Added initial C2S packet and click handler. Refactor of json messages. Clean-up of imports and removed a few log statements. Removed old experimental code.
parent da5c9769
Pipeline #11638 passed with stage
in 22 seconds
...@@ -7,16 +7,15 @@ import net.minecraft.client.render.Camera; ...@@ -7,16 +7,15 @@ import net.minecraft.client.render.Camera;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.text.Text;
import net.minecraft.util.math.Box; import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClickHandler { public class ClickHandler {
...@@ -107,7 +106,8 @@ public class ClickHandler { ...@@ -107,7 +106,8 @@ public class ClickHandler {
// Handle the click for the closest entity after the loop // Handle the click for the closest entity after the loop
if (closestEntity != null) { if (closestEntity != null) {
LOGGER.info("Clicked on text bubble above: " + closestEntity.getType().getName().getString()); LOGGER.info("Clicked on text bubble above: " + closestEntity.getType().getName().getString());
client.player.sendMessage(Text.literal("Clicked on text bubble above: " + closestEntity.getType().getName().getString()), false); //client.player.sendMessage(Text.literal("Clicked on text bubble above: " + closestEntity.getType().getName().getString()), false);
ModPackets.sendEntityClickPacket(closestEntity);
} }
} }
......
...@@ -2,14 +2,13 @@ package com.owlmaddie; ...@@ -2,14 +2,13 @@ package com.owlmaddie;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import com.owlmaddie.json.ChatGPTResponse;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer; import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.font.TextRenderer.TextLayerType; import net.minecraft.client.font.TextRenderer.TextLayerType;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.*; import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
...@@ -22,7 +21,6 @@ import net.minecraft.util.math.Box; ...@@ -22,7 +21,6 @@ import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Quaternionf; import org.joml.Quaternionf;
import org.slf4j.Logger; import org.slf4j.Logger;
......
package com.owlmaddie;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.entity.Entity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.Camera;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.font.TextRenderer.TextLayerType;
public class LabelEntityRenderer<T extends Entity> extends EntityRenderer<T> {
public LabelEntityRenderer(EntityRendererFactory.Context context) {
super(context);
}
@Override
public Identifier getTexture(T entity) {
return MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(entity).getTexture(entity);
}
@Override
public void render(T entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light) {
super.render(entity, yaw, tickDelta, matrices, vertexConsumers, light);
//MinecraftClient.getInstance().getEntityRenderDispatcher().getRenderer(entity).render(entity, yaw, tickDelta, matrices, vertexConsumers, light);
Camera camera = MinecraftClient.getInstance().gameRenderer.getCamera();
double cameraDistanceToEntity = camera.getPos().squaredDistanceTo(entity.getPos());
// Only show the label if we're within a certain distance (e.g., 25 blocks).
if (cameraDistanceToEntity <= 625) { // 25 * 25 to avoid sqrt for distance check.
// Get the name of the entity to display.
Text text = Text.literal("I'm a " + entity.getType().getName().getString());
// Calculate the position above the entity's head.
double yOffset = entity.getHeight() + 0.5; // Adjust this to position the label correctly.
// Push a new matrix onto the stack.
matrices.push();
// Translate to the entity's position.
matrices.translate(0, yOffset, 0);
// Rotate the label to always face the player.
matrices.multiply(camera.getRotation());
// Scale down the label so it's not huge.
matrices.scale(-0.025F, -0.025F, 0.025F);
// Render the text.
TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
VertexConsumerProvider.Immediate immediate = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
fontRenderer.draw(text, -fontRenderer.getWidth(text) / 2f, 0, 0xFFFFFF, false, matrices.peek().getPositionMatrix(), immediate, TextLayerType.NORMAL, 0, light);
// Pop the matrix to return to the original state.
matrices.pop();
}
}
}
\ No newline at end of file
package com.owlmaddie;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.network.PacketByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ModPackets {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
public static void sendEntityClickPacket(Entity entity) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeInt(entity.getId());
// Send C2S packet
ClientPlayNetworking.send(ExampleMod.PACKET_CLIENT_CLICK, buf);
}
}
package com.owlmaddie; package com.owlmaddie;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.resource.Resource;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import java.util.Map;
import java.util.HashMap;
import java.util.Optional;
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;
import java.util.Optional;
public class TextureLoader { public class TextureLoader {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); 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() { public TextureLoader() {
} }
...@@ -33,7 +26,7 @@ public class TextureLoader { ...@@ -33,7 +26,7 @@ public class TextureLoader {
return textureId; return textureId;
} else { } else {
// Resource not found // Resource not found
LOGGER.info(texture_path + " was not found in mobgpt"); //LOGGER.info(texture_path + " was not found in mobgpt");
return null; return null;
} }
} }
......
package com.owlmaddie; package com.owlmaddie.json;
import java.util.List; import java.util.List;
......
// GreetingResponse.java // GreetingResponse.java
package com.owlmaddie; package com.owlmaddie.json;
public class GreetingResponse { public class GreetingResponse {
private String greeting; private String greeting;
......
package com.owlmaddie; package com.owlmaddie;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.Entity;
import net.minecraft.util.Identifier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class ExampleMod implements ModInitializer { public class ExampleMod implements ModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
public static final Identifier PACKET_CLIENT_CLICK = new Identifier("mobgpt", "packet_client_click");
@Override @Override
public void onInitialize() { public void onInitialize() {
...@@ -17,6 +18,20 @@ public class ExampleMod implements ModInitializer { ...@@ -17,6 +18,20 @@ public class ExampleMod implements ModInitializer {
// However, some things (like resources) may still be uninitialized. // However, some things (like resources) may still be uninitialized.
// Proceed with mild caution. // Proceed with mild caution.
LOGGER.info("Hello COW TEXT world!"); ServerPlayNetworking.registerGlobalReceiver(PACKET_CLIENT_CLICK, (server, player, handler, buf, responseSender) -> {
int entityId = buf.readInt();
// Ensure that the task is synced with the server thread
server.execute(() -> {
// Your logic here, e.g., handle the entity click
Entity entity = player.getServerWorld().getEntityById(entityId);
if (entity != null) {
// Perform action with the clicked entity
LOGGER.info("Entity received: " + entity.getType().toString());
}
});
});
LOGGER.info("MobGPT Initialized!");
} }
} }
\ 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