MixinEntityChatData.java 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package com.owlmaddie.mixin;

import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.UUID;

@Mixin(Entity.class)
public abstract class MixinEntityChatData {
    @Shadow
    public abstract UUID getUuid();

    /**
     * When writing NBT data, if the entity has chat data then store its UUID under "CCUUID".
     */
    @Inject(method = "writeNbt", at = @At("TAIL"))
    private void writeChatData(NbtCompound nbt, CallbackInfoReturnable<NbtCompound> cir) {
        UUID currentUUID = this.getUuid();

        // Retrieve or create the chat data for this entity.
        EntityChatData chatData = ChatDataManager.getServerInstance().getOrCreateChatData(currentUUID.toString());
        // If the entity actually has chat data (for example, if its character sheet is non-empty), add CCUUID.
        if (!chatData.characterSheet.isEmpty()) {
            // Note: cir.getReturnValue() returns the NBT compound the method is about to return.
            cir.getReturnValue().putUuid("CCUUID", currentUUID);
        }
    }

    /**
     * When reading NBT data, if there is a "CCUUID" entry and it does not match the entity’s current UUID,
     * update our chat data key to reflect the change.
     */
    @Inject(method = "readNbt", at = @At("TAIL"))
    private void readChatData(NbtCompound nbt, CallbackInfo ci) {
        UUID currentUUID = this.getUuid();
        if (nbt.contains("CCUUID")) {
            UUID originalUUID = nbt.getUuid("CCUUID");
            if (!originalUUID.equals(currentUUID)) {
                ChatDataManager.getServerInstance().updateUUID(originalUUID.toString(), currentUUID.toString());
            }
        }
    }
}