Event listener not registering inside the onCommand() method

Discussion in 'Spigot Plugin Development' started by itsAdoozy, Oct 12, 2019.

  1. I'm writing my first plugin and am trying to create a command that gets the location of where the player right clicks after issuing the command. Here's my current Java code (with Eclipse):

    Code (Java):
    package io.github.ryantcameron.buildingBlocks;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.block.Action;
    import org.bukkit.Location;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;

    public final class buildingBlocks extends JavaPlugin implements Listener{
        public void onEnable() {
            getLogger().info("Enabling plugin: buildingBlocks");
        public void onDisable() {
            getLogger().info("Safely disabling plugin: buildingBlocks");
        public Location getClickLoc(PlayerInteractEvent event, CommandSender sender) {
            // IF the player right clicks on any block or air
            if ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))) {
                // Get the x,y,z position of this block. The height will be used later on.
                Location loc = event.getClickedBlock().getLocation(); //done in [x,y,z] format
                sender.sendMessage("It Worked");
                return loc;
            return null;
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args, PlayerInteractEvent event) {
            if (cmd.getName().equals("save")) {
                //Call the class and method that listens for the next 4 clicks
                Location loc1 = getClickLoc(event, sender);
            return true;

    And here's my yml but I don't think that's the problem:

    Code (YAML):
    name: buildingBlocks
    : io.github.ryantcameron.buildingBlocks.buildingBlocks
    : 0.1
    : This saves a square block of the users choosing
    : /save [height]
    : buildingBlocks.save
    When I run it in server I can issue the command but nothing happens. Can someone help me out understanding how to use events and event listeners?
  2. Firstly, you need to register your listener with PluginManager#registerEvents.

    Secondly, your event handler can only have one argument, which is the one that extends Event.
    There is no CommandSender attached to an event. Some events have a #getPlayer method, others have a #getEntity method, some have neither.

    I would suggest you take a look at the Using the Event API part of the wiki.

    You don't seem to understand what you're doing with commands either. Take a look at this wiki article.
    You need to override the exact method of public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args).
    • Agree Agree x 1
  3. Got it. I think I just don't understand where to put the Listener in the scheme of the project, is it a different file? I've read all the documentation I can find and I can't figure it out. Also, how do I get the listener to interact with a command?
  4. A Listener is just a class, it can be in a separate file, a child of an existing class, or even the plugin class itself. As long as you know how to setup and register the Listener class properly, it will just work. (Learn Java first!)
  5. Listeners and commands are two very different things. I'm not sure how you expect one to "interact" with the other.
    It seems like you've either missed something when reading through the documentation or you simply do not know enough of the basics to understand what they say.
  6. Strahan


    As previously said, the two have nothing to do with each other. You need some intermediary to bridge them. Like you could do List<UUID> playersMarking = new ArrayList<UUID>(); then in your command code, you add the player's UUID to the list. Then in the interact event, you first do if (!playersMarking.contains(event.getPlayer().getUniqueId())) return; then the rest of the code in the event will only be ran if the player had entered the command to mark. Then you just do whatever you intend to do with the data. Don't forget to remove them from the List at the end.