Skip to content

Instantly share code, notes, and snippets.

@windopper
Created March 14, 2022 11:47
Show Gist options
  • Save windopper/092c0c6faa18ef653780aefcd4283911 to your computer and use it in GitHub Desktop.
Save windopper/092c0c6faa18ef653780aefcd4283911 to your computer and use it in GitHub Desktop.
Client-sided Non-Player creation utils
public class NPCUtils {
public EntityPlayer createNPC(Location location, String npcName, String texture, String signature) {
MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
WorldServer nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), npcName);
Property property = new Property("textures", texture, signature);
gameProfile.getProperties().put("textures", property);
EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile);
npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
npc.displayName = npcName;
return npc;
}
private PlayerConnection getPlayerConnection(Player player) {
return ((CraftPlayer) player).getHandle().b;
}
private void fixSkinHelmetLayer(EntityPlayer npc, Player player) {
DataWatcher dataWatcher = npc.getDataWatcher();
dataWatcher.set(new DataWatcherObject<>(17, DataWatcherRegistry.a), (byte) 127);
getPlayerConnection(player).sendPacket(new PacketPlayOutEntityMetadata(npc.getId(), dataWatcher, true));
}
public void showNPCAsClientSide(EntityPlayer npc, Player player) {
PlayerConnection playerConnection = getPlayerConnection(player);
PacketPlayOutPlayerInfo packetPlayOutPlayerInfo = new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a,
npc
);
PacketPlayOutNamedEntitySpawn packetPlayOutNamedEntitySpawn = new PacketPlayOutNamedEntitySpawn(
npc
);
playerConnection.sendPacket(packetPlayOutPlayerInfo);
playerConnection.sendPacket(packetPlayOutNamedEntitySpawn);
CraftScoreboardManager scoreboardManager = ((CraftServer) Bukkit.getServer()).getScoreboardManager();
CraftScoreboard mainScoreboard = scoreboardManager.getNewScoreboard();
Scoreboard scoreboard = mainScoreboard.getHandle();
ScoreboardTeam scoreboardTeam = scoreboard.getPlayerTeam(npc.getName());
if(scoreboardTeam == null) {
scoreboardTeam = scoreboard.createTeam("NPC");
scoreboard.addPlayerToTeam(npc.getName(), scoreboardTeam);
} else {
scoreboard.addPlayerToTeam(npc.getName(), scoreboardTeam);
}
Bukkit.getServer().getScheduler().runTaskLater(Main.getPlugin(Main.class), () -> {
PacketPlayOutPlayerInfo removeFromTabPacket = new PacketPlayOutPlayerInfo(
PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e,
npc
);
playerConnection.sendPacket(removeFromTabPacket);
fixSkinHelmetLayer(npc, player);
}, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment