Registering events from other class (Error)

Discussion in 'Plugin Development' started by ImCenz, May 16, 2017.

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

    ImCenz

    Hey m8s, im doing a simple plugin but when i want to register events from another class i get the following error:

    [​IMG]

    I think the error is in the constructor im using in DiamondLessManager.java, but i dont know another way to do it and do it work

    Main.Java
    Code (open)

    Code:
    package me.imcenz;
    
    import java.util.ArrayList;
    import me.imcenz.Scenarios.DiamondLess.DiamondLess;
    import me.imcenz.Scenarios.DiamondLess.DiamondLessManager;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.Listener;
    
    
    public class Main extends JavaPlugin implements Listener{
      
        public ArrayList<String> scenarios = new ArrayList();
      
        @Override
        public void onEnable(){
            Bukkit.getConsoleSender().sendMessage("\n§aVantixCore§c: §aEnabled");
          
            reloadcfg();
            resetScenarios();
          
        }
      
        @Override
        public void onDisable(){
      
        }
      
        public void reloadcfg(){
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            Bukkit.getConsoleSender().sendMessage("§aVantixCore§c: §bConfig Reloaded.");
        }
    
        public void resetScenarios(){
            this.scenarios.clear();
            Bukkit.getConsoleSender().sendMessage("§aVantixCore§c: §bReseting Scenarios.");
        }
      
        public void getCommands(){
            getCommand("diamondless").setExecutor(new DiamondLess(this));
        }
      
        public void registerEvents(){
            Bukkit.getPluginManager().registerEvents(new DiamondLessManager(), this);
        }
    }
    


    The error is in this line:
    Code:
    Bukkit.getPluginManager().registerEvents(new DiamondLessManager(), this);
    DiamondLessManager.java
    Code (open)

    Code:
    package me.imcenz.Scenarios.DiamondLess;
    
    import me.imcenz.Main;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class DiamondLessManager implements Listener{
      
        private final Main plugin;
        public DiamondLessManager(Main plugin){
            this.plugin = plugin;
        }
    
      
        @EventHandler
        public void alRomper(BlockBreakEvent e){
            Block block = e.getBlock();
            if(plugin.scenarios.contains("diamondless")){
                if(block.getType().equals(Material.DIAMOND_ORE)){
                    e.setCancelled(true);
                    block.setType(Material.AIR);
                }
            }
        }
      
        @EventHandler
        public void enMuerte(PlayerDeathEvent e){
            if(plugin.scenarios.contains("diamondless")){
                e.getDrops().add(new ItemStack(Material.DIAMOND, 1));
            }
        }
    }
    


    Like i said before, i think the error is in the constructor (Yes, it say the same in the error Xd), but idk a way to make it works :(


    Thanks you :)
     
  2. Offline

    Zombie_Striker

    @ImCenz
    The instance of DiamonLessManager requires a Main class instance. Add "this" to the DiamondLessManager's parameter.
     
    Asparock likes this.
  3. Offline

    Asparock

    In addition to the good @Zombie_Striker 's answer :

    Since you seem to be new on plugin development, the exact fix in the line will be :

    Code:
    Bukkit.getPluginManager().registerEvents(new DiamondLessManager(this), this);
    As you can see on the DiamondLessManager's constructor :
    Code:
    public DiamondLessManager(Main plugin)
    An instance of your "Main" class is required and you are using it the very next line with
    Code:
    this.plugin = plugin;
     
Thread Status:
Not open for further replies.

Share This Page