Call config from a secondary class

Discussion in 'Plugin Development' started by generilisk, Jan 27, 2013.

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

    generilisk

    I'm having issues with my plugin, headgrabber. I can't get the head dropping on death to work at all. I've had difficulty calling the config file from the listener class. The idea is the user sets a percent chance for a head to drop in the config file, and the plugin rolls to see if that happens. Here's the relevant part of the main class:
    Code:
    public final class HeadGrabber extends JavaPlugin{
        HeadSet HeadSetObject = new HeadSet();
        public void onEnable(){
            this.saveDefaultConfig();//creates the default config file as outlined in config.yml
            getServer().getPluginManager().registerEvents(new HeadDeath(), this);    //enable the listener
        }
        public void onDisable(){
        }
    
    and here's the HeadDeath class:
    Code:
    package com.globaldominationhq.headgrabber;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import java.util.Random;
     
    public class HeadDeath implements Listener{
        HeadSet HeadSetObject = new HeadSet();
        HeadGrabber plugin;
        @EventHandler
        public void playerDeath(PlayerDeathEvent event, HeadGrabber instance){
            Random randomGenerator = new Random();
            plugin = instance;
            int chance = randomGenerator.nextInt(100);
            if(!(chance>plugin.getConfig().getInt("headgrabber.drops.death"))){
                String headName = event.getEntity().getName();
                ItemStack head;
                head = HeadSetObject.headSet(headName);
                event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), head);
            }   
        }
    }
    I get the following error on boot:
    Code:
    [SEVERE] [HeadGrabber] HeadGrabber v1.5 attempted to register an invalid EventHandler method signature "public void com.globaldominationhq.headgrabber.HeadDeath.playerDeath(org.bukkit.event.entity.PlayerDeathEvent,com.globaldominationhq.headgrabber.HeadGrabber)" in class com.globaldominationhq.headgrabber.HeadDeath
    I imagine my Java needs some cleanup work; I'm still learning.
    Any help would be appreciated.
     
  2. Offline

    raGan.

    Your event handler method must have only one parameter. Create constructor with "HeadGrabber instance" if you need to have it accessible in listener. Then just register. (new HeadDeath(this))
     
  3. Offline

    generilisk

    As I said, I'm new to java...I'm not sure I'm following you.
    I'm changing
    Code:
        public void playerDeath(PlayerDeathEvent event, HeadGrabber instance){
    to
    Code:
        public void playerDeath(PlayerDeathEvent event){
    , but I'm not sure how to create the constructor...so now I'm here:
    Code:
    public class HeadDeath implements Listener{
        HeadSet HeadSetObject = new HeadSet();
        HeadGrabber plugin;
        @EventHandler
        public void playerDeath(PlayerDeathEvent event){
            Random randomGenerator = new Random();
            plugin = instance;
            int chance = randomGenerator.nextInt(100);
            if(!(chance>plugin.getConfig().getInt("headgrabber.drops.death"))){
                String headName = event.getEntity().getName();
                ItemStack head;
                head = HeadSetObject.headSet(headName);
                event.getEntity().getWorld().dropItemNaturally(event.getEntity().getLocation(), head);
            } 
        }
    }
     
  4. Offline

    d33k40

    In your listener:
    Code:java
    1. public MainClass plugin;
    2. public PluginListenerClass(MainClass instance){
    3. plugin = instance;
    4. }


    In your mainclass:
    this:
    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new HeadDeath(), this);
    3.  

    to this:
    Code:java
    1.  
    2. getServer().getPluginManager().registerEvents(new HeadDeath(this), this);
    3.  
     
  5. Offline

    generilisk

    I got the changes in my main class, (Named HeadGrabber) but I can't figure out where to put the changes in the listener class. Am I adding this? Am I changing current lines? I feel stupid asking, but questions are how we learn, right?
     
  6. Offline

    d33k40

    Code:java
    1. package com.globaldominationhq.headgrabber;
    2. //imports...
    3.  
    4. public class HeadDeath implements Listener{
    5. public MainClass plugin;
    6. public PluginListenerClass(MainClass instance){
    7. plugin = instance;
    8. }
    9.  
    10. //your events
    11. }
     
  7. Offline

    generilisk

    That's done it, thank you!
     
Thread Status:
Not open for further replies.

Share This Page