Hey! I've managed to create a plugin where you can protect areas and such, and then saving them to a arraylist. But now i'm wondering on how i could save the Arraylists? I'd like to have the arraylist saved in a config file, because then i can easily delete/modify it through there. Thanks in advance, -jimbo out.
When you have your config.yml created (through saveDefaultConfig()), you can simply do this: Code: // save getConfig().set("path.to.list", list); saveConfig(); // retrieve List<String> list = getConfig().getStringList("path.to.list");
Keep in mind the generic type you pass to the arraylist has to be serializable / configurationserializable
Chinwe Thanks for a so incredibly quick answer! I also can't load it for some reason(from the main config), i don't know why I get a stacktrace when i try to save it too, can't figure it out; Code: 16:14:28 [SEVERE] Cannot load plugins\Mindwork\regionConfig.yml org.bukkit.configuration.InvalidConfigurationException: could not determine a co nstructor for the tag tag:yaml.org,2002:org.bukkit.Location in "<string>", line 3, column 5: - !!org.bukkit.Location ^ at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlCo nfiguration.java:55) at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguratio n.java:138) at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguratio n.java:105) at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Yam lConfiguration.java:175) at me.Thomas.mindwork.Mindwork.reloadregionConfig(Mindwork.java:269) at me.Thomas.mindwork.Mindwork.getregionConfig(Mindwork.java:260) at me.Thomas.mindwork.Mindwork.onEnable(Mindwork.java:240) at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217) at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader .java:457) at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage r.java:381) at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav a:282) at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer. java:264) at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:60 9) at org.bukkit.Bukkit.reload(Bukkit.java:277) at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java: 23) at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19 2) at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe r.java:523) at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon nection.java:959) at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j ava:877) at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java :834) at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49) at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296 ) at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java :116) at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37) at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3 0) at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5 92) at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2 27) at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4 88) at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java :421) at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5 83) Caused by: could not determine a constructor for the tag tag:yaml.org,2002:org.b ukkit.Location in "<string>", line 3, column 5: - !!org.bukkit.Location ^ at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructUndefined.con struct(SafeConstructor.java:475) at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseCo nstructor.java:183) at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2 (BaseConstructor.java:277) at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(Base Constructor.java:248) at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.const ruct(SafeConstructor.java:440) at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseCo nstructor.java:183) at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndSte p(BaseConstructor.java:326) at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndSte p(SafeConstructor.java:143) at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseC onstructor.java:307) at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.const ruct(SafeConstructor.java:459) at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.c onstruct(YamlConstructor.java:26) at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseCo nstructor.java:183) at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndSte p(BaseConstructor.java:326) at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndSte p(SafeConstructor.java:143) at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseC onstructor.java:307) at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.const ruct(SafeConstructor.java:459) at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.c onstruct(YamlConstructor.java:26) at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseCo nstructor.java:183) at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(Base Constructor.java:142) at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseCons tructor.java:128) at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:480) at org.yaml.snakeyaml.Yaml.load(Yaml.java:399) at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlCo nfiguration.java:53) ... 29 more It's pointing to this area; Code:java private FileConfiguration getregionConfig() {if (regionConfig == null) {reloadregionConfig();}return regionConfig;}public void reloadregionConfig() {if (regionConfigFile == null) {regionConfigFile = new File(getDataFolder(),"regionConfig.yml");}regionConfig = YamlConfiguration.loadConfiguration(regionConfigFile); InputStream defregionConfig = this.getResource("regionConfig.yml");if (defregionConfig != null) {YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defregionConfig);regionConfig.setDefaults(defConfig);}} public FileConfiguration saveregionConfig() {if (regionConfig == null || regionConfigFile == null) {return regionConfig;}try {getregionConfig().save(regionConfigFile);} catch (IOException ex) {getLogger().log(Level.SEVERE,"Kunne ikke lagre koordinater til " + regionConfigFile, ex);}return null;} Line 269: Code:java regionConfig = YamlConfiguration.loadConfiguration(regionConfigFile); Line 260: Code:java reloadregionConfig(); Line 240(in onEnable): Code:java getregionConfig().options().copyDefaults(true); Thanks!
Does your List contain Locations? Like 1Rogue said, the contents have to be serializable, which Locations are not: you could convert them to a String (x,y,z,worldname) and save that, or make your own SerializableLocation object
Yeah, my ArrayList does contain Locations. I haven't touched Serializable objects and such before, so i have no idea what to do with that/how to make one How can i convert them to a String? I might sound like a idiot, but i think i'm just dumb today Some days, i'm like, "How do i make a command?!" *thinking for hours* "Stupid me! I've done this tons of times" Chinwe
You would just need a simple method that creates a string from location.getWorld().getName() + location.getBlockX() + etc... Ie, this
Why do i need all of this at all? It did save in the default config, or do you mean that i need to serialize it to load it? Chinwe Thanks [EDIT] I think i understand what you want with the serializing, if i understood it right, i have already done this? Code:java for (int z = miz; z <= maz; z++) { Location location = new Location(player.getWorld(), x, y, z); plugin.protectedBlocks.add(location); blockCounter++; } A small little shameless bump :> EDIT by Moderator: merged posts, please use the edit button instead of double posting.
What I mean is you reduce all Locations to Strings, which you can then save in the config, ie: Code: List<Location> originalList = //blahblah List<String> newList = new ArrayList<String>(); // convert them all for (Location l : originalList) newList.add(packageLocation(l)); // save them config.set("path.to.list", newList); // loading from config originalList.clear(); newList = config.getStringList("path.to.list"); // convert for (String s : newList) originalList.add(unpackageLocation(s));