Solved Events not working???

Discussion in 'Plugin Development' started by vhbob, Jun 30, 2015.

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

    vhbob

    Hey, so my problem is that my events wont work, none of them will. heres my code, what am i doing wrong???
    Code:
    package me.vhbob.epic;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
    
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        public void login(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if (!getConfig().contains(p.getName() + ".tokens")) {
                getConfig().set(p.getName() + ".tokens", 0);
                saveConfig();
            }
        }
    
        public void GetCoinFromBlock(BlockBreakEvent e) {
            Player p = e.getPlayer();
            int i = getConfig().getInt(p.getName() + ".tokens");
            if (e.getBlock().getType().equals(Material.COAL)) {
                p.sendMessage(ChatColor.GRAY + "You've just earned "
                        + ChatColor.GREEN + "1" + ChatColor.GRAY + "coin!");
                getConfig().set(p.getName() + ".tokens", i + 1);
                saveConfig();
            }
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            // Coin stuff here
            if (cmd.getName().equalsIgnoreCase("Coins")) {
                Player p = (Player) sender;
                int i = getConfig().getInt(p.getName() + ".tokens");
                p.sendMessage(ChatColor.GRAY + "You have " + ChatColor.GREEN + i + ChatColor.GRAY + " tokens");
            }
            return true;
        }
    
    }
     
  2. You forgot to add @EventHandler above each method. Also, please use camelCasing and not AllCapsCasing. E.g.
    Code:
    public void GetCoinFromBlock(BlockBreakEvent e) {
    
    Should be:
    Code:
    @EventHandler
    public void onBlockBreak(BlockBreakEvent e) {
    
    Or if you really want to keep the same name, getCoinFromBlock.

    You also should check if the sender is a player in onCommand, if you haven't already.
    E.g.
    Code:
    if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "You must be a player to use that command.");
        return true;
    }
    
     
    The_BloodHound likes this.
  3. Offline

    vhbob

Thread Status:
Not open for further replies.

Share This Page