Event not activating

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 14, 2016.

  1. My event is not activating and I can't see why.
    Code (Java):

    package Teleport;

    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import Teleport.Commands.setLoc;
    import Teleport.Listeners.BlockBreak;

    public class Main extends JavaPlugin{
        public static Main plugin;
        Manager m = new Manager(this);
        public void onEnable(){
            getCommand("setloc").setExecutor(new setLoc(this));
            new Manager(this);
            new BlockBreak(this);
        public void registerEvents(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new BlockBreak(this), this);
    Code (Java):

    package Teleport.Listeners;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;

    import Teleport.Main;
    import Teleport.Manager;

    public class BlockBreak implements Listener{
        private Main plugin;
        public BlockBreak(Main plugin){
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
               this.plugin = plugin;
        public void onBlockBreak(BlockBreakEvent event){
            Manager m = new Manager(plugin);
            FileConfiguration fc = m.getConfig();
            Block b = event.getBlock();
              if(b.toString() == m.getConfig().getString("Block")){
                  Location loc = b.getLocation();
                  double X = loc.getX();
                  double Y = loc.getY();
                    double Z = loc.getZ();
                      if(X == fc.getDouble("Block.X") || Y == fc.getDouble("Block.Y")
                              || Z == fc.getDouble("Block.Z")){
                      for(Player player : Bukkit.getOnlinePlayers()){
              event.getPlayer().sendMessage("Wrong block mate...");
  2. @EventHandler annotation
    • Agree Agree x 1
  3. Why does everyone think that does something? I have never actually seen it do anything in any code yet everyone tells me to use it. Am i missing something except that it can use priority?
    Its still doing the exact same thing
  4. .... Sorry if i sounded rude earlier but everyone always says it and I see no results
    Also its still not working
  5. You're registering the events for the listener class in the listener class... you have the register it in the main class.
  6. When you register a listener, bukkit looks for methods annotated with EventHandler and calls the method when the event parameter (PlayerMoveEvent for example) is called... You do need it and nothing will happen without it.
  7. Ok, so I did some testing and it is in fact activating, but its like nothing is happening.
  8. Huh? His code above receives reference to Main thru the
    BlockBreak constructor, so that isn't the problem.
    • Funny Funny x 1
  9. Events never work without that annotation because the server has no
    idea that it should be listening for that event to occur, because when you
    do not have that annotation, you aren't telling the server to pay attention
    for the event.
  10. Also try putting a Bukkit.broadcastMessage("TEST! :D");

    In there to see if the event is firing, because once you have the
    annotation above the event code and the events registered, it
    will fire, but maybe your code inside the event is not written correctly
    and therefore doesn't produce any result.
  11. You didn't REGISTER IT. You created a public void to do it, but you didn't run the public void.
  12. You have to register the event in your onEnable(); You made the method but never registered it in the onEnable(); Just like you registered the command, but instead of getting plugin manager and all that, just put registerEvents(); in the onEnable()
    • Agree Agree x 1
  13. Oh "Package"
    Yeah, the name should be qualified.

    ie: me.someshit.nameofplugin ... or whatever.

    Detailed qualified package names gives anyone (if applicable)
    who would be maintaining your code a very good idea
    of what type of code and objects may be found at certain
    areas of your software.

    Without proper documentation, consider your project dead once
    you get tired of working on it, because no one else will have
    any idea on how to resume work on it.
  14. Yes they did, they registered the events here:

    public BlockBreak(Main plugin){
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    this.plugin = plugin;

    The missing @ Evenhandler annotation above the blockBreakEvent is the problem here,
    nothing else - thread unwatched -_-

    Mark this as solved, or consult StackOverFlow
    if you somehow still can figure it out. ;)