Solved how do i save a hashmap and a list to a config

Discussion in 'Spigot Plugin Development' started by KaboomRoads, Jul 26, 2021.

  1. so how could i convert the string: key
    to the Location: key
     
  2. That strongly depends on the string format.
    Can you show me the yml file contents?
     
  3. Its very long so imma just send 5 keys:
    Code (Text):
    blockStrengthConfig:
      Location{world=CraftWorld{name=world},x=-147:
        0,y=64:
          0,z=-3:
            0,pitch=0:
              0,yaw=0:
                0}: 1200
        0,y=63:
          0,z=4:
            0,pitch=0:
              0,yaw=0:
                0}: 800
          0,z=2:
            0,pitch=0:
              0,yaw=0:
                0}: 1200
          0,z=0:
            0,pitch=0:
              0,yaw=0:
                0}: 1200
          0,z=-2:
            0,pitch=0:
              0,yaw=0:
                0}: 1600
    so there are the locations and then the numbers like 800 1200 1600 and so on are the integers
     
  4. Looking at this mess, I would not go through with this.
    Instead, just make a list like this:


    Code (Text):
    list:
    loc1:
       x: 0
       y: 0
       z: 0
       world: 'name'
       strength: 4
    Just make an index key that is equal to the entire list size + 1.
    Then you can just parse the location by doing.


    Code (Java):
    int x = config(key+".x");
    int y = config(key+".y");
    int z = config(key+".z");
    World world = getWorld(config(key+".z"));

    Location loc = new Location(world, x, y, z);
     
  5. i made a list but im not sure what u meant by the index thing
    Code (Text):
    blockStrengthConfig:
      '[-143, 63, 0, world]': 1000
      '[-144, 63, 1, world]': 1000
      '[-144, 63, 0, world]': 1000
      '[-145, 63, 1, world]': 1000
    with this code:
    Code (Java):
        public void saveToConfig()
        {
            for(Map.Entry<Location, Integer> entry : blockStrength.entrySet())
            {
                int x = (int) entry.getKey().getX();
                int y = (int) entry.getKey().getY();
                int z = (int) entry.getKey().getZ();
                String world = entry.getKey().getWorld().getName();
                List<String> list = new ArrayList<String>();
                list.add("" + x);
                list.add("" + y);
                list.add("" + z);
                list.add("" + world);
                this.getConfig().set("blockStrengthConfig." + list, entry.getValue());
            }
            this.saveConfig();
        }
     
    #25 KaboomRoads, Jul 27, 2021
    Last edited: Jul 27, 2021
  6. Okay, it seems you don't quiet understand me.
    I wrote you a little example of how to fetch and store the locations.

    Code (Java):
    Configuration config = null;

    {
        // store a new location //
        int index = config.getConfigurationSection("blockStrengthConfig").getKeys(false).size()+1;
        Location location = null;
        Integer strength = 1;
        String key = "blockStrengthConfig.location"+index+".";
        config.set(key + "x", location.getBlockX());
        config.set(key + "y", location.getBlockY());
        config.set(key + "z", location.getBlockZ());
        config.set(key + "world", location.getWorld().getName());
        config.set(key + "strength", strength);
    }


    {
        // fetch location //
        config.getConfigurationSection("blockStrengthConfig").getKeys(false).forEach(k -> {
            String key = "blockStrengthConfig."+k+".";

            int x = config.getInt(key + "x");
            int y = config.getInt(key + "y");
            int z = config.getInt(key + "z");
            World world = Bukkit.getWorld(config.getString(key + "world"));

            int strength = config.getInt(key + "strength");

            Location loc = new Location(world, x, y, z);
        });
    }
     
  7. ahh so its not actually a List but its a listing of the values.
    ok so it failed to disable the plugin and turns out something is null
    java.lang.NullPointerException: null
    from this code:
    Code (Java):
    public void saveToConfig()
        {
            for(Map.Entry<Location, Integer> entry : blockStrength.entrySet())
            {
                int index = this.getConfig().getConfigurationSection("blockStrengthConfig").getKeys(false).size() + 1;
                Location location = entry.getKey();
                Integer strength = entry.getValue();
                String key = "blockStrengthConfig.location" + index + ".";
                this.getConfig().set(key + "x", location.getBlockX());
                this.getConfig().set(key + "y", location.getBlockY());
                this.getConfig().set(key + "z", location.getBlockZ());
                this.getConfig().set(key + "world", location.getWorld().getName());
                this.getConfig().set(key + "strength", strength);
            }
            this.saveConfig();
        }
    Edit: it might be because the configuration section does not exist
    Edit 2: that is not the reason
    Edit 3: or is it?
     
    #27 KaboomRoads, Jul 27, 2021
    Last edited: Jul 27, 2021
  8. I can only tell if you give me the stacktrace of the error.
     
  9. here is the stacktrace:
    Code (Text):
    Error occurred while disabling MrPlugin v1.0
    java.lang.NullPointerException: null
            at me.Muchmu.MrPlugin.Main.saveToConfig(Main.java:4043) ~[?:?]
            at me.Muchmu.MrPlugin.Main.onDisable(Main.java:168) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:265) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:367) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:501) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:493) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:544) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:845) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.Bukkit.reload(Bukkit.java:651) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:761) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:1936) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:1779) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1732) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:49) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:28) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1061) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1054) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:1038) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:970) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:273) ~[spigot-1.16.5.jar:3096-Spigot-9fb885e-296df56]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_301]
     
  10. What is at line 4043?
     
  11. Integer strength = entry.getValue();
     
  12. That means the HashEntry is empty so this is related to the hashmap called 'blockStrength'.
     
  13. thats weird because i have checks before even running the method so i have no idea how that could have happened
     
  14. i have made a lot of progress but now there is a small problem. everything that was in the config before and was not changed is still there so how would i wipe the config before i put the new stuff in
    ive heard that i should use save default config but it doesent work, in fact if i delete the config it doesent even make a new one
    solved it:
    Code (Java):
            File configFile = new File(this.getDataFolder(), "config.yml");
            if(!configFile.exists())
            {
                File config = new File(this.getDataFolder() + File.separator + "config.yml");
                try {
                    config.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    ok not really it only resets the config if im not changing it
    ok fixed it again:
    Code (Java):
        @Override
        public void onDisable()
        {
            File configFile = new File(this.getDataFolder(), "config.yml");
            if(configFile.exists())
            {
                configFile.delete();
                File config = new File(this.getDataFolder() + File.separator + "config.yml");
                try {
                    config.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            this.reloadConfig();
            if(!blockStrength.isEmpty())
                this.saveToConfig();
        }
    and then the previous code in on enable to avoid glitches and stuff
     
    #34 KaboomRoads, Jul 28, 2021
    Last edited: Jul 28, 2021
  15. FINALLY DID IT

    Thank you all for the help, and mostly DanielTheDev for
    giving me great ideas on how to store the locations and more.

    this on enable:
    Code (Java):
            File configFile = new File(this.getDataFolder(), "config.yml");
            if(!configFile.exists())
            {
                File config = new File(this.getDataFolder() + File.separator + "config.yml");
                try {
                    config.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    Code (Java):
        public void restoreFromConfig()
        {
            this.getConfig().getConfigurationSection("blockStrengthConfig").getKeys(false).forEach(key -> {
                if(key.contains("location"))
                {
                    int x = this.getConfig().getInt("blockStrengthConfig." + key + ".x");
                    int y = this.getConfig().getInt("blockStrengthConfig." + key + ".y");
                    int z = this.getConfig().getInt("blockStrengthConfig." + key + ".z");
                    World world = Bukkit.getWorld((String) this.getConfig().get("blockStrengthConfig." + key + ".world"));
                    int integer = this.getConfig().getInt("blockStrengthConfig." + key + ".blockStrength");
                    Location location = new Location(world, x, y, z);
                    blockStrength.put(location, integer);
                }
            });
            this.getConfig().getConfigurationSection("blocksMinableConfig").getKeys(false).forEach(key -> {
                if(key.contains("location"))
                {
                    int x = this.getConfig().getInt("blocksMinableConfig." + key + ".x");
                    int y = this.getConfig().getInt("blocksMinableConfig." + key + ".y");
                    int z = this.getConfig().getInt("blocksMinableConfig." + key + ".z");
                    World world = Bukkit.getWorld("" + this.getConfig().get("blocksMinableConfig." + key + ".world"));
                    Location location = new Location(world, x, y, z);
                    blocksMinable.add(location);
                }
            });
        }
    this on disable:
    Code (Java):
            File configFile = new File(this.getDataFolder(), "config.yml");
            if(configFile.exists())
            {
                configFile.delete();
                File config = new File(this.getDataFolder() + File.separator + "config.yml");
                try {
                    config.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            this.reloadConfig();
    Code (Java):
        public void saveToConfig()
        {
            for(Map.Entry<Location, Integer> entry : blockStrength.entrySet())
            {
                this.getConfig().set("blockStrengthConfig.index", "index");
                int index = this.getConfig().getConfigurationSection("blockStrengthConfig").getKeys(false).size() + 1;
                int x = entry.getKey().getBlockX();
                int y = entry.getKey().getBlockY();
                int z = entry.getKey().getBlockZ();
                String world = entry.getKey().getWorld().getName();
                int blockStrength = entry.getValue();
                this.getConfig().set("blockStrengthConfig.location" + index + ".x", x);
                this.getConfig().set("blockStrengthConfig.location" + index + ".y", y);
                this.getConfig().set("blockStrengthConfig.location" + index + ".z", z);
                this.getConfig().set("blockStrengthConfig.location" + index + ".world", world);
                this.getConfig().set("blockStrengthConfig.location" + index + ".blockStrength", blockStrength);
            }
            for(Location loc : blocksMinable)
            {
                this.getConfig().set("blocksMinableConfig.index", "index");
                int index = this.getConfig().getConfigurationSection("blocksMinableConfig").getKeys(false).size() + 1;
                int x = loc.getBlockX();
                int y = loc.getBlockY();
                int z = loc.getBlockZ();
                String world = loc.getWorld().getName();
                this.getConfig().set("blocksMinableConfig.location" + index + ".x", x);
                this.getConfig().set("blocksMinableConfig.location" + index + ".y", y);
                this.getConfig().set("blocksMinableConfig.location" + index + ".z", z);
                this.getConfig().set("blocksMinableConfig.location" + index + ".world", world);
            }
            this.saveConfig();
        }
     
    #35 KaboomRoads, Jul 28, 2021
    Last edited: Jul 29, 2021