I have this code:
public void onPlayerInteract(PlayerInteractEvent event) {
final Action action = event.getAction();
Location l1 = null;
Location l2 = null;
if (action == Action.LEFT_CLICK_BLOCK){
l1 = event.getClickedBlock().getLocation();
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l2 = event.getClickedBlock().getLocation();
}
Thread t = new Thread() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000*60*60);
Location maxx = l1.getX();
Location maxy = l1.getY();
Location maxz = l1.getZ();
Location minx = l2.getX();
Location miny = l2.getY();
Location minz = l2.getZ();
if(l1.getX() > l2.getX()){
//I can't execute this, errors!
}
} catch (InterruptedException ie) {
}
}
}
};
t.start();
It gives me errors, and says to change l1 and l2 to finals. If I change l1 and l2 to finals, it gives me another error, where it says l1 = etc., it says to remove the final.
l1andl2are local variables of the methodonPlayerInteract(). In this method, you create an anonymous inner class which uses these local variablesl1andl2. This is only possible ifl1andl2are final. But by definition, a final variable can only be assigned once, and you assign null, and then another value to them. So you need to make a copy ofl1andl2to final variables, and use those final copies inside your anonymous class: