Loop not working... HELP

Discussion in 'Plugin Development' started by CraftRay, Nov 13, 2015.

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

    CraftRay

    Why this loop can't go back when if not true ?
    Line: 130

    Code:
    package me.xcraftrayx.pl;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Lift extends JavaPlugin implements Listener {
      
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
        }
        public void onDisable(){
          
        }
        @EventHandler
        public void onPlayerClickInSign(PlayerInteractEvent e){
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (e.getClickedBlock().getState() instanceof Sign){
                    Player player = (Player)e.getPlayer();
                    Sign sign = (Sign)e.getClickedBlock().getState();
                    String signline1 = sign.getLine(0);
                    String signline2 = sign.getLine(1);
                    String signline3 = sign.getLine(2);
                    String signline4 = sign.getLine(3);
                    String message = "[Winda]";
                    String message2 = "[Na gore]";
                    String message3 = "[Na dol]";
                    String message4 = "";
                    if((message.equalsIgnoreCase(signline1) && message2.equalsIgnoreCase(signline2)) || (message.equalsIgnoreCase(signline1) && message3.equalsIgnoreCase(signline2))){
                        double x = sign.getLocation().getX();
                        double y = sign.getLocation().getY();
                        double z = sign.getLocation().getZ();
                        Location nLocation2 = new Location(Bukkit.getWorld(sign.getLocation().getWorld().getName()), x, y, z);
                        Location nLocation = nLocation2;
                        Location nLocation4 = nLocation;
                        //Do góry
                        if(message2.equalsIgnoreCase(signline2)){
                          
                            boolean a = false;
                          
                            while((nLocation2.getY()<=255 &&(!nLocation2.getBlock().getType().equals(Material.AIR)) && (a==false))){
                                nLocation2.add(0, 1, 0);
                                System.out.println(nLocation2.getY());
                                System.out.println(nLocation2.getBlock().getType());
                                System.out.println(a);
                              
                                if(nLocation2.getY()>=256){
                                    a = true;
                                    player.sendMessage("&cNie ma tutaj mejsca.");
                                }
                                else{
                                  
                                if(nLocation2.getBlock().getType().equals(Material.AIR)){
                                System.out.println("1 - Powietrze");
                                nLocation = nLocation2;
                              
                                if(nLocation.add(0, 1, 0).getBlock().getType().equals(Material.AIR)){ //Gdy drugi blok pusty
                                    System.out.println("Udalo sie tp cie na gore");
                                    a = true;
                                    System.out.println(nLocation2.getY());
                                    System.out.println(nLocation.getY());
                                    double X = nLocation.getX();
                                    double Y = nLocation.getY();
                                    double Z = nLocation.getZ();
                                    Location nLocation3 = nLocation;
                                    nLocation3.setX(X+0.5);
                                    nLocation3.setZ(Z+0.5);
                                    nLocation3.setY(Y-1);
                                    System.out.println(nLocation3);
                                    float yaw = player.getLocation().getYaw();
                                    nLocation3.setYaw(yaw);
                                    player.teleport(nLocation3);
                                    }
                                else{
                                    System.out.println("Powrot do petli");
                                    a = false;
                                }
                              }
                            }
                          }
                        }
                        else{
                            if(message3.equalsIgnoreCase(signline2)){
                      
                            boolean b = false;
                            while((nLocation2.getY()>0 && (!nLocation2.getBlock().getType().equals(Material.AIR)) && (!nLocation4.getBlock().getType().equals(Material.AIR)) && (b == false))){
                                nLocation2.subtract(0, 1, 0);
                                nLocation = nLocation2;
                                System.out.println(nLocation2.getY());
                                System.out.println(nLocation2.getBlock().getType());
                                System.out.println(b);
                              
                                if(nLocation2.getY()<=0){
                                    b = true;
                                    player.sendMessage("&cNie ma tutaj mejsca.");
                                }
                                else{
                                    System.out.println("Else");
                                if(nLocation2.getBlock().getType().equals(Material.AIR)){
                                System.out.println("Y1: "+nLocation2.getY());
                                nLocation = nLocation2;
                                  if(nLocation.subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)){//Gdy drugi blok pusty
                                    System.out.println("y2: "+nLocation.getY());
                                    nLocation4 = nLocation;
                                    if(!nLocation4.subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)){
                                        System.out.println("y3: "+nLocation4.getY());
                                    b = true;
                                    System.out.println(nLocation2.getY());
                                    System.out.println(nLocation.getY());
                                    double X = nLocation.getX();
                                    double Y = nLocation.getY();
                                    double Z = nLocation.getZ();
                                    Location nLocation3 = nLocation;
                                    nLocation3.setX(X+0.5);
                                    nLocation3.setZ(Z+0.5);
                                    nLocation3.setY(Y);
                                    System.out.println(nLocation3);
                                    float yaw = player.getLocation().getYaw();
                                    nLocation3.setYaw(yaw);
                                    player.teleport(nLocation3);
                                }
                                else{
                                    System.out.println("Ostatni blok to nie ziemia"+ nLocation4.getY()); //Powrót do pętli
                                    b = false;  
                                }
                        }
                                else{
                                    b = false;
                                    System.out.println("Drugi blok nie jest powietrzem: "+ nLocation.getY());
                                }
                      }
                                else{
                                    b = false;
                                    System.out.println("Bad back");
                                }
                    }
                  }
                  }
              }       
            }
          }
        }
      }
    }
    
     
    Last edited: Nov 14, 2015
  2. Offline

    rbrick

    mythbusterma likes this.
  3. Offline

    mcdorli

    No, that's not true, a single & in a statement works a bit different than &&. Basically, && stops after it found something incorrect

    Code:
    int x = 12;
    int y = 23;
    int z = 29;
    //                This stops here, because y is not 24
    //                V
    if (x == 12 && y == 24 && z == 29) {
    
    But a single & checks every condition, and only stop at the end:

    Code:
    int x = 12;
    int y = 23;
    int z = 29;
    //                This only stops here, it doesn't care that y is not 24
    //                             V
    if (x == 12 & y == 24 & z == 29) {
    
    The same applies to the | and ||

    This looks like something stupid first, but it has some sense. For example when you want to initialize more APIs at once

    More here: http://stackoverflow.com/questions/5564410/difference-between-and
     
  4. Offline

    mythbusterma

    @mcdorli

    Actually, the guy above you is far more correct than you. That SO question is way beyond misleading (personally, I think it should be reported). The Java language always does logical short circuiting when "anding" operands. The interesting quirk is when you do this booleans, as a boolean and is the same as a logical and, however it cannot be short-circuited around, so it must evaluate all of them. The fact that it works out the way it you said in your post is just a weird side-effect of its actual purpose, and it should never be used this way.

    TL;DR '&' and '|' are for bitwise math only, otherwise use "&&" and "||" to avoid writing confusing code.
     
    DoggyCode™ and rbrick like this.
  5. Offline

    CraftRay

    Okey I replaced all & to &&, what's next, because it still does not work
     
  6. Offline

    Scimiguy

    Show new code, and explain your problem again cus I have no idea what's wrong
     
  7. Offline

    CraftRay

    So i check 3 position when clicked sign wchich have [Winda], [Na dol]
    So when, 1 position and 2 position is AIR and 3 position is'nt AIR = Player teleport
    So when 1 position and 2 position is AIR and 3 position is AIR my code STOP Why can't go back to loop?
     
  8. Offline

    mcdorli

    I never said it is like this on a purpose. I said, that he is not correct, because you can use it inside statements, I said, it works a bit different than the normally uses &&, and it is true. It CAN be used as a &&, it isn't recommended, but you can. A single & goes trough every statement, and then returns the result. I didn't said anything incorrect in that post. If you can point out 1 sentence, that directly contained something incorrect, then please, show me what I did wrong. You can report the post if you want, I'm not going to refrain you.
     
  9. Offline

    DoggyCode™

  10. Offline

    mythbusterma

    He said you shouldn't, not that you can't. Those are two different things. It's not that a single ampersand does what you've described, it's that the single ampersand does a bitwise and, which requires all operands to be evaluated.

    I wasn't talking about your post, your post is fine. I meant the SO post.

    @CraftRay

    Try simplifying your code, it might be easier to follow.

    Uproszczenie kodu, tak aby można lepiej zrozumieć
     
  11. Offline

    CraftRay

    Okey thanks all for supported!
    I done it!
     
  12. Offline

    Scimiguy

    Please edit the title of your original post and mark the thread as solved then
     
Thread Status:
Not open for further replies.

Share This Page