Mob not Despawning

Discussion in 'Plugin Development' started by IcyFlameX, Mar 31, 2017.

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

    IcyFlameX

    I have been making a plugin basically a wanted plugin for gta. This is how it works, whenever a player kills another player pigman spawns in the location of the player. But if the pigman kills the player then i want them to despawn. I wrote the code of despawing but for some reason when i play ingame to test it, it doesnt despawn.
    I cant find out the problem.
    Whole Code : https://pastebin.com/5rmDyzd8
    Despawn Code:
    Code:
    if(killer instanceof Monster && deader instanceof Player){      
                    List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
                    player.sendMessage("There are " + enemieslength + "enemies");
                    enemieslength = enemies.size();
                    for (int x = 0; x < enemieslength; x++)
                   {
                           Entity e1 = enemies.get(x);
                           if(e1 instanceof LivingEntity){
                           ((LivingEntity)e1).setHealth(0);
                           }
                }
     
  2. Offline

    timtower Moderator Moderator

    @IcyFlameX Could you fix the formatting in the pastebin?
    My guess would be that it is in the block for player vs player.
     
  3. Offline

    IcyFlameX

    Code:
    package me.bukkit.IcyFlameX;
    import java.util.List;
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Creature;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.IronGolem;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.PigZombie;
    import org.bukkit.entity.Player;
    import org.bukkit.World;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class KillClass implements Listener {
        Wanted plugin;
        FileConfiguration config;
        int enemieslength;
        public KillClass(Wanted plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
            config =plugin.getConfig();
        }
       
        @SuppressWarnings({ "deprecation", "null" })
        @EventHandler
        public void WantedLvL(PlayerDeathEvent e) {
           
            //Player getting killed
            Entity deader = e.getEntity();
            //Player killing
            Entity killer = e.getEntity().getKiller();
           
            if(killer instanceof Player && deader instanceof Player){
                Player player = (Player) killer;
               //Increasing the Kill amount
                int killcount=0;
                String path = "Kills." + player.getName();
                if(config.contains(path)){
                    killcount= config.getInt(path);
                    }
                config.set(path, killcount+1);
               //Increase Wanted Level
                int wantlvls =0;
                String wantpath = "WantLvL." + player.getName();
                if(config.getInt("Kills."+ player.getName())== config.getInt("KW")){
                if(config.contains(wantpath)){
                        wantlvls=config.getInt(wantpath);
                }
                        config.set(wantpath,1);
                if(config.getInt("WantLvL."+ player.getName())==1){
                    Location l = player.getLocation();//Player Location
                    for(int i=0;i<config.getInt("Zombies");i++){
                        PigZombie pz = (PigZombie) l.getWorld().spawnEntity(l, EntityType.PIG_ZOMBIE);
                        pz.getEquipment().setItemInHand(new ItemStack(Material.IRON_SWORD, 1));//SpawnZombie 
                        pz.setAngry(true);   
               
                }
                }
                plugin.saveConfig();
            } //kill mob after player gets killed
                if(killer instanceof Monster && deader instanceof Player){       
                     List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
                     player.sendMessage("There are " + enemieslength + "enemies");
                     enemieslength = enemies.size();
                     for (int x = 0; x < enemieslength; x++)
                    {
                            Entity e1 = enemies.get(x);
                            if(e1 instanceof LivingEntity){
                            ((LivingEntity)e1).setHealth(0);
                            }
                }
       
        }
       
    }
    }
    }
     
  4. Offline

    timtower Moderator Moderator

    @IcyFlameX My point is that you have } on the same indentation, that should not be the case without other code in between, check the last 3 lines.
    Hit ctrl+shift+f
     
  5. Offline

    IcyFlameX

    When i replace killer instanceof Monster with killer instanceof Player then when i kill the other player the mobs despawn within 20 blocks but its not happening when i put monster there. As for the last 3 } i made that, if(Checking if killer s monster) statement a nested one, inside the main if which is checking if both are playera
     
  6. Offline

    timtower Moderator Moderator

    @IcyFlameX You can't nest those 2 statements.
    Those will be called on different times.
     
  7. Offline

    IcyFlameX

    Nope still its not solved, the mobs only despawn when there are no players nearby i.e unloaded chunk
    Code:
    package me.bukkit.IcyFlameX;
    import java.util.List;
    
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Creature;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.IronGolem;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.PigZombie;
    import org.bukkit.entity.Player;
    import org.bukkit.World;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class KillClass implements Listener {
        Wanted plugin;
        FileConfiguration config;
        int enemieslength;
        public KillClass(Wanted plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
            config =plugin.getConfig();
        }
       
        @SuppressWarnings({ "deprecation", "null" })
        @EventHandler
        public void WantedLvL(PlayerDeathEvent e) {
           
            //Player getting killed
            Entity deader = e.getEntity();
            //Player killing
            Entity killer = e.getEntity().getKiller();
           
            if(killer instanceof Player && deader instanceof Player){
                Player player = (Player) killer;
               
                int killcount=0;
                String path = "Kills." + player.getName();
                if(config.contains(path)){
                    killcount= config.getInt(path);
                    }
                config.set(path, killcount+1);
               
                int wantlvls =0;
                String wantpath = "WantLvL." + player.getName();
                if(config.getInt("Kills."+ player.getName())== config.getInt("KW")){
                if(config.contains(wantpath)){
                        wantlvls=config.getInt(wantpath);
                }
                        config.set(wantpath,1);
                if(config.getInt("WantLvL."+ player.getName())==1){
                    Location l = player.getLocation();
                    for(int i=0;i<config.getInt("Zombies");i++){
                        PigZombie pz = (PigZombie) l.getWorld().spawnEntity(l, EntityType.PIG_ZOMBIE);
                        pz.getEquipment().setItemInHand(new ItemStack(Material.IRON_SWORD, 1));
                        pz.setAngry(true);   
               
                }
                }
                plugin.saveConfig();
            }
            }// end of player kill,wanted and zombie spawn method       
                if(killer instanceof Monster && deader instanceof Player){       
                     List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
                     enemieslength = enemies.size();
                     for (int x = 0; x < enemieslength; x++)
                    {
                            Entity e1 = enemies.get(x);
                            if(e1 instanceof LivingEntity){
                            ((LivingEntity)e1).setHealth(0);
                            }
                }
       
        }//end of mob despawn
       
    
    }//end of PlayerDeathEvent
    }// end of Class
    
     
  8. Offline

    timtower Moderator Moderator

    @IcyFlameX You are killing everything near the killer, but why not kill the killer itself?
     
  9. Offline

    IcyFlameX

    @timtower
    List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
    here the killer is the monster as specified by if(killer instanceof monster)
    so if the player gets killed by that mob then List takes all the nearby entites within 20 blocks and sets their life to 0
    So it should kill the other pig zombies which gets spawned due to
    for(int i=0;i<config.getInt("Zombies");i++){
    PigZombie pz = (PigZombie) l.getWorld().spawnEntity(l, EntityType.PIG_ZOMBIE);
    pz.getEquipment().setItemInHand(new ItemStack(Material.IRON_SWORD, 1));
    pz.setAngry(true);
    But other zombies dont get killed.
    Sorry for bad English.
     
  10. Offline

    timtower Moderator Moderator

    @IcyFlameX How many entities does your code find, and how many are there?
     
  11. Offline

    IcyFlameX

    @timtower in config.yml i have set the vaule to 3 for now , so whenever i killed a player , the 3 zombie pigman will spawn. To find any living entities near the killer (mob) i m doing
    List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
     
  12. Offline

    timtower Moderator Moderator

    @IcyFlameX I know that.
    But how many entities are in that list?
     
  13. Offline

    IcyFlameX

    @timtower What do you mean ? The list is storing the desired amount of entities found
     
  14. Offline

    timtower Moderator Moderator

    @IcyFlameX Then what are the types of those entities?
     
  15. Offline

    IcyFlameX

  16. Offline

    timtower Moderator Moderator

    @IcyFlameX Loop though the list.
    Print the entity types.
    Confirm that they are livingentities
     
  17. Offline

    IcyFlameX

    Shows nothing. I have no idea right now :(
    I even tried
    if(killer instanceof Monster && deader instanceof Player){
    Player player= (Player) deader;
    for(Entity en : player.getLocaiton.player.getChunk().getEntities()){
    if(!(en instanceof Player)) {
    en.remove();
    }
    Still nothing happens
     
    Last edited by a moderator: Mar 31, 2017
  18. Offline

    timtower Moderator Moderator

    @IcyFlameX Then does the list even contain any entities?
    List<Entity> enemies = killer.getNearbyEntities(20, 20, 20);
    That list then.
     
  19. Offline

    IcyFlameX

    It should store the entities, since i m getting all the nearby entities ? if i am not wrong
     
  20. Offline

    timtower Moderator Moderator

    @IcyFlameX Print the length, I want to see a console log this time.
     
  21. @IcyFlameX
    Why don't you stuff the UUID of the three pigmen into a hashset, then put that in a bigger arraylist, then if a player is killed by a monster, see if any of the hashsets contain the UUID of the killer. If so, then remove all the entities in the same hashset. If any of the monsters are killed, then remove them from the hashsets too. That way, you can keep track of them when you spawn them up until the time when they die or are removed.
     
Thread Status:
Not open for further replies.

Share This Page