Solved Plugin Version String Returns Null

Discussion in 'Plugin Development' started by bdubz4552, Apr 15, 2015.

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

    bdubz4552

    I've got some demons living in one of my plugins. I am simply trying to stick the version string of my plugin into the help menu. Sounds simple enough, just a matter of jamming JavaPlugin.getDescription().getVersion() in there. Well, I thought it was, but for some reason that I have been unable to figure out for the last two weeks, it keeps throwing a NullPointerException. It is strange, if I tell the logger to spit out (in the main class) getDescription().getVersion(), it does it, nothing is null. But in the CommandExecutor, it just refuses. I have already tried defining the string in the main class and passing that in instead of the plugin, but then it prints out "vnull" instead of throwing a NPE. Not much better. Here's what I am dealing with:

    My onEnable():
    Code:
    public void onEnable()
        {
            log = getLogger();
            saveDefaultConfig();
          
            consumedMaterial = Material.valueOf(getConfig().getString("consumed-item"));
            consumedMaterialAmount = getConfig().getInt("consumed-item-amount");
          
            captureManager = new CaptureManager(this);
            eggTracker = new EggTracker();
            getServer().getPluginManager().registerEvents(new EventListener(this), this);
            getCommand("simpleegg").setExecutor(new CommandHandler(this, eggTracker));
        }
    My CommandExecutor implementation (The relevant bits, at least):
    Code:
    private SimpleEgg plugin;
      
        private final ChatColor a = ChatColor.AQUA;
        private final ChatColor b = ChatColor.BLUE;
      
        public CommandHandler(SimpleEgg plugin, EggTracker eggTracker) {
            this.eggTracker = eggTracker;
            this.plugin = plugin;
        }
      
        private String[] help = {
            a + "== SimpleEgg v" + plugin.getDescription().getVersion() + " by bdubz4552 ==",
            b + "Choose a topic you want help on, and use /simpleegg <help topic>",
            b + "Help Topics:",
            b + " - Capturing",
            b + " - Horses - Before you capture a horse, please read this."
        };
    And to help my explanation of why I think there are demons in this project, let me also include the code from another plugin of mine, that also does this, with no problems at all:

    The other plugin calls this void from within onEnable():
    Code:
    private void registerCommands() {
            getCommand("horsestats").setExecutor(new Horsestats(this, tl));
            getCommand("htp").setExecutor(new Htp(this, tl));
            getCommand("setowner").setExecutor(new SetOwner(this, tl));
            getCommand("untame").setExecutor(new Untame(this, tl));
            getCommand("delchest").setExecutor(new Delchest(this, tl));
            getCommand("delname").setExecutor(new Delname(this, tl));
            getCommand("slayhorse").setExecutor(new Slayhorse(this, tl));
            getCommand("hfriend").setExecutor(new HFriend(this, tl));
            getCommand("hspawn").setExecutor(new Hspawn(this, tl));
            getCommand("setstyle").setExecutor(new SetStyle(this, tl));
            getCommand("setstat").setExecutor(new SetStat(this, tl));
            getCommand("tame").setExecutor(new Tame(this, tl));
        }
    And the command:
    Code:
    public Horsestats(HorseStatsMain main, Translate tl) {
            super(main, tl);
        }
      
        private String[] genHelp =
        { " "
        , YELLOW + "== HorseStats v" + main.getDescription().getVersion() + " " + tl.horsestats("by") + " 'bdubz4552' =="
        , YELLOW + tl.horsestats("choose")
        , GREEN  + "/hs stats"
        , GREEN  + "/hs teleport"
        , GREEN  + "/hs protect <grief | interact | friends>"
        , GREEN  + tl.horsestats("command")
        , YELLOW + "/help horsestats" + GREEN + "."
        };
    As you can tell, this is a subclass of a generic command class. I don't think (but am a bit suspicious) being a subclass is why it works in this plugin, but in the event that it is, its superclass is (again removing unnecessary bits):
    Code:
    public abstract class HorseStatsCommand implements CommandExecutor {
      
        protected HorseStatsMain main;
        protected Translate tl;
      
        public HorseStatsCommand(HorseStatsMain main, Translate tl) {
            this.main = main;
            this.tl = tl;
        }
    So if anyone has any idea where the demons are hiding (or where noob moves were made by me), I would appreciate any feedback.
     
  2. Offline

    FisheyLP

    You need to use .getDescription in the class which extends JavaPlugin, or use instance.getDescription outside of the class which extends JavaPlugin.
    In your case use plugin.getDescription
     
  3. Offline

    bdubz4552

    Well it turns out that after simply making my command class a subclass of an abstract, CommandExecutor implementing class that has the plugin main class in its constructor, it magically works now. So I guess it didn't like having the plugin main class passed directly in... shrugs
     
  4. Offline

    nverdier

    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page