Error on "new BlockListener (this);"

Discussion in 'Plugin Development' started by Wind1000100, Oct 3, 2015.

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

    Wind1000100

    Hello Bukkit community,

    I have reviewed this code for hours now. There are no errors in the code which Eclipse spots and the code appears the same as in the tutorial I watched (learning plugin development right now). Every time I load the server, I am presented with this error:
    Console error report (open)

    [​IMG]

    It first states there is an error on line 19, which you can see is the code which runs the other class.

    Class 1 (open)

    Named AntiBlock
    Code:
    package me.Wind1000100;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class AntiBlock extends JavaPlugin {
     
        public Permission playerPermission = new Permission("playerAbilities.allowed");
    
        @Override
        public void onEnable() {
            getLogger().info("AntiBlock loaded!");
            new BlockListener (this);
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(playerPermission);
        }
     
        @Override
        public void onDisable() {
         
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("givemeitems") && sender instanceof Player) {
                Player player = (Player) sender;
             
                if (player.hasPermission("playerAbilities.allowed")) {
                    player.setItemInHand(new ItemStack(Material.DIAMOND_AXE));
                }else{
                    player.sendMessage("§4You don't have permission for this command.");
                }
             
                return true;
            }
            return false;
        }
    
    }
    


    Class 2 (open)

    Named BlockListener
    Code:
    package me.Wind1000100;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class BlockListener implements Listener {
    
        public BlockListener(AntiBlock plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            Player player = e.getPlayer();
         
            if (e.getBlock().getType() == Material.BEDROCK) {
                if (!player.hasPermission("playerAbilities.allowed")) {
                    player.sendMessage("§cYou cannot place " + e.getBlock().getType().toString() + ".");
                    e.setCancelled(true);
                }else{
                    player.sendMessage("§aYou can place " + e.getBlock().getType().toString() + ".");
                }
            }
         
            if (e.getBlock().getType() == Material.GLOWSTONE) {
                Location location = e.getBlock().getLocation();
             
                if (new Location(location.getWorld(), location.getX(), location.getY() - 1, location.getZ()).getBlock().getType() == Material.GRASS) {             
                    e.setCancelled(true);
                    player.sendMessage("§cYou cannot place glowstone on grass.");
                }else{
                    // Do nothing.
                }
            }
    
        }
     
    }
    


    Plugin.yml (open)

    name: AntiBlock
    main: me.Wind1000100.AntiBlock
    version: 1.0
    commands:
    givemeitems:
    description: Gives player a diamond axe.
    usage: /givemeitems


    No errors appear in Eclipse or during exporting. :(

    It's probably something really simple, but I've looked through it many times - note the AntiBlock class still runs correctly for the most part, as the /givemeitems command work; it just doesn't run BlockListener.
     
  2. Offline

    RoboticPlayer

    A) Change your package name a bit. Should be all lowercase, and add a unique identifier to that plugin (such as the project name)
    B) Don't use the § symbol, instead use the ChatColor enum
    C) Useless onDisable, you can remove it
    D) onCommand can be overridden
    E) What will you do if the sender isn't a player? You checked, but you didn't handle it
    F) Returning false says that the command was not handled, and will return the usage from the plugin.yml
    G) I would change how you are registering your events. If you are using methods or fields from your main class, add a constructor like this:
    Code:
    private AntiBlock plugin;
    
    public BlockListener(AntiBlock plugin) {
        this.plugin = plugin;
    }
    Otherwise, you don't need a constructor. Instead, just register your event in your main class's onEnable:
    Code:
    Bukkit.getServer().getPluginManager().registerEvents(new BlockListener(), this);
    // If you are using a constructor, change new BlockListener() to new BlockListener(this)
     
    Wind1000100 likes this.
  3. Offline

    mythbusterma

    @Wind1000100

    It looks like you're not JARing the file correctly. That's certainly an odd error.
     
  4. Offline

    Wind1000100

    Thanks, I'll see if I can fix the issue using the code you provided.
    Also, why is a section sign not recommended for colour codes? It works fine for all my other testing plugins, and is much quicker. ChatColor.<colour> also works, but I'm not sure why it is best.
     
  5. Offline

    RoboticPlayer

    @Wind1000100 It's just safer and more stable. If the color codes were ever to change, all of your plugins would have messed up colors.
     
Thread Status:
Not open for further replies.

Share This Page