Can't get a listener to work

Discussion in 'Plugin Development' started by Viperdream, Aug 16, 2012.

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

    Viperdream

    I'm having trouble with registering my listeners.

    Every time I'm starting up my plugin, I get this error:
    Code:
    2012-08-16 12:54:01 [SEVERE] Error occurred while enabling Brawler v0.1 (Is it up to date?)
     
    java.lang.NoSuchMethodError: com.github.viperdream.playerListener.<init>(Lcom/github/viperdream/Brawler;)V
     
        at com.github.viperdream.Brawler.onEnable(Brawler.java:42)
     
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
     
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:365)
     
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
     
        at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:265)
     
        at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:247)
     
        at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:567)
     
        at org.bukkit.Bukkit.reload(Bukkit.java:183)
     
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:21)
     
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
     
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
     
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
     
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
     
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
     
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
     
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:276)
     
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
     
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
     
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
     
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:581)
     
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
     
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
     
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
     
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    This is the onEnable() code from my main class:
    Code:
    package com.github.viperdream;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import net.milkbowl.vault.economy.Economy;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Brawler extends JavaPlugin {
     
        Logger log;
        FileConfiguration customConfig = null;
        File customConfigFile = null;
        public static Brawler plugin;
        public static Economy economy = null;
        public static List<String> brawlers = new ArrayList<String>();
        public static Map<String, Integer> bets = new HashMap<String, Integer>();
        public static Map<String, Boolean> games = new HashMap<String, Boolean>();
        public static List<Player> pbets = new ArrayList<Player>();
        public static List<String> p1bets = new ArrayList<String>();
        public static List <String> p2bets = new ArrayList<String>();
        public static Map<String, Player> activebrawlers = new HashMap<String, Player>();
        public static Map<String, Integer> time = new HashMap<String, Integer>();
       
        @SuppressWarnings("unused")
        public void onEnable(){
           
            this.log = getLogger();
            Plugin x = this.getServer().getPluginManager().getPlugin("Vault");
            loadConfig();
            new signListener(this);
            new playerListener(this);
            log.info("[Brawler] Has been enabled!");
        }
       
        public void onDisable(){
           
            log.info("[Brawler] Has been disabled!");
        }
    And this is one of my listeners:

    Code:
    package com.github.viperdream;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
     
    public class signListener implements Listener{
       
        private Brawler plugin;
     
        public signListener(Brawler plugin) {
            this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onSignChange(SignChangeEvent event){
           
            Player p = event.getPlayer();
           
            if(event.getLine(0).contains("[BJoin]")){
               
                event.setLine(0, ChatColor.DARK_RED + "[Brawler]");
                event.setLine(1, ChatColor.WHITE + "Click to que");
                event.setLine(2, ChatColor.WHITE + "for a Brawl!");
                p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Join sign ready for use!");
            }else if(event.getLine(0).contains("[BBet1]") && !event.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+") && p.hasPermission("brawler.create")){
               
                if(event.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+") && p.hasPermission("brawler.create")){
                   
                    p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": The last line must contain a number with $ in front!");
                    event.setCancelled(true);
                }
               
                event.setLine(0, ChatColor.DARK_RED + "[Brawler1]");
                event.setLine(1, ChatColor.DARK_BLUE + "Bet for");
                event.setLine(2, ChatColor.DARK_RED + "Player 1");
                p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Bet sign 1 ready for use!");
            }else if(event.getLine(0).contains("[BBet2]") && !event.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+") && p.hasPermission("brawler.create")){
               
                if(event.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+") && p.hasPermission("brawler.create")){
                   
                    p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": The last line must contain a number with $ in front!");
                    event.setCancelled(true);
                }
               
                event.setLine(0, ChatColor.DARK_GREEN + "[Brawler2]");
                event.setLine(1, ChatColor.DARK_BLUE + "Bet for");
                event.setLine(2, ChatColor.DARK_GREEN + "Player 2");
                p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Bet sign 2 ready for use!");       
            }else if(event.getLine(0).contains("[BBet2]") || event.getLine(0).contains("[BBet1]") || event.getLine(0).contains("[BJoin]") ){
               
                p.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You don't have enough permission to do that!");
            }
        }
     
    }
    
    I really don't see the problem here. I've compared it with my previous plugins, but can't find any errors. If anyone could help me, I'd appreciate it greatly!

    Thanks!
     
  2. Offline

    NSArray

    The problem is in your onEnable() method not in the listener.
     
  3. Offline

    sensus12

    Do it:

    in extends JavaPlugin {

    //here
    add:

    public final Logger logger = Logger.getLogger("Minecraft");
    }

    and onEnable()
    {
    add:

    PluginDescriptionFile pdfFile = this.getDescription();

    this.logger.info(pdfFile.getName() + " has been enabled" + pdfFile.getVersion());

    }

    and onDisable()
    {
    add:

    PluginDescriptionFile pdfFile = this.getDescription();

    this.logger.info(pdfFile.getName() + " has been disabled");
    }
     
  4. Offline

    kyle1320

    Can you post your playerListener class?
     
  5. Offline

    Viperdream

    NSArray
    I also think that, but I don't really see an immediate wrong piece of code in it

    sensus12
    I tried that, but it didn't help.
    I also don't see why adding the plugin description would help...

    kyle1320

    Here it is:
    Code:
    package com.github.viperdream;
     
    import java.io.File;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Sign;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class playerListener implements Listener {
       
        int getprice = 0;
        private SellSign plugin;
        FileConfiguration customConfig = null;
        File customConfigFile = null;
       
        public playerListener(SellSign plugin) {
            this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
           
            //Lockette Protection check.
            if ((event.getAction() == (Action.RIGHT_CLICK_BLOCK)) && (event.getClickedBlock().getState() instanceof Sign))
            {
                Sign sign = (Sign)event.getClickedBlock().getState();
               
                if (sign.getLine(0).equalsIgnoreCase("[private]")){
                    if (sign.getLine(1).equalsIgnoreCase(player.getName())){
                        if (sign.getLine(2).contains("--For Sale--") || sign.getLine(2).contains("--For Rent--")){
                            player.sendMessage("This is your property.");
                        }
                    }else if(sign.getLine(2).contains("--For Rent--")){          //if the sign is for rent
                       
                        String ps = player.getName();
                        double balance = plugin.economy.getBalance(ps);
                        int cost = Integer.parseInt(sign.getLine(3));
                       
                        if(SellSign.tenants.contains(ps)){
                            player.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "SellSign" + ChatColor.GREEN + "] " + ChatColor.RED + "Sorry, you can't rent more than 1 location!");
                        }else if(!SellSign.tenants.contains(ps) && balance < cost/2){
                            player.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "SellSign" + ChatColor.GREEN + "] " + ChatColor.RED + "Sorry, you need at least $" + cost/2);
                        }else if(balance > cost/2 && !SellSign.tenants.contains(ps)|| balance > cost/2 && !SellSign.tenants.contains(ps) && !sign.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+")){       
                           
                            String signprice = sign.getLine(3);
                            int price = Integer.parseInt(signprice);
                            String landlord = sign.getLine(1);
                           
                            plugin.economy.withdrawPlayer(ps, price/2);
                            plugin.economy.depositPlayer(landlord, price/2);
                                                 
                            int x = event.getClickedBlock().getLocation().getBlockX();
                            int y = event.getClickedBlock().getLocation().getBlockY();
                            int z = event.getClickedBlock().getLocation().getBlockZ();
                            String world = event.getClickedBlock().getLocation().getWorld().getName();
                           
                            plugin.getCustomConfig().set("signs." + ps + ".x", x);
                            plugin.getCustomConfig().set("signs." + ps + ".y", y);
                            plugin.getCustomConfig().set("signs." + ps + ".z", z);
                            plugin.getCustomConfig().set("signs." + ps + ".world", world);
                            plugin.getCustomConfig().set("signs." + ps + ".rent", price);
                            plugin.getCustomConfig().set("signs." + ps + ".landlord", landlord);
                           
                            sign.setLine(1, player.getName());
                            sign.setLine(2, "");
                            sign.setLine(3, ChatColor.RED + plugin.getCustomConfig().getString("signs." + ps + ".landlord"));
                            sign.update();
                           
                            SellSign.tenants.add(ps);
                           
                            plugin.saveCustomConfig();
                            plugin.reloadCustomConfig();
                           
                            try {
                                Save.save(SellSign.tenants, "tenants.properties");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                           
                           
                            player.sendMessage(ChatColor.GREEN + "[" + ChatColor.YELLOW + "SellSign" + ChatColor.GREEN + "] " + ChatColor.WHITE + "Congratulations, you are now a tenant!");
                        }
                    }else if(!sign.getLine(3).matches("((\\$)?[0-9]+(\\.[0-9]+)?)+")){ //if the sign is for sale
                        player.sendMessage("Invalid price, Use eg. $500");       
                    }else if(!sign.getLine(3).contains("$")){
                        player.sendMessage("Invalid price, Please include the dollar sign");       
                    }else if(sign.getLine(2).contains("--For Sale--")){
                        String[] price = sign.getLine(3).split("\\$");
                        int getprice = Integer.parseInt(price[1]);
                        String name = player.getName();
                        String seller = sign.getLine(1);
                        int balance = (int) plugin.economy.getBalance(name);
                        player.sendMessage("Price: $" + getprice);
     
                        if(balance < getprice){
                            player.sendMessage("You can't afford this property, You have: $" + balance);
                        }
                        else{
                            plugin.economy.withdrawPlayer(name, getprice);
                            plugin.economy.depositPlayer(seller, getprice);
                            int balanceafter = (int) plugin.economy.getBalance(name);
                            sign.setLine(1, player.getName());
                            sign.setLine(2, "");
                            sign.setLine(3, "");
                            sign.update();
                            player.sendMessage("You successfully bought this property for $" + getprice);
                            player.sendMessage("You now have: $" + balanceafter);
                        }
                }
                }
            }       
        }
    }
     
  6. Offline

    kyle1320

    Viperdream I think it's that you're trying to pass Brawler into the playerListener constructor (in onEnable), which wants to take an instance of SellSign.
     
  7. Offline

    Viperdream

    Oops, sorry that's the wrong playerListener

    Here's the right one:
    Code:
    package com.github.viperdream;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class playerListener implements Listener{
       
        private Brawler plugin;
       
        public playerListener(Brawler plugin) {
            this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){   
            if ((event.getAction() == (Action.RIGHT_CLICK_BLOCK)) && (event.getClickedBlock().getState() instanceof Sign)){
                    Sign sign = (Sign)event.getClickedBlock().getState();
                    Player player = event.getPlayer();
                    String name = player.getName();
                    double balance = Brawler.economy.getBalance(name);
                    String[] betstring = sign.getLine(3).split("\\$");
                    int bet = Integer.parseInt(betstring[1]);
                   
                   
                    if(sign.getLine(1).contains("Click to que")){
     
                        Brawler.brawlers.add(name);
                        player.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You have joined the que. Please wait untill a match is available");
                    }else if(sign.getLine(0).contains("[Brawler1]") && balance > bet || sign.getLine(0).contains("[Brawler1]") && balance > bet){
                       
                        if(sign.getLine(0).contains("[Brawler1]") && Brawler.games.get("match") == true && !Brawler.pbets.contains(player)){
                       
                            Brawler.economy.withdrawPlayer(name, bet);
                            Brawler.bets.put("totalbets", Brawler.bets.get("totalbets") + bet);
                            Brawler.pbets.add(player);
                            player.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You just bet $" + bet + "on" + Brawler.activebrawlers.get("player1"));
                        }else if(sign.getLine(1).contains("[Brawler2]") && Brawler.games.get("match") == true && !Brawler.pbets.contains(player)){
                       
                            Brawler.economy.withdrawPlayer(name, bet);
                            Brawler.bets.put("totalbets", Brawler.bets.get("totalbets") + bet);
                            Brawler.pbets.add(player);
                            player.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You just bet $" + bet + "on" + Brawler.activebrawlers.get("player2"));
                        }
                    }else if(sign.getLine(0).contains("[Brawler1]") && balance < bet || sign.getLine(0).contains("[Brawler2]") && balance < bet){
                       
                        player.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You need at least $" + bet + " to bet on someone!");
                    }else if(sign.getLine(0).contains("[Brawler1]") && balance < bet && Brawler.pbets.contains(player) || sign.getLine(0).contains("[Brawler2]") && balance < bet && Brawler.pbets.contains(player)){
                       
                        player.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": You can only bet once!");
                    }
                    else{
                        return;
                    }
            }
        }
       
        @EventHandler
        public void onPlayerHit(EntityDamageEvent event){
            if (event.getEntity() instanceof Player){
                Player player = (Player)event.getEntity();
               
                if(Brawler.games.get("active") == false || Brawler.games.get("active") == null){
                    if(player == Brawler.activebrawlers.get("player1") || player == Brawler.activebrawlers.get("player2")){
                        event.setCancelled(true);
                    }
                }
            }
        }
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            if(Brawler.games.get("active") == true){
                Player player = event.getEntity().getPlayer();
                   
                if(player == Brawler.activebrawlers.get("player1")){    //TODO: Divide bets and teleport player out
                    int prize = Brawler.bets.get("totalbets")/Brawler.p2bets.size();
                    int spoils = plugin.getConfig().getInt("brawler.fighters.reward");
                   
                    event.setDeathMessage(player + " Lost the brawl!");   
                    Brawler.economy.depositPlayer((Brawler.activebrawlers.get("player2").getName()), spoils);
                   
                    double x = plugin.getConfig().getDouble("brawl.exit.x");
                    double y = plugin.getConfig().getInt("brawl.exit.y");
                    double z = plugin.getConfig().getInt("brawl.exit.z");
                    float yaw = plugin.getConfig().getInt("brawl.exit.yaw");
                    float pitch = plugin.getConfig().getInt("brawl.exit.pitch");
                   
                    Player winner = Brawler.activebrawlers.get("player2");
                   
                    winner.teleport(new Location(winner.getWorld(), x, y, z, yaw, pitch));
                    winner.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Congratulations! You won the brawl and earned $" + spoils);
                   
                    for(String winners: Brawler.p2bets){
                       
                        Brawler.economy.depositPlayer(winners, plugin.getConfig().getInt("brawler.bet"));
                        Bukkit.getPlayer(winners).sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Congratulations! You gambled right and won $" +  prize + "!");
                    }
                   
                    pickPlayers.pickPlayersTask();
                   
                }else if(player == Brawler.activebrawlers.get("player2")){
                    int prize = Brawler.bets.get("totalbets")/Brawler.p1bets.size();
                    int spoils = plugin.getConfig().getInt("brawler.fighters.reward");
                   
                    event.setDeathMessage(player + " Lost the brawl!");   
                    Brawler.economy.depositPlayer((Brawler.activebrawlers.get("player1").getName()), spoils);
                   
                    double x = plugin.getConfig().getDouble("brawl.exit.x");
                    double y = plugin.getConfig().getInt("brawl.exit.y");
                    double z = plugin.getConfig().getInt("brawl.exit.z");
                    float yaw = plugin.getConfig().getInt("brawl.exit.yaw");
                    float pitch = plugin.getConfig().getInt("brawl.exit.pitch");
                   
                    Player winner = Brawler.activebrawlers.get("player1");
                   
                    winner.teleport(new Location(winner.getWorld(), x, y, z, yaw, pitch));
                    winner.sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Congratulations! You won the brawl and earned $" + spoils);
                   
                    for(String winners: Brawler.p1bets){
                       
                        Brawler.economy.depositPlayer(winners, plugin.getConfig().getInt("brawler.bet"));
                        Bukkit.getPlayer(winners).sendMessage(ChatColor.DARK_RED + "[Brawler]" + ChatColor.GREEN + ": Congratulations! You gambled right and won $" +  prize + "!");
                    }
                   
                    pickPlayers.pickPlayersTask();
                       
                }else{
                        return;
                    }
            }
        }
       
    }
     
  8. Offline

    MrZoraman

    In the onEnable() method, don't you need to put
    Code:
    getServer().getPluginManager().registerEvents(new signListener(this), this);
    getServer().getPluginManager().registerEvents(new playerListener(this), this);
    instead of
    Code:
    new signListener(this);
    new playerListener(this);
    ?
    Well thats how I do it anyways and it work well for me. I see that you already have the registerEvents(this, plugin). It might be a different way of doing it and it looks like it would work, but I do it differently and it works for me :p
     
  9. Offline

    Viperdream

    I'll try it out, but I always use new signListener(this) for all my plugins and it works fine : /

    Still haven't found an answer to my problem :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  10. Offline

    Viperdream

    I'm clueless about this : /
     
  11. Offline

    Viperdream

    And still can't find a solution
     
  12. Offline

    Firefly

    Code:
    new signListener(this);
    new playerListener(this);
    This doesn't do anything. You have to use the registerEvents method.
     
  13. Offline

    evilmidget38 Retired Staff

    [​IMG]

    Code:
     public signListener(Brawler plugin) {
           this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
  14. Offline

    Viperdream

    I managed to fix it, I googled a bit around and saw another guy had the same problem.

    I changed the name of my classes and now it works perfect. I suppose Java saw the class as a method and not as a class.

    Thanks for the help though!
     
Thread Status:
Not open for further replies.

Share This Page