Checking if entities are in certain world

Discussion in 'Spigot Plugin Development' started by Invaerne, Apr 30, 2017.

Thread Status:
Not open for further replies.
  1. Hi there,

    I'm having a small issue with the code.
    The purpose of the code is to count all the dead entities in specific world "TestWorld".

    The problem is that the code completely ignores the name of the world and counts the beings regardless of the worlds in which they are being killed.

    Any ideas?


    Code (Text):
      public void killZombie(EntityDeathEvent e)
      {
        Entity deadEntity = e.getEntity();
        Entity killer = e.getEntity().getKiller();
           
            if(killer.getLocation().getWorld().getName().equalsIgnoreCase("TestWorld"))
            {
            if(deadEntity.getLocation().getWorld().getName().equalsIgnoreCase("TestWorld"))
            {
            if (((killer instanceof Player)) && ((deadEntity instanceof Zombie)))
            {

                int killCount = this.configGetter.getConfig().getInt("ZombieKills");

                this.configGetter.getConfig().set("ZombieKills", Integer.valueOf(killCount + 1));

                saveConfig();
               
            }
           
            }
           
            }
      }
     
  2. WAS

    WAS

    Are you sure you don't have code duplicated elsewhere that does this without checking the world? Additionally you don't need to use the Integer class on (killCount + 1)

    I'm also a bit confused by your "configGetter" which doesn't actually get the config you get than get it after?
     
  3. Just chedked, I don't have the code duplicated.

    Can anyone think of an alternative ways of checking the world?
     
  4. How about this
    Code (Text):
    public void killZombie(EntityDeathEvent e, World world)
    It's hard to understand you're code, I'll make a better version
    Code (Text):
    for(Entity e : Bukkit.getWorld(p.getName).getEntitiesByClass(Zombie.class, Skeleton.class) {
    config blah blah
    e.remove()
    That's kinda pseudo code, just mess around with it
     
  5. Still can't get it to work :(

    Code (Text):
      public void killZombie(EntityDeathEvent e, World w)
      {
       
            Entity deadEntity = e.getEntity();
            Entity killer = e.getEntity().getKiller();
            Player p = (Player)e.getEntity().getKiller();
       
          for(Entity f : Bukkit.getWorld(p.getName()).getEntitiesByClass(Zombie.class)) {
           
            if(killer.getLocation().getWorld().getName().equalsIgnoreCase("TestWorld"))
            {

            if (((killer instanceof Player)) && ((deadEntity instanceof Zombie)))
            {
                int killCount = this.configGetter.getConfig().getInt("ZombieKills");

                this.configGetter.getConfig().set("ZombieKills", Integer.valueOf(killCount + 1));

                saveConfig();
             
            }
         
            }
         
          }


      }

    Update:
    After many hours I finally got it to work. The code which I've posted originally is correct.
     
    #5 Invaerne, May 2, 2017
    Last edited: May 2, 2017
Thread Status:
Not open for further replies.