kingbutton Posted August 30, 2017 Share Posted August 30, 2017 public enum State { LIGHT, COOK, BANK, WAIT; } public State getState() { RS2Object fire = objects.closest("Fire"); if (!inventory.contains("Tinderbox") // NO TINDERBOX? || inventory.onlyContains("Tinderbox") // ONLY TINDERBOX? || !inventory.contains("Filler")) { // YOU'RE DONE COOKING? return State.BANK; } else if (fire != null) { // HAHAAHAAAA WE DISCOVVEREEEDDD FIREEEE return State.COOK; } else { return State.LIGHT; // aww we have no fire :( BUT WEEEE SHALL MAKEEE ITT } return State.WAIT; } Quote Link to comment Share on other sites More sharing options...
Eliot Posted August 30, 2017 Share Posted August 30, 2017 return State.WAIT; Impossible to get there because your final else will always return first. 4 Quote Link to comment Share on other sites More sharing options...
Super Posted August 30, 2017 Share Posted August 30, 2017 } else if (fire.exists() && fire != null) { return State.COOK; } else if (!fire.exists() && fire == null) { return State.LIGHT; } else { return State.WAIT; } on top of what elliot already said, you should also check to see if the fire exists Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 30, 2017 Share Posted August 30, 2017 (edited) 1 hour ago, superuser said: } else if (fire.exists() && fire != null) { return State.COOK; } else if (!fire.exists() && fire == null) { return State.LIGHT; } else { return State.WAIT; } on top of what elliot already said, you should also check to see if the fire exists I laughed Thanks Edited August 30, 2017 by FrostBug 1 Quote Link to comment Share on other sites More sharing options...
Super Posted August 30, 2017 Share Posted August 30, 2017 (edited) 1 hour ago, FrostBug said: I laughed Thanks what did you laugh at? did i fuck something up? i posted from my phone but the code looks fine to me edit: you are very welcome though you fucking legend Edited August 30, 2017 by superuser Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 31, 2017 Share Posted August 31, 2017 8 hours ago, superuser said: what did you laugh at? did i fuck something up? i posted from my phone but the code looks fine to me edit: you are very welcome though you fucking legend Well, calling any method on a null reference will throw a NullPointerException; and exists() is a method. So the irony of doing something like !fire.exists() && fire == null can be appreciated, since it can never pass, and will throw errors whenever there's no fire. Quote Link to comment Share on other sites More sharing options...
Prolax Posted August 31, 2017 Share Posted August 31, 2017 9 hours ago, superuser said: what did you laugh at? did i fuck something up? i posted from my phone but the code looks fine to me edit: you are very welcome though you fucking legend Quote Link to comment Share on other sites More sharing options...
kingbutton Posted August 31, 2017 Author Share Posted August 31, 2017 12 hours ago, Prolax said: So I'm confused what I'm supposed to get out of this. public State getState() { RS2Object fire = objects.closest("Fire"); if (!inventory.contains("Tinderbox") // NO TINDERBOX? || inventory.onlyContains("Tinderbox") // ONLY TINDERBOX? || !inventory.contains("Filler")) { // YOU'RE DONE COOKING? return State.BANK; } else if (fire.exists() && fire != null) { // HAHAAHAAAA WE DISCOVVEREEEDDD FIREEEE return State.COOK; } else if (!fire.exists() && fire == null) { return State.LIGHT; } else { return State.WAIT; } } Did what you said, and it says Dead Code. Also I've never used exists() before. What's the difference in exists() and != null ? Quote Link to comment Share on other sites More sharing options...
dreameo Posted August 31, 2017 Share Posted August 31, 2017 Read the API and if it doesn't help you out, just have a separate file were you can test behavior of certain methods. Quote Link to comment Share on other sites More sharing options...
H0rn Posted August 31, 2017 Share Posted August 31, 2017 (edited) On 8/30/2017 at 9:06 PM, superuser said: } else if (fire.exists() && fire != null) { return State.COOK; } else if (!fire.exists() && fire == null) { return State.LIGHT; } else { return State.WAIT; } on top of what elliot already said, you should also check to see if the fire exists He doesnt need the fire.exists() the else if (fire!=null) should work fine As far as i am aware, the fire can never be null if it exists Edited August 31, 2017 by Ollie7xP Quote Link to comment Share on other sites More sharing options...
roguehippo Posted August 31, 2017 Share Posted August 31, 2017 you should check if the fire is null BEFORE if you check if it .exists() . so it would look like : if(fire != null && fire.exists()) Quote Link to comment Share on other sites More sharing options...
kingbutton Posted August 31, 2017 Author Share Posted August 31, 2017 1 hour ago, dreameo said: Read the API and if it doesn't help you out, just have a separate file were you can test behavior of certain methods. Um, of course I read the api. exists boolean exists() Checks whether this Entity still exists or not. Returns: Whether this Entity still exists or not. Still don't see how's that different from != null . Seems exactly the same to me? Plus that isn't really the main question. Main question is why is my code unreachable and how do I fix it? Because the response earlier is giving me a Dead Code. Quote Link to comment Share on other sites More sharing options...
FrostBug Posted August 31, 2017 Share Posted August 31, 2017 Stop being silly; all of you. You should under no circumstance use exists() in this scenario.. It's an expensive method, and the fire will always exist if it isn't null since he just retrieved the instance. Quote Link to comment Share on other sites More sharing options...
liverare Posted August 31, 2017 Share Posted August 31, 2017 (edited) Your question has been answered. But additionally, make fire a field variable. Otherwise, you're going to have to re-find the fire when you come to cooking on the fire. :edit: Be sure to set the fire variable in your getState function. Edited August 31, 2017 by liverare Quote Link to comment Share on other sites More sharing options...
kingbutton Posted August 31, 2017 Author Share Posted August 31, 2017 15 minutes ago, FrostBug said: Stop being silly; all of you. You should under no circumstance use exists() in this scenario.. It's an expensive method, and the fire will always exist if it isn't null since he just retrieved the instance. You seem to understand the situation, does this look good to you? I'm trying to use enums in my codes now to make them look cleaner. public State getState() { RS2Object fire = objects.closest("Fire"); if (!inventory.contains("Tinderbox") // NO TINDERBOX? || inventory.onlyContains("Tinderbox") // ONLY TINDERBOX? || !inventory.contains("Filler")) { // YOU'RE DONE COOKING? return State.BANK; } else if (myPlayer().isAnimating()) { // YOU'RE DOING STUFF??? return State.WAIT; } else if (fire != null) { // OH LOOKIEE HERE, A FIREEE? return State.COOK; } else { return State.LIGHT; // NO FIREE?? Simple. JUST MAKE ONE! } } Just now, liverare said: Your question has been answered. But additionally, make fire a field variable. Otherwise, you're going to have to re-find the fire when you come to cooking the fire. Sorry I'm dumb. The only answer I saw was the response from superuser, and it was giving me a dead code. The other thing was Frostbug explaining something but I didn't understand it. Quote Link to comment Share on other sites More sharing options...