Various problems with my plugin

Discussion in 'Plugin Development' started by Techy4198, Apr 21, 2013.

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

    Techy4198

    hi just another small holdup for my plugin here
    I have added another command, in exactly the way as I used to and it worked, but this time I get an error. you see, I want a way to load up a command only if it is enabled in the plugin's config.yml, and then once it is enabled, I need to check if the first parameter is the same as the password value in config.yml. here is my main class:
    Code:
    package me.conormcs.myceliumPlus;
     
    import org.bukkit.Server;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin
    {
     
        public Main()
        {
        }
     
        public void onEnable()
        {   
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
           
            boolean recipe = getConfig().getBoolean("recipe");
            if(recipe)
            {
                Server server = getServer();
                CraftStackMycelium.CreateRecipe(server);
            }
           
            boolean bonemeal = getConfig().getBoolean("bonemeal");
            if(bonemeal)
            {
                PlayerListener pl = new PlayerListener(this);
                getServer().getPluginManager().registerEvents(pl, this);
            }
           
            getCommand("throw").setExecutor(new CommandExecutorThrow());
           
            boolean callConsole = getConfig().getBoolean("Call console");
            if(callConsole == true){
                getCommand("callconsole").setExecutor(new CommandExecutorCallConsole());
            }
        }
       
       
    }
    
    commandexecutorthrow works fine, aswell as all the other classes, except for commandexecutorcallconsole:
    Code:
    package me.conormcs.myceliumPlus;
     
    import javax.swing.JOptionPane;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CommandExecutorCallConsole extends JavaPlugin implements CommandExecutor {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                if(cmd.getName().equalsIgnoreCase("callconsole") && args.length == 1){
                  String password = getConfig().getString("Call console password");
                  if (!(sender instanceof Player)) {
                    sender.sendMessage("Only ingame players may call the console.");
                    return true;
                  } else {
                    if(args[0] == password){
                        sender.sendMessage("Calling the console...");
                        JOptionPane.showMessageDialog(null, sender.getName() + " is calling you via your server!","Mycelium+",JOptionPane.PLAIN_MESSAGE);
                        return true;
                    } else {
                        sender.sendMessage(ChatColor.RED + "Incorrect password!");
                        return true;
                    }
                  }
            }
            return false;
        }
    }
    
    if you find any stray bits of code like 'this.getconfig().' just ignore them, i'm working on them as i speak and I literally just pasted them out of eclipse.
     
  2. Offline

    Tirelessly

    You can't extend JavaPlugin in two classes.
     
  3. Offline

    Techy4198

    ok. tell me how do I get config values in a class that doesn't extend JavaPlugin? I can't seem to make it work... just says method getConfig() is undefined
     
  4. Offline

    kreashenz

    Techy4198 You set up a constructor calling the plugin, or main class.
    Code:java
    1. public CommandExecutorCallConsole(Main main){this.main = main;}
     
  5. Offline

    Techy4198

    @kreashenz
    sorry for being such a noob, but where do I put that code? XD
    it's a wonder I ever made my 'Mycelium+' plugin work!
     
  6. Offline

    kreashenz

    Techy4198 On top of your onCommand, in the CommandExecutorCallConsole class.
     
  7. Offline

    Techy4198

    inside or outside? if I put it outside then the first 'main' in {this.main = main;} has a red line saying cannot be resolved to a type

    edit: inside has soooo many errors its unbelievable
     
  8. Offline

    kreashenz

    Techy4198 This should work..
    Code:java
    1. package me.conormcs.myceliumPlus;
    2.  
    3. import javax.swing.JOptionPane;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class CommandExecutorCallConsole extends JavaPlugin implements CommandExecutor {
    13. public CommandExecutorCallConsole(Main main){this.main = main;}
    14. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    15. if(cmd.getName().equalsIgnoreCase("callconsole") && args.length == 1){
    16. String password = getConfig().getString("Call console password");
    17. if (!(sender instanceof Player)) {
    18. sender.sendMessage("Only ingame players may call the console.");
    19. return true;
    20. } else {
    21. if(args[0] == password){
    22. sender.sendMessage("Calling the console...");
    23. JOptionPane.showMessageDialog(null, sender.getName() + " is calling you via your server!","Mycelium+",JOptionPane.PLAIN_MESSAGE);
    24. return true;
    25. } else {
    26. sender.sendMessage(ChatColor.RED + "Incorrect password!");
    27. return true;
    28. }
    29. }
    30. }
    31. return false;
    32. }
    33. }
     
  9. Offline

    Techy4198

    nope... see above
     
  10. Offline

    kreashenz

  11. Offline

    Techy4198

  12. Offline

    kreashenz

    Techy4198 Wow.. Really now? When I tried it, it had no errors. CTRL + A and replace the whole file with what I gave you, then save and the errors should go.
     
  13. Offline

    Techy4198

    @kreashenz what do I need to change in my main class? that is causing problems too now.

    and nope that error is still not gone

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

    kreashenz

    Techy4198 I'm not going to do the whole plugin for you. Remove that unused
    Code:
    public Main(){
    }
    in the main class. In the main class, you need to add do this in the onEnable
    Code:java
    1. getCommand("callconsole").setExecutor(new CommandExecutorCallConsole(this));
    2. //INSTEAD OF
    3. getCommand("callconsole").setExecutor(new CommandExecutorCallConsole());
     
  15. Offline

    Techy4198

    tried that, and it still spits out errors. i hate coding...
     
  16. Offline

    Austy

    Lol. You didn't define main field, so You can't set its value in a constructor. When You do this, You can call getConfig() on that field. And don't extend JavaPlugin in Your executor.
     
  17. Offline

    MadArkael

    Code:
    public class CommandExecutorCallConsole  implements CommandExecutor {
     
        private Main main;
     
        public CommandExecutorCallConsole(Main main){
     
        this.main = main;
        }
        //then rest of your class down here
        FileConfiguration config = this.main.getConfig();
    
    this simply means this class. If you were to call a public static variable from another class you would call it like this:

    int var = Main.myVariable;

    when dealing with non static variables in the same class its common to use this.

    So:

    this.main is referring to your private Main main; variable

    so when you register your command in your main class in onEnable() you would write

    getCommand("callconsole").setExecutor(new CommandExecutorCallConsole(this));

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

    kreashenz

    Oh I left out the Main main..
     
  19. Offline

    MadArkael

    when writing things like that are second nature, its easy to forget it in your lecture ;)
     
  20. Offline

    Techy4198

    lol im not so bad at coding after all :D I tried exactly that, and it worked, so I came here to say i'd fixed it, and found out that you ppl had already said! XD but yeah its fixed now :) well the code errors anyway imam check it out ingame

    aww XD it all looked so good, server started with no errors from my plugin, tried the command without the password, just said 'Usage: /callconsole <password>' which is exactly what I want. but when I entered the password, it all came crumbling down with about 3.5 seconds of printing errors nonstop. :(

    ...aaannnddd guess who's back. yep, it was indeed our old buddy, or rather enemy, FileCannotBeNull.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  21. Offline

    kreashenz

  22. Offline

    Techy4198

    pastebin is annoying. I will pop it here:
    and line 18 Is this:
     
  23. Offline

    kreashenz

    Techy4198 You can't have spaces in config.yml. You have to use '-'
     
  24. kreashenz
    Yes you can, that's not the error.

    Techy4198
    You're using getConfig() before onEnable() is triggered.
    Post your entire class and post your main class as well if you want quick answers.
     
  25. Offline

    Techy4198

    are you seriously that dumb?
    nobody-writes-yml-configs-like-this

    don't see where that should be happening...

    Main class
    Code:
    package me.conormcs.myceliumPlus;
     
    import java.io.IOException;
    import org.bukkit.Server;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin
    {
    public void onEnable()
    {
    getConfig().options().copyDefaults(true);
    saveDefaultConfig();
     
    boolean recipe = getConfig().getBoolean("recipe");
    if(recipe)
    {
    Server server = getServer();
    CraftStackMycelium.CreateRecipe(server);
    }
     
    boolean bonemeal = getConfig().getBoolean("bonemeal");
    if(bonemeal)
    {
    PlayerListener pl = new PlayerListener(this);
    getServer().getPluginManager().registerEvents(pl, this);
    }
     
    getCommand("throw").setExecutor(new CommandExecutorThrow());
     
    boolean callConsole = getConfig().getBoolean("Call console");
    if(callConsole == true){
    getCommand("callconsole").setExecutor(new CommandExecutorCallConsole(this));
    }
     
    try {
    Metrics metrics = new Metrics(this);
    metrics.start();
    } catch (IOException e) {
    // Failed to submit the stats :-(
    }
    }
     
     
    }
    
    CommandExecutorCallConsole
    Code:
    package me.conormcs.myceliumPlus;
     
    import javax.swing.JOptionPane;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class CommandExecutorCallConsole extends JavaPlugin implements CommandExecutor {
    @SuppressWarnings("unused")
    private Main main;
    public CommandExecutorCallConsole(Main main){this.main = main;}
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("callconsole") && args.length == 1){
    String password = getConfig().getString("Call console password");
    if (!(sender instanceof Player)) {
    sender.sendMessage("Only ingame players may call the console.");
    return true;
    } else {
    if(args[0] == password){
    sender.sendMessage("Calling the console...");
    JOptionPane.showMessageDialog(null, sender.getName() + " is calling you via your server!","Mycelium+",JOptionPane.PLAIN_MESSAGE);
    return true;
    } else {
    sender.sendMessage(ChatColor.RED + "Incorrect password!");
    return true;
    }
    }
    }
    return false;
    }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  26. I can see you're still extending JavaPlugin... use main.getConfig() and remove JavaPlugin extension already.

    But that code is a pain to read, indent your code if you want help next time.
     
  27. Offline

    kreashenz

    Also, shouldn't return false be after wrong things, like as he has, if they spell the console password wrong, it sends them the message, then return true.. Wouldn't that just allow the players to go ahead? But here's the code I hopefully just fixed.
    Main class
    Code:java
    1.  
    2. package me.conormcs.myceliumPlus;
    3.  
    4. import java.io.IOException;
    5. import org.bukkit.Server;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin
    9. {
    10. public void onEnable()
    11. {
    12. getConfig().options().copyDefaults(true);
    13. saveDefaultConfig();
    14.  
    15. boolean recipe = getConfig().getBoolean("recipe");
    16. if(recipe == true)
    17. {
    18. Server server = getServer();
    19. CraftStackMycelium.CreateRecipe(server);
    20. }
    21.  
    22. boolean bonemeal = getConfig().getBoolean("bonemeal");
    23. if(bonemeal)
    24. {
    25. PlayerListener pl = new PlayerListener(this);
    26. getServer().getPluginManager().registerEvents(pl, this);
    27. }
    28.  
    29. getCommand("throw").setExecutor(new CommandExecutorThrow());
    30.  
    31. boolean callConsole = getConfig().getBoolean("Call console");
    32. if(callConsole == true){
    33. getCommand("callconsole").setExecutor(new CommandExecutorCallConsole(this));
    34. }
    35.  
    36. try {
    37. Metrics metrics = new Metrics(this);
    38. metrics.start();
    39. } catch (IOException e) {
    40. getLogger().severe("Metrics error");
    41. e.printStackTrace();
    42. getLogger().severe("Metrics error");
    43. }
    44. }
    45.  
    46.  
    47. }

    Command executor
    Code:java
    1. package me.conormcs.myceliumPlus;
    2.  
    3. import javax.swing.JOptionPane;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10.  
    11. public class CommandExecutorCallConsole implements CommandExecutor {
    12.  
    13. private Main main;
    14.  
    15. public CommandExecutorCallConsole(Main main)
    16. {
    17. this.main = main;
    18. }
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    20. if(cmd.getName().equalsIgnoreCase("callconsole") && args.length == 1){
    21. String password = main.getConfig().getString("Call console password");
    22. if (!(sender instanceof Player)) {
    23. sender.sendMessage("Only ingame players may call the console.");
    24. return true;
    25. } else {
    26. if(args[0] == password){
    27. sender.sendMessage("Calling the console...");
    28. JOptionPane.showMessageDialog(null, sender.getName() + " is calling you via your server!","Mycelium+",JOptionPane.PLAIN_MESSAGE);
    29. return true;
    30. } else {
    31. sender.sendMessage(ChatColor.RED + "Incorrect password!");
    32. return true;
    33. }
    34. }
    35. }
    36. return false;
    37. }
    38. }
     
  28. Offline

    Techy4198

    did it not save properly? I already did remove the javaplugin bit! and sorry about the indentation being messed up but I copied kreashenz' code directly into eclipse, and forgot to indent, because I just wanted to see if it worked. I edit this with the indented & updated code when ive done it
     
  29. Techy4198
    If using Eclipse use Ctrl+Shift+F for quick formatting or Ctrl+A and Ctrl+I for indentation only.
     
  30. Offline

    Techy4198

    ok its all good, but now it won't accept my password. in config.yml do I put
    Code:
    Call console password: YourPasswordHere
    or
    Code:
    Call console password: "YourPasswordHere"
    ?

    thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page