Listeners are Detected as Unused

Discussion in 'Plugin Development' started by dkabot, Jan 13, 2012.

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

    dkabot

    I'm adding some listeners to a plugin I'm working on, but only once in a plugin of mine has a listener not been considered unused, and I can't for the life of me find out why.

    The listener
    Code:
    PlayerListener listener = new PlayerListener() {
                public void onBlockPlace(BlockPlaceEvent event) {
                    if(event.isCancelled()) return;
                    if(listeningForPlace.contains(event.getPlayer().getName())) {
                        Persistance placeClass = getDatabase().find(Persistance.class).where().eq("creatorName", event.getPlayer()).findUnique();
                        if (placeClass == null) {
                            return;
                        }
                        placeClass.setLocation(event.getBlock().getLocation());
                        event.getPlayer().sendMessage("Location for build sign set!");
                        return;
                    }
                }
                public void onBlockBreak(BlockBreakEvent event) {
                    if (event.isCancelled()) return;
                    Persistance breakClass = getDatabase().find(Persistance.class).where().eq("location", event.getBlock().getLocation()).findUnique();
                    if(breakClass == null) {
                        return;
                    }
                    else {
                        if (event.getPlayer().isOp()) {
                            return;
                        }
                        else if (event.getPlayer().hasPermission("groupbuild.breakany")) {
                            return;
                        }
                        else if (event.getPlayer() == breakClass.getCreatorName()) {
                            return;
                        }
                        else {
                            event.getPlayer().sendMessage("You do not have permission to break this build sign.");
                            return;
                        }
                    }
                }
            };
            getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, listener, Event.Priority.Low, this);
            getServer().getPluginManager().registerEvent(Event.Type.BLOCK_BREAK, listener, Event.Priority.Low, this);
    If you need to see more code, It's on GitHub, but there are no errors. The only warnings are the unused warnings for these two.
     
  2. Offline

    tomjw64

    I don't usually set up my listeners that way, so nothing really stands out as wrong to me. However, if you know the plugin is working correctly, why not just @SuppressWarnings("unused")?
     
  3. Offline

    hammale

    i think he asked this more so out of curiosity but yeah that would work :D i normally call mine in the onEnable()...
     
  4. Offline

    obnoxint

    Which IDE are you using?
    I think this appears because you are not using a proper getter. I think, what tom was saying should be enough, because you are actually refering to the instance a few lines after the declaration/definition. I'm using inner classes for listeners, too, but inside a proper getter using lazy binding.
     
  5. Offline

    hatstand

    You're using a player listener for block events, you want a block listener.
     
  6. Also, you are declaring an inner class, so no class from outside can access custom methods of it. That's why they are considered unused even when they are "public" (your methods can't be "more public" than your class).
    If these methods would override the parent class, that would work fine, ofc, but they aren't as @hatstand pointed out ;)
     
  7. Offline

    dkabot

    I'll go back and make it a block listener, and they were already in onEnable.
    If Eclipse still doesn't like it, I will be sad.
    Also, it's untested; I have no idea if it works.

    EDIT: Well, making it a block listener fixed the unused warnings. I'll have to make note of that one.
     
  8. Offline

    RROD

    You need to register a new PlayerListener to a class that you then handle.
    Registering BlockListener in onEnable():
    Code:
        public void onEnable() {
            MyBlockListener bl = new MyBlockListner();
            getServer().getPluginManager().registerEvent(Type.BLOCK_PLACE, bl, Priority.Normal, this);
        }
    Create a new Class that extends BlockListener. In my case I called it MyBlockListner.

    Code:
    package com.dkabot.GroupBuild;
    
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class MyBlockListener extends BlockListener {
    
        public void onBlockPlace(BlockPlaceEvent event) {
            //TODO: onBlockPlace Code here.
        }
    
        public void onBlockBreak(BlockPlaceEvent event) {
            //TODO: onBlockPlace Code here.
        }
    
    }
    
     
  9. Offline

    dkabot

    I never created a new class for listeners, I just register then in onEnable() directly and it works fine (after changing it to a block listener rather than a player listener). Might look into it for organization's sake, but it's not broken.
     
  10. Offline

    RROD

    It's the best practice because you're keeping the code clean. I do it this way every time I need listeners. Plus if you keep it away from the main class it stops confusion.

    So yeah, organisation.
     
    tomjw64 likes this.
Thread Status:
Not open for further replies.

Share This Page