Boolean not working

Discussion in 'Plugin Development' started by HackintoshMan, May 6, 2013.

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

    HackintoshMan

    Code:
    public void monitorPointOneLocation(Player player) {
            if (plugin.isCTF == true) {
                System.out.println(playerIsOnPoint1(player)); <--------returns true
                if (playerIsOnPoint1(player) == true) { <-------doesnt reach this statement  
                    if (hasBeenCaptured1 == false) {
                        plugin.captureTimer.timer(player, 1);
                    }
                    if (hasBeenCaptured1 == true) {
                        player.sendMessage(plugin.PREFIX
                                + "This poiont has already been captured!");
                    }
     
                    if (playerIsOnPoint1(player) == false) {
                        captureTime = 5;
                    }
                }
            }
        }
    This is my code. The if statement is supposed to fire if playerIsOnPoint1 returns true, which it does through my debug message, but if I put a debug message in the if statement, nothing happens.
     
  2. Offline

    afistofirony

    I don't know if this causes the error, but it looks like your if statements are incorrect. Another thing is that you put your playerIsOnPoint1(player) == false statement inside a playerIsOnPoint1(player) == true block, so it's impossible for that statement to be reached.

    You should make use of else statements.Also, you don't need brackets for single statements.

    Putting an exclamation point before a boolean statement basically makes the code check if that statement is false.
    if(condition) = do something if the condition is true
    if(!condition) = do something if the condition is false

    Try this:
    Code:
        if(playerIsOnPoint1(player)){
            if(!hasBeenCaptured1) plugin.captureTimer.timer(player, 1);
            else player.sendMessage(plugin.PREFIX + "This point has already been captured!");
      } else captureTime = 5;
     
  3. Offline

    HackintoshMan

    No i didn't…
    Code:
    if (plugin.isCTF == true) {
                System.out.println(playerIsOnPoint1(player));
                if (playerIsOnPoint1(player) == true) {
                    if (hasBeenCaptured1 == false) {
                        plugin.captureTimer.timer(player, 1);
                    }
     
  4. Offline

    afistofirony

    It is :p

    Code:
    if (playerIsOnPoint1(player) == true) { // Player MUST be on point 1 to reach this point.
                    if (hasBeenCaptured1 == false) {
                        plugin.captureTimer.timer(player, 1);
                    }
                    if (hasBeenCaptured1 == true) {
                        player.sendMessage(plugin.PREFIX
                                + "This poiont has already been captured!");
                    }
     
                    if (playerIsOnPoint1(player) == false) { // Can't reach this statement because the code block it's in requires the same value to be true.
                        captureTime = 5;
                    }
                }
    Anyways, I'm not particularly sure what might be causing the issue. I'd still give removing the == true statements and replacing the condition == false with !condition a shot.

    Also, which if-statement did you put your debug message in?
     
  5. Offline

    HackintoshMan

    I see what you think. Here is what happens. If the player gets on the point it starts the timer, if the player then gets off the point it stops the timer and resets it…
     
  6. Offline

    LucasEmanuel

    Can we see the code in the playerIsOnPoint1 method?
     
  7. Offline

    savagesun

    It looks like what will really happen is when the player gets on the point it will start some kind of timer, but the timer will never reset. Basically it can't reach the statement where you reset the timer because as afist mentioned to get to it the value must be true and its condition is looking for false.
     
  8. Offline

    SoThatsIt

    just btw thers no need for the "== true" or "== false". for true you just leave it and if you want it to be false you just put a exclamation point before it. "Boolean" checks if Boolean is true and "!Boolean" checks if Boolean is false.
     
  9. Offline

    HackintoshMan

    Ok, I will try that, but I don't know why I can't do ==true and ==false. The should equal true and false right?

    I just removed the == true and it still doesn't work. If i output the return value, it returns true and when I try to see if it is true then it returns false…..

    Am I using this correctly?

    Code:
    public boolean playerIsOnPoint1(Player player) {
            if (Math.abs(plugin.playerLocation.playerX(player)
                    - plugin.capturePointLocation.point1X()) <= 2
                    && Math.abs(plugin.playerLocation.playerX(player)
                            - plugin.capturePointLocation.point1X()) >= 0
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point1Y()) <= 1
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point1Y()) >= 0
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point1Z()) <= 2
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point1Z()) >= 0
                    && plugin.playerLocation.playerWorld(player).equals(
                            plugin.capturePointLocation.point1World())) {
                return true;
            }
            return false;
        }
    What is supposed to happen:

    if the player is within the range of points, return true.
    if the player is not in the range of points, return false.
    Can I use the true/false values in another if statement?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    HackintoshMan

  11. Offline

    Cirno

    Use Math.round, becuase right now, it's checking if the players is EXACTLY AT THAT POINT, decimals included.
     
  12. Offline

    HackintoshMan

    It is getting the player block location and it is being compared to a stored player block location in a config. The problem is

    Code:
    System.out.println(playerIsOnPoint1(player)); <--------prints true
                if (playerIsOnPoint1(player) == true) { <-------doesnt reach this statement  
     
  13. Offline

    HackintoshMan

  14. HackintoshMan
    I don't see any messages to confirm that it doesn't pass the condition, my guess is you're focusing on the wrong place.
    Make some messages printing values of the other conditions you're checking:
    Code:
    System.out.print("hasBeenCaptured1 = " + hasBeenCaptured1);
    And as people already said, the result of that method won't really change (I don't see any code that affects that method) so checking if it's false after checking if it's true won't work.
     
  15. Offline

    HackintoshMan

    Here is the whole class.

    Code:
    package com.hackintoshman.mcftf2;
     
    import org.bukkit.entity.Player;
     
    public class CaptureTheFlagHandler {
        public int captureTime = 5;
        public boolean hasBeenCaptured1 = false;
        public boolean hasBeenCaptured2 = false;
        public boolean hasBeenCaptured3 = false;
        public boolean isBeingCaptured1 = false;
        public boolean isBeingCaptured2 = false;
        public boolean isBeingCaptured3 = false;
     
        private MCFTF2 plugin;
     
        public CaptureTheFlagHandler(MCFTF2 plugin) {
            this.plugin = plugin;
        }
     
        public void gameHandler() {
            if (hasBeenCaptured1 == true && hasBeenCaptured2 == true
                    && hasBeenCaptured3 == true) {
                plugin.gameWin("red", "ChatColor.RED");
            }
            if (plugin.gameTime == 0) {
                plugin.gameWin("blue", "ChatColor.BLUE");
            }
        }
     
        public void monitorPointOneLocation(Player player) {
            if (plugin.isCTF == true) {
                System.out.println("playerIsOnPoint1 = " + playerIsOnPoint1(player));  //////////////////  prints true////////////////////
                if (playerIsOnPoint1(player)) {
                    System.out.println("it works");  ///////////////////////  does NOT print anything here//////////////////////
                    if (hasBeenCaptured1 == false) {
                        plugin.captureTimer.timer(player, 1);
                    }
                    if (hasBeenCaptured1 == true) {
                        player.sendMessage(plugin.PREFIX
                                + "This poiont has already been captured!");
                    }
     
                    if (playerIsOnPoint1(player) == false) {
                        captureTime = 5;
                    }
                }
            }
        }
     
        public void captureStatus(Player player, int p) {
            if (captureTime == 0) {
                plugin.getServer().broadcastMessage(
                        plugin.PREFIX + player.getName() + " has captured point "
                                + p + "!");
                if (p == 1) {
                    hasBeenCaptured1 = true;
                }
                if (p == 2) {
                    hasBeenCaptured2 = true;
                }
                if (p == 3) {
                    hasBeenCaptured3 = true;
                }
            }
        }
     
        public void incorrectCapturePoint(Player player, int p) {
            System.out.println("wrong point!");
            player.sendMessage(plugin.PREFIX + "You must capture point " + p
                    + "first!");
        }
     
        public void incorrectTeam(Player player) {
            player.sendMessage(plugin.PREFIX
                    + "You are trying to defend this point!");
        }
     
        public boolean playerIsOnPoint1(Player player) {
            if (Math.abs(plugin.playerLocation.playerX(player)
                    - plugin.capturePointLocation.point1X()) <= 2
                    && Math.abs(plugin.playerLocation.playerX(player)
                            - plugin.capturePointLocation.point1X()) >= 0
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point1Y()) <= 1
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point1Y()) >= 0
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point1Z()) <= 2
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point1Z()) >= 0
                    && plugin.playerLocation.playerWorld(player).equals(
                            plugin.capturePointLocation.point1World())) {
                isBeingCaptured1 = true;
                System.out.println("isBeingCaptured = " + isBeingCaptured1); ///////////////////  prints true/////////////////////////////
                return true;
            } else {
                return false;
            }
        }
     
        public boolean playerIsOnPoint2(Player player) {
            if (Math.abs(plugin.playerLocation.playerX(player)
                    - plugin.capturePointLocation.point2X()) <= 2
                    && Math.abs(plugin.playerLocation.playerX(player)
                            - plugin.capturePointLocation.point2X()) >= 0
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point2Y()) <= 1
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point2Y()) >= 0
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point2Z()) <= 2
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point2Z()) >= 0
                    && plugin.playerLocation.playerWorld(player).equals(
                            plugin.capturePointLocation.point2World())) {
                return true;
            } else {
                return false;
            }
     
        }
     
        public boolean playerIsOnPoint3(Player player) {
            if (Math.abs(plugin.playerLocation.playerX(player)
                    - plugin.capturePointLocation.point3X()) <= 2
                    && Math.abs(plugin.playerLocation.playerX(player)
                            - plugin.capturePointLocation.point3X()) >= 0
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point3Y()) <= 1
                    && Math.abs(plugin.playerLocation.playerY(player)
                            - plugin.capturePointLocation.point3Y()) >= 0
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point3Z()) <= 2
                    && Math.abs(plugin.playerLocation.playerZ(player)
                            - plugin.capturePointLocation.point3Z()) >= 0
                    && plugin.playerLocation.playerWorld(player).equals(
                            plugin.capturePointLocation.point3World())) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    I don't see why it wouldn't work…
     
  16. The code is pointlessly complicated and inefficient, I suggest a redesign.
    First try not to have so much repeatitive code.
    Then I suggest you use arrays (or lists, maps, etc) to store points data...
    And I strongly suggest you use player.getLocation() instead of that class with a map for each X/Y/Z/world value...
     
  17. Offline

    HackintoshMan

    Ok thanks. I was just trying to get my code organized, but I guess I went overboard…
     
Thread Status:
Not open for further replies.

Share This Page