Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/presentation/NativePresentationPackets.java
T
dmg b493cfe192
Release / release (push) Successful in 10m26s
CI / build (push) Successful in 4m6s
feat(characters): complete transfers and standalone display identity
2026-09-18 22:40:08 -04:00

192 lines
9.6 KiB
Java

package games.dmg.fantasystick.presentation;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket.Action;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket.Entry;
import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket;
import net.minecraft.network.protocol.game.ClientboundUpdateAttributesPacket.AttributeSnapshot;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Display.BillboardConstraints;
import net.minecraft.world.entity.Display.TextDisplay;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.PositionMoveRotation;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.level.GameType;
import net.minecraft.world.phys.Vec3;
/**
* Display-only codec pinned to Purpur 26.2 build 2618. No packet delivery, world insertion,
* profile/team changes, or authoritative player mutations belong here.
*
* <p>Player reads and detached-label creation/updates belong on the server thread. Pure
* packet projections accept already-captured session identity/text; the caller owns visibility,
* per-viewer range, lifecycle checks and removing each viewer's client-only labels.
*/
public final class NativePresentationPackets {
private NativePresentationPackets() { }
/**
* Replace only the selected UUID's display component when its action is already present.
* Preserve all other entry fields and the exact action set, including mixed ADD packets.
* ADD without UPDATE_DISPLAY_NAME needs a separate targeted update after acceptance.
*/
public static ClientboundPlayerInfoUpdatePacket projectPlayerInfo(
ClientboundPlayerInfoUpdatePacket packet, UUID activePlayer, String name) {
var actions = packet.actions();
if (!actions.contains(Action.UPDATE_DISPLAY_NAME)) {
return packet;
}
var entries = new ArrayList<Entry>(packet.entries().size());
boolean changed = false;
for (var entry : packet.entries()) {
if (entry.profileId().equals(activePlayer)) {
entries.add(new Entry(entry.profileId(), entry.profile(), entry.listed(), entry.latency(),
entry.gameMode(), literal(name), entry.showHat(), entry.listOrder(), entry.chatSession()));
changed = true;
} else {
entries.add(entry);
}
}
if (!changed) { return packet; }
return new ClientboundPlayerInfoUpdatePacket(EnumSet.copyOf(actions), entries);
}
/**
* Replace an existing NAME_TAG_DISTANCE snapshot with base zero/no modifiers, only for
* the selected entity. Do not inject absent attributes; use suppressNameTag on activation.
*/
public static ClientboundUpdateAttributesPacket projectAttributes(
ClientboundUpdateAttributesPacket packet, int activeEntityId) {
if (packet.getEntityId() != activeEntityId || packet.getValues().stream()
.noneMatch(value -> value.attribute().equals(Attributes.NAME_TAG_DISTANCE))) {
return packet;
}
var projected = new ClientboundUpdateAttributesPacket(activeEntityId, List.of());
for (var value : packet.getValues()) {
projected.getValues().add(value.attribute().equals(Attributes.NAME_TAG_DISTANCE)
? new AttributeSnapshot(Attributes.NAME_TAG_DISTANCE, 0, List.of()) : value);
}
return projected;
}
/** One client-only distance snapshot; never changes the server's attribute instance. */
public static ClientboundUpdateAttributesPacket suppressNameTag(int entityId) {
var packet = new ClientboundUpdateAttributesPacket(entityId, List.of());
packet.getValues().add(new AttributeSnapshot(Attributes.NAME_TAG_DISTANCE, 0, List.of()));
return packet;
}
/** Snapshot the current authoritative distance base and all modifiers, not an old default. */
public static ClientboundUpdateAttributesPacket restoreNameTag(ServerPlayer player) {
var distance = player.getAttribute(Attributes.NAME_TAG_DISTANCE);
if (distance == null) {
throw new IllegalStateException("Native player has no NAME_TAG_DISTANCE attribute");
}
var packet = new ClientboundUpdateAttributesPacket(player.getId(), List.of());
packet.getValues().add(new AttributeSnapshot(Attributes.NAME_TAG_DISTANCE,
distance.getBaseValue(), List.copyOf(distance.getModifiers())));
return packet;
}
/** UPDATE_DISPLAY_NAME only, using the current ordinary native tab display (including null). */
public static ClientboundPlayerInfoUpdatePacket refreshTab(ServerPlayer player) {
// Only UUID and displayName are serialized by this action. Do not capture unrelated
// connection/game-mode state or synthesize ADD_PLAYER/listed/profile updates.
var ordinary = player.getTabListDisplayName();
var entry = new Entry(player.getUUID(), null, false, 0,
GameType.DEFAULT_MODE, ordinary == null ? null : ordinary.copy(),
false, 0, null);
return new ClientboundPlayerInfoUpdatePacket(EnumSet.of(Action.UPDATE_DISPLAY_NAME), List.of(entry));
}
/** Pure targeted display update: no profile, listed, game-mode or reconnect actions. */
public static ClientboundPlayerInfoUpdatePacket displayName(UUID player, String name) {
var entry = new Entry(player, null, false, 0, GameType.DEFAULT_MODE, literal(name), false, 0, null);
return new ClientboundPlayerInfoUpdatePacket(EnumSet.of(Action.UPDATE_DISPLAY_NAME), List.of(entry));
}
/**
* Create a detached TextDisplay in the player's current native level at explicit coordinates.
* Native construction allocates its ID through ServerLevel.getNextEntityId; never register it.
* The caller chooses overhead position and recreates the label after world changes.
*/
public static Label createLabel(ServerPlayer player, String text, double x, double y, double z) {
validatePosition(x, y, z);
var component = literal(text);
var level = player.level();
if (level == null) { throw new IllegalStateException("Native player has no current server level"); }
// A native constructor supplies the exact metadata schema and native ID allocation.
// Never spawn, tick, wrap in a Bukkit entity, or expose this detached instance.
var display = new TextDisplay(EntityTypes.TEXT_DISPLAY, level);
display.persist = false;
display.setNoGravity(true);
display.setBillboardConstraints(BillboardConstraints.CENTER);
display.getEntityData().set(TextDisplay.DATA_LINE_WIDTH_ID, Integer.MAX_VALUE);
display.setFlags((byte) (TextDisplay.FLAG_SHADOW | TextDisplay.FLAG_USE_DEFAULT_BACKGROUND));
display.setText(component);
display.setPos(x, y, z);
return new Label(display);
}
private static Component literal(String text) {
if (text == null) { throw new IllegalArgumentException("Display text must not be null"); }
return Component.literal(text);
}
private static void validatePosition(double x, double y, double z) {
if (!Double.isFinite(x) || !Double.isFinite(y) || !Double.isFinite(z)) {
throw new IllegalArgumentException("Label coordinates must be finite");
}
}
/** A session-owned detached entity; no native entity is exposed for accidental world insertion. */
public static final class Label {
private final TextDisplay display;
private Label(TextDisplay display) { this.display = display; }
public int id() { return display.getId(); }
public UUID uuid() { return display.getUUID(); }
/** Send spawn followed by metadata to each newly eligible viewer. */
public ClientboundAddEntityPacket spawn() {
return new ClientboundAddEntityPacket(id(), uuid(), display.getX(), display.getY(), display.getZ(),
0, 0, display.getType(), 0, Vec3.ZERO, 0);
}
/** Complete metadata, not consumable packDirty state: safe for multiple viewers. */
public ClientboundSetEntityDataPacket metadata() {
return new ClientboundSetEntityDataPacket(id(), display.getEntityData().packAll());
}
/** Update only the detached label and return an absolute teleport packet. */
public ClientboundTeleportEntityPacket move(double x, double y, double z) {
validatePosition(x, y, z);
display.setPos(x, y, z);
return ClientboundTeleportEntityPacket.teleport(id(),
PositionMoveRotation.of(display), Set.of(), false);
}
/** Replace only the detached label's literal text and return complete metadata. */
public ClientboundSetEntityDataPacket rename(String text) {
display.setText(literal(text));
return metadata();
}
/** Client-only removal; never calls entity discard/remove on the server world. */
public ClientboundRemoveEntitiesPacket remove() {
return new ClientboundRemoveEntitiesPacket(id());
}
}
}