Solved Command not working?!

Discussion in 'Plugin Development' started by DaanSander, Feb 9, 2015.

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

    DaanSander

    Hello i have created a plugin with warp that can be saved but when i try to execute the command that sets the warp it isnt setting it but it is giving me a internal error

    warps class:
    Code:
    package me.daansander.hmcessentials.cmds;
    
    import me.daansander.hmcessentials.SettingsManager;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    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;
    
    /**
    * Created by Daan on 29-1-2015.
    */
    public class Warps implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
                return true;
            }
    
            Player p = (Player) sender;
    
    
            if (cmd.getName().equalsIgnoreCase("setlobby")) {
                settings.getData().set("spawn.world", p.getLocation().getWorld().getName());
                settings.getData().set("spawn.x", p.getLocation().getX());
                settings.getData().set("spawn.y", p.getLocation().getY());
                settings.getData().set("spawn.z", p.getLocation().getZ());
                settings.saveData();
                p.sendMessage(ChatColor.GREEN + "Lobby set!");
                return true;
            }
    
            if ((cmd.getName().equalsIgnoreCase("hub") || cmd.getName().equalsIgnoreCase("lobby"))) {
                if (settings.getData().getConfigurationSection("spawn") == null) {
                    p.sendMessage(ChatColor.RED + "§lCould not connect to the lobby server");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("spawn.world"));
                double x = settings.getData().getDouble("spawn.x");
                double y = settings.getData().getDouble("spawn.y");
                double z = settings.getData().getDouble("spawn.z");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage("§2§lTeleporting to hub");
                return true;
            }
    
            if (cmd.getName().equalsIgnoreCase("setserver")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a server name!");
                    return true;
                }
                settings.getData().set("warps." + args[0] + ".world", p.getLocation().getWorld().getName());
                settings.getData().set("warps." + args[0] + ".x", p.getLocation().getX());
                settings.getData().set("warps." + args[0] + ".y", p.getLocation().getY());
                settings.getData().set("warps." + args[0] + ".z", p.getLocation().getZ());
                settings.saveData();
                p.sendMessage(ChatColor.GREEN + "Set server " + args[0] + "!");
                return true;
            }
    
            if (cmd.getName().equalsIgnoreCase("server")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a server!");
                    return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                    p.sendMessage(ChatColor.RED + "§lCould not connect to " + args[0]);
                    return true;
                }
                World w = Bukkit.getServer().getWorld(settings.getData().getString("warps." + args[0] + ".world"));
                double x = settings.getData().getDouble("warps." + args[0] + ".x");
                double y = settings.getData().getDouble("warps." + args[0] + ".y");
                double z = settings.getData().getDouble("warps." + args[0] + ".z");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "§lSending you to server " + args[0] + "!");
                return true;
            }
    
            if (cmd.getName().equalsIgnoreCase("delserver")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a name!");
                    return true;
                }
                if (settings.getData().getConfigurationSection("warps." + args[0]) == null) {
                    p.sendMessage(ChatColor.RED + "§lCould not delete server " + args[0]);
                    return true;
                }
                settings.getData().set("warps." + args[0], null);
                settings.saveData();
                p.sendMessage(ChatColor.GREEN + "Removed server " + args[0] + "!");
                return true;
            }
            return true;
        }
    
    }
    
    settings manager class:
    Code:
    package me.daansander.hmcessentials;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    
    /**
    * Created by Daan on 8-2-2015.
    */
    public class SettingsManager {
    
        private SettingsManager() {
        }
    
        static SettingsManager instance = new SettingsManager();
    
        public static SettingsManager getInstance() {
            return instance;
        }
    
        Plugin p;
    
        FileConfiguration config;
        File cfile;
    
        FileConfiguration data;
        File dfile;
    
        FileConfiguration pdata;
        File pfile;
    
        public void setup(Plugin p) {
            cfile = new File(p.getDataFolder(), "config.yml");
            config = p.getConfig();
            //config.options().copyDefaults(true);
            //saveConfig();
    
            if (!p.getDataFolder().exists()) {
                p.getDataFolder().mkdir();
            }
    
            dfile = new File(p.getDataFolder(), "data.yml");
    
            if (!dfile.exists()) {
                try {
                    dfile.createNewFile();
                } catch (IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
                }
            }
            pfile = new File(p.getDataFolder(), "pdata.yml");
    
            if (!pfile.exists()) {
                try {
                    pfile.createNewFile();
                } catch (IOException e) {
                    Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not create data.yml!");
                }
            }
        }
    
        public FileConfiguration getData() {
            return data;
        }
    
        public void saveData() {
            try {
                data.save(dfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save data.yml!");
            }
        }
    
        public void reloadData() {
            data = YamlConfiguration.loadConfiguration(dfile);
        }
    
        public FileConfiguration getPfile() {
            return pdata;
        }
    
        public FileConfiguration getConfig() {
            return config;
        }
    
        public void saveConfig() {
            try {
                config.save(cfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save config.yml!");
            }
        }
    
        public void reloadConfig() {
            config = YamlConfiguration.loadConfiguration(cfile);
        }
    
        public PluginDescriptionFile getDesc() {
            return p.getDescription();
        }
    
        public void savepdata() {
            try {
                pdata.save(pfile);
            } catch (IOException e) {
                Bukkit.getServer().getLogger().severe(ChatColor.RED + "Could not save pdata.yml!");
            }
        }
        public void reloadPdata() {
            pdata = YamlConfiguration.loadConfiguration(pfile);
        }
    }
    
    
    main class:
    Code:
    package me.daansander.hmcessentials;
    
    import me.daansander.hmcessentials.BroadCaster.Messages;
    import me.daansander.hmcessentials.cmds.Gamemode;
    import me.daansander.hmcessentials.cmds.Tp;
    import me.daansander.hmcessentials.cmds.Warps;
    import me.daansander.hmcessentials.playerlisteners.Playerjoin;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.nio.file.WatchEvent;
    
    /**
    * Created by Daan on 29-1-2015
    */
    public class HMCEssentials extends JavaPlugin {
    
        public static HMCEssentials plugin;
    
        public static HMCEssentials getPlugin() {
            return plugin;
        }
    
        SettingsManager settings = SettingsManager.getInstance();
    
        public void onEnable() {
            plugin = this;
            settings.setup(this);
            Bukkit.getLogger().info(" Starting HMCEssentials by DaanSander");
            Bukkit.getServer().getPluginManager().registerEvents(new Playerjoin(), this);
            Bukkit.getLogger().info("Loading HMCEssentials...");
            Bukkit.getLogger().info("Starting broadcaster...");
            Messages.startbroadcast();
            Bukkit.getLogger().info("Loading commands...");
            Bukkit.getServer().getPluginManager().registerEvents(new Playerjoin(), this);
            getCommand("gm").setExecutor(new Gamemode());
            getCommand("tp").setExecutor(new Tp());
            getCommand("setlobby").setExecutor(new Warps());
            getCommand("lobby").setExecutor(new Warps());
            getCommand("hub").setExecutor(new Warps());
            getCommand("setserver").setExecutor(new Warps());
            getCommand("server").setExecutor(new Warps());
            getCommand("delserver").setExecutor(new Warps());
    
        }
        public void onDisable() {
            settings.saveConfig();
            settings.savepdata();
            settings.saveData();
        }
    }

    and it isnt generating the config file cfile
     
  2. Offline

    DJSkepter

    @DaanSander If it's throwing errors, post the error.
     
  3. Offline

    DaanSander

    this is the error:
    Code:
    12.02 10:04:45 [Server] INFO ... 14 more
    12.02 10:04:45 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at me.daansander.hmcessentials.cmds.Warps.onCommand(Warps.java:61) ~[?:?]
    12.02 10:04:45 [Server] INFO Caused by: java.lang.NullPointerException
    12.02 10:04:45 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.7.0_60]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_60]
    12.02 10:04:45 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_60]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8-R0.1-SNAPSHOT.jar:git-Spigot-e497721-96d09e3]
    12.02 10:04:45 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'setserver' in plugin HMCEssentials v1.0
    12.02 10:04:45 [Server] ERROR null
     
  4. Offline

    aaomidi

    Stop using statics. Also stop watching online tutorials.

    However your problem is simply that you're using something in the Warps class. line 61 that has not been initiated.

    But seriously stop using statics, 99% of your problems will be solved.
     
  5. Offline

    DaanSander

    thx man i have 1 question left why doest this work too?? its giving me a internal error:
    Code:
    package me.daansander.hmcessentials.cmds;
    
    import me.daansander.hmcessentials.HMCEssentials;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    /**
    * Created by Daan on 12-2-2015.
    */
    public class login implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("login") && sender instanceof Player) {
                Player p = (Player) sender;
                if(p.getName().equals("DaanSander")) {
                    if(args.length == 1) {
                        if(args[1].equals("Blank")) {
                            if(!HMCEssentials.login.contains(p.getUniqueId())) {
                                HMCEssentials.login.add(p.getUniqueId());
                                return true;
                            }
                            else {
                                p.sendMessage("§4§lYou are already logged in!");
                                return true;
                            }
                        }
                        else  {
                            p.sendMessage("§4§lWrong password a log has been send to a administrator");
                            return true;
                        }
                    }
                    else {
                        p.sendMessage("§4§lWrong arguments!");
                        return true;
                    }
                }
                else if (p.getName().equals("HELEMAAL_NIKS")) {
                    if(args.length == 1) {
                        if (args[1].equals("Blank")) {
                            if(!HMCEssentials.login.contains(p.getUniqueId())) {
                                HMCEssentials.login.add(p.getUniqueId());
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
    

    the command is registered
     
  6. Offline

    JustThiemoo

  7. Offline

    Justinian6

    @DaanSander What happens when the argument in the command is null?
     
  8. Offline

    DaanSander

    o lol that was the problem thx
     
  9. Offline

    Justinian6

    no prob
     
Thread Status:
Not open for further replies.

Share This Page