Very First Plugin

Discussion in 'Plugin Development' started by tyler53, Aug 1, 2011.

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

    tyler53

    I'm trying to create my very first plugin but i have no idea how. Are there any websites or videos or any kind of tutorials i can see to get me started?
     
  2. Offline

    8thDimension

    Google is your friend :)

    Anyways I started off with Adamk11s's "HUGE Plugin Tutorial"
    which after that, I kinda learned on my own with the occasional help from google.
     
  3. Offline

    tyler53

    Do you have the link to that tutorial?
     
  4. :) @tyler53 My signature or click on the documentation link and it's on the home page.
     
  5. Offline

    tyler53

    thanks so much :)

    Code:
    package me.tyler.stone_item_spawn;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    class Stone_Item_Spawn extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
            log.info("Your plugin has been enabled!");
            PluginManager pm = this.getServer().getPluginManager();
        }
    
        public void onDisable(){
            log.info("Your plugin has been disabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("stone")){ // If the player typed /stone then do the following...
                give [player] 10 5
                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.
            return false;
    }


    This is just my very first plugin, so that when you type /stone you get 10 stone in your inventory
    I got most of this down, now i just have 2 things that went wrong:
    where it says give [player] 10 5 i dont know what to put here to make it so that it actually gives them 10 stone

    and also i dont know how to export this whole thing as a .jar file so i can try it in my server

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  6. Offline

    lionking23

    isn't stone id #1
    :confused:
     
  7. Offline

    8thDimension

    You can use the following piece of code to give your player 10 blocks of stone:

    Code:
    Player player = (Player)sender
    player.getInventory().addItem(new ItemStack(1, 10)) 
    Also, if you're using Eclipse, to export your project as a jar to try out on your server do: File -> Export -> JAR File
     
  8. Offline

    tyler53

    ok, but where do i add that in?

    it doesnt work >_>

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  9. Offline

    cholo71796

    Add it where you check if the player's command was /stone.
     
  10. Offline

    8thDimension

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("stone")){ // If the player typed /stone then do the following...
                player.getInventory().addItem(new ItemStack(1, 10));
                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.
            return false;
    }
    
    You probably didn't add the semi-colons at the end which I forgot to add :p

    Something like this should work

    Don't forget to import (Ctrl + Shift + O is your friend)
     
  11. Offline

    tyler53

    no i added that, it just says that the server doesnt recognize the command

    Code:
    package me.tyler.stone_item_spawn;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    class Stone_Item_Spawn extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
            log.info("Your plugin has been enabled!");
            PluginManager pm = this.getServer().getPluginManager();
        }
    
        public void onDisable(){
            log.info("Your plugin has been disabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
               Player player = (Player)sender;
                if(cmd.getName().equalsIgnoreCase("stone")){ // If the player typed /stone then do the following...
                    player.getInventory().addItem(new ItemStack(1, 10));
                    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.
                return false;
        }    }
    on the line where i have pm = this.getServer() it says that the "value of the local variable pm is not used"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  12. Offline

    8thDimension

    1) Did you add the command to your plugin.yml?

    2) Get rid of the PluginManager pm stuff. You don't need it since you don't need any listeners and etc.
     
  13. Offline

    tyler53

    Yes i added the command to the plugin.yml
    Code:
    name: <StoneItemSpawn>
    main: <me.tyler.stone_item_spawn>.<Stone_Item_Spawn>
    version: <1.0>
    commands:
       stone:
          description: This gives a player 10 stone.
          permission: <stoneitemspawn>.stone
          usage: |
               /<command> [player]
    Its probably wrong though, (its my first plugin :))
     
  14. Offline

    8thDimension

    Code:
    name: StoneItemSpawn
    main: me.tyler.stone_item_spawn.Stone_Item_Spawn
    version: 1.0
    commands:
       stone:
          description: This gives a player 10 stone.
          permission: stoneitemspawn.stone
          usage: /stone [player]
    Try this...
    Get rid of those "<" and ">"
    You don't need them.
     
  15. Offline

    CrossBread216

    @tyler53 (I'm a new plugin dev too and I'm trying to explain why things work the way they do, so bare with me if this is too detailed.) The code in your onCommand method will never run. Although many things while programming seem to "auto-magically" work, there's always something solid going on the background. The reason onCommand will never run is because nothing in your code is calling it and although there are advanced ways of finding this method (reflection) bukkit doesn't use that and doesn't know about your method. There is a mechanism in bukkit to allow you to tell it that you have code you want it to know about and run when it gets a command from a user.

    Set the executor for a command
    The way you'll want to do this eventually is to have a separate class that is responsible for handling/executing each command. Each class should have one role. That would mean adding a line like below in your Stone_Item_Spawn class. (Just a note, the java convention is for classes to begin with capitals and use camel case, so your class name would be StoneItemSpawn. Totally up to you though, but it will make your code easier for other devs to read.) The setExecutor method takes an object of type CommandExecutor. CommandExecutor is actually an interface, so whatever class you pass in needs to implement CommandExecutor. So, In the following code StoneCommandHandler would need to implement CommandExecutor.
    Code:
    
        public void onEnable(){
            // Register Commands
    	getCommand("stone").setExecutor(new StoneCommandHandler(this));
            log.info("Your plugin has been enabled!");
        }
    
    For now, you can use the same class that the rest of you plugin is in as a command executor by making it implement CommandExecutor which adds a contract to the class that says it will have an onCommand method with specific parameters (that happen to match what you already have.) So like this:

    Code:
    ...
    class Stone_Item_Spawn extends JavaPlugin implements CommandExecutor{
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
            log.info("Your plugin has been enabled!");
            getCommand("stone").setExecutor(this);
        }
    ...
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            // Do your command stuff
        }
    }
    
    This will let bukkit know that when it sees someone type "/stone" it should take the object you gave it (this) and make the call to this's onCommand method which is guaranteed to be there because Stone_Item_Spawn implements CommandExecutor and this is a Stone_Item_Spawn object.
     
  16. Offline

    IcySeal

    All of the command executor stuff is completely unnecessary as JavaPlugin is already a CommandExector
     
  17. Offline

    tyler53

    im getting a TON of errors
    Code:
    package me.tyler.stone_item_spawn;
    
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    class Stone_Item_Spawn extends JavaPlugin [B][COLOR=void rgb(255, 0, 0)]implements CommandExecutor[/COLOR][/B]{ XXXXXXX1
    }
        Logger log = Logger.getLogger("Minecraft"); XXXXXXX2
    
        public void onEnable(){
            log.info("Your plugin has been enabled!"); XXXXXXX3
            [B][COLOR=rgb(255, 0, 0)]getCommand("stone").setExecutor(this);[/COLOR][/B] XXXXXX4
    	public void onDisable(){
    		log.info("Your plugin has been disabled."); XXXXXX5
    	}  XXXXXX6
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    	       Player player = (Player)sender;
    	        if(cmd.getName().equalsIgnoreCase("stone")){ // If the player typed /stone then do the following...
    	            player.getInventory().addItem(new ItemStack(1, 10));
    	            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.
    	        return false;
    	}	}
        }    }
    this is now my code, i have errors on the lines with XX's after them "they are not in the actual code"

    XXXXX1: -syntax error on these tokens, delete these tokens (two of those on this line)
    XXXXX2: -syntax error on tokens, AnnotationName expected instead, -syntax error insert ")" to complete MethodDecleration, -syntax error insert "Type VariableDeclaratorId" to complete FormalParamaterList
    XXXXX3: -syntax error on token ";" expression expected after this token, -log cannot be resolved
    XXXXX4: -syntax error, insert "}" to complete MethodBody, -Syntax error, insert ";" to complete statement, -Syntax error, insert ";" to complete statement, -Syntax error, insert "AssignmentOperator Expression" to complete Assignment
    XXXXX5: -log cannot be resolved, -Line breakpoint:Stone_Item_Spawn [line:20] - onDisable()
    XXXXX6: -syntax error on token "}" delete this token (i think i get this one, but i thought they were needed to close method bodies or whatever

    Sorry long post this time but it gets more and more confusing
     
  18. Offline

    CrossBread216

    Ahh, JavaPlugin implements Plugin which extends CommandExecutor. Good to know. So if you're jamming all of your command executing code into your plugin class, you're good. If you're going to split it out, then implementing CommandExecutor is useful.
     
  19. Offline

    tyler53

    so what do i do.. im getting about 14 errors
     
  20. Offline

    zhuowei

    First, check your brackets to make sure they match up. I'm sure that there's an extra closing bracket near the first error.
    Also, the [ b ] [ color ] tags before implements and after it aren't in your real code, right?
     
  21. Offline

    CrossBread216

    @tyler53 dang, you grabbed that fast, my edit removed a lot of the stuff causing you problems. I had originally tried to highlight the changes in red and bold them. I didn't realize the code block would take the formatting and interpret it as actual characters. All of the junk that looks like this:
    Code:
    [B][COLOR=rgb(255, 0, 0)]
    shouldn't be there. See my edit.

    As @IcySeal said though, you don't actually need to implement CommandExecutor since JavaPlugin implements Plugin, and Plugin extends CommandExecutor. I didn't realize this before and thought that implementing CommandExecutor might solve the issue you were running into. Sorry.
     
  22. Offline

    tyler53

    they were, i had copied and pasted from somones earlier post, anyways i updated it to
    Code:
    package me.tyler.stone_item_spawn;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    class Stone_Item_Spawn extends JavaPlugin{
     
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
            log.info("Your plugin has been enabled!");}
    
            public void onDisable(){
            log.info("Your plugin has been disabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
               Player player = (Player)sender;
                if(cmd.getName().equalsIgnoreCase("stone")){ // If the player typed /stone then do the following...
                    player.getInventory().addItem(new ItemStack(1, 10));
                    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.
                return false;
        }    }
    And i get no errors now, but it still doesnt recognize the command in game. the plugin.yml is
    Code:
    name: StoneItemSpawn
    main: me.tyler.stone_item_spawn.Stone_Item_Spawn
    version: 1.0
    commands:
       stone:
          description: This gives a player 10 stone.
          permission: stoneitemspawn.stone
          usage: /stone [player]
     
  23. Offline

    zhuowei

    Did your plugin actually load in the server? Are you seeing the "Your plugin has been enabled" message on the server console?
     
  24. Offline

    tyler53

    no i dont see it, how do i fix that?
     
  25. Offline

    zhuowei

    First, make sure to add plugin.yml to your .jar file, according to the instructions at the HUGE plugin tutorial. Then make sure that your plugin's jar file is placed into the plugins folder on your server. Finally, make sure that you are running the latest CraftBukkit server.
     
  26. Offline

    tyler53

    the latesst craftbukkit server is for the 1.7.2 patch correct?
     
  27. Offline

    zhuowei

    1.7.3 actually.
     
  28. Offline

    tyler53

    hmmm... odd
    i dont see the 17.3 update "im using servercraft server hosting"
     
  29. Offline

    zhuowei

    The download at Bukkit's homepage states that build #1000 is compatible with 1.7.3. Your plugin should be able to run on 1.7.2, however.
     
  30. Offline

    Tster

    Heres your problem:
    Code:
    name: StoneItemSpawn
    main: me.tyler.stone_item_spawn.Stone_Item_Spawn
    version: 1.0
    commands:
       stone:
          description: This gives a player 10 stone.
          permission: stoneitemspawn.stone
          usage: /stone [player]
    How to fix it,
    Firstly let me point out, that java is a bloody thing for syntax a captial letter in the wring place can permenantly fuck up the code, but if I, a 12 year old, can do it, you can!
    Firstly:
    name: (enter the exact name of your plugin, what is your class file called, e.g Exactname_OFPlugin, you can read the name of of the left side, in the package explorer)
    Secondly:

    Code:
    package me.tyler.stone_item_spawn;
    main: (enter the exact direction to the main class, so go to the very first line of your code, and copy the bit that says, me.tyler etc., then to the end of that, place a ., and the the name of the plugin so: me.tyler.stone_item_spawn.Exactname_OFPlugin)

    Follow pl0x?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
Thread Status:
Not open for further replies.

Share This Page