Commit b2450841 by Jonathan Thomas

Merge branch 'bucketable' into 'develop'

Fixing Bucketable Entities w/ ChatData

See merge request !4
parents c602f2cd c165ed73
Pipeline #12212 passed with stage
in 1 minute 51 seconds
...@@ -12,11 +12,13 @@ All notable changes to **CreatureChat** are documented in this file. The format ...@@ -12,11 +12,13 @@ All notable changes to **CreatureChat** are documented in this file. The format
- Added support for commands to use different data types (`String`, `Integer`) - Added support for commands to use different data types (`String`, `Integer`)
### Changed ### Changed
- Water bucket is now ignored for item-giving detection (since the entity is despawned immediately)
- Updated error messages to `RED` color for maximum attention - Updated error messages to `RED` color for maximum attention
- Updated `/creaturechat help` output - Updated `/creaturechat help` output
- Updated `README.md` with new command documentation - Updated `README.md` with new command documentation
### Fixed ### Fixed
- Bucketing a creature now maintains chat history when respawned
- Chats broken when OS locale is non-English language (i.e. `assistant to ass\u0131stant`) - Chats broken when OS locale is non-English language (i.e. `assistant to ass\u0131stant`)
## [1.0.3] - 2024-05-10 ## [1.0.3] - 2024-05-10
......
...@@ -429,6 +429,21 @@ public class ChatDataManager { ...@@ -429,6 +429,21 @@ public class ChatDataManager {
return entityChatDataMap.computeIfAbsent(entityId, k -> new EntityChatData(entityId, "")); return entityChatDataMap.computeIfAbsent(entityId, k -> new EntityChatData(entityId, ""));
} }
// Update the UUID in the map (i.e. bucketed entity and then released, changes their UUID)
public void updateUUID(String oldUUID, String newUUID) {
EntityChatData data = entityChatDataMap.remove(oldUUID);
if (data != null) {
data.entityId = newUUID;
entityChatDataMap.put(newUUID, data);
LOGGER.info("Updated chat data from UUID (" + oldUUID + ") to UUID (" + newUUID + ")");
// Broadcast to all players
ServerPackets.BroadcastPacketMessage(data);
} else {
LOGGER.info("Unable to update chat data, UUID not found: " + oldUUID);
}
}
// Generate quest data for this server session // Generate quest data for this server session
public void generateQuest() { public void generateQuest() {
// Get items needed for Quest prompt // Get items needed for Quest prompt
......
package com.owlmaddie.mixin;
import com.owlmaddie.chat.ChatDataManager;
import net.minecraft.entity.Bucketable;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.UUID;
/**
* The {@code MixinBucketable} mixin class handles entities that are placed into a bucket, despawned, respawned
* and updates our chat history for the newly spawned entity.
*/
@Mixin(Bucketable.class)
public interface MixinBucketable {
//
@Inject(method = "copyDataToStack(Lnet/minecraft/entity/mob/MobEntity;Lnet/minecraft/item/ItemStack;)V", at = @At("TAIL"))
private static void addCCUUIDToStack(MobEntity entity, ItemStack stack, CallbackInfo ci) {
Logger LOGGER = LoggerFactory.getLogger("creaturechat");
UUID originalUUID = entity.getUuid();
LOGGER.info("Saving original UUID of bucketed entity: " + originalUUID);
// Add the original UUID to the ItemStack NBT as "CCUUID"
NbtCompound nbt = stack.getOrCreateNbt();
nbt.putUuid("CCUUID", originalUUID);
}
// New method to read CCUUID from NBT
@Inject(method = "copyDataFromNbt(Lnet/minecraft/entity/mob/MobEntity;Lnet/minecraft/nbt/NbtCompound;)V", at = @At("TAIL"))
private static void readCCUUIDFromNbt(MobEntity entity, NbtCompound nbt, CallbackInfo ci) {
Logger LOGGER = LoggerFactory.getLogger("creaturechat");
UUID newUUID = entity.getUuid();
if (nbt.contains("CCUUID")) {
UUID originalUUID = nbt.getUuid("CCUUID");
LOGGER.info("Duplicating bucketed chat data for original UUID (" + originalUUID + ") to cloned entity: (" + newUUID + ")");
ChatDataManager.getServerInstance().updateUUID(originalUUID.toString(), newUUID.toString());
}
}
}
\ No newline at end of file
...@@ -5,6 +5,7 @@ import com.owlmaddie.network.ServerPackets; ...@@ -5,6 +5,7 @@ import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
...@@ -25,7 +26,7 @@ public class MixinMobEntity { ...@@ -25,7 +26,7 @@ public class MixinMobEntity {
MobEntity thisEntity = (MobEntity) (Object) this; MobEntity thisEntity = (MobEntity) (Object) this;
// Check if the player successfully interacts with an item // Check if the player successfully interacts with an item
if (!itemStack.isEmpty() && player instanceof ServerPlayerEntity) { if (!itemStack.isEmpty() && player instanceof ServerPlayerEntity && itemStack.getItem() != Items.WATER_BUCKET) {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
String itemName = itemStack.getItem().toString(); String itemName = itemStack.getItem().toString();
int itemCount = itemStack.getCount(); int itemCount = itemStack.getCount();
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
"mixins": [ "mixins": [
"MixinMobEntity", "MixinMobEntity",
"MixinMobEntityAccessor", "MixinMobEntityAccessor",
"MixinLivingEntity" "MixinLivingEntity",
"MixinBucketable"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1
......
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