dmulloy2 / ProtocolLib

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

Use of ADD_PLAYER #2979

Open ryanalexander opened 2 months ago

ryanalexander commented 2 months ago

Make sure you're doing the following

Describe the question Attempting to create a non-player character in tab

API method(s) used PacketContainer(PacketType.Play.Server.PLAYER_INFO) playerData.getPlayerInfoActions() playerData.getPlayerInfoDataLists()

Expected behavior A player "DummyPlayer" is added to the tab list for all players

Code ` package net.stelch.minegames.MineClient.utils;

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.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.mojang.authlib.GameProfile; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.EntityPlayer; import net.minecraft.server.level.WorldServer; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import redis.clients.jedis.Jedis;

import java.util.*;

public class TabManager { Jedis jedis; List dummyPlayers = new ArrayList<>();

public TabManager(JavaPlugin plugin, ConfigurationSection config) {
    jedis = new Jedis(config.getString("host"), config.getInt("port"));
    jedis.select(config.getInt("database"));

    ProtocolManager packetManager = ProtocolLibrary.getProtocolManager();

    // Adding dummy players
    DummyPlayer dummyPlayer = new DummyPlayer();
    dummyPlayer.uuid = UUID.randomUUID();
    dummyPlayer.name = "Dummy";
    dummyPlayer.gameMode = GameMode.SURVIVAL;
    dummyPlayer.latency = 0;
    dummyPlayers.add(dummyPlayer);

    new BukkitRunnable(){
        @Override
        public void run() {
            reloadDummyPlayers();
        }
    }.runTaskTimer(plugin, 0, 20);
}

class InferredPlayer implements HumanEntity {
}

public void reloadDummyPlayers() {
    ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
    List<PlayerInfoData> playerInfoDataList = new ArrayList<>();

    dummyPlayers.forEach(dummyPlayer -> {
        WrappedGameProfile gameProfile = new WrappedGameProfile(dummyPlayer.uuid, dummyPlayer.name);
        PlayerInfoData playerInfoData = new PlayerInfoData(
                gameProfile,
                dummyPlayer.latency,
                EnumWrappers.NativeGameMode.fromBukkit(dummyPlayer.gameMode),
                WrappedChatComponent.fromText(dummyPlayer.name)
        );
        playerInfoDataList.add(playerInfoData);
    });

    if (!playerInfoDataList.isEmpty()) {
        PacketContainer playerData = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);
        playerData.getPlayerInfoActions().write(0, Collections.singleton(EnumWrappers.PlayerInfoAction.ADD_PLAYER));
        playerData.getPlayerInfoDataLists().write(1, playerInfoDataList);

        Bukkit.getOnlinePlayers().forEach(player -> {
            protocolManager.sendServerPacket(player, playerData);
        });
    } else {
        Bukkit.getLogger().warning("No player info data to send.");
    }
}

public class DummyPlayer {
    UUID uuid;
    String name;
    GameMode gameMode;
    int latency;
}

}

`

Additional context NA

ryanalexander commented 2 months ago

Context: I am unsure if this is just me approaching this wrong, but there are no errors reported and the expected result is not occuring