Hello Guys Some days ago I started programming Bukkit 1.9 Plugins, but I just dont't understand how yml files work... I've checked many other posts but I just don't get it... I use Code: File yml = new File("plugins/tickets.yml"); if (!yml.exists()) { try { yml.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } every time I load the plugin, is this right or should I do this different? Anyway, the code above works, but how do I write something to this file now? I,ve tried Code: plugin.getConfig().set("the.location", "My message") but this won't write anything into my file... :/ Thanks, Tekk
Code: File yml = new File(getDataFolder(), "tickets.yml"); if (!yml.exists()) { try { yml.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } YamlConfiguration ticketsYml= YamlConfiguration.loadConfiguration(yml); ticketsYml.set("path.to.result", "HI"); ticketsYml.save(yml); Let me know if this works/helps
Official bukkit wiki: http://wiki.bukkit.org/Configuration_API_Reference#Using_an_alternate_configuration_file
Works better than my code, but there is still an error in the first line Code: File yml = new File(getDataFolder(), "tickets.yml"); by getDataFolder: The method getDataFolder() is undefined for the type ticketExecutor May this error appear because of this part of my code is in the command executor class?
Pass the plugin instance and get from there. The JavaPlugin class. Sendt fra min ALE-L21 med Tapatalk
Thank you, works now If someone other needs this: Code: public nameOfMainClass(Plugin plugin) { this.plugin = plugin; } Still have an issue... Here is my code: Code: // get senders name String name = sender.getName(); // create tickets.yml if file don't exists File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml"); if (!yml.exists()) { try { yml.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // fill tickets.yml YamlConfiguration ticketsYml = YamlConfiguration.loadConfiguration(yml); ticketsYml.set(name + ".title", parts[1]); try { ticketsYml.save(yml); } catch (IOException e) { e.printStackTrace(); } Error code in console: Code: [16:27:20 ERROR]: null org.bukkit.command.CommandException: Unhandled exception executing command 'tick et' in plugin easyTicket v0.1 at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi got-1.9.jar:git-Spigot-2038f4a-cd36f6f] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14 1) ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe r.java:645) ~[spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon nection.java:1350) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java :1185) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java :45) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java :1) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1 3) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [ ?:1.8.0_111] at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_111] at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9 .jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7 21) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4 00) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6 60) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java :559) [spigot-1.9.jar:git-Spigot-2038f4a-cd36f6f] at java.lang.Thread.run(Unknown Source) [?:1.8.0_111] Caused by: java.lang.NullPointerException at ch.tekk.easyTicketExecutor.onCommand(easyTicketExecutor.java:53) ~[?: ?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi got-1.9.jar:git-Spigot-2038f4a-cd36f6f] ... 15 more > Any ideas? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Assuming this is line #53 Code: ticketsYml.set(name + ".title", parts[1]); It could be the variable "name" is not set. Double check that at some point during the codes execution, this gets set. the CommandSender did not specify a second argument. Reminder: Arrays start at 0, not 1. To access the first(and sometime's only) object in an array, you need to use array[0].
If this is solved please mark it as sich. If not please give some more details on the error. What on line 53 in easyTicketExecutor.java? Sendt fra min ALE-L21 med Tapatalk
This cannot cause a NPE, except when ticketsYml is null, but it shouldn't if the file was created successfully (or is already there), the error must be somewhere else. 1. Would just result in: null.title 2. Would throw ArrayIndexOutOfBoundsException @Tekk Please post this line or we can't help you with that. Or even better: help yourself by learning how to troubleshoot errors (look at sticky post in plugin development forum)
If i remember correctly getdatafolder returns ~/plugins/PluginName? Sendt fra min ALE-L21 med Tapatalk
Woke up like 10 minute's before writing that reply, my bad xD Only thing i could think of is Code: File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml"); need's to be Code: File yml = new File(plugin.getDataFolder(), "tickets.yml"); Maybe because the /plugins/ dir doesnt exist, as plugin.getDataFolder() already directs them to the plugins/<plugin> folder
Hello, Line 53 is Code: File yml = new File(plugin.getDataFolder(), "plugins/tickets.yml"); so I canged it do Code: File yml = new File(plugin.getDataFolder(), "tickets.yml"); but I get the same result... And there is no tickes.yml file. I have no idea why this won't work anymore... This code is correct to create a new file, isn't it? Code: File yml = new File(plugin.getDataFolder(), "tickets.yml"); if (!yml.exists()) { try { yml.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Are you accessing the plugin object correctly? In your main JavaPlugin class, add this: Code: public static JavaPlugin plugin; then in your onEnable, add this: Code: plugin = this; then, when loading config's from other classes, use: Code: File yml = new File(<main class name>.plugin.getDataFolder(), "tickets.yml"); If you are trying to make the new file within the JavaPlugin class, you can just use: Code: File yml = new File(getDataFolder(), "tickets.yml");
@RelicObject Do not promote static abuse. Unless you know that the other member knows how to manage static objects (and even then, you don't need statics for this), do not recommend it. Insteads, he should pass the main instance through the other class's constructor.
Like Zombie said, i also wouldnt recommend permanently using the code that i used. More as a test to make sure that wasnt the problem, use constructor arguments to pass the javaplugin object as suggested above.
Thank you guys for you nice support, I tried the answere from RelicObject and it works perfectly now But what do you mean with "Do not promote static abuse"?