[Tutorial] How to Make A Basic Plugin

Discussion in 'Resources' started by jay275475, Aug 26, 2013.

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

    jay275475

    Hello This Is jay275475 Here And I Well Show you how to make a basic Plugin that would just say "Hello World" when you type /test

    Eclipse Downloads
    64 Bit 32 Bit
    Setting up WorkSpace
    Step 1
    Create A Java Project
    Step 2
    Right Click Java Project Folder And Press Properties
    Step 3
    Press Java Build Path add External Jars And Upload The Latest API Which can Be found here
    Setting up Package
    Right Click Package and Press New Package And Name it me.(YourBukkitName).TestPlugin
    Making The Main Class
    Right Click Your Package and Press New Class And Name is main
    What We Do next is
    Code:java
    1. public class main extends JavaPlugin{
    2. //We Want to Extend our Main Class To JavaPlugin


    For this Tutorial Were going to Put a Basic OnEnable And OnDisable Method in our Project
    Code:java
    1. public void onEnable(){
    2. getLogger().info("Disabled!");
    3. }
    4. // These To well logged in the Console Saying "Disabled!" and "Enabled!"
    5. public void onDisable(){
    6. getLogger().info("Enabled!");
    7. }


    What We Should Have So Far is
    Code:java
    1. package me.(YourBukkitName).TestPlugin
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class main extends JavaPlugin{
    6.  
    7. public void onEnable(){
    8. getLogger().info("Disabled!");
    9. }
    10.  
    11. public void onDisable(){
    12. getLogger().info("Enabled!");
    13. }


    Now We Have to Create Our Command.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player player = (Player) sender;
    3. if(cmd.getName().equalsIgnoreCase("test")){
    4. player.sendMessage(ChatColor.AQUA + "Hello World!");
    5. //ChatColor.AQUA well make the Hello World! Have Aqua In it.
    6. }
    7. return false;
    8. }
    9. }


    So What We Should Have For Our Main Class Done And It Should Look Like This
    Code:java
    1. package me.(YourBukkitName).TestPlugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class main extends JavaPlugin{
    10.  
    11. public void onEnable(){
    12. getLogger().info("Disabled!");
    13. }
    14.  
    15. public void onDisable(){
    16. getLogger().info("Enabled!");
    17. }
    18. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    19. Player player = (Player) sender;
    20. if(cmd.getName().equalsIgnoreCase("test")){
    21. player.sendMessage(ChatColor.AQUA + "Hello World!");
    22. }
    23. return false;
    24. }
    25. }


    PLUGIN YML
    First Of All We need to Create the Plugin.yml by right clicking the Test Plugin Folder and Press New File And name the file as plugin.yml
    Here's What We Need In Our Plugin.yml for this tutorial
    Code:
    name: Test Plugin
    version: 1
    main: me.(YourBukkitName).TestPlugin.Main
    commands:
      test:
        description: This is a test Plugin.

    Thank you For reading My Tutorial I'll Keep It updated And If you need help or you get Any Errors Contact Me!

    Also If there's Anything else you think I should add Just PM me it

     
  2. Offline

    metalhedd

    you should NEVER name a class with a lower case letter. 'main' should be Main.

    also the enabled and disabled message you're printing are redundant, bukkit will already print them when a plugin is enabled or disabled. your code will print it twice, that's not an example you want to set.
     
  3. Offline

    Samthelord1

    jay275475 for your first reference to your package, you're not using a semicolon
     
  4. Offline

    jay275475

    metalhedd I didnt mean to do it main with lower case Sorry
     
  5. Offline

    metalhedd

    You don't need to apologize to me :p just fix it so that the people who follow your tutorial aren't learning bad practices.
     
  6. Offline

    ZeusAllMighty11

    Also, don't cast Player to sender. Why can't console run the command? Console gets lonely :(


    Just do instance check first
     
    CeramicTitan and lenis0012 like this.
  7. Offline

    Ivan

    Also, you don't need to print out that the plugin was enabled or disabled, it prints that out automatically.
     
  8. Offline

    metalhedd

    jay275475 Can you make the fixes that have been pointed out in this thread already? People are going to start following this tutorial and its filled with bad practices. please either make the corrections or remove it.
     
  9. Offline

    evilmidget38

    You also shouldn't be always returning false, as that displays the usage message.
     
    lukegb likes this.
  10. Offline

    xTrollxDudex

    jay275475
    Umm. I really don't think we need another plugin tutorial...
     
  11. Oh and you don't have to use me.yourbukkitname.projectname. I use com.tips48 since I own tips48.com
     
  12. Offline

    breezeyboy

    tips48 Or your email, lol.
     
  13. Pssh, i guess
     
  14. Offline

    ratedam

    Hello,

    This results if we are working on a localhost server but if the server is hosted somewhere else, the same applies? The same type of coding? Like Player player = event.getPlayer(); ?

    Thank you.
     
  15. Offline

    Necrodoom

    Removed unofficial build discussion from thread.
     
  16. Offline

    tasp42911

    Your so amazing you made a Godaddy website for free! That no who has heard of!
     
  17. Offline

    breezeyboy

    tasp42911 You didn't need to post that, he was replying with a tip.
     
  18. Offline

    wildwolf15

    Code:java
    1. package me.wildwolf15.testPlugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class main extends JavaPlugin{
    10.  
    11. public void onEnable(){
    12. getLogger().info("Disabled!");
    13. }
    14.  
    15. public void onDisabled(){
    16. getLogger().info("Enabled!!");
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    20. Player player = (Player) sender;
    21. if(cmd.getName().equalsIgnoreCase("test"));
    22. player.sendMessage(ChatColor.AQUA + "Hello! World!");
    23. }
    24. return false;
    25. }
    26. }
    27.  
    28.  


    What's wrong with my code???? I keep getting this error:

    DescriptionResourcePathLocationType
    Syntax error on token "}", { expected after this tokenmain.java/testPlugin/src/me/wildwolf15/testPluginline 23Java Problem
     
  19. Offline

    mactown21

    Let me fix that for you.....


    Code:
    package me.wildwolf15.testPlugin;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin{
     
    public void onEnable(){
    getLogger().info("Disabled!");
    }
     
    public void onDisabled(){
    getLogger().info("Enabled!!");
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(sender instanceof Player){
    Player player = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("test"));
    player.sendMessage(ChatColor.AQUA + "Hello! World!");
    }
    return false;
    }
    }
    Make sure you added if(sender instanceof Player){ so sender can be registered as player. Everything else looks fine.

    Make sure in your plugin.yml you have something like this
    main: me.wildwolf15.testPlugin.main
     
  20. Offline

    WinX64

    Code:java
    1. package me.wildwolf15.testPlugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class main extends JavaPlugin {
    10.  
    11. public void onEnable() {
    12. getLogger().info("Disabled!");
    13. }
    14.  
    15. public void onDisabled() {
    16. getLogger().info("Enabled!!");
    17. }
    18.  
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. Player player = (Player) sender;
    21. if (cmd.getName().equalsIgnoreCase("test"));
    22. player.sendMessage(ChatColor.AQUA + "Hello! World!");
    23. }
    24. return false;
    25. }
    26. }


    Replace
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("test"));

    with
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("test")) {


    Also, i suggest checking for the sender first, as it can be the console executing the command as well and not always a player.
     
  21. Offline

    Xyplo

    Can't someone just close this thread for bad coding especially if this is aimed at 'beginners'.
     
  22. Offline

    MineStein

    tasp42911 I don't think you would like it if someone was saying "No one has hear of your site". I think you should consider before posting.
     
  23. Offline

    WolfLeader116

    Well it can show how to make a custom message when your plugin loads like saying "The config file is broken! Please fix it!"
     
  24. Offline

    Pigems

    How do I import it to a jar file like all the other plugins?
     
  25. Offline

    awesomegamer

    I didn't realize how much of an idiot I was when I couldn't figure this out, and I read, "Basic."
    I need a life..
     
  26. Offline

    Dirtypotato101

    I wanna make a skyblock plugin so can you make a tutorial with more detail???
     
  27. Offline

    Cirno

    Just going to be a little rude with this post:
    • As pointed out, class names should be CamelCase.
    • You don't need to use onEnable and onDisable to log enable and disable messages. I believe, even at the current time in which this thread was created, Bukkit handled enable and disable messages automatically.
    • Package name conventions state you either use a domain you own or use your email.
    • Automatically casting Player to CommandSender is bad. What if the console uses the command?
    • You don't even need to cast Player to CommandSender if you're going to do sendMessage. CommandSender already has that method.
    • You should return true after the sendMessage; otherwise, it falls through and returns false, which sends the user a message saying the usage of the command.
    • You didn't include @Override on top of your onCommand. While it isn't completely necessary, it'd be best if you did because then others who read your code can say "Oh, this command overrides the implementation of onCommand" rather than saying "Where is onCommand called...? Do (s)he even need it?"
    • I am replying to a year old post. I am so good at this.
    Anyways, to anyone new to the Bukkit API/Java in general, I suggest you go to another tutorial. This tutorial is filled with bad habits and does not follow the conventions. While you technically don't have to follow the conventions, it's best you do so as it helps other developers who read your code understand it better. Conventions are like grammar; you can read a sentence with hardly any grammar and get the gist of it, but to understand what the sentence is trying to convey is another story.
     
    Dragonphase and Jozeth like this.
  28. Offline

    ChipDev

    How to create a java project?
    Not in depth.
     
Thread Status:
Not open for further replies.

Share This Page