Starting my first plugin! Would like some help!

Discussion in 'Plugin Development' started by timy659, Aug 22, 2011.

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

    timy659

    Hello,

    I'd love to create my first plugin! I am already busy with learning java.

    My idea for a first simple plugin was a basic God-like creature. Which will make 1 person God and that people can use /pray <message>. And then all the prayers will go to the God person!

    So my question was if this is hard to make? And where can I find the syntaxes? Is there anyone who would like to help me with creating this plugin? (Someone who wants to answer my questions and can help me with some syntaxes)

    I appreciate any effort towards helping me!
     
  2. Offline

    ItsHarry

  3. Offline

    timy659

  4. Offline

    ItsHarry

    Well yes of course:

    Player player = Bukkit.getServer().getPlayerByName("guy you want to be god");
    player.sendMessage("message");
     
  5. Offline

    timy659

    Many thanks, I am busy with coding right now, I bet it will take me some time.
     
  6. Offline

    thehutch

    Good luck although in my opinion unless you have a strong knowledge of Java itself this seems quite difficult but good luck and have fun.
     
  7. Offline

    timy659

    @thehutch I don't really have a strong knowledge. Well, actually, I started yesterday XD but I think that one day, it will certainly work :D

    But, hutch, do you perhaps know an easier idea to begin with?
     
  8. Offline

    thehutch

    Well i'd say start with something simple like an anti-grief plugin (BUT DO NOT POST) or else you will be trolled and laughed at :) everyone is fine with you creating a plugin to learn but don't post it if there is already on there. Since your new to the bukkit then i'd create a plugin using most of the events in the aPI so you can try out using them all so you know your way around the code. But good luck with your journey.
     
  9. Offline

    timy659

    @ItsHarry I have got a question. Should I use the playerlistener? If so, what should I replace PLAYER_MOVE with?

    I think I will actually not fail at this :D at least, I hope I wont.
    Code:
    package me.timy659.praytogod;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PraytoGod extends JavaPlugin {
    
        public static void main(String[] args) {
    
            Logger log = Logger.getLogger("Minecraft");
    
            public void onEnable() {
                log.info("[PraytoGod] Your plugin has been enabled!");
    
                PluginManager pm = this.getServer().getPluginManager();
    
                Player player = Bukkit.getServer().getPlayerByName("timy659");
                player.sendMessage("");
    
            }
            public void onDisable() {
                log.info("[PraytoGod] Your plugin has been disabled!");
            }
        }
    
    }
    
    thats the code so far. Do you know if it makes sense?

    BTW: I get like 7 errors in this small piece of text :S

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

    thehutch

    public static void main(String[] args) {
    this is not needed lol did you just add this because it was in the "helloworld" application thing

    no offence but most of that makes no sense no offence


    Code:
                Player player = Bukkit.getServer().getPlayerByName("timy659");
                player.sendMessage("");
    
    and

    Code:
                PluginManager pm = this.getServer().getPluginManager();
    because your not registering any events
     
  11. Offline

    timy659

    @thehutch okay lol. But the plugin should send messages to timy659 by the command /pray <message>. What syntax should I use for that?
    Code:
    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.
        return false;
    }
    Thats the code for a command, but how do I send/store the information which comes after /pray ?









    And I see that if I hit run (im using eclipse) that it says: java.lang.NoSuchMethodError: main
    Exception in thread "main"

    Which I think means that this class is not stated as main class :S

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

    sddddgjd

    And what do you expect to happen when you hit run?
     
  13. Offline

    timy659

    I think I need to have a good look how some easy plugin is made + commands and extras. Maybe thats a better idea.

    @sddddgjd Nothing, but just to check if the code is correct and if it has no bugs.

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

    sddddgjd

    And how would eclipse know to run your program?
    What i'm trying to point out is, eclipse can't run java plugins,because it doesn't make sense...you need to export it to a jar,then put it in the bukkit plugins,then run!
     
  15. Offline

    timy659

    @sddddgjd Ah okay, I thought that I could check for flaws this way too. Do you know a simple plugin to check its code? Thnx for helping.
     
  16. Offline

    sddddgjd

    PM'd you my skype id,i'll help you (because i'm bored :d)
     
  17. Offline

    timy659

    I changed plans!

    Instead of sending a message to 'god' people will just see a line saying: Your prayer has been heard. I know its easy and all but it's something to start with and it makes everything a lot easier!

    This is my code so far:

    Code:
    package me.timy659.praytogod;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PraytoGod extends JavaPlugin {
    
     
            Logger log = Logger.getLogger("Minecraft");
    
            public void onEnable() {
                log.info("[PraytoGod] Your plugin has been enabled!");
      
                public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    if(cmd.getName().equalsIgnoreCase("pray")){
                        sender.sendMessage(ChatColor.RED + "Your prayer has been heard!");
                        return true;
                    }
                    return false;
     
                Player player = Bukkit.getServer().getPlayerByName("timy659");
                player.sendMessage("");
    
            }
            public void onDisable() {
                log.info("[PraytoGod] Your plugin has been disabled!");
            }
        }
    
    }
    
    And I am going to try this plugin now, I'll let you hear if I got any errors.

    (I will.)
     
  18. Offline

    ItsHarry

    One problem with your plugin is:

    The onCommand method should be NOT be in the onEnable method.
    Put it OUTSIDE of onEnable. Also, did you register the command in plugin.yml?
     
  19. Offline

    Lolmewn

    You can also add me on skype! sybren.gjaltema
     
  20. Offline

    timy659

    @ItsHarry I did. This is my plugin.yml:

    name: PraytoGod
    main: PraytoGod.PraytoGod
    version: 1.0
    commands:
    basic:
    description: Pray command.
    permission: Pray.PraytoGod
    usage: |
    /pray

    Is that the right way?

    and should I use some line called:

    Player player = (Player)sender; ?

    Because I found it in someones plugin.

    And if I have done this, do you know a tutorial where I can find out how to compile this to a jar?
     
  21. Offline

    sddddgjd

    Replace /pray with /[command], you don't need pray...get rid of "permission",it's useless...other than that,seems ok!
    On eclipse,you click file,export,jar file,select all the files,select where to save the jar,and run it...(or something similar)
    On netbeans,you press the build button!
     
  22. Offline

    Banana937

    Sounds like a fun idea, but you might get some atheists hating on it xD
     
  23. Offline

    timy659

    @ItsHarry I got a warning when compiling without plugin.yml: JAR export finished with warnings. See details for additional information.
    Exported with compile errors: PraytoGod/src/me/timy659/praytogod/PraytoGod.java

    and with a plugin.yml it gives an error:

    JAR creation failed. See details for additional information.
    Exported with compile errors: PraytoGod/src/me/timy659/praytogod/PraytoGod.java
    Resource is out of sync with the file system: '/PraytoGod/plugin.yml'.


    should I export as a jar file? or as a runnable jar file?

    NEVER MIND I got it!

    DAMN! Okay I put my plugin in but then I got this error: [SEVERE] Could not load 'plugins\PraytoGod.jar' in folder 'plugins':
    while scanning for the next token
    found character '\t' that cannot start any token
    in "<reader>", line 8, column 5:
    /[command]
    ^

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

    MineDev

    Something is wrong with your YAML, try using an editor like Notepad++.
    Make sure instead of using tabs you do 4 spaces :)
     
  25. Offline

    timy659

    @MineDev I spotted a tab -.- changed it, testing now

    Got another error:

    Code:
    [SEVERE] Could not load 'plugins\PraytoGod.jar' in folder 'plugins':
    java.lang.ClassNotFoundException: PraytoGod.PraytoGod
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:36)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:24)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:168)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:207)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:130)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:128)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:97)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:51)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:132)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    And btw: Thanks to everyone who is helping me! I kinda get the feeling that I didn't even create this myself, because I asked for so muc help :|

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

    MineDev

    ok PrayToGod.PrayToGod is incorrect check that you have it right so in eclipse if your package is PrayToGod and your main class is PrayToGod, then it should work.

    Tell me what your package and main class is :)
     
  27. Offline

    timy659

    @MineDev main class is called PraytoGod.java and this is its public class line : public class PraytoGod extends JavaPlugin {

    The package is called = me.timy659.praytogod but I dont think you mean that package do you? and the plugin itself is called : PraytoGod.jar (well thats the file)
     
  28. Offline

    MineDev

    ok
    In the yml change PrayToGod.PrayToGod...

    to me.timy659.praytogod.PraytoGod

    Case SenSiTiVE
     
  29. Offline

    timy659

    @MineDev I changed it, the plugin works now. Well, its loaded but when typing /pray it still says: unknown console command :C

    Any one knows what I did wrong? Here is the updated code uptil now :

    Code:
    package me.timy659.praytogod;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class PraytoGod extends JavaPlugin {
    
            Logger log = Logger.getLogger("Minecraft");
    
     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    if(cmd.getName().equalsIgnoreCase("pray")){
                        sender.sendMessage(ChatColor.YELLOW + "Your prayer has been heard!");
                        return true;
                    }
                    return false;
    
    }
            public void onEnable() {
                log.info("[PraytoGod] Your plugin has been enabled!");
    
            }
            public void onDisable() {
                log.info("[PraytoGod] Your plugin has been disabled!");
            }
        }
    
     
  30. Offline

    MineDev

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                    if(cmd.getName().equalsIgnoreCase("pray")){
                                if (args.length == 0) {
                        sender.sendMessage(ChatColor.YELLOW + "Your prayer has been heard!");
                        return true;
                    }
    }
                    return false;
    
    }
    try that.

    and did you register the commands in the yml?
     
Thread Status:
Not open for further replies.

Share This Page