Commit c80b0fee by Jonathan Thomas

More refactoring around PlayerData and player-based friendship. Also, adding in…

More refactoring around PlayerData and player-based friendship. Also, adding in migration of data from entity-based friendship to player-based.
parent 802d4d67
Pipeline #12734 passed with stages
in 1 minute 57 seconds
...@@ -4,6 +4,7 @@ import com.google.gson.Gson; ...@@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.owlmaddie.chat.ChatDataManager; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData; import com.owlmaddie.chat.EntityChatData;
import com.owlmaddie.chat.PlayerData;
import com.owlmaddie.ui.BubbleRenderer; import com.owlmaddie.ui.BubbleRenderer;
import com.owlmaddie.ui.PlayerMessageManager; import com.owlmaddie.ui.PlayerMessageManager;
import com.owlmaddie.utils.ClientEntityFinder; import com.owlmaddie.utils.ClientEntityFinder;
...@@ -100,7 +101,7 @@ public class ClientPackets { ...@@ -100,7 +101,7 @@ public class ClientPackets {
ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> { ClientPlayNetworking.registerGlobalReceiver(ServerPackets.PACKET_S2C_MESSAGE, (client, handler, buffer, responseSender) -> {
// Read the data from the server packet // Read the data from the server packet
UUID entityId = UUID.fromString(buffer.readString()); UUID entityId = UUID.fromString(buffer.readString());
String playerIdStr = buffer.readString(); UUID playerId = UUID.fromString(buffer.readString());
String message = buffer.readString(32767); String message = buffer.readString(32767);
int line = buffer.readInt(); int line = buffer.readInt();
String status_name = buffer.readString(32767); String status_name = buffer.readString(32767);
...@@ -113,18 +114,18 @@ public class ClientPackets { ...@@ -113,18 +114,18 @@ public class ClientPackets {
if (entity != null) { if (entity != null) {
ChatDataManager chatDataManager = ChatDataManager.getClientInstance(); ChatDataManager chatDataManager = ChatDataManager.getClientInstance();
EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString()); EntityChatData chatData = chatDataManager.getOrCreateChatData(entity.getUuidAsString());
chatData.playerId = playerIdStr; PlayerData playerData = chatData.getPlayerData(playerId);
if (!message.isEmpty()) { if (!message.isEmpty()) {
chatData.currentMessage = message; chatData.currentMessage = message;
} }
chatData.currentLineNumber = line; chatData.currentLineNumber = line;
chatData.status = ChatDataManager.ChatStatus.valueOf(status_name); chatData.status = ChatDataManager.ChatStatus.valueOf(status_name);
chatData.sender = ChatDataManager.ChatSender.valueOf(sender_name); chatData.sender = ChatDataManager.ChatSender.valueOf(sender_name);
chatData.friendship = friendship; playerData.friendship = friendship;
if (chatData.sender == ChatDataManager.ChatSender.USER && !playerIdStr.isEmpty()) { if (chatData.sender == ChatDataManager.ChatSender.USER) {
// Add player message to queue for rendering // Add player message to queue for rendering
PlayerMessageManager.addMessage(UUID.fromString(chatData.playerId), chatData.currentMessage, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE); PlayerMessageManager.addMessage(playerId, chatData.currentMessage, ChatDataManager.TICKS_TO_DISPLAY_USER_MESSAGE);
} }
// Play sound with volume based on distance (from player or entity) // Play sound with volume based on distance (from player or entity)
......
...@@ -3,6 +3,7 @@ package com.owlmaddie.ui; ...@@ -3,6 +3,7 @@ package com.owlmaddie.ui;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import com.owlmaddie.chat.ChatDataManager; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData; import com.owlmaddie.chat.EntityChatData;
import com.owlmaddie.chat.PlayerData;
import com.owlmaddie.utils.EntityHeights; import com.owlmaddie.utils.EntityHeights;
import com.owlmaddie.utils.EntityRendererAccessor; import com.owlmaddie.utils.EntityRendererAccessor;
import com.owlmaddie.utils.TextureLoader; import com.owlmaddie.utils.TextureLoader;
...@@ -10,6 +11,7 @@ import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; ...@@ -10,6 +11,7 @@ import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
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.network.ClientPlayerEntity;
import net.minecraft.client.render.*; import net.minecraft.client.render.*;
import net.minecraft.client.render.entity.EntityRenderer; import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
...@@ -470,10 +472,15 @@ public class BubbleRenderer { ...@@ -470,10 +472,15 @@ public class BubbleRenderer {
// Get position matrix // Get position matrix
Matrix4f matrix = matrices.peek().getPositionMatrix(); Matrix4f matrix = matrices.peek().getPositionMatrix();
// Get the player
ClientPlayerEntity player = MinecraftClient.getInstance().player;
// Look-up greeting (if any) // Look-up greeting (if any)
EntityChatData chatData = null; EntityChatData chatData = null;
PlayerData playerData = null;
if (entity instanceof MobEntity) { if (entity instanceof MobEntity) {
chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getUuidAsString()); chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getUuidAsString());
playerData = chatData.getPlayerData(player.getUuid());
} else if (entity instanceof PlayerEntity) { } else if (entity instanceof PlayerEntity) {
chatData = PlayerMessageManager.getMessage(entity.getUuid()); chatData = PlayerMessageManager.getMessage(entity.getUuid());
} }
...@@ -519,13 +526,13 @@ public class BubbleRenderer { ...@@ -519,13 +526,13 @@ public class BubbleRenderer {
drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, true); drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, true);
// Draw text background (no smaller than 50F tall) // Draw text background (no smaller than 50F tall)
drawTextBubbleBackground("text-top", matrices, -64, 0, 128, scaledTextHeight, chatData.friendship); drawTextBubbleBackground("text-top", matrices, -64, 0, 128, scaledTextHeight, playerData.friendship);
// Draw face icon of entity // Draw face icon of entity
drawEntityIcon(matrices, entity, -82, 7, 32, 32); drawEntityIcon(matrices, entity, -82, 7, 32, 32);
// Draw Friendship status // Draw Friendship status
drawFriendshipStatus(matrices, 51, 18, 31, 21, chatData.friendship); drawFriendshipStatus(matrices, 51, 18, 31, 21, playerData.friendship);
// Draw 'arrows' & 'keyboard' buttons // Draw 'arrows' & 'keyboard' buttons
if (chatData.currentLineNumber > 0) { if (chatData.currentLineNumber > 0) {
...@@ -545,10 +552,10 @@ public class BubbleRenderer { ...@@ -545,10 +552,10 @@ public class BubbleRenderer {
drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, false); drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, false);
// Draw 'resume chat' button // Draw 'resume chat' button
if (chatData.friendship == 3) { if (playerData.friendship == 3) {
// Friend chat bubble // Friend chat bubble
drawIcon("button-chat-friend", matrices, -16, textHeaderHeight, 32, 17); drawIcon("button-chat-friend", matrices, -16, textHeaderHeight, 32, 17);
} else if (chatData.friendship == -3) { } else if (playerData.friendship == -3) {
// Enemy chat bubble // Enemy chat bubble
drawIcon("button-chat-enemy", matrices, -16, textHeaderHeight, 32, 17); drawIcon("button-chat-enemy", matrices, -16, textHeaderHeight, 32, 17);
} else { } else {
...@@ -561,7 +568,7 @@ public class BubbleRenderer { ...@@ -561,7 +568,7 @@ public class BubbleRenderer {
drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, true); drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING, true);
// Draw text background // Draw text background
drawTextBubbleBackground("text-top-player", matrices, -64, 0, 128, scaledTextHeight, chatData.friendship); drawTextBubbleBackground("text-top-player", matrices, -64, 0, 128, scaledTextHeight, playerData.friendship);
// Draw face icon of player // Draw face icon of player
drawPlayerIcon(matrices, entity, -75, 14, 18, 18); drawPlayerIcon(matrices, entity, -75, 14, 18, 18);
......
...@@ -15,10 +15,7 @@ import org.slf4j.LoggerFactory; ...@@ -15,10 +15,7 @@ import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
...@@ -137,11 +134,11 @@ public class ChatDataManager { ...@@ -137,11 +134,11 @@ public class ChatDataManager {
} }
// Save chat data to file // Save chat data to file
public String GetLightChatData() { public String GetLightChatData(UUID playerId) {
try { try {
// Create "light" version of entire chat data HashMap // Create "light" version of entire chat data HashMap
HashMap<String, EntityChatDataLight> lightVersionMap = new HashMap<>(); HashMap<String, EntityChatDataLight> lightVersionMap = new HashMap<>();
this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion())); this.entityChatDataMap.forEach((id, entityChatData) -> lightVersionMap.put(id, entityChatData.toLightVersion(playerId)));
// Convert light chat data to JSON string // Convert light chat data to JSON string
return GSON.toJson(lightVersionMap).toString(); return GSON.toJson(lightVersionMap).toString();
...@@ -174,6 +171,11 @@ public class ChatDataManager { ...@@ -174,6 +171,11 @@ public class ChatDataManager {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(loadFile), StandardCharsets.UTF_8)) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream(loadFile), StandardCharsets.UTF_8)) {
Type type = new TypeToken<ConcurrentHashMap<String, EntityChatData>>(){}.getType(); Type type = new TypeToken<ConcurrentHashMap<String, EntityChatData>>(){}.getType();
this.entityChatDataMap = GSON.fromJson(reader, type); this.entityChatDataMap = GSON.fromJson(reader, type);
// Post-process each EntityChatData object
for (EntityChatData entityChatData : entityChatDataMap.values()) {
entityChatData.postDeserializeInitialization();
}
} catch (Exception e) { } catch (Exception e) {
LOGGER.error("Error loading chat data", e); LOGGER.error("Error loading chat data", e);
this.entityChatDataMap = new ConcurrentHashMap<>(); this.entityChatDataMap = new ConcurrentHashMap<>();
......
package com.owlmaddie.chat;
import java.util.ArrayList;
import java.util.List;
public class PlayerData {
public List<ChatMessage> messages;
public int friendship;
public PlayerData() {
this.messages = new ArrayList<>();
this.friendship = 0;
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package com.owlmaddie.mixin; ...@@ -2,6 +2,7 @@ package com.owlmaddie.mixin;
import com.owlmaddie.chat.ChatDataManager; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData; import com.owlmaddie.chat.EntityChatData;
import com.owlmaddie.chat.PlayerData;
import com.owlmaddie.network.ServerPackets; import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
...@@ -31,8 +32,9 @@ public class MixinLivingEntity { ...@@ -31,8 +32,9 @@ public class MixinLivingEntity {
private void modifyCanTarget(LivingEntity target, CallbackInfoReturnable<Boolean> cir) { private void modifyCanTarget(LivingEntity target, CallbackInfoReturnable<Boolean> cir) {
if (target instanceof PlayerEntity) { if (target instanceof PlayerEntity) {
LivingEntity thisEntity = (LivingEntity) (Object) this; LivingEntity thisEntity = (LivingEntity) (Object) this;
EntityChatData chatData = getChatData(thisEntity); EntityChatData entityData = getChatData(thisEntity);
if (chatData.friendship > 0) { PlayerData playerData = entityData.getPlayerData(target.getUuid());
if (playerData.friendship > 0) {
// Friendly creatures can't target a player // Friendly creatures can't target a player
cir.setReturnValue(false); cir.setReturnValue(false);
} }
......
...@@ -2,6 +2,7 @@ package com.owlmaddie.mixin; ...@@ -2,6 +2,7 @@ package com.owlmaddie.mixin;
import com.owlmaddie.chat.ChatDataManager; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData; import com.owlmaddie.chat.EntityChatData;
import com.owlmaddie.chat.PlayerData;
import com.owlmaddie.network.ServerPackets; import com.owlmaddie.network.ServerPackets;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.TameableEntity; import net.minecraft.entity.passive.TameableEntity;
...@@ -53,7 +54,8 @@ public class MixinMobEntity { ...@@ -53,7 +54,8 @@ public class MixinMobEntity {
// Get chat data for entity // Get chat data for entity
ChatDataManager chatDataManager = ChatDataManager.getServerInstance(); ChatDataManager chatDataManager = ChatDataManager.getServerInstance();
EntityChatData chatData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString()); EntityChatData entityData = chatDataManager.getOrCreateChatData(thisEntity.getUuidAsString());
PlayerData playerData = entityData.getPlayerData(player.getUuid());
// Check if the player successfully interacts with an item // Check if the player successfully interacts with an item
if (player instanceof ServerPlayerEntity) { if (player instanceof ServerPlayerEntity) {
...@@ -73,11 +75,11 @@ public class MixinMobEntity { ...@@ -73,11 +75,11 @@ public class MixinMobEntity {
String giveItemMessage = "<" + serverPlayer.getName().getString() + String giveItemMessage = "<" + serverPlayer.getName().getString() +
action_verb + "you " + itemCount + " " + itemName + ">"; action_verb + "you " + itemCount + " " + itemName + ">";
if (!chatData.characterSheet.isEmpty() && chatData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) { if (!entityData.characterSheet.isEmpty() && entityData.auto_generated < chatDataManager.MAX_AUTOGENERATE_RESPONSES) {
ServerPackets.generate_chat("N/A", chatData, serverPlayer, thisEntity, giveItemMessage, true); ServerPackets.generate_chat("N/A", entityData, serverPlayer, thisEntity, giveItemMessage, true);
} }
} else if (itemStack.isEmpty() && chatData.friendship == 3) { } else if (itemStack.isEmpty() && playerData.friendship == 3) {
// Player's hand is empty, Ride your best friend! // Player's hand is empty, Ride your best friend!
player.startRiding(thisEntity, true); player.startRiding(thisEntity, true);
} }
......
...@@ -3,6 +3,7 @@ package com.owlmaddie.network; ...@@ -3,6 +3,7 @@ package com.owlmaddie.network;
import com.owlmaddie.chat.ChatDataManager; import com.owlmaddie.chat.ChatDataManager;
import com.owlmaddie.chat.EntityChatData; import com.owlmaddie.chat.EntityChatData;
import com.owlmaddie.chat.ChatDataSaverScheduler; import com.owlmaddie.chat.ChatDataSaverScheduler;
import com.owlmaddie.chat.PlayerData;
import com.owlmaddie.commands.ConfigurationHandler; import com.owlmaddie.commands.ConfigurationHandler;
import com.owlmaddie.goals.EntityBehaviorManager; import com.owlmaddie.goals.EntityBehaviorManager;
import com.owlmaddie.goals.GoalPriority; import com.owlmaddie.goals.GoalPriority;
...@@ -169,7 +170,7 @@ public class ServerPackets { ...@@ -169,7 +170,7 @@ public class ServerPackets {
LOGGER.info("Server send compressed, chunked login message packets to player: " + player.getName().getString()); LOGGER.info("Server send compressed, chunked login message packets to player: " + player.getName().getString());
// Get lite JSON data & compress to byte array // Get lite JSON data & compress to byte array
String chatDataJSON = ChatDataManager.getServerInstance().GetLightChatData(); String chatDataJSON = ChatDataManager.getServerInstance().GetLightChatData(player.getUuid());
byte[] compressedData = Compression.compressString(chatDataJSON); byte[] compressedData = Compression.compressString(chatDataJSON);
if (compressedData == null) { if (compressedData == null) {
LOGGER.error("Failed to compress chat data."); LOGGER.error("Failed to compress chat data.");
...@@ -314,19 +315,21 @@ public class ServerPackets { ...@@ -314,19 +315,21 @@ public class ServerPackets {
entity.setPersistent(); entity.setPersistent();
} }
// Iterate over all players and send the packet
for (ServerPlayerEntity player : serverInstance.getPlayerManager().getPlayerList()) {
PlayerData playerData = chatData.getPlayerData(player.getUuid());
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer()); PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
// Write the entity's chat updated data // Write the entity's chat updated data
buffer.writeString(chatData.entityId); buffer.writeString(chatData.entityId);
buffer.writeString(chatData.playerId); buffer.writeString(player.getUuidAsString());
buffer.writeString(chatData.currentMessage); buffer.writeString(chatData.currentMessage);
buffer.writeInt(chatData.currentLineNumber); buffer.writeInt(chatData.currentLineNumber);
buffer.writeString(chatData.status.toString()); buffer.writeString(chatData.status.toString());
buffer.writeString(chatData.sender.toString()); buffer.writeString(chatData.sender.toString());
buffer.writeInt(chatData.friendship); buffer.writeInt(playerData.friendship);
// Iterate over all players and send the packet
for (ServerPlayerEntity player : serverInstance.getPlayerManager().getPlayerList()) {
LOGGER.debug("Server broadcast message to client: " + player.getName().getString() + " | Message: " + chatData.currentMessage); LOGGER.debug("Server broadcast message to client: " + player.getName().getString() + " | Message: " + chatData.currentMessage);
ServerPlayNetworking.send(player, PACKET_S2C_MESSAGE, buffer); ServerPlayNetworking.send(player, PACKET_S2C_MESSAGE, buffer);
} }
......
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