Solved Need help to translate from spigot to paper, i'm trying to create a fake sleeping player using NMS

Kamisenin

New member
Jan 16, 2024
6
0
1
Hi, firstly thank you for reading this even if you don't help

So juste like said in the title i'm trying to create a fake sleeping plyaer using NMS and packets but since there is not much tutorial on how to use Packets and NMS i only managed to found one but on spigot
Tutorial in Spigot
And I managed to translate some of the functions in Paper but when it comes to using DataWatcher and Packets I didnt find what where the Paper function, i sincerely hope someone will be able to help me with that and thank you in advance

Here is a whole code

import com.mojang.authlib.GameProfile;
import fr.lvegroup.lveplugin.Data.PlayerData;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerPlayer;
import com.mojang.authlib.properties.Property;
import net.minecraft.server.network.ServerPlayerConnection;
import net.minecraft.world.entity.Pose;
import org.apache.logging.log4j.core.util.WrappedFileWatcher;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_20_R3.CraftServer;
import org.bukkit.craftbukkit.v1_20_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;

import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;


import java.util.UUID;

import static fr.lvegroup.lveplugin.LVE.getPlayerData;


public class KnockedManager {

public static void SetKO(Player player) {
PlayerData Pdata = getPlayerData(player);
Pdata.setData("KO", true);
Location playerLocation = player.getLocation();
player.setPose(org.bukkit.entity.Pose.SITTING);
}

public static void CorpseEntity(Player player){
//Creation NPC et récupération skin
ServerPlayer craftPlayer = ((CraftPlayer) player).getHandle();
Property textures = (Property) craftPlayer.getGameProfile().getProperties().get("textures").toArray()[0];
GameProfile gameProfile = new GameProfile(UUID.randomUUID(),player.getName());
gameProfile.getProperties().put("textures", new Property("textures", textures.value(), textures.signature()));

//Creation du corp par terre
ServerPlayer sleeping = new ServerPlayer(
((CraftServer) Bukkit.getServer()).getServer(),
((CraftWorld) player.getWorld()).getHandle(),
gameProfile,
ClientInformation.createDefault());
sleeping.teleportTo(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());

//Fake Bed
Location bed = player.getLocation().add(1,0,0);
BlockPos position = new BlockPos((int) Math.floor(bed.getX()), (int) Math.floor(bed.getY()), (int) Math.floor(bed.getZ()));
sleeping.startSleeping(position);

//Hide Nametag
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
Team team = scoreboard.registerNewTeam(String.valueOf(sleeping.getDisplayName()));
team.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.ALWAYS);

//Make so the Fake Player position is sleeping
sleeping.setPose(Pose.SLEEPING);

DataWatcher watcher = sleeping.getDataWatcher();
byte b = 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40;
watcher.set(new WrappedFileWatcher.WrappedFileWatcherObject(17, DataWatcherRegistry.Byte), b);

//Make so Fake player is positioned in a diagonal way from the blocks rather than parrallel
PacketPlayOutEntity.PacketPlayOutRelEntityMove move = new PacketPlayerOutEntity.PacketPlayOutRelEntityMove(
sleeping.getId(), (byte) 0, (byte) ((player.getLocation().getY() - 1.7 - player.getLocation().getY()) *32),
(byte) 0, false);

//packet sending
for (Player on : Bukkit.getOnlinePlayers()){
ServerPlayerConnection connection = ((CraftPlayer)on).getHandle().connection;

connection.send(new PlayerPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a, sleeping));
connection.send(new PlayerPlayOutNamedEntitySpawn(sleeping));

connection.sendPacket(new PacketPlayOutEntityMetadata(sleeping.getId(), watcher, true));
connection.sendPacket(move);

new BukkitRunnable(){
public void run() {
connection.sendpacket(new PlayerPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, sleeping))
}
}.runTaskAsynchronously(LVEPlugin.getPlugin(KnockedManager.class));
}
}
}


And Here is the problematic part which are the function DataWatcher, PacketPlayerOutEntity, PlayerPlayOutNamedEntitySpawn, PlayerPlayOutPlayerInfo, PacketPlayOutEntityMetadata and .sendPacket :

//Make so the Fake Player position is sleeping
sleeping.setPose(Pose.SLEEPING);

DataWatcher watcher = sleeping.getDataWatcher();
byte b = 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40;
watcher.set(new WrappedFileWatcher.WrappedFileWatcherObject(17, DataWatcherRegistry.Byte), b);

//Make so Fake player is positioned in a diagonal way from the blocks rather than parrallel
PacketPlayOutEntity.PacketPlayOutRelEntityMove move = new PacketPlayerOutEntity.PacketPlayOutRelEntityMove(
sleeping.getId(), (byte) 0, (byte) ((player.getLocation().getY() - 1.7 - player.getLocation().getY()) *32),
(byte) 0, false);

//packet sending
for (Player on : Bukkit.getOnlinePlayers()){
ServerPlayerConnection connection = ((CraftPlayer)on).getHandle().connection;

connection.send(new PlayerPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a, sleeping));
connection.send(new PlayerPlayOutNamedEntitySpawn(sleeping));

connection.sendPacket(new PacketPlayOutEntityMetadata(sleeping.getId(), watcher, true));
connection.sendPacket(move);

new BukkitRunnable(){
public void run() {
connection.sendpacket(new PlayerPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, sleeping))
}
}.runTaskAsynchronously(LVEPlugin.getPlugin(KnockedManager.class));
}
}
}
 

Kamisenin

New member
Jan 16, 2024
6
0
1
Papers tooling uses Mojangs mappings, and so if you're missing classes, it's likely because you're trying to use spigot names for stuff; https://mappings.cephx.dev/1.20.4/ will let you look up mappings to convert your code over
OK ok thx, i managed to find how to convert Datawatcher but there is no such thing as PacketPlayOutEntity even with spigot in the site, do you have any idea on how it could be translated in paper as it is used to manage the packet of an entity ?
 

electronicboy

Administrator
Staff member
Dec 11, 2021
280
17
56
28
tried replacing PacketPlayOutPlayerinfo with ClientboundPlayerInfoUpdatePacket but i dont think it is that, do you have any idea
Yes, the PlayerInfo packet was essentially rewritten/replaced, so it is that
PacketPlayOutEntityMetadata

I would generally suggest trying to learn what the code is actually doing and then aiming to understand the modern codebase if you're messing with internals, doing so is generally not supported and, as you can see here, stuff changes
 

Kamisenin

New member
Jan 16, 2024
6
0
1
Yes, the PlayerInfo packet was essentially rewritten/replaced, so it is that


I would generally suggest trying to learn what the code is actually doing and then aiming to understand the modern codebase if you're messing with internals, doing so is generally not supported and, as you can see here, stuff changes
YEah thats what i'll do but its honestly very difficult to find tutorial on these things on internet, thx for helping me
 

Kamisenin

New member
Jan 16, 2024
6
0
1
OK got some news, to train myself i tried to simply create a random npc by using a command and everything was working well until this error showed up :


3:59:35 WARN]: Nag author(s): '[]' of 'LVEPLugin v1.0.1' about their usage of System.out/err.print. Please use your plugin's logger instead (JavaPlugin#getLogger).
[23:59:35 ERROR]: Error occurred while enabling LVEPLugin v1.0.1 (Is it up to date?)
java.lang.NoClassDefFoundError: net/minecraft/server/level/ServerPlayer
at fr.lvegroup.lveplugin.LVE.registerCommand(LVE.java:71) ~[LVEPlugin-1.0.2-dev.jar:?]
at fr.lvegroup.lveplugin.LVE.onEnable(LVE.java:35) ~[LVEPlugin-1.0.2-dev.jar:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:281) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at io.papermc.paper.plugin.manager.PaperPluginInstanceManager.enablePlugin(PaperPluginInstanceManager.java:190) ~[paper-1.20.4.jar:git-Paper-365]
at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.enablePlugin(PaperPluginManagerImpl.java:104) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:507) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugin(CraftServer.java:637) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugins(CraftServer.java:548) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.reload(CraftServer.java:1097) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.Bukkit.reload(Bukkit.java:1004) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.dispatchCommand(CraftServer.java:987) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.dispatchServerCommand(CraftServer.java:972) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.dedicated.DedicatedServer.handleConsoleInputs(DedicatedServer.java:500) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:447) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1518) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1214) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:321) ~[paper-1.20.4.jar:git-Paper-365]
at java.lang.Thread.run(Thread.java:840) ~[?:?]
Caused by: java.lang.ClassNotFoundException: net.minecraft.server.level.ServerPlayer
at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:197) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:164) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?]
... 20 more
[23:59:35 INFO]: [LVEPLugin] Disabling LVEPLugin v1.0.1
[23:59:35 INFO]: CONSOLE: Reload complete.
[23:59:35 INFO]: Timings Reset




And here is my code :


public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)){
System.out.println("cette commande ne peut etre executée que par un joueur");
return false;
}
Player player = (Player)sender;

CraftPlayer craftPlayer = (CraftPlayer) player;
ServerPlayer sp = craftPlayer.getHandle();

MinecraftServer server = sp.getServer();
ServerLevel level = sp.serverLevel();
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), player.getName());

ServerPlayer npc = new ServerPlayer(server, level, gameProfile, sp.clientInformation());

ServerGamePacketListenerImpl ps = sp.connection;

ps.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, npc));

ps.send(new ClientboundAddEntityPacket(npc));
player.sendMessage("npc created");
System.out.println(player);


return false;
}
}

It seems the problem is with ServerPlayer as it does not recognise it but my IDE and the site you gave me tells me that it is the good type so do you have any idea, cause right now i can't even enable the plugin to test
 

Lumine1909

New member
May 6, 2024
2
0
1
OK got some news, to train myself i tried to simply create a random npc by using a command and everything was working well until this error showed up :


3:59:35 WARN]: Nag author(s): '[]' of 'LVEPLugin v1.0.1' about their usage of System.out/err.print. Please use your plugin's logger instead (JavaPlugin#getLogger).
[23:59:35 ERROR]: Error occurred while enabling LVEPLugin v1.0.1 (Is it up to date?)
java.lang.NoClassDefFoundError: net/minecraft/server/level/ServerPlayer
at fr.lvegroup.lveplugin.LVE.registerCommand(LVE.java:71) ~[LVEPlugin-1.0.2-dev.jar:?]
at fr.lvegroup.lveplugin.LVE.onEnable(LVE.java:35) ~[LVEPlugin-1.0.2-dev.jar:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:281) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at io.papermc.paper.plugin.manager.PaperPluginInstanceManager.enablePlugin(PaperPluginInstanceManager.java:190) ~[paper-1.20.4.jar:git-Paper-365]
at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.enablePlugin(PaperPluginManagerImpl.java:104) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:507) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugin(CraftServer.java:637) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.enablePlugins(CraftServer.java:548) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.reload(CraftServer.java:1097) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.Bukkit.reload(Bukkit.java:1004) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.dispatchCommand(CraftServer.java:987) ~[paper-1.20.4.jar:git-Paper-365]
at org.bukkit.craftbukkit.v1_20_R3.CraftServer.dispatchServerCommand(CraftServer.java:972) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.dedicated.DedicatedServer.handleConsoleInputs(DedicatedServer.java:500) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:447) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1518) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1214) ~[paper-1.20.4.jar:git-Paper-365]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:321) ~[paper-1.20.4.jar:git-Paper-365]
at java.lang.Thread.run(Thread.java:840) ~[?:?]
Caused by: java.lang.ClassNotFoundException: net.minecraft.server.level.ServerPlayer
at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:197) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:164) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:525) ~[?:?]
... 20 more
[23:59:35 INFO]: [LVEPLugin] Disabling LVEPLugin v1.0.1
[23:59:35 INFO]: CONSOLE: Reload complete.
[23:59:35 INFO]: Timings Reset




And here is my code :


public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)){
System.out.println("cette commande ne peut etre executée que par un joueur");
return false;
}
Player player = (Player)sender;

CraftPlayer craftPlayer = (CraftPlayer) player;
ServerPlayer sp = craftPlayer.getHandle();

MinecraftServer server = sp.getServer();
ServerLevel level = sp.serverLevel();
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), player.getName());

ServerPlayer npc = new ServerPlayer(server, level, gameProfile, sp.clientInformation());

ServerGamePacketListenerImpl ps = sp.connection;

ps.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, npc));

ps.send(new ClientboundAddEntityPacket(npc));
player.sendMessage("npc created");
System.out.println(player);


return false;
}
}

It seems the problem is with ServerPlayer as it does not recognise it but my IDE and the site you gave me tells me that it is the good type so do you have any idea, cause right now i can't even enable the plugin to test
It seems you’re using PaperWeight on version < 1.20.5, so don’t forget re-obfuscate your plugin to Notch name.
You can add "reobfJar" to your build command, like "gradle clean build reobfJar".