Villager removing

Discussion in 'Plugin Development' started by Speaw, Sep 26, 2016.

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

    Speaw

    hey, I'm spawning the villager with that codes but it's despawning when chunk unloaded.

    Code:
    public class VillagerManager {
          
        public static Field GOAL_FIELD;
        public static Map<Class<? extends Entity>, Integer> ENTITY_CLASS_TO_INT;
        public static Map<Integer, Class<? extends Entity>> ENTITY_INT_TO_CLASS;
    
        public static void spawnVillager(Location loc) {
            World world = ((CraftWorld) loc.getWorld()).getHandle();
            Entity entity = new SoVillager(world,loc);
            Villager villager = (Villager)entity.getBukkitEntity();
            villager.setRemoveWhenFarAway(false);
            entity.setPosition(loc.getX(), loc.getY(), loc.getZ());
            world.addEntity(entity, SpawnReason.CUSTOM);
            Bukkit.broadcastMessage("test: " + loc.getWorld().getName());
        }
        public MinecraftServer getServer(){
            return ((CraftServer)Bukkit.getServer()).getHandle().getServer();
        }
      
        public static Field getField(Class<?> clazz, String field) {
            Field f = null;
            try {
                f = clazz.getDeclaredField(field);
                f.setAccessible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return f;
        }
      
        @SuppressWarnings("unchecked")
        public static void registerEntityClass(Class<? extends Entity> clazz) {
            GOAL_FIELD = getField(PathfinderGoalSelector.class, "b");
            try {
                Field field = getField(EntityTypes.class, "e");
                ENTITY_INT_TO_CLASS = (Map<Integer, Class<? extends Entity>>) field.get(null);
                field = getField(EntityTypes.class, "f");
                ENTITY_CLASS_TO_INT = (Map<Class<? extends Entity>, Integer>) field.get(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
          
            if (ENTITY_CLASS_TO_INT.containsKey(clazz))
                return;
    
            Class<?> search = clazz;
            while ((search = search.getSuperclass()) != null && Entity.class.isAssignableFrom(search)) {
                if (!ENTITY_CLASS_TO_INT.containsKey(search)) {
                    continue;
                }
                int code = ENTITY_CLASS_TO_INT.get(search);
                ENTITY_INT_TO_CLASS.put(code, clazz);
                ENTITY_CLASS_TO_INT.put(clazz, code);
                return;
            }
            throw new IllegalArgumentException("unable to find valid entity superclass");
        }
      
        public Field getGoalField(){
            return GOAL_FIELD;
        }
    Code:
    public class SoVillager extends EntityVillager {
      
        public SoVillager(World world, Location loc) {
            super(world);
            VillagerManager.registerEntityClass(SoVillager.class);
            clearGoals(this.goalSelector, this.targetSelector);
          
            this.goalSelector.a(1, new PathfinderGoalRandomLookaround(this));
        }
      
        private void clearGoals(PathfinderGoalSelector... goalSelectors) {
            if (VillagerManager.GOAL_FIELD == null || goalSelectors == null) return;
            for (PathfinderGoalSelector selector : goalSelectors) {
                try {
                    List<?> list = (List<?>) VillagerManager.GOAL_FIELD.get(selector);
                    list.clear();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
      
        protected String z() {//Idle Sound
            return null;
        }
    
        protected String bo() {//Hurt Sound
            return null;
        }
      
        protected String bp() {//Death sound
            return null;
        }
      
        @Override
        protected void a(BlockPosition blockposition, Block block) {//No Move Sound
            return;
        }
      
        @Override
        public void move(double d0, double d1, double d2) {
            return;
        }
      
        @Override
        public void g(double x, double y, double z) {
            return;
        }
    
        @Override
        public boolean isInvulnerable(DamageSource d) {
            return true;
        }
      
        @Override
        public float c(float f) {
            return 0;
        }
      
    }
    
     
  2. Offline

    HeartandSoul

    What's the class SoVillager?

    Postttt
     
Thread Status:
Not open for further replies.

Share This Page