Commit 558cebb2 by Jonathan Thomas

- Fix drawEntityName to support both MobEntity and PlayerEntity classes

- Filter out the current player if in 1st person, otherwise render their bubbles
- Refactor render code to better support missing chatData, for example a Player with no chatData still needs a name rendered
parent 2f458d42
Pipeline #12001 passed with stage
in 20 seconds
...@@ -222,11 +222,17 @@ public class BubbleRenderer { ...@@ -222,11 +222,17 @@ public class BubbleRenderer {
int fullBright, float yOffset) { int fullBright, float yOffset) {
TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer; TextRenderer fontRenderer = MinecraftClient.getInstance().textRenderer;
// Get custom name (if any) // Get Name of entity
String nameText = "N/A"; String nameText = "N/A";
if (entity instanceof MobEntity) {
// Custom Name Tag (MobEntity)
if (entity.getCustomName() != null) { if (entity.getCustomName() != null) {
nameText = entity.getCustomName().getLiteralString(); nameText = entity.getCustomName().getLiteralString();
} }
} else if (entity instanceof PlayerEntity) {
// Player Name
nameText = entity.getName().getLiteralString();
}
// Truncate long names // Truncate long names
if (nameText.length() > 14) { if (nameText.length() > 14) {
...@@ -238,16 +244,18 @@ public class BubbleRenderer { ...@@ -238,16 +244,18 @@ public class BubbleRenderer {
} }
public static void drawTextAboveEntities(WorldRenderContext context, long tick, float partialTicks) { public static void drawTextAboveEntities(WorldRenderContext context, long tick, float partialTicks) {
// Access the Minecraft client instance // Set some rendering constants
MinecraftClient client = MinecraftClient.getInstance(); float lineSpacing = 1F;
PlayerEntity player = client.player; float textHeaderHeight = 40F;
float textFooterHeight = 5F;
int fullBright = 0xF000F0;
double renderDistance = 9.0;
// Get camera
Camera camera = context.camera(); Camera camera = context.camera();
Entity cameraEntity = camera.getFocusedEntity(); Entity cameraEntity = camera.getFocusedEntity();
if (cameraEntity == null) return; if (cameraEntity == null) return;
World world = cameraEntity.getEntityWorld(); World world = cameraEntity.getEntityWorld();
double renderDistance = 9.0;
// Calculate radius of entities // Calculate radius of entities
Vec3d pos = cameraEntity.getPos(); Vec3d pos = cameraEntity.getPos();
...@@ -265,35 +273,15 @@ public class BubbleRenderer { ...@@ -265,35 +273,15 @@ public class BubbleRenderer {
// Get all entities // Get all entities
List<Entity> nearbyEntities = world.getOtherEntities(null, area); List<Entity> nearbyEntities = world.getOtherEntities(null, area);
// Filter to include only MobEntity & PlayerEntity but exclude the current player and any entities with passengers // Filter to include only MobEntity & PlayerEntity but exclude any camera 1st person entity and any entities with passengers
List<Entity> relevantEntities = nearbyEntities.stream() List<Entity> relevantEntities = nearbyEntities.stream()
.filter(entity -> (entity instanceof MobEntity || entity instanceof PlayerEntity)) .filter(entity -> (entity instanceof MobEntity || entity instanceof PlayerEntity))
.filter(entity -> !entity.getUuid().equals(player.getUuid())) // Exclude current player by UUID .filter(entity -> !entity.hasPassengers())
.filter(entity -> !entity.hasPassengers()) // Exclude entities with passengers .filter(entity -> !(entity.equals(cameraEntity) && !camera.isThirdPerson()))
.collect(Collectors.toList()); .collect(Collectors.toList());
for (Entity entity : relevantEntities) { for (Entity entity : relevantEntities) {
// Look-up greeting (if any)
ChatDataManager.EntityChatData chatData = null;
if (entity instanceof MobEntity) {
chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getUuidAsString());
} else if (entity instanceof PlayerEntity) {
chatData = PlayerMessageManager.getMessage(entity.getUuid());
}
// Bail if no chatData found, or if they have passengers
if (chatData == null || entity.hasPassengers()) {
// Skip
continue;
}
List<String> lines = chatData.getWrappedLines();
// Set the range of lines to display
int starting_line = chatData.currentLineNumber;
int ending_line = Math.min(chatData.currentLineNumber + ChatDataManager.DISPLAY_NUM_LINES, lines.size());
// Push a new matrix onto the stack. // Push a new matrix onto the stack.
matrices.push(); matrices.push();
...@@ -371,17 +359,32 @@ public class BubbleRenderer { ...@@ -371,17 +359,32 @@ public class BubbleRenderer {
// Apply the pitch rotation to the matrix stack // Apply the pitch rotation to the matrix stack
matrices.multiply(pitchRotation); matrices.multiply(pitchRotation);
// Determine max line length // Get position matrix
float linesDisplayed = ending_line - starting_line;
float lineSpacing = 1F;
float textHeaderHeight = 40F;
float textFooterHeight = 5F;
int fullBright = 0xF000F0;
Matrix4f matrix = matrices.peek().getPositionMatrix(); Matrix4f matrix = matrices.peek().getPositionMatrix();
// Calculate size of text scaled to world // Look-up greeting (if any)
float scaledTextHeight = linesDisplayed * (fontRenderer.fontHeight + lineSpacing); ChatDataManager.EntityChatData chatData = null;
if (entity instanceof MobEntity) {
chatData = ChatDataManager.getClientInstance().getOrCreateChatData(entity.getUuidAsString());
} else if (entity instanceof PlayerEntity) {
chatData = PlayerMessageManager.getMessage(entity.getUuid());
}
float linesDisplayed = 0;
float minTextHeight = (ChatDataManager.DISPLAY_NUM_LINES * (fontRenderer.fontHeight + lineSpacing)) + (DISPLAY_PADDING * 2); float minTextHeight = (ChatDataManager.DISPLAY_NUM_LINES * (fontRenderer.fontHeight + lineSpacing)) + (DISPLAY_PADDING * 2);
float scaledTextHeight = minTextHeight;
if (chatData != null) {
// Set the range of lines to display
List<String> lines = chatData.getWrappedLines();
int starting_line = chatData.currentLineNumber;
int ending_line = Math.min(chatData.currentLineNumber + ChatDataManager.DISPLAY_NUM_LINES, lines.size());
// Determine max line length
linesDisplayed = ending_line - starting_line;
// Calculate size of text scaled to world
scaledTextHeight = linesDisplayed * (fontRenderer.fontHeight + lineSpacing);
scaledTextHeight = Math.max(scaledTextHeight, minTextHeight); scaledTextHeight = Math.max(scaledTextHeight, minTextHeight);
// Update Bubble Data for Click Handling using UUID (account for scaling) // Update Bubble Data for Click Handling using UUID (account for scaling)
...@@ -446,7 +449,7 @@ public class BubbleRenderer { ...@@ -446,7 +449,7 @@ public class BubbleRenderer {
drawIcon("button-chat", matrices, -16, textHeaderHeight, 32, 17); drawIcon("button-chat", matrices, -16, textHeaderHeight, 32, 17);
} else if (chatData.sender == ChatDataManager.ChatSender.USER && chatData.status == ChatDataManager.ChatStatus.DISPLAY) { } else if (chatData.sender == ChatDataManager.ChatSender.USER && chatData.status == ChatDataManager.ChatStatus.DISPLAY) {
// Draw Entity (Custom Name) // Draw Player Name
drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING); drawEntityName(entity, matrix, immediate, fullBright, 24F + DISPLAY_PADDING);
// Draw text background // Draw text background
...@@ -459,6 +462,17 @@ public class BubbleRenderer { ...@@ -459,6 +462,17 @@ public class BubbleRenderer {
drawMessageText(matrix, lines, starting_line, ending_line, immediate, lineSpacing, fullBright, 40.0F + DISPLAY_PADDING); drawMessageText(matrix, lines, starting_line, ending_line, immediate, lineSpacing, fullBright, 40.0F + DISPLAY_PADDING);
} }
} else if (entity instanceof PlayerEntity) {
// Scale down before rendering textures (otherwise font is huge)
matrices.scale(-0.02F, -0.02F, 0.02F);
// Translate above the player
matrices.translate(0F, -scaledTextHeight - textHeaderHeight - textFooterHeight, 0F);
// Draw Player Name
drawEntityName(entity, matrices.peek().getPositionMatrix(), immediate, fullBright, 24F + DISPLAY_PADDING);
}
// Pop the matrix to return to the original state. // Pop the matrix to return to the original state.
matrices.pop(); matrices.pop();
} }
......
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