"Unreachable Code" In Eclipse Project

Discussion in 'Plugin Development' started by RRServer, Apr 29, 2014.

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

    RRServer

    I'm working a plugin for my server, however, in this event there is an instance of "Unreachable Code" on this section.

    Code:java
    1. Entity e = (Entity)localIterator2.next();


    Could anyone please help me resolve this issue, here is the entirity of the class.

    Code:java
    1. package me.RRServer.CombatCheck;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.Iterator;
    6. import java.util.List;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.block.Block;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.entity.LivingEntity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.plugin.Plugin;
    17. import org.bukkit.plugin.PluginManager;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class CombatCheck
    21. extends JavaPlugin
    22. implements Listener
    23. {
    24. public HashMap<String, Long> delay = new HashMap();
    25. Plugin plugin;
    26.  
    27. public void onEnable()
    28. {
    29. Bukkit.getPluginManager().registerEvents(this, this);
    30.  
    31. this.plugin = this;
    32. }
    33.  
    34. @EventHandler
    35. public void onHit(EntityDamageByEntityEvent event)
    36. {
    37. if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof LivingEntity)))
    38. {
    39. Player player = (Player)event.getDamager();
    40.  
    41. LivingEntity entity = (LivingEntity)event.getEntity();
    42.  
    43. List<Block> bs = player.getLineOfSight(null, 4);
    44.  
    45. List<Entity> sight = player.getNearbyEntities(15.0D, 15.0D, 15.0D);
    46.  
    47. List<Entity> near = new ArrayList();
    48. Iterator localIterator2;
    49. for (Iterator localIterator1 = bs.iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    50. {
    51. Block block = (Block)localIterator1.next();
    52. localIterator2 = sight.iterator(); continue;Entity e = (Entity)localIterator2.next();
    53. if ((e.getLocation().distance(block.getLocation()) < 4.0D) &&
    54. ((e instanceof LivingEntity))) {
    55. near.add(e);
    56. }
    57. }
    58. if (!near.contains(entity)) {
    59. player.kickPlayer("Test Message!!");
    60. }
    61. }
    62. }
    63. }
    64.  
     
  2. Offline

    Arcticmike

    I think you cant make an entity equals to an iterator
     
  3. Offline

    Not2EXceL

    You don't know java clearly.

    RRServer

    learn to read through your own code
    your issue is here
    Code:
    localIterator2 = sight.iterator(); continue;Entity e = (Entity)localIterator2.next();
    
    also dafuq u doing here?
    Code:
    Iterator localIterator2;
    for (Iterator localIterator1 = bs.iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    
    Just make the Iterator's type param a Block, and same for the Entity Iterator. And make your for loop's increment segment set the next block
     
  4. Offline

    garbagemule

    Unreachable code means you have code that the compiler's static analysis has deemed "dead" because there is no permutation of variable values that would ever bring the control flow to that part of the code. The continue statement is what's causing the issue - this statement tells the JVM to skip the rest of the loop body and continue to the next iteration. Since the statement is unconditional, it will always be executed, which means nothing in the loop body below it will be reached. I have a feeling you are blindly copy/pasting this from somewhere...

    There is absolutely no reason to explicitly use an Iterator here (and there rarely is). You are iterating a List, so use a for-each loop:
    Code:
    for (Type var : collection) {
        // do stuff with var here
    }
    In this case, Type is Block, and collection is the list called bs. You can read the loop as "for every variable 'var' of type 'Type' in the collection 'collection', do ..." For-each loops (or enhanced for-loops) can be used on any Collection, and indeed objects of any class that implements the Iterable-interface.

    Furthermore, you are using an Iterator for a List, just so you can call the next() method on it. Why not simply call get(0) on the List? I think you would do yourself a favor by looking up some Java basics tutorials somewhere.

    Bold words from a guy who clearly can't write a correct update-statement in an explicit iterator loop ;)
     
  5. Offline

    Not2EXceL

    Where thr f**k am I writing an incorrect update-statement? I didn't write a single line of code in my response.
     
  6. Offline

    garbagemule

    Calm down, kiddo. No need to swear.

    I assumed the second code block was your attempt at fixing things, but I see that you were just bashing more of the guy's code without providing any useful information. I guess the hostile attitude in your post just got the best of me :p I apologize for the misunderstanding.
     
  7. Offline

    Not2EXceL

    Sorry, just been in a pissy mood today. Also I did provide some useful information relative to that portion. Yes, seeing as he doesnt modify the list, he should just use the for-each loop. But he didn't even use his for loop properly anyways.

    Anyways misunderstanding resolved XD
     
  8. Offline

    Zach_1919

    RRServer Plain and simply, you have this right before that line of code:
    Code:java
    1. continue;

    In Java, continue skips to the next item in a list. Anything directly after continue is unreachable code because no matter what happens, it will always continue to the next item before that following line of code is executed.
     
Thread Status:
Not open for further replies.

Share This Page