Skip to content

Commit

Permalink
[Code style] Keep expected tablist display names as TabComponent inst…
Browse files Browse the repository at this point in the history
…ead of platform components
  • Loading branch information
NEZNAMY committed Feb 16, 2025
1 parent 6f14cbe commit e16b474
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean isDisguised() {
@Override
@Nullable
public TabList.Skin getSkin() {
return ((TabListBase<?>)getTabList()).getSkin();
return ((TabListBase)getTabList()).getSkin();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* TabList handler using the almighty Bukkit API.
*/
public class BukkitTabList extends TabListBase<String> {
public class BukkitTabList extends TabListBase {

/**
* Constructs new instance with given player.
Expand All @@ -31,10 +31,10 @@ public void removeEntry(@NonNull UUID entry) {

@Override
@SuppressWarnings("deprecation")
public void updateDisplayName(@NonNull UUID entry, @Nullable String displayName) {
public void updateDisplayName0(@NonNull UUID entry, @Nullable TabComponent displayName) {
Player p = Bukkit.getPlayer(entry);
if (p == null) return;
p.setPlayerListName(displayName);
p.setPlayerListName(displayName == null ? null : displayName.toLegacyText());
}

@Override
Expand Down Expand Up @@ -63,13 +63,7 @@ public void updateHat(@NonNull UUID entry, boolean showHat) {
}

@Override
public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin, boolean listed, int latency,
int gameMode, @Nullable String displayName, int listOrder, boolean showHat) {
public void addEntry0(@NonNull Entry entry) {
// Shrug
}

@Override
public String toComponent(@NonNull TabComponent component) {
return player.getPlatform().toBukkitFormat(component);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.authlib.GameProfile;
import lombok.NonNull;
import lombok.SneakyThrows;
import me.neznamy.chat.component.TabComponent;
import me.neznamy.tab.platforms.bukkit.BukkitTabPlayer;
import me.neznamy.tab.platforms.bukkit.nms.BukkitReflection;
import me.neznamy.tab.shared.ProtocolVersion;
Expand Down Expand Up @@ -144,15 +145,15 @@ public void updateHat(@NonNull UUID entry, boolean showHat) {
@NotNull
@Override
public Object createPacket(@NonNull Action action, @NonNull UUID id, @NonNull String name, @Nullable Skin skin,
boolean listed, int latency, int gameMode, @Nullable Object displayName, int listOrder, boolean showHat) {
boolean listed, int latency, int gameMode, @Nullable TabComponent displayName, int listOrder, boolean showHat) {
Object packet = newPlayerInfo.newInstance(actionToEnumSet.get(action), Collections.emptyList());
PLAYERS.set(packet, Collections.singletonList(newPlayerInfoData(
id,
action == Action.ADD_PLAYER ? createProfile(id, name, skin) : null,
listed,
latency,
gameModes[gameMode],
displayName,
displayName == null ? null : displayName.convert(),
showHat,
listOrder,
null
Expand All @@ -176,9 +177,9 @@ public void onPacketSend(@NonNull Object packet) {
int listOrder = v1_21_2Plus ? PlayerInfoData_ListOrder.getInt(nmsData) : 0;
boolean showHat = BukkitReflection.is1_21_4Plus() && PlayerInfoData_ShowHat.getBoolean(nmsData);
if (actions.contains(actionUpdateDisplayName)) {
Object expectedName = getExpectedDisplayNames().get(id);
if (expectedName != null && expectedName != displayName) {
displayName = expectedName;
TabComponent expectedName = getExpectedDisplayNames().get(id);
if (expectedName != null && expectedName.convert() != displayName) {
displayName = expectedName.convert();
rewriteEntry = rewritePacket = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* TabList handler for 1.7- servers using packets.
*/
public class PacketTabList17 extends TabListBase<String> {
public class PacketTabList17 extends TabListBase {

private static TriFunctionWithException<String, Boolean, Integer, Object> newPacket;
private static PacketSender packetSender;
Expand Down Expand Up @@ -83,10 +83,10 @@ public void removeEntry(@NonNull UUID entry) {

@Override
@SneakyThrows
public void updateDisplayName(@NonNull UUID entry, @Nullable String displayName) {
public void updateDisplayName0(@NonNull UUID entry, @Nullable TabComponent displayName) {
if (!displayNames.containsKey(entry)) return; // Entry not tracked by TAB
packetSender.sendPacket(player, newPacket.apply(displayNames.get(entry), false, 0));
addEntry(entry, userNames.get(entry), null, false, 0, 0, displayName, 0, false);
addEntry0(new Entry(entry, userNames.get(entry), null, false, 0, 0, displayName, 0, false));
}

@Override
Expand Down Expand Up @@ -118,18 +118,16 @@ public void updateHat(@NonNull UUID entry, boolean showHat) {

@Override
@SneakyThrows
public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin, boolean listed, int latency,
int gameMode, @Nullable String displayName, int listOrder, boolean showHat) {
String display = displayName == null ? name : displayName;
packetSender.sendPacket(player, newPacket.apply(display, true, latency));
userNames.put(id, name);
displayNames.put(id, display);
}

@Override
public String toComponent(@NonNull TabComponent component) {
String name = component.toLegacyText();
if (name.length() > Limitations.MAX_DISPLAY_NAME_LENGTH_1_7) name = name.substring(0, Limitations.MAX_DISPLAY_NAME_LENGTH_1_7);
return name;
public void addEntry0(@NonNull Entry entry) {
String displayName;
if (entry.getDisplayName() != null) {
displayName = entry.getDisplayName().toLegacyText();
if (displayName.length() > Limitations.MAX_DISPLAY_NAME_LENGTH_1_7) displayName = displayName.substring(0, Limitations.MAX_DISPLAY_NAME_LENGTH_1_7);
} else {
displayName = entry.getName();
}
packetSender.sendPacket(player, newPacket.apply(displayName, true, entry.getLatency()));
userNames.put(entry.getUniqueId(), entry.getName());
displayNames.put(entry.getUniqueId(), displayName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.NonNull;
import lombok.Setter;
import lombok.SneakyThrows;
import me.neznamy.chat.component.TabComponent;
import me.neznamy.tab.platforms.bukkit.BukkitTabPlayer;
import me.neznamy.tab.platforms.bukkit.BukkitUtils;
import me.neznamy.tab.platforms.bukkit.nms.converter.ComponentConverter;
Expand All @@ -25,7 +26,7 @@
*/
@Setter
@SuppressWarnings({"unchecked", "rawtypes"})
public class PacketTabList18 extends TabListBase<Object> {
public class PacketTabList18 extends TabListBase {

protected static Class<?> PlayerInfoClass;
protected static Constructor<?> newPlayerInfo;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void removeEntry(@NonNull UUID entry) {
}

@Override
public void updateDisplayName(@NonNull UUID entry, @Nullable Object displayName) {
public void updateDisplayName0(@NonNull UUID entry, @Nullable TabComponent displayName) {
packetSender.sendPacket(player,
createPacket(Action.UPDATE_DISPLAY_NAME, entry, "", null, false, 0, 0, displayName, 0, false));
}
Expand Down Expand Up @@ -149,10 +150,10 @@ public void updateHat(@NonNull UUID entry, boolean showHat) {
}

@Override
public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin, boolean listed, int latency,
int gameMode, @Nullable Object displayName, int listOrder, boolean showHat) {
public void addEntry0(@NonNull Entry entry) {
packetSender.sendPacket(player,
createPacket(Action.ADD_PLAYER, id, name, skin, listed, latency, gameMode, displayName, listOrder, showHat));
createPacket(Action.ADD_PLAYER, entry.getUniqueId(), entry.getName(), entry.getSkin(), entry.isListed(),
entry.getLatency(), entry.getGameMode(), entry.getDisplayName(), entry.getListOrder(), entry.isShowHat()));
}

/**
Expand Down Expand Up @@ -183,7 +184,7 @@ public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin
@SneakyThrows
@NotNull
public Object createPacket(@NonNull Action action, @NonNull UUID id, @NonNull String name, @Nullable Skin skin,
boolean listed, int latency, int gameMode, @Nullable Object displayName, int listOrder, boolean showHat) {
boolean listed, int latency, int gameMode, @Nullable TabComponent displayName, int listOrder, boolean showHat) {
Object packet = newPlayerInfo.newInstance(Enum.valueOf(ActionClass, action.name()), Collections.emptyList());
List<Object> parameters = new ArrayList<>();
if (newPlayerInfoData.getParameterTypes()[0] == PlayerInfoClass) {
Expand All @@ -192,7 +193,7 @@ public Object createPacket(@NonNull Action action, @NonNull UUID id, @NonNull St
parameters.add(createProfile(id, name, skin));
parameters.add(latency);
parameters.add(gameModes[gameMode]);
parameters.add(displayName);
parameters.add(displayName == null ? null : displayName.convert());
if (BukkitReflection.getMinorVersion() >= 19) parameters.add(null);
PLAYERS.set(packet, Collections.singletonList(newPlayerInfoData.newInstance(parameters.toArray())));
return packet;
Expand Down Expand Up @@ -228,8 +229,8 @@ public void onPacketSend(@NonNull Object packet) {
GameProfile profile = (GameProfile) PlayerInfoData_Profile.get(nmsData);
UUID id = profile.getId();
if (action.equals(Action.UPDATE_DISPLAY_NAME.name()) || action.equals(Action.ADD_PLAYER.name())) {
Object expectedName = getExpectedDisplayNames().get(id);
if (expectedName != null) PlayerInfoData_DisplayName.set(nmsData, expectedName);
TabComponent expectedName = getExpectedDisplayNames().get(id);
if (expectedName != null) PlayerInfoData_DisplayName.set(nmsData, expectedName.convert());
}
if (action.equals(Action.UPDATE_LATENCY.name()) || action.equals(Action.ADD_PLAYER.name())) {
int oldLatency = PlayerInfoData_Latency.getInt(nmsData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

/**
* Base TabList class for all implementations.
*
* @param <C>
* Component class
*/
public abstract class TabListBase<C> extends TrackedTabList<BukkitTabPlayer, C> {
public abstract class TabListBase extends TrackedTabList<BukkitTabPlayer> {

/** Instance function */
@Getter
@Setter
private static FunctionWithException<BukkitTabPlayer, TabListBase<?>> instance;
private static FunctionWithException<BukkitTabPlayer, TabListBase> instance;

@Nullable
protected static SkinData skinData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import lombok.NonNull;
import lombok.SneakyThrows;
import me.neznamy.chat.component.TabComponent;
import me.neznamy.tab.platforms.bungeecord.BungeeTabPlayer;
import me.neznamy.tab.shared.TAB;
import me.neznamy.chat.component.TabComponent;
import me.neznamy.tab.shared.platform.decorators.TrackedTabList;
import me.neznamy.tab.shared.util.ReflectionUtils;
import net.md_5.bungee.UserConnection;
Expand All @@ -16,7 +16,6 @@
import net.md_5.bungee.protocol.packet.PlayerListItemUpdate;
import net.md_5.bungee.tab.ServerUnique;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.UUID;
Expand All @@ -25,7 +24,7 @@
* Abstract TabList class for BungeeCord containing
* common code for all implementations.
*/
public abstract class BungeeTabList extends TrackedTabList<BungeeTabPlayer, BaseComponent> {
public abstract class BungeeTabList extends TrackedTabList<BungeeTabPlayer> {

/** Pointer to UUIDs in player's TabList */
private final Collection<UUID> uuids;
Expand Down Expand Up @@ -63,44 +62,27 @@ public Item item(@NonNull UUID id) {
}

/**
* Converts entry data to item.
* Converts entry to item.
*
* @param id
* Entry UUID
* @param name
* Entry name
* @param skin
* Entry skin
* @param listed
* Whether entry should be listed or not
* @param latency
* Entry latency
* @param gameMode
* Entry game mode
* @param displayName
* Entry display name
* @param listOrder
* Entry list order
* @param showHat
* Show hat flag
* @return Converted item from parameters
* @param entry
* Entry to convert
* @return Converted item from entry
*/
@NotNull
public Item entryToItem(@NonNull UUID id, @NonNull String name, @Nullable Skin skin, boolean listed, int latency,
int gameMode, @Nullable BaseComponent displayName, int listOrder, boolean showHat) {
Item item = item(id);
item.setUsername(name);
item.setDisplayName(displayName);
item.setGamemode(gameMode);
item.setListed(listed);
item.setPing(latency);
if (skin != null) {
item.setProperties(new Property[]{new Property(TEXTURES_PROPERTY, skin.getValue(), skin.getSignature())});
public Item entryToItem(@NonNull Entry entry) {
Item item = item(entry.getUniqueId());
item.setUsername(entry.getName());
item.setDisplayName(entry.getDisplayName() == null ? null : toComponent(entry.getDisplayName()));
item.setGamemode(entry.getGameMode());
item.setListed(entry.isListed());
item.setPing(entry.getLatency());
if (entry.getSkin() != null) {
item.setProperties(new Property[]{new Property(TEXTURES_PROPERTY, entry.getSkin().getValue(), entry.getSkin().getSignature())});
} else {
item.setProperties(new Property[0]);
}
item.setListOrder(listOrder);
item.setShowHat(showHat);
item.setListOrder(entry.getListOrder());
item.setShowHat(entry.isShowHat());
return item;
}

Expand Down Expand Up @@ -130,8 +112,8 @@ public void onPacketSend(@NonNull Object packet) {
PlayerListItem listItem = (PlayerListItem) packet;
for (PlayerListItem.Item item : listItem.getItems()) {
if (listItem.getAction() == PlayerListItem.Action.UPDATE_DISPLAY_NAME || listItem.getAction() == PlayerListItem.Action.ADD_PLAYER) {
BaseComponent expectedDisplayName = getExpectedDisplayNames().get(item.getUuid());
if (expectedDisplayName != null) item.setDisplayName(expectedDisplayName);
TabComponent expectedDisplayName = getExpectedDisplayNames().get(item.getUuid());
if (expectedDisplayName != null) item.setDisplayName(toComponent(expectedDisplayName));
}
if (listItem.getAction() == PlayerListItem.Action.UPDATE_LATENCY || listItem.getAction() == PlayerListItem.Action.ADD_PLAYER) {
item.setPing(TAB.getInstance().getFeatureManager().onLatencyChange(player, item.getUuid(), item.getPing()));
Expand All @@ -144,8 +126,8 @@ public void onPacketSend(@NonNull Object packet) {
PlayerListItemUpdate update = (PlayerListItemUpdate) packet;
for (PlayerListItem.Item item : update.getItems()) {
if (update.getActions().contains(PlayerListItemUpdate.Action.UPDATE_DISPLAY_NAME)) {
BaseComponent expectedDisplayName = getExpectedDisplayNames().get(item.getUuid());
if (expectedDisplayName != null) item.setDisplayName(expectedDisplayName);
TabComponent expectedDisplayName = getExpectedDisplayNames().get(item.getUuid());
if (expectedDisplayName != null) item.setDisplayName(toComponent(expectedDisplayName));
}
if (update.getActions().contains(PlayerListItemUpdate.Action.UPDATE_LATENCY)) {
item.setPing(TAB.getInstance().getFeatureManager().onLatencyChange(player, item.getUuid(), item.getPing()));
Expand All @@ -162,8 +144,8 @@ public boolean containsEntry(@NonNull UUID entry) {
return uuids.contains(entry);
}

@Override
public BaseComponent toComponent(@NonNull TabComponent component) {
@NotNull
protected BaseComponent toComponent(@NonNull TabComponent component) {
return player.getPlatform().transformComponent(component, player.getVersion());
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package me.neznamy.tab.platforms.bungeecord.tablist;

import lombok.NonNull;
import me.neznamy.chat.component.TabComponent;
import me.neznamy.tab.platforms.bungeecord.BungeeTabPlayer;
import me.neznamy.tab.shared.ProtocolVersion;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.protocol.packet.PlayerListItem.Item;
import net.md_5.bungee.protocol.packet.PlayerListItemRemove;
import net.md_5.bungee.protocol.packet.PlayerListItemUpdate;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.EnumSet;
import java.util.UUID;

/**
* TabList handler for 1.19.3+ players using the new tab list packets.
Expand Down Expand Up @@ -73,9 +74,9 @@ public void removeEntry(@NonNull UUID entry) {
}

@Override
public void updateDisplayName(@NonNull UUID entry, @Nullable BaseComponent displayName) {
public void updateDisplayName0(@NonNull UUID entry, @Nullable TabComponent displayName) {
Item item = item(entry);
item.setDisplayName(displayName);
if (displayName != null) item.setDisplayName(toComponent(displayName));
sendPacket(updateDisplayName, item);
}

Expand Down Expand Up @@ -117,9 +118,8 @@ public void updateHat(@NonNull UUID entry, boolean showHat) {
}

@Override
public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin, boolean listed, int latency,
int gameMode, @Nullable BaseComponent displayName, int listOrder, boolean showHat) {
addUuid(id);
public void addEntry0(@NonNull Entry entry) {
addUuid(entry.getUniqueId());
EnumSet<PlayerListItemUpdate.Action> actions;
if (player.getVersion().getNetworkId() >= ProtocolVersion.V1_21_4.getNetworkId()) {
actions = addPlayer_1_21_4;
Expand All @@ -128,7 +128,7 @@ public void addEntry(@NonNull UUID id, @NonNull String name, @Nullable Skin skin
} else {
actions = addPlayer_legacy;
}
sendPacket(actions, entryToItem(id, name, skin, listed, latency, gameMode, displayName, listOrder, showHat));
sendPacket(actions, entryToItem(entry));
}

private void sendPacket(@NonNull EnumSet<PlayerListItemUpdate.Action> actions, @NonNull Item item) {
Expand Down
Loading

0 comments on commit e16b474

Please sign in to comment.