Skip to content

Commit

Permalink
1.19.3: initial player packet update, initial compilability
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 8, 2022
1 parent 357230f commit eb0121b
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 96 deletions.
Expand Up @@ -2,7 +2,6 @@

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.ObjectTag;
Expand Down
Expand Up @@ -8,7 +8,6 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.chat.ComponentSerializer;

public class PaperElementExtensions {

Expand Down
Expand Up @@ -29,7 +29,7 @@ public class PlayerReceivesTablistUpdateScriptEvent extends BukkitScriptEvent {
// @Cancellable true
//
// @Context
// <context.mode> returns the update mode: 'add', 'remove', 'update_gamemode', 'update_latency', or 'update_display'.
// <context.mode> returns the update mode: 'add', 'remove', 'update_gamemode', 'update_latency', 'update_listed', or 'update_display'. As of 1.19.3, you can also receive update combos like "update_gamemode|update_latency".
// <context.uuid> returns the packet's associated UUID.
// <context.name> returns the packet's associated name (if any).
// <context.display> returns the packet's associated display name (if any).
Expand Down Expand Up @@ -58,7 +58,7 @@ public PlayerReceivesTablistUpdateScriptEvent() {

public static class TabPacketData {

public TabPacketData(String mode, UUID id, String name, String display, String gamemode, String texture, String signature, int latency) {
public TabPacketData(String mode, UUID id, boolean isListed, String name, String display, String gamemode, String texture, String signature, int latency) {
this.mode = mode;
this.id = id;
this.name = name;
Expand All @@ -67,6 +67,7 @@ public TabPacketData(String mode, UUID id, String name, String display, String g
this.texture = texture;
this.signature = signature;
this.latency = latency;
this.isListed = isListed;
}

public UUID id;
Expand All @@ -75,6 +76,8 @@ public TabPacketData(String mode, UUID id, String name, String display, String g

public int latency;

public boolean isListed;

public boolean cancelled = false;

public boolean modified = false;
Expand Down
Expand Up @@ -21,7 +21,6 @@
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ScriptUtilities;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
Expand Down
Expand Up @@ -352,7 +352,7 @@ public boolean processTablistPacket(Packet<?> packet, GenericFutureListener<? ex
signature = property.getSignature();
}
String modeText = update.getGameMode() == null ? null : update.getGameMode().name();
PlayerReceivesTablistUpdateScriptEvent.TabPacketData data = new PlayerReceivesTablistUpdateScriptEvent.TabPacketData(mode, profile.getId(), profile.getName(),
PlayerReceivesTablistUpdateScriptEvent.TabPacketData data = new PlayerReceivesTablistUpdateScriptEvent.TabPacketData(mode, profile.getId(), true, profile.getName(),
update.getDisplayName() == null ? null : FormattedTextHelper.stringify(Handler.componentToSpigot(update.getDisplayName())), modeText, texture, signature, update.getLatency());
PlayerReceivesTablistUpdateScriptEvent.fire(player.getBukkitEntity(), data);
if (data.modified) {
Expand Down
Expand Up @@ -30,7 +30,6 @@
import net.md_5.bungee.api.chat.hover.content.Text;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.ByteArrayTag;
import net.minecraft.nbt.StringTag;
Expand All @@ -40,7 +39,6 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerBossEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.BossEvent;
import net.minecraft.world.Container;
Expand Down
Expand Up @@ -15,9 +15,7 @@
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.core.*;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.game.ClientboundUpdateTagsPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand Down
Expand Up @@ -18,9 +18,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.resources.ResourceLocation;
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.denizenscript.denizen.nms.v1_19.Handler;
import com.denizenscript.denizen.nms.v1_19.ReflectionMappingsInfo;
import com.denizenscript.denizen.nms.v1_19.impl.ImprovedOfflinePlayerImpl;
import com.denizenscript.denizen.nms.v1_19.impl.ProfileEditorImpl;
import com.denizenscript.denizen.nms.v1_19.impl.entities.CraftFakePlayerImpl;
import com.denizenscript.denizen.nms.v1_19.impl.entities.EntityItemProjectileImpl;
import com.denizenscript.denizen.nms.v1_19.impl.network.handlers.AbstractListenerPlayInImpl;
Expand All @@ -29,7 +30,6 @@
import net.md_5.bungee.api.ChatColor;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.game.*;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -395,29 +395,25 @@ public long getLastActionTime(Player player) {

@Override
public void sendPlayerInfoAddPacket(Player player, ProfileEditMode mode, String name, String display, UUID id, String texture, String signature, int latency, GameMode gameMode) {
ClientboundPlayerInfoPacket.Action action = mode == ProfileEditMode.ADD ? ClientboundPlayerInfoPacket.Action.ADD_PLAYER :
(mode == ProfileEditMode.UPDATE_DISPLAY ? ClientboundPlayerInfoPacket.Action.UPDATE_DISPLAY_NAME : ClientboundPlayerInfoPacket.Action.UPDATE_LATENCY);
ClientboundPlayerInfoPacket packet = new ClientboundPlayerInfoPacket(action);
boolean listed = true; // TODO: 1.19.3: Add 'listed' input
ClientboundPlayerInfoUpdatePacket.Action action = mode == ProfileEditMode.ADD ? ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER : (mode == ProfileEditMode.UPDATE_DISPLAY ? ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME : ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY);
GameProfile profile = new GameProfile(id, name);
if (texture != null) {
profile.getProperties().put("textures", new Property("textures", texture, signature));
}
packet.getEntries().add(new ClientboundPlayerInfoPacket.PlayerUpdate(profile, latency, GameType.byName(CoreUtilities.toLowerCase(gameMode.name())), display == null ? null : Handler.componentToNMS(FormattedTextHelper.parse(display, ChatColor.WHITE)), null));
PacketHelperImpl.send(player, packet);
ClientboundPlayerInfoUpdatePacket.Entry entry = new ClientboundPlayerInfoUpdatePacket.Entry(id, profile, listed, latency, GameType.byName(CoreUtilities.toLowerCase(gameMode.name())), display == null ? null : Handler.componentToNMS(FormattedTextHelper.parse(display, ChatColor.WHITE)), null);
PacketHelperImpl.send(player, ProfileEditorImpl.createInfoPacket(EnumSet.of(action), Collections.singletonList(entry)));
}

@Override
public void sendPlayerRemovePacket(Player player, UUID id) {
ClientboundPlayerInfoPacket packet = new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.REMOVE_PLAYER);
GameProfile profile = new GameProfile(id, "name");
packet.getEntries().add(new ClientboundPlayerInfoPacket.PlayerUpdate(profile, 0, null, null, null));
PacketHelperImpl.send(player, packet);
PacketHelperImpl.send(player, new ClientboundPlayerInfoRemovePacket(Collections.singletonList(id)));
}

@Override
public void sendClimbableMaterials(Player player, List<Material> materials) {
Map<ResourceKey<? extends Registry<?>>, TagNetworkSerialization.NetworkPayload> packetInput = TagNetworkSerialization.serializeTagsToNetwork(((CraftServer) Bukkit.getServer()).getServer().registryAccess());
Map<ResourceLocation, IntList> tags = ReflectionHelper.getFieldValue(TagNetworkSerialization.NetworkPayload.class, ReflectionMappingsInfo.TagNetworkSerializationNetworkPayload_tags, packetInput.get(Registry.BLOCK_REGISTRY));
Map<ResourceKey<? extends Registry<?>>, TagNetworkSerialization.NetworkPayload> packetInput = TagNetworkSerialization.serializeTagsToNetwork(((CraftServer) Bukkit.getServer()).getServer().registries());
Map<ResourceLocation, IntList> tags = ReflectionHelper.getFieldValue(TagNetworkSerialization.NetworkPayload.class, ReflectionMappingsInfo.TagNetworkSerializationNetworkPayload_tags, packetInput.get(BuiltInRegistries.BLOCK.key()));
IntList intList = tags.get(BlockTags.CLIMBABLE.location());
intList.clear();
for (Material material : materials) {
Expand Down
Expand Up @@ -7,7 +7,6 @@
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
Expand Down
Expand Up @@ -12,7 +12,9 @@
import com.denizenscript.denizen.nms.util.PlayerProfile;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import io.netty.buffer.Unpooled;
import net.md_5.bungee.api.ChatColor;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -22,9 +24,8 @@
import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.invoke.MethodHandle;
import java.util.List;
import java.util.UUID;
import java.lang.reflect.Field;
import java.util.*;

public class ProfileEditorImpl extends ProfileEditor {

Expand All @@ -51,8 +52,8 @@ public static boolean handleAlteredProfiles(ClientboundPlayerInfoUpdatePacket pa
if (ProfileEditor.mirrorUUIDs.isEmpty() && !RenameCommand.hasAnyDynamicRenames()) {
return true;
}
ClientboundPlayerInfoUpdatePacket.Action action = packet.actions();
if (action != ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER && action != ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME) {
EnumSet<ClientboundPlayerInfoUpdatePacket.Action> action = packet.actions();
if (!action.contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER) && !action.contains(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME)) {
return true;
}
List<ClientboundPlayerInfoUpdatePacket.Entry> dataList = packet.entries();
Expand All @@ -62,7 +63,7 @@ public static boolean handleAlteredProfiles(ClientboundPlayerInfoUpdatePacket pa
try {
boolean any = false;
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
if (ProfileEditor.mirrorUUIDs.contains(data.profile().getId()) || RenameCommand.customNames.containsKey(data.profile().getId())) {
if (ProfileEditor.mirrorUUIDs.contains(data.profileId()) || RenameCommand.customNames.containsKey(data.profileId()) || fakeProfiles.containsKey(data.profileId())) {
any = true;
}
}
Expand All @@ -71,28 +72,23 @@ public static boolean handleAlteredProfiles(ClientboundPlayerInfoUpdatePacket pa
}
GameProfile ownProfile = manager.player.getGameProfile();
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
if (!ProfileEditor.mirrorUUIDs.contains(data.profile().getId()) && !RenameCommand.customNames.containsKey(data.profile().getId())) {
ClientboundPlayerInfoUpdatePacket newPacket = new ClientboundPlayerInfoUpdatePacket(action);
List<ClientboundPlayerInfoUpdatePacket.Entry> newPacketDataList = newPacket.entries();
newPacketDataList.add(data);
manager.oldManager.send(newPacket);
if (!ProfileEditor.mirrorUUIDs.contains(data.profileId()) && !RenameCommand.customNames.containsKey(data.profileId()) && !fakeProfiles.containsKey(data.profileId())) {
manager.oldManager.send(createInfoPacket(action, Collections.singletonList(data)));
}
else {
String rename = RenameCommand.getCustomNameFor(data.profile().getId(), manager.player.getBukkitEntity(), false);
ClientboundPlayerInfoUpdatePacket newPacket = new ClientboundPlayerInfoUpdatePacket(action);
List<ClientboundPlayerInfoUpdatePacket.Entry> newPacketDataList = newPacket.entries();
GameProfile patchedProfile = new GameProfile(data.profile().getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : data.profile().getName());
if (ProfileEditor.mirrorUUIDs.contains(data.profile().getId())) {
String rename = RenameCommand.getCustomNameFor(data.profileId(), manager.player.getBukkitEntity(), false);
GameProfile baseProfile = fakeProfiles.containsKey(data.profileId()) ? getGameProfile(fakeProfiles.get(data.profileId())) : data.profile();
GameProfile patchedProfile = new GameProfile(baseProfile.getId(), rename != null ? (rename.length() > 16 ? rename.substring(0, 16) : rename) : baseProfile.getName());
if (ProfileEditor.mirrorUUIDs.contains(data.profileId())) {
patchedProfile.getProperties().putAll(ownProfile.getProperties());
}
else {
patchedProfile.getProperties().putAll(data.profile().getProperties());
patchedProfile.getProperties().putAll(baseProfile.getProperties());
}
String listRename = RenameCommand.getCustomNameFor(data.profile().getId(), manager.player.getBukkitEntity(), true);
String listRename = RenameCommand.getCustomNameFor(data.profileId(), manager.player.getBukkitEntity(), true);
Component displayName = listRename != null ? Handler.componentToNMS(FormattedTextHelper.parse(listRename, ChatColor.WHITE)) : data.displayName();
ClientboundPlayerInfoUpdatePacket.Entry newData = new ClientboundPlayerInfoUpdatePacket.Entry(data.profileId(), patchedProfile, data.listed(), data.latency(), data.gameMode(), displayName, data.chatSession());
newPacketDataList.add(newData);
manager.oldManager.send(newPacket);
manager.oldManager.send(createInfoPacket(action, Collections.singletonList(newData)));
}
}
return false;
Expand All @@ -103,25 +99,27 @@ public static boolean handleAlteredProfiles(ClientboundPlayerInfoUpdatePacket pa
}
}

public static void updatePlayerProfiles(ClientboundPlayerInfoUpdatePacket packet) {
ClientboundPlayerInfoUpdatePacket.Action action = packet.actions();
if (action != ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER) {
return;
}
List<ClientboundPlayerInfoUpdatePacket.Entry> dataList = packet.entries();
if (dataList != null) {
public static final Field ClientboundPlayerInfoUpdatePacket_Action_writer = ReflectionHelper.getFields(ClientboundPlayerInfoUpdatePacket.Action.class).getFirstOfType(ClientboundPlayerInfoUpdatePacket.Action.Writer.class);

public static ClientboundPlayerInfoUpdatePacket createInfoPacket(EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions, List<ClientboundPlayerInfoUpdatePacket.Entry> entries) {
// Based on ClientboundPlayerInfoUpdatePacket#write
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeEnumSet(actions, ClientboundPlayerInfoUpdatePacket.Action.class);
buf.writeCollection(entries, (inBuf, entry) -> {
try {
for (ClientboundPlayerInfoUpdatePacket.Entry data : dataList) {
GameProfile gameProfile = data.profile();
if (fakeProfiles.containsKey(gameProfile.getId())) {
playerInfoData_gameProfile_Setter.invoke(data, getGameProfile(fakeProfiles.get(gameProfile.getId())));
}
inBuf.writeUUID(entry.profileId());
Iterator var3 = actions.iterator();
while (var3.hasNext()) {
ClientboundPlayerInfoUpdatePacket.Action action = (ClientboundPlayerInfoUpdatePacket.Action) var3.next();
ClientboundPlayerInfoUpdatePacket.Action.Writer writer = (ClientboundPlayerInfoUpdatePacket.Action.Writer) ClientboundPlayerInfoUpdatePacket_Action_writer.get(action);
writer.write(inBuf, entry);
}
}
catch (Throwable e) {
Debug.echoError(e);
catch (Throwable ex) {
Debug.echoError(ex);
}
}
});
return new ClientboundPlayerInfoUpdatePacket(buf);
}

private static GameProfile getGameProfile(PlayerProfile playerProfile) {
Expand All @@ -131,6 +129,4 @@ private static GameProfile getGameProfile(PlayerProfile playerProfile) {
}
return gameProfile;
}

public static final MethodHandle playerInfoData_gameProfile_Setter = ReflectionHelper.getFinalSetterForFirstOfType(ClientboundPlayerInfoUpdatePacket.Entry.class, GameProfile.class);
}

0 comments on commit eb0121b

Please sign in to comment.