Spawn NPC player

Discussion in 'Plugin Development' started by AztroCreationz, Jan 3, 2016.

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

    AztroCreationz

    Hello! I am trying to create an NPC for an RPG-plugin that I am making. I would not like to use any API since I don't want any dependencies. I am able to spawn an EntityPlayer, but it disappears after like 1 tick. Also, the player still shows up in the tab.

    Code:
    Code:
                    else if(args[0].equals("friend")){
                        Player player = (Player)sender;
                       
                        MinecraftServer server = ((CraftServer)Bukkit.getServer()).getServer();
                        WorldServer world = ((CraftWorld)Bukkit.getServer().getWorlds().get(0)).getHandle();
                       
                        EntityPlayer friend = new EntityPlayer(server, world, new GameProfile(UUID.fromString([UUID goes here]), "[Name goes here]"), new PlayerInteractManager(world));
                       
                        friend.teleportTo(player.getLocation(), false);
                       
                        PacketPlayOutPlayerInfo playerInfoPacket = new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, friend);
                        PacketPlayOutNamedEntitySpawn namedEntityPacket = new PacketPlayOutNamedEntitySpawn(friend);
                           
                       
                        for(Player players : Bukkit.getOnlinePlayers()){
                        PlayerConnection connection = ((CraftPlayer)players).getHandle().playerConnection;
                            connection.sendPacket(playerInfoPacket);
                            connection.sendPacket(namedEntityPacket);
                        }
                        }
    Some quick questions: Is there any way to display skins of players which are not online? Doesn't seem to work for me. Also, is there a way to add path finders to them?
     
  2. Offline

    Zombie_Striker

    And what reason do you have for this?

    1 tick is (roughly) a 20th of a second. I don't think you actually mean 1 tick.

    Yes. Entities retrieve skin textures by sending the UUID of the player to the minecraft servers. All you would need is to provide the UUID of player that has the skin.
    Yes. Have you tried using google for this? https://www.spigotmc.org/threads/tutorial-creating-custom-entities-with-pathfindergoals.18519/
     
  3. Offline

    AztroCreationz

    1. Well, that is not the only reason, I also want as much customisation as possible. It is also good practice as I will learn stuff along the way.

    2. I meant one tick, you can barely see it, it is just a very quick flash.

    3. I tried providing the UUID of the player with the skin, just turns into Steve.

    4. I know, but that is not the way to add custom pathfinders to classes which extend EntityPlayer.
     
    Last edited: Jan 3, 2016
  4. Offline

    Zombie_Striker

    "Re-inventing the wheel" is not a good way to learn. Instead, if you really want to learn how it works/what to do, you should look at the API source code.

    Are you sure you're providing the right UUID?

    Then you either have to A) Disguise another entity as a player or B) Add the necessary methods and fields to custom entity.
     
  5. Offline

    AztroCreationz

    I am providing the right UUID, I used this to get it. If I used my name it worked.

    Thanks, I will check out the A and B methods tomorrow when I have slept. I am leaving the thread as "un-solved" for further replies and alternative solutions.
     
  6. Offline

    Zombie_Striker

    @AztroCreationz
    Do you use the full UUID or the trimmed UUID? I don't know which is used for retrieving the skin, so you should try both.
     
  7. Offline

    AztroCreationz

    I used the full one both times. It worked with my skin, but when I tried with Notch it turned into Steve.

    ---------------------------------------------------------------------------------------------------------------------------

    So, I changed the .teleportTo() method to a .setLocation() method, and now the player doesn't disappear for some reason... But, I still can't get the skins of people who are offline.

    ---------------------------------------------------------------------------------------------------------------------------

    Okay, so instead I tried to create an NMS EntityPlayer, but when I add it to the world both the client and server crash. I am guessing that it is trying to update the player and can't find it.

    Crash log (open)

    ---- Minecraft Crash Report ----
    // Surprise! Haha. Well, this is awkward.

    Time: 04/01/16 16:26
    Description: Exception in server tick loop

    java.lang.NullPointerException: Exception in server tick loop
    at net.minecraft.server.v1_8_R2.EntityTrackerEntry.broadcastIncludingSelf(EntityTrackerEntry.java:288)
    at net.minecraft.server.v1_8_R2.EntityTrackerEntry.b(EntityTrackerEntry.java:253)
    at net.minecraft.server.v1_8_R2.EntityTrackerEntry.track(EntityTrackerEntry.java:179)
    at net.minecraft.server.v1_8_R2.EntityTracker.updatePlayers(EntityTracker.java:177)
    at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:800)
    at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368)
    at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651)
    at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554)
    at java.lang.Thread.run(Unknown Source)


    A detailed walkthrough of the error, its code path and all known details is as follows:
    ---------------------------------------------------------------------------------------

    -- System Details --
    Details:
    Minecraft Version: 1.8.3
    Operating System: Windows 10 (amd64) version 10.0
    CPU: 8x Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
    Java Version: 1.8.0_65, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 342592248 bytes (326 MB) / 592445440 bytes (565 MB) up to 954728448 bytes (910 MB)
    JVM Flags: 1 total; -Xmx1024M
    IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
    CraftBukkit Information:
    Running: CraftBukkit version git-Spigot-2ec6f06-7722428 (MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT) true
    Plugins: { WorldEdit v6.1;no_git_id com.sk89q.worldedit.bukkit.WorldEditPlugin [], BuildAreas v1 me.zyphicx.BuildAreas [], Axelflaxflax v1 com.gmail.zyphicxs.Main [], ZyphicxssRPG v0.01 com.gmail.zyphicxs.ZyphicxssRPG.ZyphicxsRPG [],}
    Warnings: DEFAULT
    Reload Count: 0
    Threads: { TIMED_WAITING Timer-0: [java.lang.Object.wait(Native Method), java.util.TimerThread.mainLoop(Unknown Source), java.util.TimerThread.run(Unknown Source)], TIMED_WAITING Spigot Metrics Thread: [java.lang.Object.wait(Native Method), java.util.TimerThread.mainLoop(Unknown Source), java.util.TimerThread.run(Unknown Source)], RUNNABLE Netty Server IO #1: [sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method), sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source), sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source), sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source), sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source), sun.nio.ch.SelectorImpl.select(Unknown Source), io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622), io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310), io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116), java.lang.Thread.run(Unknown Source)], WAITING Chunk I/O Executor Thread-1: [sun.misc.Unsafe.park(Native Method), java.util.concurrent.locks.LockSupport.park(Unknown Source), java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source), java.util.concurrent.LinkedBlockingQueue.take(Unknown Source), java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source), java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source), java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source), java.lang.Thread.run(Unknown Source)], TIMED_WAITING Server Infinisleeper: [java.lang.Thread.sleep(Native Method), net.minecraft.server.v1_8_R2.DedicatedServer$1.run(DedicatedServer.java:54)], WAITING Thread-5: [java.io.FileOutputStream.writeBytes(Native Method), java.io.FileOutputStream.write(Unknown Source), java.io.BufferedOutputStream.flushBuffer(Unknown Source), java.io.BufferedOutputStream.flush(Unknown Source), java.io.PrintStream.flush(Unknown Source), java.io.FilterOutputStream.flush(Unknown Source), java.io.PrintStream.flush(Unknown Source), java.io.FilterOutputStream.flush(Unknown Source), sun.nio.cs.StreamEncoder.implFlush(Unknown Source), sun.nio.cs.StreamEncoder.flush(Unknown Source), java.io.outputStreamWriter.flush(Unknown Source), org.bukkit.craftbukkit.libs.jline.console.ConsoleReader.flush(ConsoleReader.java:976), org.bukkit.craftbukkit.v1_8_R2.util.TerminalConsoleWriterThread.run(TerminalConsoleWriterThread.java:42), java.lang.Thread.run(Unknown Source)], WAITING Finalizer: [java.lang.Object.wait(Native Method), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)], RUNNABLE Signal Dispatcher: [], RUNNABLE Netty Server IO #2: [sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method), sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source), sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source), sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source), sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source), sun.nio.ch.SelectorImpl.select(Unknown Source), io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622), io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310), io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116), java.lang.Thread.run(Unknown Source)], TIMED_WAITING File IO Thread: [java.lang.Thread.sleep(Native Method), net.minecraft.server.v1_8_R2.FileIOThread.c(SourceFile:44), net.minecraft.server.v1_8_R2.FileIOThread.run(SourceFile:30), java.lang.Thread.run(Unknown Source)], TIMED_WAITING Spigot Watchdog Thread: [java.lang.Thread.sleep(Native Method), org.spigotmc.WatchdogThread.run(WatchdogThread.java:92)], WAITING Reference Handler: [java.lang.Object.wait(Native Method), java.lang.Object.wait(Unknown Source), java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)], WAITING Head Conversion Thread - 0: [sun.misc.Unsafe.park(Native Method), java.util.concurrent.locks.LockSupport.park(Unknown Source), java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source), java.util.concurrent.LinkedBlockingQueue.take(Unknown Source), java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source), java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source), java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source), java.lang.Thread.run(Unknown Source)], TIMED_WAITING Snooper Timer: [java.lang.Object.wait(Native Method), java.util.TimerThread.mainLoop(Unknown Source), java.util.TimerThread.run(Unknown Source)], RUNNABLE Attach Listener: [], RUNNABLE DestroyJavaVM: [], WAITING Head Conversion Thread - 1: [sun.misc.Unsafe.park(Native Method), java.util.concurrent.locks.LockSupport.park(Unknown Source), java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source), java.util.concurrent.LinkedBlockingQueue.take(Unknown Source), java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source), java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source), java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source), java.lang.Thread.run(Unknown Source)], RUNNABLE Server thread: [java.lang.Thread.dumpThreads(Native Method), java.lang.Thread.getAllStackTraces(Unknown Source), org.bukkit.craftbukkit.v1_8_R2.CraftCrashReport.call(CraftCrashReport.java:29), net.minecraft.server.v1_8_R2.CrashReportSystemDetails.a(SourceFile:78), net.minecraft.server.v1_8_R2.CrashReport.h(CrashReport.java:136), net.minecraft.server.v1_8_R2.CrashReport.<init>(CrashReport.java:34), net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:574), java.lang.Thread.run(Unknown Source)], WAITING NonBlockingInputStreamThread: [java.lang.Object.wait(Native Method), org.bukkit.craftbukkit.libs.jline.internal.NonBlockingInputStream.run(NonBlockingInputStream.java:278), java.lang.Thread.run(Unknown Source)], WAITING Head Conversion Thread - 2: [sun.misc.Unsafe.park(Native Method), java.util.concurrent.locks.LockSupport.park(Unknown Source), java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source), java.util.concurrent.LinkedBlockingQueue.take(Unknown Source), java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source), java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source), java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source), java.lang.Thread.run(Unknown Source)], RUNNABLE Netty Server IO #0: [sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method), sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source), sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source), sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source), sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source), sun.nio.ch.SelectorImpl.select(Unknown Source), io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622), io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310), io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116), java.lang.Thread.run(Unknown Source)], RUNNABLE Server console handler: [org.fusesource.jansi.internal.Kernel32.ReadConsoleInputW(Native Method), org.fusesource.jansi.internal.Kernel32.readConsoleInputHelper(Kernel32.java:761), org.fusesource.jansi.internal.Kernel32.readConsoleKeyInput(Kernel32.java:794), org.fusesource.jansi.internal.WindowsSupport.readConsoleInput(WindowsSupport.java:97), org.bukkit.craftbukkit.libs.jline.WindowsTerminal.readConsoleInput(WindowsTerminal.java:215), org.bukkit.craftbukkit.libs.jline.WindowsTerminal.access$000(WindowsTerminal.java:55), org.bukkit.craftbukkit.libs.jline.WindowsTerminal$1.read(WindowsTerminal.java:157), org.bukkit.craftbukkit.libs.jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:169), org.bukkit.craftbukkit.libs.jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:137), org.bukkit.craftbukkit.libs.jline.internal.NonBlockingInputStream.read(NonBlockingInputStream.java:246), org.bukkit.craftbukkit.libs.jline.internal.InputStreamReader.read(InputStreamReader.java:261), org.bukkit.craftbukkit.libs.jline.internal.InputStreamReader.read(InputStreamReader.java:198), org.bukkit.craftbukkit.libs.jline.console.ConsoleReader.readCharacter(ConsoleReader.java:2145), org.bukkit.craftbukkit.libs.jline.console.ConsoleReader.readLine(ConsoleReader.java:2349), net.minecraft.server.v1_8_R2.DedicatedServer$2.run(DedicatedServer.java:79)],}
    Recent tasks from 335-365{}
    Profiler Position: N/A (disabled)
    Player Count: 1 / 20; [EntityPlayer['Zyphicx'/95, l='world', x=1229.61, y=72.09, z=-1250.40](Zyphicx at 1229.605779897389,72.09352603718777,-1250.3995467782445)]
    Is Modded: Definitely; Server brand changed to 'Spigot'
    Type: Dedicated Server (map_server.txt)


    Code (open)

    Code:
    package creatures;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R2.CraftServer;
    import org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer;
    import com.mojang.authlib.GameProfile;
    
    import net.minecraft.server.v1_8_R2.EntityPlayer;
    import net.minecraft.server.v1_8_R2.MinecraftServer;
    import net.minecraft.server.v1_8_R2.PlayerInteractManager;
    import net.minecraft.server.v1_8_R2.WorldServer;
    
    public class NPCCreature extends EntityPlayer{
    
        MinecraftServer server;
        WorldServer world;
        GameProfile gameProfile;
        PlayerInteractManager playerInteractManager;
    
        CraftPlayer bukkitEntity;
    
    
        public NPCCreature(MinecraftServer minecraftServer,
                WorldServer worldServer, GameProfile gameProfile,
                PlayerInteractManager playerInteractManager) {
            super(minecraftServer, worldServer, gameProfile, playerInteractManager);
        
            this.server = minecraftServer;
            this.world = worldServer;
            this.gameProfile = gameProfile;
            this.playerInteractManager = playerInteractManager;
        
            world.addEntity(this);
            world.players.remove(this);
        
            bukkitEntity = new CraftPlayer((CraftServer) Bukkit.getServer(), this);
        }
    
    }
    
    
    
    And to spawn it:

    Code:
                        MinecraftServer server = ((CraftServer)Bukkit.getServer()).getServer();
                        WorldServer world = ((CraftWorld)Bukkit.getServer().getWorlds().get(0)).getHandle();
                        new NPCCreature(server, world, new GameProfile(UUID.fromString("[UUID Here]"), "CloneArmyRecruit"), new PlayerInteractManager(world));


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jan 5, 2016
  8. Offline

    AztroCreationz

    Bumpedy bump
     
  9. Offline

    Zombie_Striker

    "[UUID Here]","CloneArmyRecruit"
    Can you post the UUID you used? Are you sure the name/uuid is for the same player?

    BTW:
    Bukkit.getServer().getWorlds().get(0)
    You should be able to replace this line with something like
    Bukkit.getServer().getDefaultWorld()
     
  10. Offline

    AztroCreationz

    @Zombie_Striker
    The UUID: c41b168c-fd60-4988-afff-71595375d515

    And no, I found no method called "getDefaultWorld".

    Okay, so let me explain my current situation a bit more in-depth. I am creating an RPG-server with my friend (the one with the UUID). I am trying to make EntityPlayer NPCs so that I can use them for quests. So, I want them to be right-clickable and I also want to be able to make it seem like they are walking. One way of doing this is by using packets to spawn them and make them move. But, as I said I want them right-clickable. The only way possible(as far as I am concerned) is by detecting the EntityUse packet. But, I don't know how to listen for it. I tried googling and didn't find much.
     
  11. Offline

    Zombie_Striker

    Actually, if you want them to be "clickable", then they need to actually be entities created in the world. What you would need to do is create a custom class that extends EntityPlayer, look around the source code and try to override the moving/looking methods, and then spawn the player in the world

    Look at this link on how to spawn custom entities:
    https://bukkit.org/threads/tutorial-custom-entities-meteor.93899/
     
  12. Offline

    AztroCreationz

    I'll try that, but meanwhile I would suggest looking at this. :)
     
  13. Offline

    AztroCreationz

  14. Offline

    AztroCreationz

    Bumpedy bump
     
  15. @AztroCreationz
    you will only see your skin because it's the only one loaded, you should retrieve the skin from minecraft webpage and load them manually, there's an api for that, just google it
     
  16. Offline

    AztroCreationz

    This is the last bump (Also, my main problem is not the skin, it is just being able to interact with a packet entityplayer.)!
     
    Last edited: Jan 29, 2016
  17. Offline

    Zombie_Striker

    @AztroCreationz
    You don't really have control about testing if a player interacts with an entity unless you check the ID of the "entity" the player interacted with. Whenever you create a entity via packets, you have to provide the "Id" of the entity. If you know that entity with id of something like "9001" will always be your entity (because you sent out the packet), then you can test when a packet comes in with an entity interacting with entity "9001".
     
  18. Offline

    AztroCreationz

    Yes, but how would I go about listening for that packet?
     
  19. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page