Config file trouble !

Discussion in 'Plugin Development' started by Jacek, Sep 7, 2011.

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

    Jacek

    I'm sure there will be an easy way to do this but I can't work it out...

    If I have a config file like this

    Code:
            mobs:
            - ZOMBIE
            - SKELETON
            - SPIDER
            - CREEPER
    
    How can I check to see if a Creature is on that list ?
     
  2. Use bukkits build-in configuration methods (yml) or bml made by @codename_B
     
  3. Offline

    Jacek

    Yes I did that. But how do I get from a Creature object to a string like ZOMBIE ?
     
  4. Well, check what getString(); returns. If it's not helpfull I think you'll have to if/else for every mob type, like this:
    Code:
    String mob;
    if(creature instanceof Creeper)
      mob = "CREEPER";
    else if(creature instanceof Zombie)
      mob = "ZOMBIE";
    ...
     
  5. Offline

    Jacek

    Yeah that is basically how I did it

    Code:
        public boolean isCreatureOnMobList(String key, Creature entity){
            List<String> list = this.getStringList(key);
    
            if (entity instanceof Zombie){
                return list.contains("ZOMBIE");
            }
    
            if (entity instanceof Skeleton){
                return list.contains("SKELETON");
            }
    
            if (entity instanceof Creeper){
                return list.contains("CREEPER");
            }
    
            if (entity instanceof Spider){
                return list.contains("SPIDER");
            }
    
            return false;
        }
    I was hoping there was a more elegant way ;)

    I will look into using toString() I never really thought of that so hope it can work D
     
  6. Offline

    Jogy34

    Try using else and else if's like:
    Code:
    public boolean isCreatureOnMobList(String key, Creature entity){
    List<String> list = this.getStringList(key);
     if (entity instanceof Zombie){
     return list.contains("ZOMBIE");
     }
    else if (entity instanceof Skeleton){
      return list.contains("SKELETON");
     }
    else if (entity instanceof Creeper){
     return list.contains("CREEPER");
     }
     else if (entity instanceof Spider){
      return list.contains("SPIDER");
      }else{
     return false;
    }
    }
    
     
  7. Offline

    Jacek

    That's the same as what I have now, just a little messier.

    Update, it seems that toString() will return the name of the class for example CraftZombie for a Zombie, based on there here is the updated version of the previous method I posted

    Code:
        public boolean isCreatureOnMobList(String key, Creature entity){
            List<String> list = this.getStringList(key);
    
            String mobName = entity.toString().toUpperCase();
    
            if (mobName.startsWith("CRAFT")){
                mobName = mobName.substring(5);
            }
    
            return list.contains(mobName);
        }
    I'm not sure if the check for CRAFT being there is necessary, but I doubt it will hurt much.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  8. The if/else thing would uae less resources, I think (nit sure atm)... ;)
     
  9. Offline

    Jogy34

    what you have now is
    Code:
    if something return mob... return false
    
    what you should have is
    Code:
    if something return mob
    else return false
    
    right now you are always returning false no matter what
     
  10. @Jogy34: You're codes are exactly the same, yours is only uglier formatted. So show us the differences or stop trolling. :p
     
  11. Offline

    Daniel Heppner

    I suggest using a switch statement. Not sure about how to use them, I need to reread that part of my book.
     
  12. Offline

    Jacek

    No, because the return statement in each condition will prevent the following checks being done.

    No, I don't return mob, I return is mob on list and because I return something the return false will only happen if none of the checks are met (like if it is a chicken).

    Why ?

    Has anyone spotted that I solved this problem ? Stop giving useless advice !
     
  13. Offline

    Daniel Heppner

    I'm only trying to help, and it's not useless advice. If you're going to be mean to people just because you've already solved it, you don't deserve the help.
     
  14. Offline

    Jacek

    I wasn't trying to be mean, sorry if you took what I said that way :(
     
  15. Offline

    Daniel Heppner

    Anyway, sorry for the misinterpretation.
     
Thread Status:
Not open for further replies.

Share This Page