[Tutorial] Custom Events

Discussion in 'Resources' started by pokuit, Sep 14, 2013.

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

    pokuit

    Custom Events
    Custom events have been possible in bukkit for a long time. There is a few posts around showing them to be posssible but there is no "easy" tutorials... So as you can see I decided to make one
    Why Would you Need a Custom Event?
    Well first of all with plugins I see a lot of plugin developers not usingJava how its supposed to be used (by my hippo-critical standards at least). Its object orientated! Plus back onto the point its supposed to be made quite modular. What this means is that you build every thing you do into modules which fit together so even if you added another module you shouldn't have to change the ones you already have. Custom events can also be used in an api type plugin which creates interaction between multiple plugins.
    Now lets say you had a plugin which was like voxelsniper or world edit and you wanted other people to be able to access events such as when a person selects an area with a wand.
    Lets call the plugin WorldSniper (Very original)
    Creating the Custom Event class
    First You Want to create the events class
    WorldSniperWandSelectionEvent.class (open)

    Code:JAVA
    1.  
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.Cancellable;
    5. import org.bukkit.event.HandlerList;
    6. import org.bukkit.Location;
    7.  
    8. /**
    9. *
    10. * An Event that fires when a player Selects something with the World sniper wand
    11. *
    12. * @see Event
    13. *
    14. */
    15. public class WorldSniperWandSelectionEvent extends Event implements Cancellable {
    16.  
    17. private static final HandlerList handlers = new HandlerList();
    18. private boolean cancelled;
    19. String p; // Player Stored as a string (the players name) to prevent memory leak
    20. Location pos1;
    21. Location pos2;
    22.  
    23. /**
    24. * Constructor of a WorldSniperWandSelectionEvent * * Between pos1 and pos2 is the selection *
    25. * @param p - Player that is selecting Something WIth the wand * @param pos1 - The First Position
    26. * @param pos2 - The Second Position
    27. */
    28. public WorldSniperWandSelectionEvent(String p, Location pos1, Location pos2) {
    29.  
    30. this.p = p.getName();
    31. this.pos1 = pos1;
    32. this.pos2 = pos2;
    33. this.cancelled = false;
    34.  
    35. }
    36.  
    37. public Location getPos1() {
    38. return pos1;
    39. }
    40.  
    41. public Location getPos2() {
    42. return pos2;
    43. }
    44.  
    45. public String getPlayerName() {
    46. return p;
    47. }
    48.  
    49. public Player getPlayer() throws NullPointerException {
    50. return Bukkit.getServer().getPlayer(p);
    51. }
    52.  
    53. @Override
    54. public HandlerList getHandlers() {
    55.  
    56. return handlers;
    57. }
    58.  
    59. public static HandlerList getHandlerList() {
    60. return handlers;
    61. }
    62.  
    63. @Override
    64. public boolean isCancelled() {
    65. return cancelled;
    66. }
    67.  
    68. @Override
    69. public void setCancelled(boolean cancel) {
    70.  
    71. this.cancelled = cancel;
    72.  
    73. }
    74.  
    75.  
    76. }
    77.  

    If you wanted with that class you could make it so it would get all the blocks and do tons of other things but thats up to you.​
    Creating the Event Caller Method
    Ok So Now That We've done that we can create a method to call the event for example​
    Code:JAVA
    1.  
    2. public void handleWandSelection(Player p, Location loc1, Location loc2) {[/LEFT]
    3.  
    4. WorldSniperWandSelectionEvent e = new WorldSniperWandSelectionEvent(p, loc1, loc2);
    5.  
    6. //Calls The Event
    7. pl.getServer().getPluginManager().callEvent(e);
    8.  
    9. //Checks If the event was cancelled by any other plugin or in another part of this plugin
    10. if(!e.isCancelled()) {
    11. //OTHER SELECTION STUFF
    12. p.sendMessage("Sucessfully Selection");
    13. } else {
    14. p.sendMessage("Sorry Selection Not Allowed");
    15. }
    16. }
    17.  


    Creating the Listener
    And Finally we need to Have A Listener for the Event

    Code:JAVA
    1.  
    2.  
    3. public class normalListener implements Listener {
    4.  
    5. public normalListener(Plugin pl) {
    6. pl.getServer().getPluginManager().registerEvents(this,pl);
    7. }
    8.  
    9. //Demonstration - It works like normal bukkit events
    10. @EventHandler
    11. public void onPlayerJoinEvent(PlayerJoinEvent e) {
    12. e.getPlayer().sendMessage("Hi!");
    13. }
    14.  
    15. @EventHandler
    16. public void onWorldSniperWandSelectionEvent(WorldSniperWandSelectionEvent e) {
    17. if(e.getPlayerName().equalsIgnoreCase("pokuit")) {
    18. e.setCancelled(true);
    19. }
    20. }
    21. }
    22.  
    23.  


    Conclusion
    I think that covers everything. if I missed anything out or if you think I could make certain aspects of it clearer just point it out in the comments and I will change it accordingly :)
    Any Comments are appreciated​

    Reserved Comment Space

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    Phasesaber likes this.
  2. Offline

    DarkBladee12

  3. Offline

    pokuit

    Ops I never knew that exsisted :p
     
  4. Offline

    DarkBladee12

    pokuit That's the first site that shows up when I google "bukkit custom events" :D
     
  5. Offline

    MTN

    Please remove the font styles in your main post, it's hard to read.
     
Thread Status:
Not open for further replies.

Share This Page