Example of custom events

Discussion in 'Resources' started by iffa, Aug 19, 2011.

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

    iffa

    Create a class for the event e.g. 'MyCustomEvent':
    Code:java
    1.  
    2. package me.iffa.bananaspace;
    3.  
    4. import org.bukkit.event.Cancellable;
    5. import org.bukkit.event.Event;
    6.  
    7. public class MyCustomEvent extends Event implements Cancellable {
    8. private String text;
    9. private boolean cancelled;
    10.  
    11. public MyCustomEvent(String event, String text) {
    12. super(event);
    13. this.text = text;
    14. }
    15.  
    16. public String getText() {
    17. return this.text;
    18. }
    19.  
    20. public void setText(String text) {
    21. this.text = text;
    22. }
    23.  
    24. public boolean isCancelled() {
    25. return cancelled;
    26. }
    27.  
    28. public void setCancelled(boolean bln) {
    29. this.cancelled = bln;
    30. }
    31.  
    32. }
    33.  

    Once you have that you can make a listener class for it:
    Code:java
    1.  
    2. package me.iffa.bananaspace;
    3.  
    4. import org.bukkit.event.CustomEventListener;
    5. import org.bukkit.event.Event;
    6. import org.bukkit.event.Listener;
    7.  
    8. public class MyCustomEventListener extends CustomEventListener implements Listener {
    9. /**
    10.   * This is called when your custom event is triggered.
    11.   *
    12.   * @param event Event data
    13.   */
    14. public void onMyCustomEvent(MyCustomEvent event) {
    15. }
    16.  
    17. /**
    18.   * This is required for your events to be triggered correctly.
    19.   *
    20.   * @param event Event data
    21.   */
    22. @Override
    23. public void onCustomEvent(Event event) {
    24. if (event instanceof MyCustomEvent) {
    25. onMyCustomEvent((MyCustomEvent) event);
    26. }
    27. // you can continue this with else if's as long as you like (if you have many events)
    28. }
    29. }
    30.  

    As you can see, making the listener and events is pretty easy. Now to calling your custom event from your code (an example of a main class that calls the event everytime it disables and enables):
    Code:java
    1.  
    2. package me.iffa.bananaspace;
    3.  
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class MyPlugin extends JavaPlugin {
    7.  
    8. @Override
    9. public void onDisable() {
    10. String text = "disabled";
    11. /* Event calling start */
    12. MyCustomEvent event = new MyCustomEvent("MyCustomEvent", text);
    13. getServer().getPluginManager().callEvent(event);
    14. // Example of cancelling the actions after this if the event was cancelled.
    15. if (event.isCancelled()) {
    16. return;
    17. }
    18. /* Event calling end */
    19. System.out.println(event.getText());
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. String text = "enabled";
    25. /* Event calling start */
    26. MyCustomEvent event = new MyCustomEvent("MyCustomEvent", text);
    27. getServer().getPluginManager().callEvent(event);
    28. // Example of cancelling the actions after this if the event was cancelled.
    29. if (event.isCancelled()) {
    30. return;
    31. }
    32. /* Event calling end */
    33. System.out.println(event.getText());
    34. }
    35. }
    36.  
     
  2. Offline

    feildmaster Retired Staff

    this is a great basic example for how to make custom events. ^^
     
  3. Offline

    RPFeltz

    I agree with above.
     
  4. Offline

    codename_B

    I'll be implementing these into Pinapp - nice one!
     
  5. Offline

    Lolmewn Retired Staff

    So, is there any need in making it fire when the plugin is launched, or can I just fire it whenever I want?
     
  6. Offline

    iffa

    You can fire it whenever you want.
     
  7. Offline

    tips48

    I don't think your custom event listener has to implement listener :confused:
     
  8. Offline

    iffa

    I learnt that from Spout.
     
  9. Offline

    Lolmewn Retired Staff

    is it needed?
     
  10. Offline

    tips48

    NO! :D
     
  11. Offline

    md_5

    Finally a simple custom event example
     
  12. Offline

    iffa

    Glad it helped.
     
  13. Offline

    md_5

    lol havent actually tried it. Just good to know its there :)
     
    Live2Pwn likes this.
Thread Status:
Not open for further replies.

Share This Page