Update how packet p[layer info is handled

This commit is contained in:
libraryaddict 2023-01-03 08:01:40 +13:00
parent b098e2028d
commit bce4e96f55
13 changed files with 105 additions and 84 deletions

View File

@ -235,14 +235,14 @@ public class ReflectionManager implements ReflectionManagerAbstract {
}
@Override
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible) {
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions) {
ClientboundPlayerInfoPacket.PlayerUpdate entry =
new ClientboundPlayerInfoPacket.PlayerUpdate((GameProfile) gameProfile.getHandle(), 0, GameType.SURVIVAL, new TextComponent(displayName));
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
StructureModifier<Object> modifier = packet.getModifier();
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(action.name()));
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(actions[0].name()));
modifier.write(1, Collections.singletonList(entry));
return packet;

View File

@ -235,14 +235,14 @@ public class ReflectionManager implements ReflectionManagerAbstract {
}
@Override
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible) {
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions) {
ClientboundPlayerInfoPacket.PlayerUpdate entry =
new ClientboundPlayerInfoPacket.PlayerUpdate((GameProfile) gameProfile.getHandle(), 0, GameType.SURVIVAL, new TextComponent(displayName));
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
StructureModifier<Object> modifier = packet.getModifier();
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(action.name()));
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(actions[0].name()));
modifier.write(1, Collections.singletonList(entry));
return packet;

View File

@ -236,14 +236,14 @@ public class ReflectionManager implements ReflectionManagerAbstract {
}
@Override
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible) {
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions) {
ClientboundPlayerInfoPacket.PlayerUpdate entry =
new ClientboundPlayerInfoPacket.PlayerUpdate((GameProfile) gameProfile.getHandle(), 0, GameType.SURVIVAL, new TextComponent(displayName));
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
StructureModifier<Object> modifier = packet.getModifier();
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(action.name()));
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(actions[0].name()));
modifier.write(1, Collections.singletonList(entry));
return packet;

View File

@ -242,14 +242,14 @@ public class ReflectionManager implements ReflectionManagerAbstract {
}
@Override
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible) {
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions) {
ClientboundPlayerInfoPacket.PlayerUpdate entry =
new ClientboundPlayerInfoPacket.PlayerUpdate((GameProfile) gameProfile.getHandle(), 0, GameType.SURVIVAL, Component.literal(displayName), null);
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
StructureModifier<Object> modifier = packet.getModifier();
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(action.name()));
modifier.write(0, ClientboundPlayerInfoPacket.Action.valueOf(actions[0].name()));
modifier.write(1, Collections.singletonList(entry));
return packet;

View File

@ -2,6 +2,7 @@ package me.libraryaddict.disguise.utilities.reflection.v1_19_R2;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.EnumWrappers;
@ -43,7 +44,6 @@ import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.animal.CatVariant;
import net.minecraft.world.entity.animal.FrogVariant;
import net.minecraft.world.entity.animal.camel.Camel;
import net.minecraft.world.entity.decoration.PaintingVariant;
import net.minecraft.world.entity.npc.VillagerData;
import net.minecraft.world.entity.npc.VillagerProfession;
@ -88,14 +88,18 @@ import org.bukkit.util.Vector;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ReflectionManager implements ReflectionManagerAbstract {
public boolean hasInvul(Entity entity) {
@ -248,8 +252,38 @@ public class ReflectionManager implements ReflectionManagerAbstract {
return net.minecraft.core.Direction.from2DDataValue(direction);
}
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible) {
if (action == EnumWrappers.PlayerInfoAction.REMOVE_PLAYER) {
@Override
public void handleTablistPacket(PacketEvent event, Function<UUID, Boolean> shouldRemove) {
ClientboundPlayerInfoUpdatePacket packet = (ClientboundPlayerInfoUpdatePacket) event.getPacket().getHandle();
if (!packet.actions().contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER)) {
return;
}
List<ClientboundPlayerInfoUpdatePacket.Entry> canKeep = new ArrayList<>();
for (ClientboundPlayerInfoUpdatePacket.Entry entry : packet.entries()) {
if (shouldRemove.apply(entry.profileId())) {
continue;
}
canKeep.add(entry);
}
if (canKeep.size() == packet.entries().size()) {
return;
}
if (canKeep.isEmpty()) {
event.setCancelled(true);
return;
}
event.getPacket().getModifier().write(1, canKeep);
}
public PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions) {
if (actions[0] == EnumWrappers.PlayerInfoAction.REMOVE_PLAYER) {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO_REMOVE);
packet.getModifier().write(0, Collections.singletonList(gameProfile.getUUID()));
@ -262,8 +296,10 @@ public class ReflectionManager implements ReflectionManagerAbstract {
PacketContainer packet = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
StructureModifier<Object> modifier = packet.getModifier();
EnumSet<ClientboundPlayerInfoUpdatePacket.Action> enumSet =
EnumSet.copyOf(Arrays.stream(actions).map(action -> ClientboundPlayerInfoUpdatePacket.Action.valueOf(action.name())).collect(Collectors.toList()));
modifier.write(0, EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.valueOf(action.name())));
modifier.write(0, enumSet);
modifier.write(1, Collections.singletonList(entry));
return packet;

View File

@ -985,16 +985,14 @@ public abstract class Disguise {
PlayerDisguise disguise = (PlayerDisguise) this;
if (disguise.isDisplayedInTab()) {
PacketContainer[] addTabs = ReflectionManager.createTablistAddPackets(disguise);
PacketContainer addTab = ReflectionManager.createTablistAddPackets(disguise);
for (Player player : Bukkit.getOnlinePlayers()) {
if (!((TargetedDisguise) this).canSee(player)) {
continue;
}
for (PacketContainer packet : addTabs) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab);
}
}
}

View File

@ -522,7 +522,7 @@ public class PlayerDisguise extends TargetedDisguise {
}
if (isDisplayedInTab()) {
PacketContainer[] addTabs = ReflectionManager.createTablistAddPackets(this);
PacketContainer addTab = ReflectionManager.createTablistAddPackets(this);
PacketContainer deleteTab = ReflectionManager.createTablistPacket(this, PlayerInfoAction.REMOVE_PLAYER);
for (Player player : Bukkit.getOnlinePlayers()) {
@ -531,10 +531,7 @@ public class PlayerDisguise extends TargetedDisguise {
}
ProtocolLibrary.getProtocolManager().sendServerPacket(player, deleteTab);
for (PacketContainer packet : addTabs) {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
}
ProtocolLibrary.getProtocolManager().sendServerPacket(player, addTab);
}
}

View File

@ -452,9 +452,7 @@ public class DisguiseListener implements Listener {
PlayerDisguise disguise = (PlayerDisguise) targetedDisguise;
if (disguise.isDisplayedInTab()) {
for (PacketContainer packet : ReflectionManager.createTablistAddPackets(disguise)) {
ProtocolLibrary.getProtocolManager().sendServerPacket(p, packet);
}
ProtocolLibrary.getProtocolManager().sendServerPacket(p, ReflectionManager.createTablistAddPackets(disguise));
}
}
}

View File

@ -43,10 +43,10 @@ public class PacketsManager {
// Because it kicks you for hacking.
clientInteractEntityListener = new PacketListenerClientInteract(LibsDisguises.getInstance());
// PacketListener tabListListener = new PacketListenerTabList(LibsDisguises.getInstance());
PacketListener tabListListener = new PacketListenerTabList(LibsDisguises.getInstance());
ProtocolLibrary.getProtocolManager().addPacketListener(clientInteractEntityListener);
// ProtocolLibrary.getProtocolManager().addPacketListener(tabListListener);
ProtocolLibrary.getProtocolManager().addPacketListener(tabListListener);
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketListenerClientCustomPayload());
// Now I call this and the main listener is registered!

View File

@ -162,9 +162,7 @@ public class PacketHandlerSpawn implements IPacketHandler {
if (DisguiseUtilities.isFancyHiddenTabs() || !playerDisguise.isDisplayedInTab() || !playerDisguise.isNameVisible()) {
// Send player info along with the disguise
for (PacketContainer sendTab : ReflectionManager.createTablistAddPackets(playerDisguise)) {
packets.addPacket(sendTab);
}
packets.addPacket(ReflectionManager.createTablistAddPackets(playerDisguise));
skin = LibsDisguises.getInstance().getSkinHandler().addPlayerSkin(observer, playerDisguise);
skin.setDoTabList(!DisguiseUtilities.isFancyHiddenTabs());

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
import com.comphenix.protocol.wrappers.PlayerInfoData;
@ -10,6 +11,7 @@ import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -17,8 +19,11 @@ import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
public class PacketListenerTabList extends PacketAdapter {
public PacketListenerTabList(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.PLAYER_INFO);
}
@ -30,56 +35,51 @@ public class PacketListenerTabList extends PacketAdapter {
}
Player observer = event.getPlayer();
Set<PlayerInfoAction> actions;
Function<UUID, Boolean> shouldRemove = uuid -> {
Player player = Bukkit.getPlayer(uuid);
if (player == null) {
return false;
}
Disguise disguise = DisguiseAPI.getDisguise(observer, player);
return disguise != null && disguise.isHidePlayer();
};
if (NmsVersion.v1_19_R2.isSupported()) {
actions = event.getPacket().getPlayerInfoActions().read(0);
} else {
actions = EnumSet.of(event.getPacket().getPlayerInfoAction().read(0));
}
if (actions.stream().noneMatch(a -> a == PlayerInfoAction.ADD_PLAYER)) {
ReflectionManager.getNmsReflection().handleTablistPacket(event, shouldRemove);
return;
}
List<PlayerInfoData> list = event.getPacket().getPlayerInfoDataLists().read(NmsVersion.v1_19_R2.isSupported() ? 1 : 0);
PacketContainer packet = event.getPacket();
if (packet.getPlayerInfoAction().read(0) != PlayerInfoAction.ADD_PLAYER) {
return;
}
List<PlayerInfoData> list = packet.getPlayerInfoDataLists().read(0);
Iterator<PlayerInfoData> itel = list.iterator();
Iterator<PlayerInfoAction> actionItel = actions.iterator();
boolean modified = false;
while (itel.hasNext() && actionItel.hasNext()) {
while (itel.hasNext()) {
PlayerInfoData data = itel.next();
if (NmsVersion.v1_19_R2.isSupported()) {
if (actionItel.next() != PlayerInfoAction.ADD_PLAYER) {
continue;
}
}
if (data == null) {
continue;
}
Player player = Bukkit.getPlayer(data.getProfile().getUUID());
if (player == null) {
continue;
}
Disguise disguise = DisguiseAPI.getDisguise(observer, player);
if (disguise == null) {
continue;
}
if (!disguise.isHidePlayer()) {
if (!shouldRemove.apply(data.getProfile().getUUID())) {
continue;
}
itel.remove();
modified = true;
}
if (NmsVersion.v1_19_R2.isSupported()) {
actionItel.remove();
}
if (!modified) {
return;
}
if (list.isEmpty()) {
@ -87,12 +87,6 @@ public class PacketListenerTabList extends PacketAdapter {
return;
}
event.getPacket().getPlayerInfoDataLists().write(NmsVersion.v1_19_R2.isSupported() ? 1 : 0, list);
if (!NmsVersion.v1_19_R2.isSupported()) {
return;
}
event.getPacket().getPlayerInfoActions().write(0, actions);
packet.getPlayerInfoDataLists().write(0, list);
}
}

View File

@ -18,6 +18,7 @@ import com.comphenix.protocol.wrappers.WrappedParticle;
import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
import com.mojang.authlib.GameProfile;
import lombok.Getter;
import lombok.SneakyThrows;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises;
@ -189,6 +190,7 @@ public class ReflectionManager {
private static Field playerConnection;
private static Method incrementedInventoryStateId;
private static Field playerInventoryContainer;
@Getter
private static ReflectionManagerAbstract nmsReflection;
private static Field trackedPlayers;
private static Method clearEntityTracker;
@ -953,9 +955,9 @@ public class ReflectionManager {
}
public static PacketContainer updateTablistVisibility(Player player, boolean visible) {
if (NmsVersion.v1_19_R2.isSupported()) {
return nmsReflection.getTabListPacket(player.getPlayerListName(), ReflectionManager.getGameProfile(player),
EnumWrappers.PlayerInfoAction.UPDATE_LISTED, visible);
if (DisguiseUtilities.isFancyHiddenTabs()) {
return nmsReflection.getTabListPacket(player.getPlayerListName(), ReflectionManager.getGameProfile(player), visible,
EnumWrappers.PlayerInfoAction.UPDATE_LISTED);
}
PlayerInfoData playerInfo =
@ -970,25 +972,18 @@ public class ReflectionManager {
return addTab;
}
public static PacketContainer[] createTablistAddPackets(PlayerDisguise disguise) {
public static PacketContainer createTablistAddPackets(PlayerDisguise disguise) {
if (!NmsVersion.v1_19_R2.isSupported()) {
return new PacketContainer[]{createTablistPacket(disguise, EnumWrappers.PlayerInfoAction.ADD_PLAYER)};
return createTablistPacket(disguise, EnumWrappers.PlayerInfoAction.ADD_PLAYER);
}
PacketContainer[] packets = new PacketContainer[disguise.isDisplayedInTab() ? 3 : 2];
packets[0] = createTablistPacket(disguise, EnumWrappers.PlayerInfoAction.ADD_PLAYER);
packets[1] = createTablistPacket(disguise, EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME);
if (disguise.isDisplayedInTab()) {
packets[2] = createTablistPacket(disguise, EnumWrappers.PlayerInfoAction.UPDATE_LISTED);
}
return packets;
return nmsReflection.getTabListPacket(disguise.getName(), disguise.getGameProfile(), disguise.isDisplayedInTab(),
EnumWrappers.PlayerInfoAction.ADD_PLAYER, EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME, EnumWrappers.PlayerInfoAction.UPDATE_LISTED);
}
public static PacketContainer createTablistPacket(PlayerDisguise disguise, EnumWrappers.PlayerInfoAction action) {
if (nmsReflection != null) {
return nmsReflection.getTabListPacket(disguise.getName(), disguise.getGameProfile(), action, disguise.isDisplayedInTab());
return nmsReflection.getTabListPacket(disguise.getName(), disguise.getGameProfile(), disguise.isDisplayedInTab(), action);
}
try {

View File

@ -1,6 +1,7 @@
package me.libraryaddict.disguise.utilities.reflection;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedDataWatcher;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
@ -24,6 +25,7 @@ import org.bukkit.util.Vector;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
public interface ReflectionManagerAbstract {
boolean hasInvul(Entity entity);
@ -72,7 +74,7 @@ public interface ReflectionManagerAbstract {
Enum getEnumDirection(int direction);
PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, EnumWrappers.PlayerInfoAction action, boolean nameVisible);
PacketContainer getTabListPacket(String displayName, WrappedGameProfile gameProfile, boolean nameVisible, EnumWrappers.PlayerInfoAction... actions);
Object getNmsEntity(Entity entity);
@ -145,6 +147,9 @@ public interface ReflectionManagerAbstract {
ItemMeta getDeserializedItemMeta(Map<String, Object> meta);
default void handleTablistPacket(PacketEvent event, Function<UUID, Boolean> shouldRemove) {
}
/**
* Implement this for custom metadata values that are not backwards compatible
*/