Solved Using booleans in separate classes

Discussion in 'Plugin Development' started by robotballs, Sep 17, 2015.

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

    robotballs

    So I need the boolean I am using to transfer classes. To do that I have done this
    Code:
      public static boolean toggle = false;
      public static boolean ReturnToggle () {return toggle;}
    But it doesn't underline in red in my second class so I am not 100% sure what the problem is.
    What I am trying to do is get the boolean in the listeners class to see if it is true or false to change what happens on an event. Here is my event hander.
    Code:
         @EventHandler
         public void eggThrow(PlayerEggThrowEvent e)
         {
           Player player = e.getPlayer();
           Egg egg = e.getEgg();
           if (player.hasPermission("enderegg.throw"))
           {
              //If toggle boolean is true
              if(MainClass.toggle = true){
              e.setHatching(false);
             Location newLoc = egg.getLocation();
             Location old = player.getLocation();
             newLoc.setPitch(old.getPitch());
             newLoc.setYaw(old.getYaw());
             player.teleport(newLoc);
             
             if (player.hasPermission("enderegg.unlimited")) {
               player.getInventory().addItem(new ItemStack(Material.EGG));
    
             }
             
               }
           }
         }
    
    I need it so when the boolean is false it doesn't run the event.
     
  2. Offline

    WPM

    Pass an instance and get it from there?

    Edit: ignore that post ^

    Check if the Boolean is false. If it is return. But in your code the Boolean will always be false.
     
  3. Offline

    567legodude

    @robotballs Well, since you made it public and static (I don't think they recommend that here), you should be able to call ClassName.toggle for the value.
     
  4. Offline

    SuperSniper

    @robotballs Since you're using the "static" modifier, you should be able to do what @567legodude said above.

    Code:
    <className with Boolean>.toggle.method
    
     
  5. Offline

    Xerox262

    if(MainClass.toggle = true){
    change to
    if(MainClass.ReturnToggle()){
    or
    if(MainClass.toggle == true){
    or
    if(MainClass.ReturnToggle() == true){

    one equals sign implies you're setting a value which is why you were getting an error, two equals signs is for checking a value

    Instead of making it static you should just pass a instance of "MainClass" to the command class or where-ever you're trying to use it Listener/Listeners, and you should change toggle to be private and ReturnToggle to isToggle
     
    Last edited: Sep 17, 2015
    robotballs likes this.
  6. @robotballs first of all if you set the field to "public static" every class can read and write that field. so there is no reason to define a getter for it. you would only need a getter if it is "protected" or "private" because then it is not visible for every class. so "public static boolean toggle = false" is enough.

    To solve your task you just check if the boolean is true before you execute your code and if it is false you just dont execute it. would look like this:
    Code:
    @EventHandler
         public void eggThrow(PlayerEggThrowEvent e) {
           if(MainClass.toggle){
             // execute your event-code here
           }
         }
    
     
  7. Offline

    mythbusterma

    @Shmobi

    The point being it shouldn't be public or static.

    This is terrible design.
     
  8. @mythbusterma if it is just a single-existing flag so it wont be needed multiple times and it shall be configurateable from everywhere, this is the design you should be going for. why would you want to create an instance for it in that case. Should be static then, but i would put it then in the listener-class because thats where the flag belongs to it seems like. Now you can discuss about private static with getter/setter or public static and access the field directly. since its just a boolean on which you cant execute methods anyways because it primitiv and you got read- and writeaccess for it, you actually can go with public static. i dont see a reason why this would be terrible design in that case. thats what the modifiers public and static can be combinated for. actually for a flag which should exist only once in your whole logic you should go with static, because so you can be sure that you are always accessing the same field
     
  9. Offline

    mythbusterma

    @Shmobi

    In fact, this is the exact reason it shouldn't be public and static.

    Making something public rather than using accessor methods makes something significantly harder to reason about. It is very easy to see what a setter method does and when it's called, it's a lot harder to find all of the assignments of a variable in a program, when it's spread across many places. In other languages (Java less so), it is also possible to accidentally perform an assignment to this without realising it, making debugging extremely difficult.
     
  10. Offline

    robotballs

    Yes, thank you. I only had one = sign. It was late at night and I was doing math homework at the same time. Probably not the best idea, but I did it anyways.
     
    Xerox262 likes this.
  11. dont take this rude now. but teach me then in which cases you would use the modifiers public and static in combination if not to make an objectindependend field accessable for all others?
     
  12. Offline

    mythbusterma

    @Shmobi

    In general, the modifiers 'public' and 'static' should never be used together except when they are followed by the modifier 'final.' The phrase 'public static final' implies a global constant. This is very easy to reason about, because the value of this field never changes.

    When something is only 'public static,' it is very difficult to reason about, because it can be changed anywhere in the program flow, without calling a method that can be easily identified.
     
  13. Offline

    567legodude

    @mythbusterma So really it's just because it makes it harder to debug?
     
  14. Offline

    mythbusterma

    @567legodude

    Not only to debug, but to modify, maintain, and read (and readability trumps all else).

    But yes, that's the reason for it.
     
Thread Status:
Not open for further replies.

Share This Page