Solved Custom NPC dissapers

Discussion in 'Plugin Development' started by ffghts, Aug 15, 2017.

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

    ffghts

    Hey,
    i'm relatively new in minecraft and bukkit. I tried to make a custom trader npc, which i can spawn through a command. After spawn the npc get saved in a config.yml. The npc spawns and i also can trade with him. But if i'm far away the NPC dissapers. If i reload the plugin, he spawns again through the config. I do not know, what i'm doing wrong.

    This is my npc class:
    NPC Class (open)

    PHP:
    public class NPC extends EntityVillager {

        public 
    NPC(World worldLocation locationString name) {
            
    super(world);
            
    setLocation(location.getBlockX(), location.getBlockY(), location.getBlockZ(), location.getYaw(), location.getPitch());
            
    setCustomName(name);
            
    setCustomNameVisible(true);
        }


        @
    Override
        
    public void makeSound(String sfloat ffloat f1) {
            
    super.makeSound(""00);
        }

        @
    Override
        
    public void move(double d0double d1double d2) {
            
    super.move(000);
        }

        @
    Override
        
    public boolean damageEntity(DamageSource damagesourcefloat f) {
            return 
    false;
        }

        @
    Override
        
    public void collide(Entity entity) {

            if (
    entity instanceof EntityPlayer) {
                return;
            }
            
    super.collide(entity);
        }
    }


    This is where i register the npc:
    Register (open)

    PHP:
    @SuppressWarnings("unchecked")
        public static void registerEntities(){

                Field c = EntityTypes.class.getDeclaredField("c");
                Field f = EntityTypes.class.getDeclaredField("f");

                c.setAccessible(true);
                f.setAccessible(true);

                ((Map<Class<? extends EntityInsentient>, String>) c.get(null)).put(NPC.class, "Villager");
                ((
    Map<Class<? extends EntityInsentient>, Integer>) f.get(null)).put(NPC.class, 120);
    }


    And this is where is spawn the npc:
    Spawn npc (open)

    PHP:
     public void spawnNpcCommand(final String name, final String type, final Location location, final Player player) {

                    
    World world = ((CraftWorldBukkit.getWorld("world")).getHandle();
                    
    VolkNPC npc = new VolkNPC(worldlocationname);
                    
    world.addEntity(npc);
        }


    I use bukkit 1.7.10.
    Hope someone can explain my issue.
     
  2. Offline

    Zombie_Striker

  3. @ffghts
    Is there a specific reason you have to create a custom NMS entity? You should really avoid it at all costs and use the built-in events instead.
     
  4. Offline

    ffghts

    @Zombie_Striker
    The oudated version was not my decision. Our builders used the conquest texture pack v.11.1 i think. Because of the meta data. They said the newer version of it doesn't support this meta data. But i don't know it.
    Maybe a dumb question, but how can i check this?

    @AlvinB
    I just googled how to create custom npc's and there were a lot of posts where the people used the custom npc entity. So i thought it would be a good way.
    If there is a better way to create trader npc's which are not pushable and just stand around. I would be happy to use this way.
     
  5. @ffghts
    Well, the above uses NMS (net.minecraft.server), which is version dependent, has obfuscated fields, changes a lot and is a general nightmare to work with. What you should be doing is using the Bukkit API methods (IE in the org.bukkit classes).

    For your problem, I believe a simple call to Villager#setAI(false). It both prevents the villager from moving on its own and prevents pushing.
     
  6. Offline

    ffghts

    @AlvinB
    After quick reading i think it should look like this?:
    PHP:
    LivingEntity villager Bukkit.getWorld("world").spawn(player.getLocation(), Villager.class);
            
    villager.setAI(false);
    But setAI() is not available in version 1.7.10. And i don't find a method with the same function. That's annoying. I think the best solution would be to update the server version? Or do you have another idea?
     
  7. Update the server.
     
  8. Offline

    ffghts

    Okay, I'll do it. Thanks to all.
     
Thread Status:
Not open for further replies.

Share This Page