dmulloy2 / ProtocolLib

Provides read and write access to the Minecraft protocol with Bukkit.
GNU General Public License v2.0
1.04k stars 257 forks source link

1.20.4 PlayerList fakeplayer creation. #2931

Open Somikyy opened 6 months ago

Somikyy commented 6 months ago

hey, im wondering how to create fake players in my players list to customize it a little bit.

So i have this code:

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction;
import net.md_5.bungee.api.chat.TextComponent;

import java.util.*;

import org.bukkit.GameMode;
import org.bukkit.entity.Player;

public class FakePlayerManager {
    private final SNTabList plugin;
    private ProtocolManager protocolManager;
    private final Map<String, WrappedGameProfile> fakePlayers = new HashMap<>();

    public FakePlayerManager(SNTabList plugin) {
        this.plugin = plugin;
        this.protocolManager = ProtocolLibrary.getProtocolManager();
    }

    public void addFakePlayer(Player player) {
        PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.PLAYER_INFO);
        packet.getPlayerInfoAction().write(0, PlayerInfoAction.ADD_PLAYER);

        WrappedGameProfile fakeProfile = new WrappedGameProfile(UUID.randomUUID(), "FakePlayer");
        PlayerInfoData playerInfoData = new PlayerInfoData(fakeProfile, 1, EnumWrappers.NativeGameMode.SURVIVAL, WrappedChatComponent.fromText("Fake Player"));

        packet.getPlayerInfoDataLists().write(0, Collections.singletonList(playerInfoData));

        try {
            protocolManager.sendServerPacket(player, packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updatePlayers() {
        ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
        PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.PLAYER_INFO);
        packet.getPlayerInfoAction().write(0, PlayerInfoAction.REMOVE_PLAYER);

        for (WrappedGameProfile fakeProfile : fakePlayers.values()) {
            PlayerInfoData playerInfoData = new PlayerInfoData(fakeProfile, 1, EnumWrappers.NativeGameMode.fromBukkit(GameMode.SURVIVAL), WrappedChatComponent.fromText(new TextComponent("Fake Player").toPlainText()));
            packet.getPlayerInfoDataLists().write(0, Collections.singletonList(playerInfoData));

            for (Player player : plugin.getServer().getOnlinePlayers()) {
                try {
                    protocolManager.sendServerPacket(player, packet);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        fakePlayers.clear();
    }
}

But it's giving me a console error as:

[19:27:18 ERROR]: Could not pass event PlayerJoinEvent to SNTabList v1.0.0-ALPHA
com.comphenix.protocol.reflect.FieldAccessException: Field index 0 is out of bounds for length 0
        at com.comphenix.protocol.reflect.FieldAccessException.fromFormat(FieldAccessException.java:49) ~[ProtocolLib.jar:?]
        at com.comphenix.protocol.reflect.StructureModifier.write(StructureModifier.java:315) ~[ProtocolLib.jar:?]
        at me.somikyy.sntablist.FakePlayerManager.addFakePlayer(FakePlayerManager.java:31) ~[SNTabList-1.0.0-ALPHA.jar:?]
        at me.somikyy.sntablist.SNTabList.onJoin(SNTabList.java:41) ~[SNTabList-1.0.0-ALPHA.jar:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor28.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:81) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:git-Paper-385]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.20.4.jar:git-Paper-385]
        at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:126) ~[paper-1.20.4.jar:git-Paper-385]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:615) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at net.minecraft.server.players.PlayerList.placeNewPlayer(PlayerList.java:344) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.server.network.ServerConfigurationPacketListenerImpl.handleConfigurationFinished(ServerConfigurationPacketListenerImpl.java:130) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket.handle(ServerboundFinishConfigurationPacket.java:18) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket.a(ServerboundFinishConfigurationPacket.java:9) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:54) ~[?:?]
        at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153) ~[?:?]
        at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
        at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1458) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:194) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126) ~[?:?]
        at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1435) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1358) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136) ~[?:?]
        at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:1336) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1220) ~[paper-1.20.4.jar:git-Paper-385]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:321) ~[paper-1.20.4.jar:git-Paper-385]
        at java.lang.Thread.run(Thread.java:840) ~[?:?]

I'll be really appreciated if someone helps me.

Somikyy commented 6 months ago

i tried different methodes but all of them was giving me a road to this error.

Jeppa commented 6 months ago

If your server is >1.19.2 you should use getPlayerInfoActions() and not getPlayerInfoAction() !!

like this: Packet.getPlayerInfoActions().write(0, EnumSet.of(com.comphenix.protocol.wrappers.EnumWrappers.PlayerInfoAction.ADD_PLAYER)) Maybe that helps.