Simpler and Easier way to store data and booleans? (SettingsManager)

Discussion in 'Plugin Development' started by DiamGamingWTF, Aug 4, 2014.

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

    DiamGamingWTF

    I'm using a file to store data from players whenever they unlock something on the server, and if they try to use and do not have it unlocked, they cannot use it. I have this working but the config files seem a bit crowded, same for the amount of methods I need to create and call (get1Unlock, get2Unlock, get3Unlock etc...);

    What i'm asking is, is there a way I can do the same thing but with only two methods (get and set)
    where I can set a certain value and possibly have the config look like:

    Code:
    Player UUID: false, true, false, false
    Player238 UUID: true, false, true, false
    Instead of:


    Code:
    Unlock 1:
      - Player UUID: 1
      - Player238 UUID: 0
    Unlock 2:
      - Player UUID: 1
      - Player238: 1
    I'm only asking this because I do not like the way I currently store data and the amount of different methods I need to create for each "unlock". If you don't understand what I mean, please tell me in a reply. Thanks for reading.

    PS. How do I store booleans?
     
  2. Offline

    Luke_Lax

    You could get the yaml string UUID, then use the .split("") function, that'll separate each word based on a common character. For example, if you made the list look like:

    UUID: true/false/false/true

    You could then grab that string and split it using UUIDstring.split("//") - if you do not know regex, we use a double slash because it's an escape character. You can use any character, it doesn't have to be a forward slash.

    So it would look like: String unlocks[] = UUIDString.split("//");
    You can now get the length and iterate over that and do as you wish, create your methods. You could have it accept an int (and player) then check if it's true or false for unlocks[int]. Booleans return true or false and are a primitive data type so you can get the result to string and append it to your file.
    I know you said this was for the look of the config file but I'd like to add that is may look neater but it's no quicker when grabbing the data.
     
  3. Offline

    SGrayMe

    DiamGamingWTF
    It sounds like your real problem is that you're hardcoding the "unlocks". You shouldn't need to make a bunch of get/set methods when you're retrieving the same kind of information using the same set of variables every time. Change one of get/set pairs to accept the name of the "unlock" and build your config path with it. Remove the extra get/set methods that are then redundant.

    This is a simple "set" method that can be used without change whether there are 2 or 38 types of unlocks.
    Code:java
    1. protected void setUnlock(String unlock, String uuid, boolean value) {
    2. getConfig().set(unlock + "." + uuid, value);
    3. saveConfig();
    4. }


    Regarding the config format, you're asking to lose clear organization for the sake of marginal filesize benefit and increased complexity. You'll gain far more focusing on making the plugin code more efficient.

    Saving booleans basically is the same as strings. For retrieving data, I suggest reviewing this
    http://jd.bukkit.org/dev/apidocs/org/bukkit/configuration/ConfigurationSection.html
     
    DiamGamingWTF likes this.
  4. Offline

    DiamGamingWTF

    SGrayMe
    Right, I just want to make sure I am understanding this correctly. Would this get the boolean back?
    Code:java
    1. protected boolean getUnlock(String unlock, String uuid) {
    2. return Cosmetic.getBoolean(unlock + "." + uuid);
    3. }

    Also, what do I need to modify about this to be able to get the values properly?
    Code:java
    1. public ArrayList<String> getValues() {
    2. Map<String, Object> map = Cosmetic.getValues(true);
    3. ArrayList<String> lines = new ArrayList<String>();
    4.  
    5. for (Entry<String, Object> e : map.entrySet()) {
    6. lines.add(e.getValue() + " " + e.getKey());
    7. }
    8. return lines;
    9. }
     
  5. Offline

    SGrayMe

    DiamGamingWTF
    At a glance, it looks like you have things right. Best way to be certain is doing testing though. :)
     
    DiamGamingWTF likes this.
Thread Status:
Not open for further replies.

Share This Page