Custom Wither name updating problem

Discussion in 'Plugin Development' started by antesar, Feb 15, 2019.

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

    antesar

    Hey,
    I created custom wither which works fine as long as I am online.
    When i logout and login wither name stops updating.

    custom wither class
    Code:
    public class CustomWither extends EntityWither {
    
        public CustomWither(World world) {
            super(world);
    
            NBTtags(this.getBukkitEntity(), "NoAI", 1);
            Wither wither = (Wither) this.getBukkitEntity();
            wither.setMaxHealth(1000);
    
            this.setHealth(1000);
            this.updateName();
            this.setCustomNameVisible(true);
            this.persistent = true;
    
        }
    
        @Override
        public void makeSound(String s, float f, float f1) {
            if (s.equalsIgnoreCase("mob.wither.idle")) {
                return;
            }
            super.makeSound(s, f, f1);
        }
    
        @Override
        protected void dropDeathLoot(boolean flag, int i) {
            // super.dropDeathLoot(flag, i);
        }
    
        private void despawn() {
            World nmsworld = ((CraftWorld) getBukkitEntity().getWorld()).getHandle();
            nmsworld.removeEntity(this);
            this.getBukkitEntity().remove();
        }
    
        private void NBTtags(org.bukkit.entity.Entity entity, String NBTtag, int value) {
            Entity nmsEnt = ((CraftEntity) entity).getHandle();
            NBTTagCompound tag = nmsEnt.getNBTTag();
    
            if (tag == null) {
                tag = new NBTTagCompound();
            }
    
            nmsEnt.c(tag);
            tag.setInt(NBTtag, value);
            nmsEnt.f(tag);
        }
    
        public void regenerate() {
            if (this.getHealth() >= 1000) {
                this.r(0);
                return;
            }
            this.setHealth(this.getHealth() + 1);
            updateName();
    
        }
    
    
        private static final String NAME_FORMAT = ChatUtil.colour("&4&m----[&8&m----&2  {NOW}/{MAX}  &8&m----&4&m]&m----");
        public void updateName() {
            this.setCustomName(StringUtils.replaceEach(NAME_FORMAT,
                    new String[] { "{NOW}", "{MAX}" },
                    new String[] {String.valueOf((int)this.getHealth()), String.valueOf((int)this.getMaxHealth())}
            ));
            }
        }
    
    }
    updating name task
    Code:
        @Override
        public void run() {
            for (Guild guild : GuildManager.getGuilds()) {
                CustomWither wither = guild.getWither();
                if (wither == null) {
                    continue;
                }
                wither.regenerate();
            }
        }
    Tried a lot of things but i cant figure this out, can anyone help me?
     
  2. Offline

    MightyOne

    I am really not an expert in custom entities but not long ago I worked on an existing plugin with such. There the solution for this problem was to never unload chunks with custom entities inside and to remove them on every server shut down. Maybe this can help you.
     
  3. Offline

    antesar

    Im pretty sure it would work since its happening when chunks are unloaded but having ~ 400 chunks loaded doesn't seem like a good idea and im hoping there is other solution
     
  4. Offline

    MightyOne

    Well if you feel funny you can save all information about the withers and remove them when their chunkis unloading. On chunk loading you can spawn them again. Maybe.
     
Thread Status:
Not open for further replies.

Share This Page