[EASY] Get bukkit's build number.

Discussion in 'Resources' started by filoghost, Sep 13, 2013.

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

    filoghost

    Hello, this is my first tutorial. Getting the bukkit build may be useful to check for bugs, and tell the users to update their bukkit, or disable the plugin. This tutorial is based on a simple regex.

    This is how you get the bukkit version:
    Code:java
    1. Bukkit.getVersion()

    This is the String for the RB:
    Code:
    git-Bukkit-1.6.2-R1.0-b2879jnks (MC: 1.6.2)
    But the build number (2879) is between a "b" and "jnks".
    We need to compile a very simple pattern to get that number:
    Code:java
    1. String version = Bukkit.getVersion();
    2. Pattern pattern = Pattern.compile("(b)([0-9]+)(jnks)");
    3. Matcher matcher = pattern.matcher(version);
    4.  
    5. if (matcher.find()) {
    6. System.out.println("Bukkit build number: " + matcher.group(2));
    7.  

    The pattern will try to match a String that starts with b (b), that contains one or more digits ([0-9]+) and ends with jnks (jnks). The second group is the number.
    If you don't know how to use java regex, you can copy and paste this method:
    Code:java
    1.  
    2. /**
    3. * @return the Bukkit build, or null if not found.
    4. */
    5. public String getBukkitBuild() {
    6. String version = Bukkit.getVersion();
    7. Pattern pattern = Pattern.compile("(b)([0-9]+)(jnks)");
    8. Matcher matcher = pattern.matcher(version);
    9.  
    10. if (matcher.find()) {
    11. return matcher.group(2);
    12. }
    13.  
    14. return null;
    15. }


    I hope that you find this tutorial useful, and please tell me if I made grammar errors :)
     
    Skyost and Minecrell like this.
  2. Offline

    Minecrell

    filoghost
    Why are you using a while loop in the method?
    Code:java
    1. if (matcher.find()) {
    2. return matcher.group(2);
    3. }
    4.  
    5. while (matcher.find()) {
    6. return matcher.group(2);
    7. }
    What's the difference? If matcher.find() returns true it will return matcher.group(2)and stop the method there, but if it's false, it will stop the while loop. So it's actually the same like an if clause, isn't it? ;)

    I don't know exactly how to write regex, but your method is looking good. :)
     
  3. Offline

    filoghost

    Minecrell You're right, there's no need to use a while loop. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page