Solved Variable From @EventHandler To Other @EventHandler

Discussion in 'Plugin Development' started by RolandMC, Aug 14, 2017.

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

    RolandMC

    So, I Do A Variable In An @EventHandler That I Want To Be Valid In An Other @EventHandler.
    Here's My Code:

    Code:
    public class JListener implements Listener {
    
        public JListener(Jobs plugin) {
               
        }
    
           int acces = 1
        @EventHandler
    public void onBlockClick(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
                   
            if ( action.equals( Action.RIGHT_CLICK_BLOCK ) && action.equals( Action.LEFT_CLICK_BLOCK ) ) {
               
                if ( item != null && item.getType() == Material.BED ) {
             acces = 2
        }       
    
            }
        }
    @EventHandler
    public void chat(AsyncPlayerChatEvent event)
    if ( acces != 2) {
    //do something
    } else {
    //do something
    }
    
    }
    so, in the 2nd @EventHandler, the variable acces isn't valid.
    and i want acces to be "2" if he clicked and to be "1" if not.

    Yes, I Have All The Imports.


    Any Ideas? I'm Really Urgent With This Plugin.. Really..
     
  2. @RolandMC
    1. Reformat your code.
    2. To access data between event handlers and support multiple players, you're going to need a Map. I'd create a Map<UUID, YourData> and use the UUID to identify the players.
     
  3. Offline

    AdamDev

    @RolandMC , @AlvinB
    Why not just make it in the same EventHandler:
    Code:
    @EventHandler
    public void Events(PlayerInteractEvent e1, AsyncPlayerChatEvent e2) {
        //Code here
    }
     
  4. Offline

    Zombie_Striker

    @AdamDev
    That won't work; methods can only receive one event. That method will never be called

    @RolandMC
    Do what @AlvinB suggested.
     
  5. Offline

    HeartandSoul

    I formatted it for him.
    Code:
    public class JListener implements Listener {
    
        public JListener(Jobs plugin) {
      
        }
    
        int acces = 1
        @EventHandler
        public void onBlockClick(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
            ItemStack item = event.getItem();
          
            if ( action.equals( Action.RIGHT_CLICK_BLOCK ) && action.equals( Action.LEFT_CLICK_BLOCK ) ) {
      
                if ( item != null && item.getType() == Material.BED ) {
                    acces = 2
                }
            }
        }
        @EventHandler
        public void chat(AsyncPlayerChatEvent event) { //Here
            if ( acces != 2) {
                //do something
            } else {
                //do something
            }
        } //Here
    }
    So you're saying you want the field "acces" to be used and changed in the 2nd event handler? Not a problem. It looks like you're doing it right though, you'd use it as a field. I'd recommend you changing it to:
    Code:
    private int acces;
    Similarly, it's faster if you use 'player', 'action' and 'item' as fields so the values can be constantly changed and re-used, it tends to make it faster (disregarding the GC, of course).

    Also, for stuff like this, It's easier to use Enums, it makes your code easier to read. Booleans would work too, because it seems like you're only using two values. I can teach you this if you'd like.

    If there was an error in your console with your code, please post it here, it makes it easier for us to debug it.

    EDIT: I also noticed while formatting it that he was missing two brackets for the 2nd EventHandler, I commented them. I'm opting this to be closed as a typo, if it was a problem.
     
    Last edited: Aug 14, 2017
  6. Offline

    RolandMC

    it's || not && but anyway i will try :)

    thanks man

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 15, 2017
Thread Status:
Not open for further replies.

Share This Page