How would I do this?

Discussion in 'Plugin Development' started by Connor2weirdness, Jan 28, 2014.

Thread Status:
Not open for further replies.
  1. So, I have this current code which I'm trying to cancel if a boolean is 1, and continue if 0. I have a worldguard region listener class changing boolean to 1 or 0 depending on whether they leave or enter a region. The problem I am having however is just due to a lack of knowledge in Java. If I have the region code somewhere in the class, "boolean" cannot be resolved to a variable. I then created another class and put the code there but it still won't work. Here is what I have:

    Boolean code:
    Code:
    package superprojs.Connor2weirdness;
     
    import org.bukkit.event.EventHandler;
     
    import com.mewin.WGRegionEvents.events.RegionEnterEvent;
    import com.mewin.WGRegionEvents.events.RegionLeaveEvent;
     
    public class regionlistener {
     
        @EventHandler
        public void onRegionEnter(RegionEnterEvent e) {
            {
                int bool = 1;
            }
        }
     
        public void onRegionLeave(RegionLeaveEvent e) {
            int bool = 0;
        }
     
    }
    
    Snippet of main code:
    How would I get it to be recognized in the "?????????" area? Thanks.
     
  2. Offline

    afistofirony

    Connor2weirdness You should use the native boolean type; it's essentially the same thing but more performance-friendly because it only uses 1 bit, not 32.

    Then, in your code, you'd do this:
    Code:
    boolean bool = true;
     
    // ...
    if (!(player.getGameMode() == GameMode.SURVIVAL && !bool)) {}

    However, if you still want to use an integer (perhaps for support of an 'undefined' value), the issue arises when you try to assign a value:
    Code:
    if (int bool == 0) {}

    This is the syntax for declaring a value, not checking a value; as such, it throws a compiler error. Instead, you need to use this:
    Code:
    if (bool == 0) {}

    EDIT: Also, you are declaring it as a value inside a method, so it's not available outside the method. You need to declare it as a field, which is accessible throughout the class (and if made public, throughout your plugin):

    Code:
    public class RegionListener {
        boolean bool = true;
        // Note: you can make this static so you can access it through RegionListener.bool
     
        public void doSomething () {
            if (bool) {} // can be accessed here
            // Note: if you have a variable named bool in your method, you can access it through
            // this.bool (or RegionListener.bool if you make the field static)
            // If the method is static and the field isn't, you cannot access it
        }
    }
     
  3. I'm sorry, I'm really not very good with this kind of stuff. This is what I have now:
    Code:
    public class superprojsmain extends JavaPlugin implements Listener {
        @EventHandler
        public void onRegionEnter(RegionEnterEvent e) {
            {
                Boolean bool = true;
            }
        }
     
        public void onRegionLeave(RegionLeaveEvent e) {
            Boolean bool = false;
        }
     
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new superprojsmain(),
                    this);
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Material stick = Material.STICK;
            Player player = event.getPlayer();
            @SuppressWarnings("deprecation")
            Block block = player.getTargetBlock(null, 100);
            Location loc = block.getLocation();
            if (!(player.getGameMode() == GameMode.SURVIVAL && bool == 0)) {
                if (event.getAction().equals(Action.LEFT_CLICK_AIR)
                        || (event.getAction().equals(Action.RIGHT_CLICK_AIR))) {
    Thank you.
     
  4. Offline

    afistofirony

    Connor2weirdness No problem; however I'd like to point out that your if statement still won't work properly because you check if it equals 0. You should replace 'bool == 0' with '!bool' (without the exclamation mark if you want it to be true). Also, you still won't be able to access your value -- it's still inside a method. It needs to be declared outside of any methods to be globally accessible :p

    By the way, you should use the primitive boolean type rather than Boolean (easier to use in general):
    Code:
    boolean bool = true;
     
  5. What my 1-bit Java disabled brain read:
    No problem; however I'd like to point out that your if statement still won't work properly because you check if it equals 0. You should replace 'bool == 0' with '!bool' (without the exclamation mark if you want it to be true). Also, you still won't be able to access your value -- it's still inside a blah. It needs to be blahed outside of any methods to be blahally blahssible :p

    Sorry, I really have NO idea what I'm doing. All I can do is create simple plugins at the moment and took the easy way out by learning the Bukkit API before I actually learned Java :( Really sorry and thanks!
     
  6. Offline

    afistofirony

    Connor2weirdness It's fine, I started out the same way too :)

    Basically, what I'm saying is this:

    Code:
    public class superprojsmain extends JavaPlugin implements Listener {
        // your value needs to be out here...
        boolean bool = true;
     
        public void onRegionEnter (RegionEnterEvent e) {
            boolean bool = true;
            // not in here.
        }
    }
    
    You can think of it like a hallway of one-way doors. Anything at the beginning of the hallway can go to the end of the hallway. Things at the middle of the hallway can go to the end of the hallway, but can't go back to the beginning.
     
  7. Thank you but, if I move the Boolean then I can't change it when the event triggers? I'm so confused D:
     
  8. Offline

    afistofirony

  9. Yeah but I gotta change it with the region event arrrgggh *brain explosion*
     
  10. Offline

    WhatAaCow

    make it static like
    Code:java
    1. public static boolean yourBool = false;
     
  11. Arigatou! :)
     
  12. Offline

    _Filip

    WhatAaCow
    DO NOT make it static like
    Code:java
    1. public static boolean yourBool = false;
     
  13. Offline

    WhatAaCow

  14. Offline

    _Filip

  15. Offline

    Garris0n

  16. Offline

    WhatAaCow

    swampshark19 lol sorry...my thoughts were somewhere else. It doesn't mind if you use static or not for this bool.
     
Thread Status:
Not open for further replies.

Share This Page