Using constructor and getCommand

Discussion in 'Plugin Development' started by xiTango, May 21, 2016.

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

    xiTango

    Hey all! I am working on making a /sethome plugin, and I have ran into a bit of a problem. I have the Main class, which imports all the commands, and the SetHome class. I have used a constructor to create an instance of the main class for the SetHome class to access to it can edit the config, however when I come to use the getCommand("sethome").setExecutor(new SetHome()): , it returns an error and does not let me leave it like that. Here is my code, I hope you can help.

    Main Class (open)

    Code:
    package com.xiTango.xiEssentials;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.xiTango.xiEssentials.commands.Teleport;
    import com.xiTango.xiEssentials.commands.Craft;
    import com.xiTango.xiEssentials.commands.Donate;
    import com.xiTango.xiEssentials.commands.Feed;
    import com.xiTango.xiEssentials.commands.Heal;
    import com.xiTango.xiEssentials.commands.MOTD;
    import com.xiTango.xiEssentials.commands.PlayerJoin;
    import com.xiTango.xiEssentials.commands.SetHome;
    import com.xiTango.xiEssentials.commands.SetSpawn;
    import com.xiTango.xiEssentials.commands.Spawn;
    
    public class Main extends JavaPlugin{
    
        public void onEnable(){
                getLogger().info("Plugin Initialized!");
                Teleport tpa = new Teleport();
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new PlayerJoin(), this);
               
                final FileConfiguration config = this.getConfig();
                config.options().copyDefaults(true);
                saveConfig();
    
                getCommand("tpa").setExecutor(tpa);
                getCommand("tpaccept").setExecutor(tpa);
                getCommand("tpdeny").setExecutor(tpa);
                getCommand("sethome").setExecutor(new SetHome());
        }
    }
    

    SetHome Class (open)

    Code:
    package com.xiTango.xiEssentials.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.xiTango.xiEssentials.Main;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class SetHome implements CommandExecutor {
       
        Main plugin;
       
        public SetHome(Main instance){
            plugin = instance;
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sethome")){
                if (player.hasPermission("xiEssentials.sethome")){
                    if (player.hasPermission("xiEssentials.sethome.three")){
                        if (!(args.length == 1)){
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.RED +
                                "Correct usage: /sethome <homename>");
                            return false;
                        }else{
                            String world = player.getWorld().getName();
                            double x = player.getLocation().getX();
                            double y = player.getLocation().getY();
                            double z = player.getLocation().getZ();
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".world", world);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".x", x);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".y", y);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".z", z);
                            plugin.saveConfig();
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.AQUA +
                                    "Home " + args[0] + " succesfully set!");
                            return true;
                        }
                    }
                   
                   
                }
               
            }
            return false;
        }
       
       
    
    }
    

    I have made sure to put all the commands into my plugin.yml, there is also and error message in the console when I try /sethome hey.

    Console Error (open)

    Code:
    [19:52:04 INFO]: xiTango issued server command: /sethome hey
    [19:52:04 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sethome' in plugin xiEssentials v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at com.xiTango.xiEssentials.commands.SetHome.onCommand(SetHome.java:41) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            ... 15 more


    Thanks for any help!
     
  2. Offline

    DoggyCode™

    Line 41 in SetHome.java

    This line returns null: plugin.getConfig().set(player.getName()+"."+ args[0]+".y", y); aka it throws a NullPointerException

    This means that it doesn't exist in the config.
     
  3. Offline

    xiTango

    But that line is writing to the config, so why would it say it doesn't exist?
     
  4. Offline

    DoggyCode™

    If your IDE displays line numbers on the left, go to the SetHome class and tell me exactly what it says at Line 41!
     
  5. Offline

    xiTango

    plugin.getConfig().set(player.getName()+"."+ args[0]+".world", world);

    Is what it says at line 41
     
  6. Offline

    teej107

    @xiTango
    That line does not return null. The line itself is throwing a NullPointerException which means that one of your Objects you are using is null. Make sure you have your variables initialized and read this: https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/
     
  7. Offline

    xiTango

    Ok, thanks for that, I will try do do that in future. However I think the problem is with the way I have a constructor creating and instance of my Main class within my SetHome class, and it is not allowing my to use the setExecutor within my main class unless i create another constructor, but my doing that I am making it so line 41 (plugin.getConfig()...) does not wortk. Any help on how I can get around this?
     
  8. Offline

    teej107

    Look at the Objects you are using on that line. Make sure each one of them are not null.
     
  9. Offline

    xiTango

    Ok, none of them seem to be null, I made it so it would convert the doubles to string and then send the player the string of all 4 values, and it all seemed to work well. So it does not seem to be a problem with values.

    Here is the sethome class I tested this on:
    SetHome (open)

    Code:
    package com.xiTango.xiEssentials.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.xiTango.xiEssentials.Main;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class SetHome implements CommandExecutor {
      
        Main plugin;
      
        public SetHome(Main instance){
            plugin = instance;
        }
      
        public SetHome() {
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sethome")){
                if (player.hasPermission("xiEssentials.sethome")){
                    if (player.hasPermission("xiEssentials.sethome.three")){
                        if (!(args.length == 1)){
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.RED +
                                "Correct usage: /sethome <homename>");
                            return false;
                        }else{
                            String world = player.getWorld().getName();
                            double x = player.getLocation().getX();
                            double y = player.getLocation().getY();
                            double z = player.getLocation().getZ();
                            String sx = new Double(x).toString();
                            String sy = new Double(y).toString();                      
                            String sz = new Double(z).toString();                      
                            player.sendMessage(world);
                            player.sendMessage(sx);
                            player.sendMessage(sy);
                            player.sendMessage(sz);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".world", world);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".x", x);      
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".y", y);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".z", z);
                            plugin.saveConfig();
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.AQUA +
                                    "Home " + args[0] + " succesfully set!");
                            return true;
                        }
                    }
                }
            }
            return false;
        }
      
      
    
    }
    


    It still has a NullPointerException at line 48.
     
    Last edited: May 23, 2016
  10. Offline

    Max8801

    Does the config file generate correctly after the plugin enables? If it does not the getConfig() method you call in line 41 cannot return anything which makes the plugin throw a NullPointerException
     
    Last edited: May 23, 2016
  11. Offline

    xiTango

    Well the config file seems to generate empty, but i would expect that to happen because i have not added any values to it until I use /sethome
    Please correct me if i am wrong.
     
  12. Offline

    Max8801

    So if a config file exists and you have ensured that none of the objects in the line are null, I basically have no idea what should throw that Exception.
     
  13. Offline

    xiTango

    Ok, well I think it is something to do with the 'plugin.getConfig()...', specifically the plugin part, if you look at my main class:

    Main (open)

    Code:
    package com.xiTango.xiEssentials;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.xiTango.xiEssentials.commands.Teleport;
    import com.xiTango.xiEssentials.commands.Craft;
    import com.xiTango.xiEssentials.commands.Donate;
    import com.xiTango.xiEssentials.commands.Feed;
    import com.xiTango.xiEssentials.commands.Heal;
    import com.xiTango.xiEssentials.commands.MOTD;
    import com.xiTango.xiEssentials.commands.PlayerJoin;
    import com.xiTango.xiEssentials.commands.SetHome;
    import com.xiTango.xiEssentials.commands.SetSpawn;
    import com.xiTango.xiEssentials.commands.Spawn;
    
    public class Main extends JavaPlugin{
    
        public void onEnable(){
                getLogger().info("Plugin Initialized!");
                Teleport tpa = new Teleport();
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new PlayerJoin(), this);
               
                final FileConfiguration config = this.getConfig();
                config.options().copyDefaults(true);
                saveConfig();
                           
                getCommand("heal").setExecutor(new Heal());
                getCommand("feed").setExecutor(new Feed());
                getCommand("spawn").setExecutor(new Spawn());
                getCommand("setspawn").setExecutor(new SetSpawn());
                getCommand("tpa").setExecutor(tpa);
                getCommand("tpaccept").setExecutor(tpa);
                getCommand("tpdeny").setExecutor(tpa);
                getCommand("donate").setExecutor(new Donate());
                getCommand("motd").setExecutor(new MOTD());
                getCommand("craft").setExecutor(new Craft());
                getCommand("sethome").setExecutor(new SetHome());
        }
    }
    


    I have imported the SetHome class to use .setExecutor, but on my SetHome class, eclipse wants me to create a new Constructor to import the SetHome class, however I already have a constructor creating an instance of the main class in the SetHome class so i can use .getConfig.

    Here is my Sethome class:

    SetHome (open)

    Code:
    package com.xiTango.xiEssentials.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import com.xiTango.xiEssentials.Main;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class SetHome implements CommandExecutor {
       
        Main plugin;
       
        public SetHome(Main instance){
            plugin = instance;
        }
       
        public SetHome() {
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players can use this command!");
                return false;
            }
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("sethome")){
                if (player.hasPermission("xiEssentials.sethome")){
                    if (player.hasPermission("xiEssentials.sethome.three")){
                        if (!(args.length == 1)){
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.RED + 
                                "Correct usage: /sethome <homename>");
                            return false;
                        }else{
                            String world = player.getWorld().getName();
                            double x = player.getLocation().getX();
                            double y = player.getLocation().getY();
                            double z = player.getLocation().getZ();
                            String sx = new Double(x).toString();
                            String sy = new Double(y).toString();                       
                            String sz = new Double(z).toString();                       
                            player.sendMessage(world);
                            player.sendMessage(sx);
                            player.sendMessage(sy);
                            player.sendMessage(sz);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".world", world);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".x", x);       
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".y", y);
                            plugin.getConfig().set(player.getName() + "." + args[0] + ".z", z);
                            plugin.saveConfig();
                            player.sendMessage(ChatColor.GRAY + "[" + ChatColor.BLUE + "Homes" + ChatColor.GRAY + "] " + ChatColor.AQUA + 
                                    "Home " + args[0] + " succesfully set!");
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    }
    
    


    As you can see, I have two constructor, one for creating the instance of the Main class, and the other, im not too sure, but it will not work without it.

    Anyone have any idea what i can do here?
     
  14. Offline

    timtower Administrator Administrator Moderator

    @xiTango Remove the second constructor, use the first one in your main class
     
  15. Offline

    xiTango

    eclipse wont let me do that, it wants me to do one of these things:

    [​IMG]

    Though none of them actually help.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @xiTango First one, use it.
    You need to pass it the main class as argument.
     
  17. Offline

    xiTango

    Ok so I put 'new SetHome(this);' in the main class and chose the Add argument option, and still getting the same error.
     
  18. Offline

    timtower Administrator Administrator Moderator

    @xiTango Build the project.
    Restart Eclipse if needed.
     
  19. Offline

    xiTango

    If by build you mean export, I tried that and still getting g the same error
     
  20. Offline

    teej107

    Not true. The method returns an Object whether you have a config file created or not.

    @xiTango From all the Objects you are using, I bet it's your "plugin" variable is null. All the other Objects you are using are supplied by Bukkit which won't be null. Print out debug statements to find out which one is null before the error occurs.
     
Thread Status:
Not open for further replies.

Share This Page