Example Plugin

Discussion in 'Plugin Development' started by djmaster329, Jan 27, 2012.

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

    djmaster329

    Hello,
    I'm completely new to programming and Bukkit server plugins.
    My question:
    Could someone make an example plugin?
    Just with every required class?
    Because that will help me learn to code it :)

    Thanks :)
     
  2. Offline

    masteroftime

  3. Offline

    djmaster329

    Yeah, I tried that. I don't know how to register events and stuff.
    That's why I asked if someone could help me ;)
     
  4. Offline

    masteroftime

    Ok I'll give you a quick example with many comments:

    Code:java
    1.  
    2. /*
    3.  * extends pugin means that it is a plugin
    4.  * implements Listener declares this class as an event listener. It makes sense to put listeners for
    5.  * different stuff into different classes but this is just a simple example
    6.  */
    7. public class Example extends Plugin implements Listener {
    8. public void onEnable() {
    9. //this method is called once your plugin is enabled
    10.  
    11. //this gets the plugin manager for our plugin
    12. PluginManager pm = this.getServer().getPluginManager();
    13.  
    14. /*
    15.   * this registers our listener. The first argument is the listener the second the plugin
    16.   * if you put the listener into a different class you have to put an istance of that class
    17.   * as the first argument
    18.   */
    19. pm.registerListeners(this, this)
    20. }
    21.  
    22. public void onDisable() {
    23. //this method is called once your plugin is diabled you can save stuff or clean up here
    24. }
    25.  
    26. /*
    27.   * The on command method is called every time a command which is registered for your plugin is called
    28.   * @sender: this is the sender of the command. Can be a player or the command line
    29.   * [USER=55020]CMD[/USER]: a command object. Look into the API docs for what you can do with it
    30.   * @commandLabel: a string of which command has been issued. Useful if you have more than one command
    31.   * @args: an array of arguments supplied to the command
    32.   */
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    34. if(cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
    35. //doSomething
    36. return true;
    37. } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
    38. //you should always return true if the command has been processed by your plugin and false if not
    39. return false;
    40. }
    41.  
    42. /*
    43.   * this is an event hanldler event handlers have to be in classes which implement Listener
    44.   * the @EventHandler annotation marks this method as an event handler
    45.   * the method name can be anything you like
    46.   * event handler have one parameter which also defines which event they handle
    47.   */
    48. @EventHandler
    49. public void loginHandler(PlayerLoginEvent event) {
    50. /*
    51.   * this method is called every time a player logs in you can find out more about the event
    52.   * via the given parameter
    53.   * e.g. we can determine which player logged in and send him a welcome message
    54.   */
    55. event.getPlayer().sendMessage("Welcome to the sever!");
    56. }
    57.  
    58. /*
    59.   * This is another event handler with high priority. So if there are other handlers for the same
    60.   * event this one will be called first
    61.   */
    62. @EventHandler(Priority = EventPriority.HIGH)
    63. public void tntWatchdog(BlockPlacedEvent event) {
    64. /*
    65.   * Here we check if the placed block is of tnt. And if it is we cancel the event
    66.   */
    67. if(event.getBlock().getType() == Material.TNT) {
    68. event.setCanceled(true);
    69. event.getPlayer().sendMessage("How dare you destroy my server!!!");
    70. }
    71. }
    72. }
    73.  
     
  5. Offline

    djmaster329

    Sorry it took so long to reply,

    I tried to add the code into my project.
    Code:
    package me.djmaster329.ServerInfo;
     
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
     
    /*
    * extends pugin means that it is a plugin
    * implements Listener declares this class as an event listener. It makes sense to put listeners for
    * different stuff into different classes but this is just a simple example
    */
    public class ServerInfo extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            //this method is called once your plugin is enabled
     
            //this gets the plugin manager for our plugin
            PluginManager pm = this.getServer().getPluginManager();
     
            /*
            * this registers our listener. The first argument is the listener the second the plugin
            * if you put the listener into a different class you have to put an istance of that class
            * as the first argument
            */
            pm.registerListeners(this, this);
        }
     
        public void onDisable() {
            //this method is called once your plugin is diabled you can save stuff or clean up here
        }
     
        /*
        * The on command method is called every time a command which is registered for your plugin is called
        * @sender: this is the sender of the command. Can be a player or the command line
        * [USER=55020]CMD[/USER]: a command object. Look into the API docs for what you can do with it
        * @commandLabel: a string of which command has been issued. Useful if you have more than one command
        * @args: an array of arguments supplied to the command
        */
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
                //doSomething
                return true;
            } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            //you should always return true if the command has been processed by your plugin and false if not
            return false;
        }
     
        /*
        * this is an event hanldler event handlers have to be in classes which implement Listener
        * the @EventHandler annotation marks this method as an event handler
        * the method name can be anything you like
        * event handler have one parameter which also defines which event they handle
        */
        @EventHandler
        public void loginHandler(PlayerLoginEvent event) {
            /*
            * this method is called every time a player logs in you can find out more about the event
            * via the given parameter
            * e.g. we can determine which player logged in and send him a welcome message
            */
            event.getPlayer().sendMessage("Welcome to the sever!");
        }
     
        /*
        * This is another event handler with high priority. So if there are other handlers for the same
        * event this one will be called first
        */
        @EventHandler(Priority = EventPriority.HIGH)
        public void tntWatchdog(BlockPlacedEvent event) {
            /*
            * Here we check if the placed block is of tnt. And if it is we cancel the event
            */
            if(event.getBlock().getType() == Material.TNT) {
                event.setCanceled(true);
                event.getPlayer().sendMessage("How dare you destroy my server!!!");
            }
        }
    }
     
    
    and this is the error:
    [​IMG]
    What did I do wrong? :p
     
  6. Offline

    kumpelblase2

    Looks like you didn't added the bukkit api...
     
  7. Offline

    obnoxint

  8. Offline

    RizzelDazz

    You need to focus on learning java first before attempting the bukkit API. Skipping that will just end up with you asking us for help for everything. Try a book on java, but trust me you need to learn java before attempting to code with bukkit.
     
  9. Offline

    Assist

    RizzelDazz
    This thread is about 2 years old..
     
    almazio1, Chinwe and MOMOTHEREAL like this.
  10. Offline

    RizzelDazz

    Wow what the heck, I must of searched something and had it open on my iPhone, wow my bad.
     
Thread Status:
Not open for further replies.

Share This Page