[Solved]Data storing methods

Discussion in 'Plugin Development' started by beatcomet, Jul 17, 2011.

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

    beatcomet

    Hi, I need some help here.
    I'm making a plugin that uses 3 variables per key, and I saw some plugins using it
    Example :

    beatcomet: 25,5,1

    As you can see the key beatcomet has 3 values. how can I make a file that will allow me to store 3 values per key?
     
  2. Offline

    Tster

    offtopic: Ca you helpmme with the damage system
    ontopic: Split with slash?
     
  3. Offline

    beatcomet

    I think I can help you
    on topic : The problem is that I need to store and read data without using predefiend values.
     
  4. Offline

    Tster

    Im not very familiar with java, but in visual basic you would
    load file
    file.split(/)
    value1 = array(1)
    value2 = array(2)
    value3 = array(3)
    loop for i in array
    file = file + "/" + i
     
  5. Offline

    ItsHarry

    Don't think it's possible. Best solution would probably be to make 3 different maps to store the 3 values
     
  6. Offline

    Raeon

    PHP:
    String a "health:15:4:5"

    String b[] = a.split(":");

    String health1 b[1];
    String health2 b[2];
    String health3 b[3];

    System.out.println("Health values: " health1 ", " health2 ", " health3 ".");
    Above code will output in:
    Code:
    Health values: 15, 4, 5
    Data saving and reading is up to you. Good luck!
     
  7. Offline

    FrozenBrain

    Use Bukkits build-in configuration system.

    Load the config with
    Code:
    Configuration config = this.getConfiguration();
    config.load();
    And read the list with getList, getIntList, getStringList, whatever you need.
     
  8. Offline

    krinsdeath

    Haha.

    Code:
    List<Integer> beatcomet = config.getIntList("beatcomet", new ArrayList<Integer>());
    // iterate through each item in the list
    int index = 0;
    for (int i : beatcomet) {
      System.out.println("index " + index + " is " + i);
      index++;
     }
    // fetch each value in a static fashion
    int key1 = beatcomet.get(0);
    int key2 = beatcomet.get(1);
    int key3 = beatcomet.get(2);
    
    If you know the order of the items in the list (ie it's not editable by players/server admins), static access should be fine. If you have some random values that will be there and you're using them to manipulate data (in no particular order), iterate through the keys with a for loop.

    edit: wrap the int list in the .yml (or whatever storage type) with [], to indicate to the parser that it's a list.
     
  9. Offline

    beatcomet

    What about doing something like this :
    Code:java
    1.  
    2. //lets assume that I have imported every thing needed and defined the config file and every thing
    3.  
    4. //setting the code in the data file
    5. publc void setCode(String code, int id, int amount, int uses){
    6. String last = id+","+amount+","+uses;
    7. config.setProperty(code, last);
    8. config.save();
    9. }
    10. //reading the code
    11.  
    12. config.getProperty(code);
    13. String a[] = config.getProperty(code).split(",");
    14. int id = Integer.parseInt(a[0]);
    15. int amount =Integer.parseInt(a[1]);
    16. int uses = Integer.parseInt(a[2]);
    17.  


    can I do something like this ?
     
  10. Offline

    krinsdeath

    Yes, that should work. getIntList() already does that, and is type safe and won't throw problems at you for a null pointer. Only thing I'd do different in that is make sure that the returned property (or split string) has a length of 3, so you don't get ArrayIndexOutOfBounds.

    Good solution, though. :o
     
  11. Offline

    beatcomet

    How can I check it ?
    Will this one work :
    Code:java
    1.  
    2. if(a.length == 3){
    3. //get integers
    4. }
    5. else{
    6. player.sendMessage("Error");
    7.  



    Thanks that is what I wante to hear. I am really glad you helped me :)
    You brought me one step closer towards finishing my CR plugin recode xD
     
  12. Offline

    krinsdeath

    Where 'a' is the object array which contains the split string, yes. Don't manipulate any portion of the array (even a[0]) unless you've checked it for length.

    Good luck!
     
  13. Offline

    beatcomet

Thread Status:
Not open for further replies.

Share This Page