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 ?
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"; ...
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
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; } }
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.
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
@Jogy34: You're codes are exactly the same, yours is only uglier formatted. So show us the differences or stop trolling.
I suggest using a switch statement. Not sure about how to use them, I need to reread that part of my book.
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 !
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.