Commit 13979b85 by Jonathan Thomas

Updated ServerEntityFinder::getEntityByUUID to be more generic and so it can find players and mobs.

parent 4c63e1dc
......@@ -15,6 +15,7 @@ All notable changes to **CreatureChat** are documented in this file. The format
- Improved **FLEE** behavior, to make it more reliable and more random.
- Improved **FOLLOW** behavior, supporting teleporting entities (Enderman, Endermite, and Shulker)
- Refactored **ATTACK** behavior to allow more flexibility (in order to support PROTECT behavior)
- Updated ServerEntityFinder::getEntityByUUID to be more generic and so it can find players and mobs.
## [1.0.6] - 2024-06-17
......
......@@ -189,7 +189,7 @@ public class ChatDataManager {
contextData.put("world_moon_phase", moonPhaseDescription);
// Get Entity details
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), UUID.fromString(entityId));
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), UUID.fromString(entityId));
if (entity.getCustomName() == null) {
contextData.put("entity_name", "");
} else {
......@@ -256,7 +256,7 @@ public class ChatDataManager {
ParsedMessage result = MessageParser.parseMessage(output_message.replace("\n", " "));
// Apply behaviors (if any)
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), UUID.fromString(entityId));
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), UUID.fromString(entityId));
for (Behavior behavior : result.getBehaviors()) {
LOGGER.info("Behavior: " + behavior.getName() + (behavior.getArgument() != null ?
", Argument: " + behavior.getArgument() : ""));
......
......@@ -30,7 +30,7 @@ public class FollowPlayerGoal extends Goal {
@Override
public boolean canStart() {
// Start only if the target player is more than 8 blocks away
return this.targetPlayer != null && this.entity.squaredDistanceTo(this.targetPlayer) > 64;
return this.targetPlayer != null && this.targetPlayer.isAlive() && this.entity.squaredDistanceTo(this.targetPlayer) > 64;
}
@Override
......
......@@ -59,7 +59,7 @@ public class ServerPackets {
// Ensure that the task is synced with the server thread
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) {
......@@ -76,7 +76,7 @@ public class ServerPackets {
// Ensure that the task is synced with the server thread
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
......@@ -96,7 +96,7 @@ public class ServerPackets {
// Ensure that the task is synced with the server thread
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 3.5F);
......@@ -115,7 +115,7 @@ public class ServerPackets {
// Ensure that the task is synced with the server thread
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
// Set talk to player goal (prevent entity from walking off)
TalkPlayerGoal talkGoal = new TalkPlayerGoal(player, entity, 7F);
......@@ -144,7 +144,7 @@ public class ServerPackets {
// Ensure that the task is synced with the server thread
server.execute(() -> {
MobEntity entity = ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(player.getServerWorld(), entityId);
if (entity != null) {
ChatDataManager.EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(entity.getUuidAsString());
if (chatData.characterSheet.isEmpty()) {
......@@ -270,7 +270,7 @@ public class ServerPackets {
public static void BroadcastPacketMessage(ChatDataManager.EntityChatData chatData) {
for (ServerWorld world : serverInstance.getWorlds()) {
UUID entityId = UUID.fromString(chatData.entityId);
MobEntity entity = ServerEntityFinder.getEntityByUUID(world, entityId);
MobEntity entity = (MobEntity)ServerEntityFinder.getEntityByUUID(world, entityId);
if (entity != null) {
// Set custom name (if null)
String characterName = chatData.getCharacterProp("name");
......
package com.owlmaddie.utils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.server.world.ServerWorld;
import java.util.UUID;
/**
* The {@code ServerEntityFinder} class is used to find a specific MobEntity by UUID, since
* The {@code ServerEntityFinder} class is used to find a specific LivingEntity by UUID, since
* there is not a built-in method for this.
*/
public class ServerEntityFinder {
public static MobEntity getEntityByUUID(ServerWorld world, UUID uuid) {
public static LivingEntity getEntityByUUID(ServerWorld world, UUID uuid) {
for (Entity entity : world.iterateEntities()) {
if (entity.getUuid().equals(uuid) && entity instanceof MobEntity) {
return (MobEntity)entity;
if (entity.getUuid().equals(uuid) && entity instanceof LivingEntity) {
return (LivingEntity) entity;
}
}
return null; // Entity not found
......
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