Is there a cleaner or more semantic way to do this?

Discussion in 'Plugin Development' started by Jake6177, Nov 12, 2013.

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

    Jake6177

    So in my main class, I have some public final variables that define my plugin sort of.

    They are:

    Code:java
    1. public final String name = "name";
    2. public final Double version = 1.0;


    With these, in my onEnable and onDisable methods, I use a String.format() method to add the name and version into them (and I use this throughout to output the name of the plugin in log and player.sendMessage() outputs.

    It looks sort of like this all around the plugin:

    Code:java
    1. onEnable/onDisable example:
    2.  
    3. logger.log(Level.INFO, String.format("[%s] %s v%s has been enabled.", this.name, this.name, this.version);
    4.  
    5.  
    6. player.sendMessage() example:
    7.  
    8. player.sendMessage(String.format("[%s] You have been teleported to OraBrush.", this.plugin.getPluginName());
    9.  
    10.  


    Anyway, I suppose what I'm asking is, how do others perform the same that I'm doing here (that is providing a variable for the name of the plugin so that when it's changed, it effectively changes everywhere)?
     
  2. Offline

    MrSparkzz

    Jake6177
    Code:java
    1. public String name = this.getName();

    to change it
    Code:java
    1. MainClass.name = "New Name";
     
  3. Offline

    amhokies

    What I'll usually do is create a method for sending messages to players. Something like this:
    Code:java
    1. public static void sendMessage(Player p, String msg) {
    2. p.sendMessage("[" + pluginName + "]" + " " + msg);
    3. }

    With this, all you would have to do is change the value of pluginName, and everytime you call the sendMessage() method, it will reflect those changes.
     
  4. Offline

    exload

    MrSparkzz you can't do that. You declared name as final. Also in order for you to reference then name from another class like you suggested the variable would need to be static.

    In addition Bukkit has a way to get the name of your plugin. (This can also be used to get the version, authors, etc. Everything in the plugin.yml)

    Below 'this' is referring to your class that extends JavaPlugin.
    Code:java
    1. this.getDescription().getName();


    Just use that so that anytime you change your plugin's name in the plugin.yml it will update everywhere.
     
    Jake6177 likes this.
  5. Offline

    MrSparkzz

    exload
    Yeah, just noticed that. I've been getting a lot of Alerts tonight and trying to get through them all is pretty tough, so I am not being very thorough when reading through the posts.
     
  6. Offline

    Jake6177

    Well that seems easier ;) Thanks!
     
Thread Status:
Not open for further replies.

Share This Page