Two events in one class?

Discussion in 'Spigot Plugin Help' started by szymek1223, Jul 13, 2021.

  1. Can i make two events in one class ? If yes, how?
     
  2. This is possible.

    Code (Text):
    @EventHandler
    public void test1(BlockPlaceEvent e) {
       // your code
    }

    @EventHandler
    public void test2(BlockBreakEvent e) {
      // your code
    }
     
  3. And don't forget to make the class "implements Listener" :D
     
  4. No you can't. Custom events have to be their own class. You can however put multiple classes in one source file.

    For example:
    Code (Java):
    //file: mypackage/Events.java

    package mypackage;

    import org.bukkit.event.*;

    class FirstEvent extends Event {
     
        private static final HandlerList handlers = new HandlerList();
     
     
        @Override
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
     
    }

    class SecondEvent extends Event {
     
        private static final HandlerList handlers = new HandlerList();
     
     
        @Override
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
     
    }
    The java compiler lets you put multiple classes in one file at the top level. The only restriction is that if your class is public, it must have the same name as the file. If you want multiple public classes in one file then you should use nested classes.
     
    #4 Jannyboy11, Jul 13, 2021
    Last edited: Jul 13, 2021
  5.  
    • Useful Useful x 1