Solved [HELP] Event Listener with configuration file

Discussion in 'Plugin Development' started by Lionhard, Dec 16, 2013.

Thread Status:
Not open for further replies.
  1. Hello, I've started making plugins and it is really cool :D, but I can't get out how I can use informations stored in a config.yml in a EventListener. Can please someone help me?

    I have this code:

    Code:
    public class PlayerListener implements Listener{
       
        public static ItemEffects plugin;
       
        @EventHandler
        public void PlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            ItemStack item = new ItemStack(Material.getMaterial(plugin.getConfig().getInt("effects.speed.item", 353)));
            int duration = plugin.getConfig().getInt("effects.speed.duration", 200);
           
            if(player.getInventory().getItemInHand().equals(item)){
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, duration, 2));
                }
            }       
        }
    }
    
    What I'm trying to do is getting the item and it's duration out from the config.yml and use them in the code. When I don't try to get the integers from the config.yml it works, just when i try to get it from there it won't work.

    Does someone have a solution?

    Thanks in advance, Lionhard98
     
  2. Offline

    qhenckel

    the listener is a different class. this is a java thing here is what you have to do:

    Create a constructor for the class:

    Code:
    public class PlayerListener implements Listener{
     
        public static ItemEffects plugin;
     
        PlayerListener(int d){
     
        }
     
        @EventHandler
     
        public void PlayerInteract(PlayerInteractEvent event){
     
            Player player = event.getPlayer();
     
            ItemStack item = new
     
        ItemStack(Material.getMaterial(plugin.getConfig().getInt("effects.speed.item",
     
        353)));
     
            int duration = plugin.getConfig().getInt("effects.speed.duration", 200);
     
            if(player.getInventory().getItemInHand().equals(item)){
     
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==
     
            Action.RIGHT_CLICK_BLOCK){
     
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
     
            duration, 2));
     
                }
     
            }
     
        }
     
    }
    Create a field in the class:

    Code:
    public class PlayerListener implements Listener{
     
        int duration;
     
        public static ItemEffects plugin;
     
        PlayerListener(int d){
     
        }
     
        @EventHandler
     
        public void PlayerInteract(PlayerInteractEvent event){
     
            Player player = event.getPlayer();
     
            ItemStack item = new
     
        ItemStack(Material.getMaterial(plugin.getConfig().getInt("effects.speed.item",
     
        353)));
     
            int duration = plugin.getConfig().getInt("effects.speed.duration", 200);
     
            if(player.getInventory().getItemInHand().equals(item)){
     
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==
     
            Action.RIGHT_CLICK_BLOCK){
     
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
     
            duration, 2));
     
                }
     
            }
     
        }
     
    }
    Assign the passed in variable to field:

    Code:
    public class PlayerListener implements Listener{
     
        int duration;
     
        public static ItemEffects plugin;
     
        PlayerListener(int d){
     
        duration = d;
     
        }
     
        @EventHandler
     
        public void PlayerInteract(PlayerInteractEvent event){
     
            Player player = event.getPlayer();
     
            ItemStack item = new
     
        ItemStack(Material.getMaterial(plugin.getConfig().getInt("effects.speed.item",
     
        353)));
     
            int duration = plugin.getConfig().getInt("effects.speed.duration", 200);
     
            if(player.getInventory().getItemInHand().equals(item)){
     
                if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==
     
            Action.RIGHT_CLICK_BLOCK){
     
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED,
     
            duration, 2));
     
                }
     
            }
     
        }
     
    }
     
    
    Pass in the variable:

    find this line:
    Code:
    getServer().getPluginManager().registerEvents(new
    PlayerListener(), Plugin);
    and add this to it:
    Code:
    getServer().getPluginManager().registerEvents(new PlayerListener(getConfig().getInt("effects.speed.duration", 200)), Plugin);
    then you should be able to use the "duration variable any where you want.
    if this makes no sense to you look up a tutorial about passing Java classes variables
     
    Lionhard likes this.
  3. First of all, thank you for your answer, I'm going to try it out in a few minutes. Thank you very much. ;) I'll tell if it worked. ;)

    Somehow it doesn't work. :/ But I'll try to find a tutorial about passing java classes variables.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Can it be that you missunderstood me? After this, I can use the integer "duration" from the PlayerListener class in other classes, right? I need that I can use duration and itemID from ANOTHER class IN the PlayerListener class. Sorry, I didn't explain it right.

    EDIT: Please can you give me another instruction, please? I looked already at several tutorials of passing variables into other classes, but that a) Didnt work, b) I didnt understand it. :/

    Thanks in advance, Lionhard98
     
  5. qhenckel Yes! I just tried it again and it works perfectly. I don't know how I can thank you. ;)
    A [​IMG] for you. ;)
     
Thread Status:
Not open for further replies.

Share This Page