Solved Multiple Config Help

Discussion in 'Spigot Plugin Development' started by minecrft, May 29, 2016.

  1. Hello all!

    I am trying to create a plugin that allows you to set warps (not very hard), but I am running into an issue with creating a "warps.yml" file.
    Main -
    Code (Text):
    package main.bukkit.minecrft;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    import main.bukkit.minecrft.commands.SoulKraft;
    import main.bukkit.minecrft.listeners.InventoryClick;
    import main.bukkit.minecrft.listeners.PlayerChatEvent;
    import main.bukkit.minecrft.other.PluginFile;

    public class Main extends JavaPlugin {

        public static Plugin plugin;

     

        public void onEnable() {
            registerEvents(this, new PlayerChatEvent(), new InventoryClick());
            getCommand("soulkraft").setExecutor(new SoulKraft());
            plugin = this;
            new Warps().warpFile = new PluginFile(this, "warps.yml");
        }

        public void onDisable() {
            plugin = null;
        }

        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }

        public static Plugin getPlugin() {
            return plugin;
        }

    }
     
    Warps -
    Code (Text):
    package main.bukkit.minecrft;

    import main.bukkit.minecrft.other.PluginFile;

    public class Warps {

        public PluginFile warpFile;
     
    }

     
    PluginFile -
    Code (Text):
    package main.bukkit.minecrft.other;


    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;

    @SuppressWarnings("serial")
    public class PluginFile extends File {
     
        protected FileConfiguration fileConfig;

        public PluginFile(Plugin plugin, String name) {
            super(plugin.getDataFolder(), name);

            this.fileConfig = YamlConfiguration.loadConfiguration(this); // give settings a file to look into if it is the database file

            if (!this.exists()) {
                try {
                    createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            this.saveFile();
        }

        public void saveFile() {
            try {
                fileConfig.save(this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public FileConfiguration getFileConfig() {
            return this.fileConfig;
        }

        public void setFileConfig(FileConfiguration fileConfig) {
            this.fileConfig = fileConfig;
        }

    }
     
    Using the warps file -
    Code (Text):
    package main.bukkit.minecrft.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import main.bukkit.minecrft.Warps;
    import main.bukkit.minecrft.other.PluginFile;
    import net.md_5.bungee.api.ChatColor;

    public class SoulKraft implements CommandExecutor {
         
       
        PluginFile warpFile = new Warps().warpFile;
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("soulkraft") && sender instanceof Player) {
                Player p = (Player) sender;
                if (args.length == 0) {
                    p.sendMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "[]==+=={ " + ChatColor.DARK_PURPLE + "Help"
                            + ChatColor.GRAY + ChatColor.BOLD.toString() + " }==+==[]\n" + ChatColor.LIGHT_PURPLE
                            + "/SoulKraft warp - Warp Menu!\n" + ChatColor.GRAY + ChatColor.BOLD.toString() + "[]==+=={ "
                            + "----" + ChatColor.GRAY + ChatColor.BOLD.toString() + " }==+==[]");
                } else if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("help")) {
                        p.sendMessage(ChatColor.GRAY + ChatColor.BOLD.toString() + "[]==+=={ " + ChatColor.DARK_PURPLE
                                + "Help" + ChatColor.GRAY + ChatColor.BOLD.toString() + " }==+==[]\n"
                                + ChatColor.LIGHT_PURPLE + "/SoulKraft warp - Warp Menu!\n" + ChatColor.GRAY
                                + ChatColor.BOLD.toString() + "[]==+=={ " + "----" + ChatColor.GRAY
                                + ChatColor.BOLD.toString() + " }==+==[]");
                    } else if (args[0].equalsIgnoreCase("warp")) {
                 
                 
                    }
                } else if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("warp")) {
                        if(warpFile.getFileConfig().contains(args[1])) {
                            Location loc = new Location(Bukkit.getWorld(warpFile.getFileConfig().get(args[1] + ".world").toString()), (double) warpFile.getFileConfig().get(args[2] + ".x"), (double) warpFile.getFileConfig().get(args[2] + ".y"), (double) warpFile.getFileConfig().get(args[2] + ".z"));
                            p.teleport(loc);
                        } else {
                         
                        }
                    }
                } else if (args.length == 3) {
                    if (args[0].equalsIgnoreCase("warp")) {
                        if (args[1].equalsIgnoreCase("set")) {
                         
                            if (!(warpFile.getFileConfig().contains(args[2]))) {
                             
                                warpFile.getFileConfig().createSection(args[2] + ".x");
                                warpFile.getFileConfig().createSection(args[2] + ".y");
                                warpFile.getFileConfig().createSection(args[2] + ".z");
                                warpFile.getFileConfig().createSection(args[2] + ".world");
                                warpFile.getFileConfig().set(args[2] + ".x", Double.toString(p.getLocation().getX()));
                                warpFile.getFileConfig().set(args[2] + ".y", Double.toString(p.getLocation().getY()));
                                warpFile.getFileConfig().set(args[2] + ".z", Double.toString(p.getLocation().getZ()));
                                warpFile.getFileConfig().set(args[2] + ".world", p.getWorld().toString());
                                warpFile.saveFile();
                            }
                        }
                    }
                }
            }
            return true;
        }

    }
     
    It creates the file, but doesn't write to it just gives internal error. In the console it just throws a null pointer exception
    Code (Text):
    [13:04:07 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'soulkraft' in plugin SoulKraft v0.01
            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_74]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_74]
            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_74]
    Caused by: java.lang.NullPointerException
            at main.bukkit.minecrft.commands.SoulKraft.onCommand(SoulKraft.java:52) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.4.jar:git-Spigot-4af49dc-c5e9a16]
            ... 15 more
     
    #1 minecrft, May 29, 2016
    Last edited: May 29, 2016
  2. line 52 -
    Code (Text):
    if (!(warpFile.getFileConfig().contains(args[2]))) {
     
  3. Or if anyone has any other methods that would help?
     
  4. 1. Remove all statics and get instances
    2. Replace #label with cmd#getName()
    3. Your config is null so u cant grab from config. Learn to initialize.
    4. Debug your code
    5. Come back with results
     
  5. @TheBlackTeddy thanks for saying this! Before this, I didn't really go back and look at my code, I just kinda went for it. You reminded me to go back through all of it. Turns out I had some sillies errors (you probably already knew haha) And that fixed it! Works like a charm!
    Thanks!
     
    • Like Like x 1