Plugin doesn't register events.

Discussion in 'Plugin Development' started by NoFontNL, Nov 3, 2018.

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

    NoFontNL

    Hi. this code is not working for some reason. The onEnable() thing works, but the EventHandler doesn't register events. I also tried PlayerMoveEvent and PlayerToggleSneakEvent, but both didn't output any message. Also, I tried to add a command, but it reads: 'Unknown command. Type "/help" for help.'

    Code:
    package fPlugin;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    public class fPlugin extends JavaPlugin {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("test") && sender instanceof Player) {
                Player player = (Player) sender;
                player.sendMessage(ChatColor.GOLD +"you tested succesfully!");
                return true;
            }
            return false;
        }
        @EventHandler
        public void onDrop(PlayerDropItemEvent event) {
            Player player = event.getPlayer();
            event.setCancelled(true);
            player.sendMessage("Don't drop stuff.");
        }
        @Override
        public void onEnable() {
            getLogger().info("Plugin Enabled!");
        }
        @Override
        public void onDisable() {
            getLogger().info("Plugin Disabled!");
        }
    }
    
    Thanks in advance.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @NoFontNL You aren't registering the events in the onEnable
     
  3. Offline

    NoFontNL

    I hate it when my comment is under Moderator Review because no-one is going to read it. BESIDES, how do I register the event in onEnable?
    getServer().getPluginManager().registerEvents(this, this);
    What should "this" and "this" be?
     
  4. Offline

    Tommy_T0mmY

    put this inside onEnable()

    Code:
    Bukkit.getPluginManager().registerEvents(this, this);
     
  5. @NoFontNL you should also implement CommandExecutor and Listener. And you have to register the command as well as the event as timtower said
     
  6. Offline

    timtower Administrator Administrator Moderator

    Not before I approve it, patience.
    And this and this would be just fine.

    JavaPlugin handles the CommandExecutor already.
    Registering the command is only needed when it isn't the main class.
     
  7. oh okay
    but he still has to implement listener
     
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page