Solved Stumped

Discussion in 'Plugin Development' started by TheDiamond06, Mar 30, 2016.

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

    TheDiamond06

    Alright so I have a plugin with language support I am adding in. I have three yml files that are in the source folder. Each of them is for a language. These files contain String variables that I pull out for sending a message. The language config is then chosen from the main config file. I can successfully pull out the strings from these files and get the string I want. I can grab these String's from the configuration file with no flaws and output the String message on any language.

    However, this plugin contains at an estimate 1,000 String messages. If I try to pull a String for example #954, it has to scan all the way to String #954causing server lag.

    My question is how can I efficiently get these Strings without directly pulling them from the configuration file. Also I do not want 1,000 String variables that are set upon enabling.
     
    Last edited: Mar 31, 2016
  2. Offline

    mine-care

    Yes there is.
    You can create a wrapper class where you in bike the method "getMessage(String name)" and it returns the corresponding message from config directly. You don't need 1000 and 1 variables..
    I don't see why this is not ok :/
     
    Last edited: Mar 30, 2016
  3. Offline

    timtower Administrator Administrator Moderator

    @TheDiamond06 Only have 1 language in your config, let the user translate, just switch put the entire config when a different language is wanted.
    An names, use names instead of numbering the messages.
     
  4. Offline

    mcdorli

    Instead of having every variable as static, you should use an instance of the class, to get the values, also, maybe use more convenient names, like deathMessage instead of msg1-15, this way the code will be more maintainable.
     
  5. Offline

    TheDiamond06

    I let the user do change the language in the main configuration file. I supposed I did not mean to mention this. Currently I have it set up so it does load the configuration file with the correct language based on their input. I have 3 different yml files in my src folder that I get with a FileOutputStream. Each of these files has the same variables that contain the String messages. However each of them have different languages. That is the current set up I have for it.
    I just had them static and volatile just as an example. I am really looking for another to do this besides having all those String variables.
    I don't want to grab the String from the configuration file directly, if there are 1,000 variables with Strings and I try to grab String #997 it will take some time to grab that String. This will then delay the message being sent, causing lag.

    I'm going to re-write my original post to hopefully make it make more sense.
     
  6. Offline

    mine-care

    @TheDiamond06 although that sounds too many, I remind you that all these are cached in ram during runtime so you are getting some info from a very fast mean of storage. A 1000 iteration loop is nothing for Java... Not to mention if you implement a binary search algorythm with Average efficiency of O(log(n))
    To speed things up even more... Keeping 1000 strings in is not good because as I saw, you intended to make them static, and if that is the case you would have to turn them into null onDisable() because plugins loaded in bukkit do not unload those thus they are ram leaky. (I think)
    Third and most importantly, you can create an enum with the keys of the configuration file and use it instead like getMessage(Messages.DISCONNECT_MESSAGE);
     
  7. Offline

    mythbusterma

    @TheDiamond06

    But why not only one yaml file? Just have them configure what they want it to say. You can distribute various versions of the same file for different languages. Although, there's nothing particularly wrong with your way.

    You should just make a class that has the same getString() method as ConfigurationSection does, and have a reference to that wherever you need it, rather than this convoluted scheme.

    Also, you're completely off base with your "lag" implications, so just stop that.

    @mine-care

    Why would you put in the extra effort to make it slower? A HashMap is already O(1), and a HashMap is what backs the ConfigurationSection class. Also, they won't leak memory. They will be unloaded when they need to be.

    TL;DR: just use what mine-care said with a class to manage them
     
    mine-care likes this.
  8. Offline

    mine-care

    @mythbusterma You are right, i didn't know how ConfigurationSection loads configurations (Although i should have figured it was a map -_-) but yeah... in that case the binary search algorythm is not needed whatsoever. O(1) is perfect.
     
  9. Offline

    TheDiamond06

    I re-made the original post to say that I would not be using 1,000 Strings. That is what I thought at first, but then realized that wouldn't work. However, if you want me to put each String into an enum, wouldn't there be 1,000 Objects in that enum? Would that cause problems?
     
  10. Offline

    mine-care

    @TheDiamond06 not what I ment, I mean, you may have a class that has a method called "getMessage". Once you want a message from the file which has as a key: "deathmessage", you will call the method getMessage("deathmessage"); and it will grab it from the config and return It. So, to speed things up a notch and make them simpler and safer, you may create an enum with all key values so instead of passing "deathmessage" to the method you will pass "Enum.DEATH_MESSAGE" and it will return the same thing. I don't think this would case any problems, and ridiculously long enum S already exist in bukkit (see Material) so I think it will be fine but you may still ignore the enums and use strings directly.
     
  11. Offline

    TheDiamond06

    Alright I will use that, thank you for helping.
     
Thread Status:
Not open for further replies.

Share This Page