Register custom entity

Discussion in 'Spigot Plugin Development' started by Mats9799, Apr 22, 2017.

  1. Hello Spigot,

    I am trying to register my custom entity so it isn't nonexistent but so far the methods I have found on the web haven't worked so far. The all I'm getting are the invisible entities which don't make sound or anything, but can be teleported to. The version I'm using is 1.11.R1. Thanks for help in advance.

    My current register method:
    Code (Text):
    public void spawn(Location location) {
            setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());

            world.addEntity(this);

            EntityTypes.b.a((int) entityType.getTypeId(), new MinecraftKey(getHealthBar()), getClass());

            targetPlayer();
        }
     
    #1 Mats9799, Apr 22, 2017
    Last edited: Apr 23, 2017
  2. bump

    Desperately looking for a solution
     
  3. Are you registering it every single time you spawn it?
     
  4. This is how I spawn/register it:
    Code (Text):

    Monster monster = MonsterFactory.getNewMonster(EntityType.ZOMBIE); //Returns the custom entity, but as my Monster interface which it implements
    monster.setAttackDamage(10.0);
    monster.spawn(spawns[0]); //It should register right here

    plugin.getServer().getOnlinePlayers().get(0).teleport(monster.getEntity()); //Teleports you to the correct location, but the entity isn't present
     
    I am calling the registering every time I spawn the entity but it's still invisible. Not stack traces in the console either.
     
    #4 Mats9799, Apr 23, 2017
    Last edited: Apr 23, 2017
  5. You should only register it on the plugin startup. Not sure if this is the cause of your problems, though.

    Now, though, make sure to call the prepare(DifficultyDamageScaler, GroupDataEntity) method before spawning.
    You can acquire those arguments by doing this:
    Code (Java):
    prepare(world.D(new BlockPosition(this)), null);
    Now, world.D(BlockPosition) returns the DifficultyDamageScaler at your entity's location (that's why the new BlockPosition(Entity)). The last argument is nullable, and in most case (like this) you can just throw the null there and let the super methods do the rest.

    EDIT: Call 'prepare' after setting the location of the entity! Otherwise it'll spawn at 0, 0, 0, or even worse; won't spawn because the chunks aren't loaded.

    EDIT 2: Pff well, I'll write a resource post about how I do this. Good luck though, I'll gladly help if this doesn't help either.
     
    • Like Like x 1
  6. I have never EVER had to do that for a custom entity - i'd use enums to register the entities as they are initialised correctly on startup. Secondly, what you're looking for the .setLocation method before you spawn it.

    The correct flow is:

    (startup) register entity

    (onspawn)
    new Instance
    nmsEntity.setLocation
    nmsWorld.addEntity

    Looking at the above code, the only error I can see which is preventing the entity from being visible is registering the entity after spawning it - Move that into an enum is my suggestion, there's a few guides dotted around these forums.
     
  7. Ok, I moved the register to the onEnable but I still get the same results.

    Code (Text):
    public void onEnable() {
            MonsterFactory.registerEntities();
        }
    Code (Text):
    public static void registerEntities() {
            if (version.equals("v1_11_R1")) {
                EntityTypes.b.a((int) EntityType.ZOMBIE.getTypeId(), new MinecraftKey("Zombieasdf"), Zombie1_11_R1.class);
            }
        }
    Code (Text):
    public void spawn(Location location) {
            setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
            prepare(world.D(new BlockPosition(this)), null);
            world.addEntity(this);
            targetPlayer();
        }
     
  8. Could you show us your entire entity class?
     
  9. Do you spawn them manually, or let Spigot spawn them?
     
  10. Manually
     
  11. Strange, what version of the game?
    When I tried to spawn the Entity in with nms.world.addEntity(Entity, SpawnReason) it wouldn't show up before prepare() was called.
    Maybe you spawned it with the Bukkit API somehow, because it calls prepare()?

    EDIT: This is going a bit offtopic, by the way.
     
  12. I know what I'm doing. It does not use Bukkit API.

    Also, in every version I've used from 1.9 to the current latest build,
     
  13. OH, maybe you can even use CustomEntitiesAPI!!!
     
  14. I just found something: I only did world.addEntity(this); when spawning the entity but didn't specify the SpawnReason. However when I do I get this error code:
    Code (Text):
    [18:56:29 WARN]: [Zombies] Task #23 for Zombies v1.01 generated an exception
    java.lang.ClassCastException: java.util.LinkedHashSet cannot be cast to java.util.List
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1.resetFields(Zombie1_11_R1.java:115) ~[?:?]
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1.access$100(Zombie1_11_R1.java:26) ~[?:?]
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1$1.run(Zombie1_11_R1.java:194) ~[?:?]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:738) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    [18:56:32 WARN]: [Zombies] Task #23 for Zombies v1.01 generated an exception
    java.lang.ClassCastException: java.util.LinkedHashSet cannot be cast to java.util.List
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1.resetFields(Zombie1_11_R1.java:115) ~[?:?]
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1.access$100(Zombie1_11_R1.java:26) ~[?:?]
            at com.matsg.zombies.entity.zombie.Zombie1_11_R1$1.run(Zombie1_11_R1.java:194) ~[?:?]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:738) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [Spigot-1.11.2.jar:git-Spigot-b4417b8-3c48711]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Code (Text):
    private void resetFields() {
            /*LINE 115*/ List goalB = (List) ReflectionUtils.getField(PathfinderGoalSelector.class, "b", goalSelector);
            List goalC = (List) ReflectionUtils.getField(PathfinderGoalSelector.class, "c", goalSelector);
            List targetB = (List) ReflectionUtils.getField(PathfinderGoalSelector.class, "b", targetSelector);
            List targetC = (List) ReflectionUtils.getField(PathfinderGoalSelector.class, "c", targetSelector);

            goalB.clear();
            goalC.clear();
            targetB.clear();
            targetC.clear();

            goalSelector.a(0, new PathfinderGoalFloat(this));
            goalSelector.a(2, new PathfinderGoalMeleeAttack(this, 1.0, false));
            goalSelector.a(3, new PathfinderGoalMeleeAttack(this, 1.0, false));
            goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 3.0));
            goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 1.0, false));
            goalSelector.a(7, new PathfinderGoalRandomStroll(this, 1.0));
            goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
            goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
            targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
            targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 50, true, true, null));
        }
    EDIT: Changed the lists to sets. Removed the error but didn't fix the issue
     
  15. Just created a resource about registering / creating custom entities, you could take a look. Designed for 1.11.2, which you seem to be using already.