Hello everyone. I've successfully created a yml file in the appropriate plugin directory. Now I need my plugin to read it. I read it's supposed to be done with SnakeYAML, however, I have no idea on how to start it... Could anyone lend me a hand here? What I want my plugin to do, is check through the whole yml file and see if one of the lines is the same as a line I wrote in the actual plugin.
Well you no longer need to use SnakeYAML, bukkit already as methods for this Starting the reader: Code: private static final File myfile= new File("plugins/myfile.yml"); public Configuration load() { try { Configuration PluginPropConfig = new Configuration(myfile); PluginPropConfig.load(); return PluginPropConfig; } catch (Exception e) { } return null; } Reading something: Code: public int rank() { Configuration config = load(); String root = "quests.ranks"; return config.getInt(); } public String name( { try { Configuration config = load(); String root = "players.names" return config.getString(root); } catch (NullPointerException e) { return null; } }
Do you mind explaining a bit what some things do? 1. The "try" and "catch" thing 2. What does it load and how does it know what to load? 3.How does it know where to look in the file? It happens at the getString part, but I don't know much more... 4. What does NullPointerException do and the "e"? Sorry if these questions are bothersome, but I want to make sure I know what I'm doing so I can do it again without (much) help And of course to maybe expand the code you gave me.
As I'm Sammy's help ninja I'll jump in and explain (Personal Joke...Sorta) The try catch bit is basically saying try to do this.... and if you get this exception do this... Configuration throws a general exception. Loading configuration throws a NullPointerException, that's where the file can't be found. You can also get this printed in the console for help debugging by using exceptioneventvariable.printStackTrace(); eg inside the first try catch: } catch (Exception e) { e.printStackTrace(); } The same goes for the other catch event.
Sooo... It tries to "throw" something, like return PluginPropConfig; to load the variable, right? If it loads correct, it will catch with the exception, then execute whatever is standing next to "Exception" or "NullPointerException", correct? Would you mind awnsering my other 3 questions as well? I don't really understand these 2 parts: Code: public int rank() { Configuration config = load(); String root = "quests.ranks"; return config.getInt(root, 0); } public String name() { try { Configuration config = load(); String root = "players.names"; return config.getString(root); } catch (NullPointerException e) { return null; } For instance, how does it know what config it has to load, or where did it say what the variable "config" is? Then I don't understand what part it looks at when loading the yml file. Again sorry if I sound kinda stupid, but it's my third day of Java EDIT: did something wrong with Quote tags >.<
It's fine, we all have to start somewhere You've got it the wrong way round, Java will only throw the exception if something goes wrong. That's why I use e.printStackTrace(); so if something goes wrong I can see in the console. Onto your next question... Code: private static final File myfile= new File("plugins/myfile.yml"); ... Configuration PluginPropConfig = new Configuration(myfile); This code creates the new configuration file in the specified location, 'myfile'. Do you understand now? I'f you have more questions don't be hesitant
If you have already information in the config file and just wanna use it make this: first string: Code: public string KickMessage then in the reloadConfig: Code: KickMessage= config.getString("Kick-Message", "You kick Message in config"); now you just need to implent it. eg if someone gets kicked: Code: player.kickPlayer(plugin.KickMessage) I know i cant explain but i ve tired it Finished
Aaah.... Okay. So last thing, how does the plugin know what do read? For instance the code Sammy posted says: String root = "quests.ranks"; Will it look for the part "quests.ranks" in the config file and look in that branch of the yml? And what about when I want to add a string to the config file from in-game?
Root is the indication of there to read, for example: Code: quest: rank: 1 the root would be root = "quests.rank" @Adamki11s damnn you ninjaaa ! ^^
You've got it I don't use config files/ haven't used them yet but I'm guessing there's a config.put or config.add method. Your IDE should show you suggestions.
@Adamki11s I don't use them as property files but as a way to save stuff without needing to use a db ^^ It's quicker than Buffer Readers
Alrighty, thanks. Now I got this error though, one of you might know what it means... Code: Could not pass event PLAYER_INTERACT to <mypluginname> java.lang.NullPointerException Does this mean it doesn't catch at the NullPointerException part?
I think this means the plugin is having trouble passing the player interact event to the player class
Well that's weird... I didn't change anything about the event passing. getServer().getPluginManager().registerEvent(Event.Type.PLAYER_INTERACT, (Listener) playerListener, Priority.Normal, this); That's in my onEnable(); in my main class (br) And public void onPlayerInteract(PlayerInteractEvent event){ That's in my brPlayerListener class. If I must I will send you the 2 source files, I'm too close to finishing what I'm creating to stop D:
Sending me the source would probably be best, just send me the link in a PM if you want. Not to sound rude but there's probably not anything i haven't seen before
Unfortunately java keeps throwing the NullPointerException error. It's doing this because the file you are trying to load information from is blank. I have no experience with the file formats/types & methods you are using so I can't really help you but i am in the process of making a tutorial, which will be finished very soon which will explain how to save, load and read from saved files so I'll send you the link when it's done
I got it working, thanks for the effort. Now there's one last thing before my plugin is complete. My string outputs a list off stuff like this [STONE, DIRT, TNT]. I want it to compare to example to STONE. How would I break up the string for it to read every word and see if any one of them match?
You could use string.split(","); to split your string by the comma separating each one, put each one into an array and loop through it. If you do a google search for the Java string split method I'm sure you'll find something
Well... In my file, it reads this: Code: items: - lava_bucket - water_bucket - stone - dirt - tnt I made a variable of all the items. So then when I let the plugin post in chat what the variable has, it says [LAVA_BUCKET, WATER_BUCKET, STONE, DIRT, TNT]. I also let the plugin post in chat what block I just placed. That'd be STONE. After I did the comparison, I let it say in that "file sync", but that never comes up. So it doesn't get the comparison good. Now when I try the "split" func, it doesn't work anymore... But I also don't understand what you mean by "putting each of them in an array".
Sorry, I have no experience in working with these file types. Also I'm in the middle of writing a big tutorial for plugins so I don't have to test this. If you just wait a few hours it will be finished and have an easier way, or at least I think so method of saving and loading files.
Anyone else has an idea here? And I'll check out your tutorial once it's done Adam. It'd be easier for me to finish it like this, publish it, then maybe change it to your way of saving and loading things if that'd be more accessible. The idea of my plugin is to have it easy, but that you do have total control over what... well what the plugin does.. xD You'll see what I mean once it's finished
If you want an explanation of what de split method do, here it is: You take a string: private String myString = "hello,and,good,morning"; (I use coma to take the same syntax than you) now, you want to split each part to take hello,and,good,morning separatly so you declare an array of String like this: String[] words = myString.split(","); So you split the String, considering the separator of each part of the String is the coma. Now, and I think you'll understand, I have: words[0] = hello words[1] = and words[2] = good words[3] = morning Is that the kind of explanation you were expecting?
Yes yes. That worked. Might you also know how to check everything instead of each seperate word? This way I won't have to make 100 if(blabla == words[1]) if you know what I mean.
What would "root" and "null" be then? Sorry, but I didn't understand what the code said in my editor :/