Solved Get all NPC's as a list

Discussion in 'Plugin Development' started by Eisi05, Jul 6, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Eisi05

    Hello.
    So i am trying to make a NPC plugin, but now i can't find a method, how i can get all NPC's (The Npc's are EntityPlayers/ServerPlayers) as a list (without setting them manuely in a list) like the 'Bukkit.getOnlinePlayers()' method.
    Here is the code how i created my NPC's:
    Code:
    MinecraftServer server = ((CraftServer) Bukkit.getServer()).getServer();
    ServerLevel world = ((CraftWorld) player.getWorld()).getHandle();
    GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "Bot");
    
    SkinFetcher.Skin skin = new SkinFetcher().defaultSkin();
    gameProfile.getProperties().put("textures",new Property("textures",skin.getValue(),skin.getSignature()));
    
    ServerPlayer serverPlayer = new ServerPlayer(server,world,gameProfile,null);
    
    serverPlayer.moveTo(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getLocation().getPitch(), player.getLocation().getYaw());
    
    if (player.getWorld().getName().equals(serverPlayer.level.getWorld().getName())) {
                ServerGamePacketListenerImpl connection = ((CraftPlayer) player).getHandle().connection;
    
                connection.send(new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.ADD_PLAYER, serverPlayer));
                connection.send(new ClientboundAddEntityPacket(serverPlayer));
                connection.send(new ClientboundEntityEventPacket(serverPlayer, (byte) (serverPlayer.getYHeadRot() * 256 / 360)));
    
                SynchedEntityData watcher = new SynchedEntityData(serverPlayer);
                watcher.define(new EntityDataAccessor<>(17, EntityDataSerializers.BYTE), (byte) 127);
                connection.send(new ClientboundSetEntityDataPacket(serverPlayer.getId(), watcher, true));
    
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        connection.send(new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.REMOVE_PLAYER, serverPlayer));
                    }
                }.runTaskLater(Main.plugin, 50);
            }
    Thanks for help.
     
  2. Offline

    Strahan

    So I assume the ServerPlayer is the NPC then, right? If so, I'd create a collection like a List<ServerPlayer> somewhere globally accessible then use that. Maybe if you need to associate them somehow to some sort of identifying marker, use a Map<(index data type), ServerPlayer> instead then.
     
  3. Offline

    Eisi05

    The problem with this is you can only use this in the plugin where the NPC is created from, but i want to use it in other plugins too
     
  4. Offline

    timtower Administrator Administrator Moderator

    If those are also written by you: make a getter for the list.
     
  5. Offline

    Eisi05

    So i did it like this https://www.spigotmc.org/threads/get-variable-from-another-plugin.365747/ , but now it gave me an error:
    Code:
    [17:06:36] [Server thread/ERROR]: Error occurred while enabling Test v1.0-SNAPSHOT (Is it up to date?)
    java.lang.ClassCastException: class de.npc.Main cannot be cast to class de.npc.Main (de.npc.Main is in unnamed module of loader org.bukkit.plugin.java.PluginClassLoader @33052dbc; de.npc.Main is in unnamed module of loader org.bukkit.plugin.java.PluginClassLoader @77b03c2)
            at de.test.Main.onEnable(Main.java:14) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:479) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_19_R1.CraftServer.enablePlugin(CraftServer.java:513) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at org.bukkit.craftbukkit.v1_19_R1.CraftServer.enablePlugins(CraftServer.java:427) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at org.bukkit.craftbukkit.v1_19_R1.CraftServer.reload(CraftServer.java:912) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at org.bukkit.Bukkit.reload(Bukkit.java:801) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
            at org.bukkit.craftbukkit.v1_19_R1.CraftServer.dispatchCommand(CraftServer.java:821) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at org.bukkit.craftbukkit.v1_19_R1.CraftServer.dispatchServerCommand(CraftServer.java:806) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at net.minecraft.server.dedicated.DedicatedServer.bg(DedicatedServer.java:419) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:395) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1197) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at net.minecraft.server.MinecraftServer.v(MinecraftServer.java:1010) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:291) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3507-Spigot-fa893f0-c3f219e]
            at java.lang.Thread.run(Thread.java:833) [?:?]
    What does that mean?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Eisi05 Well, I am not seeing your code, so no idea.
    Multiple options here, the one that I see happening is that you are not depending on the npc plugin.
     
    Strahan likes this.
  7. Offline

    Strahan

    You could always make an API.

    Yea, unfortunately peoples' expertise here tends to be in the area of Java, not mind reading ;)
     
Thread Status:
Not open for further replies.

Share This Page