Can't save arraylist to custom config

Discussion in 'Plugin Development' started by alex27339, Apr 13, 2016.

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

    alex27339

    I am trying to make a custom minigame plugin and I need the arena to be saved and loaded to it's own config file every round. I this so far, it's suppose to find every block between to locations, save it to an arraylist, create new custom config file, and save the array list to the file.
    Code:
    package com.alex27339.kotl.setup;
    
    import java.io.File;
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import com.alex27339.kotl.Main;
    
    public class SaveArena implements CommandExecutor {
        public static ArrayList<Block> l1 = new ArrayList<Block>();
        public int minx = Main.minx;
        public int miny = Main.miny;
        public int minz = Main.minz;
        public int maxx = Main.maxx;
        public int maxy = Main.maxy;
        public int maxz = Main.maxz;
        public boolean min, max;
       
         private Main plugin;
    
            public SaveArena(Main plugin) {
                this.plugin = plugin;
            }   
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("kotl")) {
                if (sender instanceof Player) {
                    Player p = (Player) sender;
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("setmin")) {
                            minx = p.getLocation().getBlockX();
                            miny = p.getLocation().getBlockY();
                            minz = p.getLocation().getBlockZ();
                            Main.world1 = p.getWorld();
                            sender.sendMessage(ChatColor.GREEN + "Min successfully set!");
                            min = true;
                            return true;
                        }
                        if (args[0].equalsIgnoreCase("setmax")) {
                            maxx = p.getLocation().getBlockX();
                            maxy = p.getLocation().getBlockY();
                            maxz = p.getLocation().getBlockZ();
                            Main.world2 = p.getWorld();
                            sender.sendMessage(ChatColor.GREEN + "Max successfully set!");
                            max = true;
                            return true;
                        }
                        if (args[0].equalsIgnoreCase("add")) {
                            sender.sendMessage(ChatColor.RED + "Error! Incorrect usage! /kotl add <arenaname>");
                            return false;
                        }
                        return false;
                    }
                    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("add")) {
                            if (max && min) {
                                if (Main.world1 == Main.world2) {
                                    for (int x = minx; x <= maxx; ) {
                                        for (int z = minz; z <= maxz;) {
                                            for (int y = miny; y <= maxy;) {
                                                Block block = Main.world1.getBlockAt(x, y, z);
                                                l1.add(block);
                                                FileConfiguration data = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), args[1] + ".yml"));
                                                data.set("map", l1);
                                               
                                                return true;
                                                }
    
                                            }
                                            sender.sendMessage(
                                                    ChatColor.RED + "Error! The minimum y must be smaller then maximum y!");
                                            return false;
    
                                        }
                                        sender.sendMessage(
                                                ChatColor.RED + "Error! The minimum z must be smaller then maximum z!");
                                        return false;
    
                                    }
                                    sender.sendMessage(
                                            ChatColor.RED + "Error! The minimum x must be smaller then maximum x!");
                                    return false;
    
                                }
    
                            }
                        }
                    sender.sendMessage(ChatColor.RED + "Error! Set the minimum and maximum positions first!");
    
                    }
                    sender.sendMessage(ChatColor.RED + "Error! The arena must be in the same world!");
                    return false;
                }
                return false;
    
        }
    
    }
    
    But when I run the command to add the arena after setting the min and max coordinates I get a message saying an internal error occurred. Here's my log:
    Code:
    [17:28:29] [Server thread/INFO]: alex27339 issued server command: /kotl add yay
    [17:28:29] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'kotl' in plugin King_Of_The_Ladder v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-18fbb24]
        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_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_74]
    Caused by: java.lang.NullPointerException
        at com.alex27339.kotl.setup.SaveArena.onCommand(SaveArena.java:71) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-18fbb24]
        ... 15 more
    [17:31:33] [Server thread/WARN]: Can't keep up! Did the system time change, or is the server overloaded? Running 2694ms behind, skipping 53 tick(s)
    [17:36:34] [Server thread/WARN]: Can't keep up! Did the system time change, or is the server overloaded? Running 2206ms behind, skipping 44 tick(s)
    
    I'm not sure whats wrong so please someone help me.
     
  2. Offline

    mine-care

  3. Offline

    timtower Moderator Moderator

    Moved to plugin development.
     
  4. Offline

    Gonmarte

    If you cant troubleshot your own stacktraces i dont think you are ready to make a minigame on bukkit.
    Follow the link that @mine-care provided to you and if you need any more help (not relative to troubleshots consoles erros) tag me back.
     
    MasterDoctor and mine-care like this.
  5. Offline

    Zombie_Striker

    You do not own alex27339.com. Do not use it as a package name. Only use domains you own, or if you do not own any domains, use me.<name>.<project>

    Don't abuse static! Most of the time, you should never have to use static for anything. Statics should only be used when you are trying to get something before it even exists! It should not be used as a lazy way to get objects. Remove the static modifiers from those variables in the main class.

    Use cmd.getName() instead of label. Label should no longer be used, as it does not work with aliases.
     
    MasterDoctor likes this.
  6. Offline

    alex27339

    @Gonmarte and @mine-care
    I do know how to find the place of my error in the log

    Also I updated my code a little because I realized I didn't actually make the file:
    Code:
    for (int y = miny; y <= maxy;) {
                                                Block block = world1.getBlockAt(x, y, z);
                                                l1.add(block);
                                                File f = new File(plugin.getDataFolder(), args[1] + ".yml"); //Errored code
                                                try {
                                                    f.createNewFile();
                                                } catch (IOException e) {
                                                   
                                                    e.printStackTrace();
                                                }
                                                FileConfiguration data = YamlConfiguration.loadConfiguration(f);
                                                data.set("map", l1);
                                              
                                                return true;
                                                
    The place I labeled "Errored Code" is where the console is saying the error is, but I don't know what my error is.
     
  7. Offline

    Zombie_Striker

    • plugin can be null
    • The data folder is most likely not null
    • The second arg may not exist. Makes sure there are two args first before getting the second arg.
     
    MasterDoctor and mine-care like this.
  8. Offline

    alex27339

    Okay so I updated my code:
    Code:
    public static Main main;
    
        public Main() {
            main = this;
        }
    Code:
    File f = new File(Main.main.getDataFolder(), args[1] + ".yml");
    Now it creates the custom config with the name the user requested but it still doesn't save the arraylist to the config.
     
  9. Offline

    Zombie_Striker

    @alex27339
    1. Don't call your main class "Main". instead, call it the name of your plugin.
    2. A) You do not need a static instance of your main class. Remove it.
    3. Because of the above, you do not need the constructor for your main class.
    4. You never save your file after you set the values. Save it after you set the arraylist.
     
  10. Offline

    MasterDoctor

    OK, You're going a bit Java Nazi here, the others I can understand but there isn't a problem with this provided you use the plugin name in the package - otherwise they conflict
     
  11. Offline

    Zombie_Striker

    @MasterDoctor
    True, but although it is not necessary to name the main class of your plugin the same name as your plugin, It is
    A) Convention, which you should always follow.
    B) Will prevent any further problems with Class names (E.g What you already provided as an example), and
    C) Will help both you and any other developers reading your code with understanding what your class does/represents.

    So, although I do not really need to point this out, it is something that should be done.
     
  12. Offline

    MasterDoctor01

    I understand that, I was half-joking. But I do disagree with C - I think "Main" is more descriptive than the plugin name

    Sent from my SM-T710 using Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page