[PoC] Create Custom Event

Discussion in 'Resources' started by Dark_Balor, Jul 30, 2011.

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

    Dark_Balor

    Hi,

    I just found a way to create Custom Event. When I mean Custom Event, is not using the Event.Type.CUSTOM_EVENT but by adding in this Enum a new Type, letting you create a real new Event.

    You must now that for now it's only a Proof of Concept. It's working file and after some testing, I maybe release a plugin (like BukkitContrib/Spout) to permit other plugin to add there event.

    That could be a + for any Plugin API. Letting other plugin cancel and event, change it, change the target, etc ... Everything is possible you just have to create your own Event, with the type.

    Source : https://github.com/Belphemur/CustomEvent
    jar : http://dl.dropbox.com/u/1773950/CustomEvent.jar

    They only command that is set is /testevent. It's trigger a custom event PlayerTestEvent with type PLAYER_TEST, that is catch by TestListener.
    The listener send a message to the user, and to the log.
     
  2. Offline

    IcySeal

    Interesting Dark.
     
  3. Offline

    alta189

    I find your solution a little over complicated. The way a custom listener should work is like so:

    Core.java
    Code:
    package com.alta189.customeventfun;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
    
        @Override
        public void onDisable() {
    
        }
    
        @Override
        public void onEnable() {
    
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("test")) {
                TestEvent event = new TestEvent("TestEvent");
                event.setSender(sender.toString());
                this.getServer().getPluginManager().callEvent(event);
            }
            return false;
        }
    
    }
    
    TestEvent.java
    Code:
    package com.alta189.customeventfun;
    
    import org.bukkit.event.Event;
    
    public class TestEvent extends Event{
    
        private String sender;
    
        public TestEvent(String name) {
            super(name);
        }
    
        public String getSender() {
            return sender;
        }
    
        public void setSender(String sender) {
            this.sender = sender;
        }
    
    }
    
    FunEventListener.java
    Code:
    package com.alta189.customeventfun;
    
    import org.bukkit.event.CustomEventListener;
    import org.bukkit.event.Event;
    
    public abstract class FunEventListener extends CustomEventListener {
    
        @Override
        public void onCustomEvent(Event event) {
            if (event instanceof TestEvent) {
                this.onTestEvent((TestEvent)event);
            }
        }
    
        public abstract void onTestEvent(TestEvent event);
    
    }
    
    Now how would someone use your listener.

    Here would be the listener that they would use:
    TestListener.java
    Code:
    package com.alta189.customeventfun;
    
    public class TestListener extends FunEventListener {
    
        @Override
        public void onTestEvent(TestEvent event) {
            System.out.println(event.getSender());
        }
    
    }
    and in their onEnable in their main class, they would register it with this:
    Code:
    getServer().getPluginManager().registerEvent(Type.CUSTOM_EVENT, new TestListener(), Priority.Normal, this);
     
  4. Offline

    Dark_Balor

    That's clearly a better way to do it :)
    To be true I just wanted to see if there were a way to edit Enum dynamically, and I tried on Event.
    Your method is better and I think that could make a + in plugin API.
     
  5. Offline

    alta189

    :) Its how we do it in Spout
     
  6. Offline

    Styx Reborn

    :) Its how we do it in StyxSpace
     
Thread Status:
Not open for further replies.

Share This Page