Things I need to know!

Discussion in 'Plugin Development' started by Yudaz, Jan 18, 2012.

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

    Yudaz

    Hi, I am a beginner as Plugin Developer and I wonder if you could answer me some questions?
    (If you want me to take this to another forum or to a post, just tell me and I delete this post.)

    Questions:
    1. Is it posible to make a variable that shows the version of my plugin, how do I do it?
    2. I need to make a new line (I dont know what you call it) in a string, how do I do that?
    3. If I am gonna have a command extra such as telling [playername] how do I do that?
    4. If I want a "precommand" (or whatever its called) such as having a command before the command, like /command help, how do I do that?
    5. How do I make a configuiration file (yaml) that pluginusers can edit?
    6. How do I make the plugin use the settings in the configuiration?
    Its okay if you dont know all of the questions at least please answer some of them? :)

    Thanks for the help! ;) :D

    BTW, I have read the tutorial for Bukkit Plugins, but I dont get it (on whats in it) and it doesnt answers almost any of my questions...
     
  2. 1. sure. String version = getPluginDescription().getVersion();
    2. \n
    3. check the args of the command
    Code:
    if(args.length > 0)
    {
      if(args[0] == "command_parameter")
      {
        // do whatever here
      }
    }
    4. defining the pre command in the plugin.yml and check if it's called in the onCommand. then just do the thing above with 'help'
    5. check the wiki ;)
    6. same as 5.

    hope this helps a bit ;)
     
  3. Offline

    Yudaz

    Yeah it does, thank you very much! :) ;) :D

    But as in 1 here,
    I made this string:
    Code:
     String strpv = (c3 + "[" + getPluginDescription().getVersion(); + c3 + "] ");
    And then I get "Syntax error on token ";", delete this token", and "The method getPluginDescription() is undefined for the type cmdexecutor"... Why so?
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  4. for the syntax error... come on, that is basic java.
    Ah, sorry for the other thing. first it's getDescription() instead of getPluginDescription() and second you can only call that via your main class.
     
  5. Offline

    Yudaz

    Yeah I found out the syntax error my self.. :) lol

    But, is it possible to make the string variable to work in another class than main class? I have now made it in my main class and it works, but I need it in another... Can I make a connection or something?
     
  6. Offline

    randomman159

    getConfig().setString("my.node.here", "example");

    getConfig().getString("my.node.here"); //returns "example"


    I'm pretty sure that's right ^ but thats just the basics, you can add defaults, etc.

    As kumpelblase2 said, check the wiki.
     
  7. making a static variable might work if you set the value in the constructor, otherwise you would need to make a public variable or getter-function and pass the main class over.
     
  8. Offline

    Yudaz

    Im sorry Im not so good at Java either (I know some JavaScript, but Java is a little different...), so... If you got an idea, how would it look like?
     
  9. you could do a function inside your class where you need the version as well.

    Code:
    //make a variable for the version inside your class:
    String version = "";
     
    // somewhere in the class:
    public void setVersions(<YourMainClass> main)
    {
      this.version = main.getDescription().getVersion();
    }
    if you would need the main-class more than once in the class you needed the version, you could also make a variable of that:

    Code:
    private <YourMainClass> plugin;
     
    public void setMain(<YourMainClass> main)
    {
      this.plugin = main;
    }
    (like I said this can be done via the constructor as well)
     
  10. Offline

    Yudaz

    "The local variable version may not have been initialized" - Is what I get, Im sure it easy but I dont know Java :p

    And now I also got:

    "version cannot be resolved or is not a field"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  11. can you post your code? would help a lot.
     
  12. Offline

    Yudaz

    Code:
    package me.ProperPunishment.ProperPunishment;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
     
    public class cmdexecutor implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
        String[] args) {
            ChatColor ce = ChatColor.YELLOW;
            ChatColor c6 = ChatColor.GOLD;
            ChatColor c3 = ChatColor.DARK_AQUA;
            ChatColor cc = ChatColor.RED;
            ChatColor c1 = ChatColor.BLUE;
            String strpp = (c3 + "[" + c6 + "ProperPunishment" + c3 + "] ");
            String version = (version); 
    //Error here, ^^^ (The local variable version may not have been initialized)
           
            if(commandLabel.equalsIgnoreCase("properpunishment")) {
                if(!sender.isOp()); { sender.sendMessage(strpp + ce + "This command isnt used for this plugin. Please use " + cc + "/pp" + ce + " to get help and a list of avaiable commands."); }
                    return true;
            }
            if(commandLabel.equalsIgnoreCase("pp")) {
                        sender.sendMessage(strpp + version + ce + "Here is a list of commands avaiable for ProperPunishment: " + "/n" + c3 + "[" + cc + "Red" + ce + "color is main command and " + c1 + "blue" + ce + "color is params." + c3 + "]");
                            return true;
            }
            if(commandLabel.equalsIgnoreCase("reload")) {
                sender.sendMessage(strpp + ce + "Reloading configuiration.");
               
                    return true;
            }
            if(commandLabel.equalsIgnoreCase("help")) {
                sender.sendMessage(strpp + version + ce + "Here is a list of commands avaiable for ProperPunishment: " + "/n" + c3 + "[" + cc + "Red" + ce + "color is main command and " + c1 + "blue" + ce + "color is params." + c3 + "]");
                    return true;
            }
            if(commandLabel.equalsIgnoreCase("?")) {
                sender.sendMessage(strpp + version + ce + "Here is a list of commands avaiable for ProperPunishment: " + "/n" + c3 + "[" + cc + "Red" + ce + "color is main command and " + c1 + "blue" + ce + "color is params." + c3 + "]");
                    return true;
            }
            return false;
            }
       
        public void setVersions(main main)
        {
          this.version = main.getDescription().getVersion();
    //And error here, ^^^ ( version cannot be resolved or is not a field )
        }
    }
    The errors appears where I got "//Error here" or something like that...
    Btw, thank you so much for helping me at all! :)
     
  13. Offline

    MrMag518

    Uhm.. thats .. a lot wrong, I would recommend go reading some basic java before asking.
     
  14. Ah, I see.
    So you need to declare the variable as a class variable. Meaning that it's outside of the function 'onCommand'.
    The difference is that if a variable is initialized inside a function/method, it's only 'alive' inside the function and if the function is done, the variable is no longer available. If you declare it as a class-variable, it'll be available until the class (in this case the commandExecutor) is destroyed which will be when them plugin get unloaded/server stops.

    also "String version = (version);" will never work. Because you set the variable to itself before it even has a value. so just do "String version = "";" initialization.

    Last thing: do you really named you main class (that one which extends JavaPlugin) "main"? otherwise change that in the setVersion function (btw: made a typo there :p)
     
  15. Offline

    Yudaz

    Do you mean "String version = ""; initialization" or dd you actually meant "String version = "";" initializatiion"? Yeah main class is named "main"... :)

    - Btw, whats typo?

    So you mean that I just should move the function into the body of onCommand? Or something else? :p
     
  16. typo: typing mistake. (i wanted to write 'getVersion' but actually wrote 'getVersions').
    yes, I meant the second option.
    but NO! don't move the function inside the onCommand-functions. the getVersion function is fine.
     
  17. Offline

    Yudaz

    Ok, but Im not so sure of what you meant... So it would look like:?
    Code:
    String version = "";
     
  18. correct.

    But like MrMag518 said, and what i suggest as well, learn some more java because in 60% of all cases it's because the lack of knowing java.
     
  19. Offline

    Yudaz

    Yeah I will... But, Im not sure if this has something to do with what we you have helped me with here, but when I try starting the plugin/server with the plugin I get this error:
    Code:
    174 recipes
    27 achievements
    19:45:20 [INFO] Starting minecraft server version 1.0.1
    19:45:20 [INFO] Loading properties
    19:45:20 [INFO] Starting Minecraft server on *:25565
    19:45:20 [INFO] This server is running Craftbukkit version git-Bukkit-1.0.1-R1-b
    1597jnks (MC: 1.0.1) (Implementing API version 1.0.1-R1)
    19:45:20 [INFO] Preparing level "world"
    19:45:20 [INFO] Default game type: 0
    19:45:21 [INFO] Preparing start region for level 0 (Seed: 7952138064958413481)
    19:45:22 [INFO] Preparing spawn area: 24%
    19:45:23 [INFO] Preparing start region for level 1 (Seed: -1708048003815095964)
    19:45:23 [INFO] Preparing spawn area: 3%
    19:45:24 [INFO] Preparing start region for level 2 (Seed: -1708048003815095964)
    19:45:24 [INFO] Preparing spawn area: 8%
    19:45:25 [INFO] [ProperPunishment][]Starting up.
    19:45:25 [SEVERE] Error occurred while enabling ProperPunishment v0.1 (Is it up
    to date?): null
    java.lang.NullPointerException
            at me.ProperPunishment.ProperPunishment.main.onEnable(main.java:17)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:188)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:968)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:280)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:186)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:169
    )
            at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:348)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:335)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:165)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:399)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    19:45:25 [INFO] Server permissions file permissions.yml is empty, ignoring it
    19:45:25 [INFO] Done (0,441s)! For help, type "help" or "?"
    And in main class I have:
    Code:
    package me.ProperPunishment.ProperPunishment;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin {
     
        @Override
        public void onDisable() {
            System.out.println("ProperPunishment is disabled.");
        }
       
        @Override
        public void onEnable() {
            System.out.println("[ProperPunishment][" + version + "]Starting up.");
            getCommand("properpunishment").setExecutor(new cmdexecutor());
            getCommand("pp").setExecutor(new cmdexecutor());
            getCommand("reload").setExecutor(new cmdexecutor());
            getCommand("help").setExecutor(new cmdexecutor());
            System.out.println("[ProperPunishment][0.1] Successfully enabled.");
        }
           
        String version = "";
           
        public void setVersion(main main)
        {
        this.version = main.getDescription().getVersion();
        }
       
    }
     
  20. Offline

    lose_the_grimm

    Code:
    19:45:25 [SEVERE] Error occurred while enabling ProperPunishment v0.1 (Is it up
    to date?): null
    java.lang.NullPointerException
            at me.ProperPunishment.ProperPunishment.main.onEnable(main.java:17)
    I would harbor a guess that "reload" is not in your plugin.yml file in the commands list.

    Also, for aesthetic and conformist reasons I would rename your JavaPlugin class to something like ProperPunishment instead of main.

    A bit about code style. Class fields and such generally go at the top of the class definition. Like so...
    Code:
    public class main extends JavaPlugin {
      String version = "";
     
      @Override
      public void onDisable() {
    
    But this is probably immaterial since you're duplicating data unnecessarily. What might be best is a convenience method on main that accesses the data you're looking for. So replacing:
    Code:
        String version = "";
         
        public void setVersion(main main)
        {
        this.version = main.getDescription().getVersion();
        }
    and the usage:
    Code:
    System.out.println("[ProperPunishment][" + version + "]Starting up.");
    With the following:
    Code:
    public String getPluginVersion() {
      return this.getDescription().getVersion();
    }
    and use it in this way:
    Code:
    System.out.println("[ProperPunishment][" + this.getPluginVersion() + "] Starting up.");
    But then the getPluginVersion() convenience method is used so infrequently why not just use this.getDescription().getVersion() outright? As in:
    Code:
    System.out.println("[ProperPunishment][" + this.getDescription().getVersion() + "] Starting up.");
    To add voice to the previous replies; do look up some Java tutorials. They will be a great great help. This is a hell of a lot to bite off at one time.

    Good Luck!
     
  21. Offline

    Yudaz

    Thanks! :) Ill read and watch now!!! ;)
     
Thread Status:
Not open for further replies.

Share This Page