Get String[] array from config file list

Discussion in 'Plugin Development' started by RE3ELL, May 20, 2013.

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

    RE3ELL

    Hi,
    i am fairly new to Java and Bukkit develompent and i am unable to get the list from my config and make an String[] Array out of it.
    I have this list in my config:

    Code:
    Configuration:
      Modules:
        BlockRestriction:
          # Usage:  <ID> . [Metadata] = Limit
          # Spaces do not matter. The Metadata is optional.
          # E.g.: Players can only have 2 Personal Anchors (Railcraft Mod)
          Blocklist:
          - 1053.2 = 2
          - 1 = 5
          - 5.2 = 1
    I need to replace some part of the strings contained in it with String.replace().
    E.g.
    Split each string of the list into 2 parts at the "=" to get the Id and the Metadata from the Block to convert it into integers to combine it with the id and meta at the onBlockPlaceEvent() .

    My Problem is now:
    How can i get the list from the config and convert it into a string array, to fit my needs?

    If you know a better way to get the id, meta and limit a user types into a config file and compare it with the block on onBlockPlaceEvent(), please tell me. It would be a very very nice help. :)

    Thx,
    re3ell
     
  2. Offline

    Jogy34

    Why not just use a list in the first place? But to convert it into an array you would do something like this:
    Code:
    List<String> list = config.getStringList("path");
    String[] array = list.toArray(new String[0]);
     
  3. Offline

    RE3ELL

    Thanks for the fast reply, but also your code works, i don´t get it to do what i want.^^

    A quick summary of what i want to do:
    My plugin intends to set a block place limit for blocks a user defines in config. E.g. Block users from placeing more than 3 GoldBlocks.

    As i am new to java i might do it extremly complicated but here is what i do to make it happen:

    - Let user write blocks that shall be blocked in config.yml in this format: BlockID.Metadata = Limitofthisblock
    - Read that list out and extract the ID and Metadata from it <--- Here is my problem
    - Have an sqlite database which saves the amount of blocks a player has placed of that kind
    - Compare the id from the config.yml list with the block id on BlockPlace and BlockBreak event

    But, my problem atm is to get this code to work.
    Code:
    public void registerConfig() {
            List<String> blockslist = getConfig().getStringList("Configuration.Modules.BlockRestriction.Blocklist");
            blocks = blockslist.toArray(new String[0]); // <-- This is a public String[] Array
     
            // Set Syntax of every String in list to fit needs
            for (String entry : blocks) {
                entry.replace(" ", "");
                if (!entry.contains(".")) {
                    String[] splited = entry.split("=");
                    entry = splited[0] + ".0=" + splited[1];
                }
            }
            System.out.println(Arrays.toString(blocks));
            // Save and Default Stuff
            this.getConfig().options().copyDefaults(true);
            saveDefaultConfig();
        }
    When it is printed to console it does not replace the string from the config.yml list where it says:
    1 = 5
    with:
    1.0=5

    I want it to write the ".0" and remove spaces. But i can´t understand why it doesn´t do it. What am i doing wrong?


    P.S. And why i haven´t done it with lists? --> I have no idea of lists as i only know arrays from other programming languages and sadly haven´t got the time to learn every aspect of Java.
     
  4. Offline

    felixfritz

    You're probably thinking that taking objects out of arrays and editing them "updates" them automatically, but sadly they don't. In the beginning of the for loop, you're creating an entirely new object (or string) called "entry", which has absolutely no connection with the array blocks, except that it copied the object. So instead, you'd have to get the string and put it back into the array again.

    Also, in the first line of the for loop, the entry.replace() method creates a whole new string object, so instead you'd have to reassign the string entry by saying: entry = entry.replace(" ", "");

    In pseudo, I'd put the for-loop like this:
    PHP:
    //we need the value x to put it back into the index of the array blocks
    for(int x 0blocks.lengthx++) {
        
    entry entry.replace(" """);
     
        if(!
    entry.contains(".")) {
            
    String[] splited entry.split("=");
            
    entry splited[0] + ".0=" splited[1];
        }
     
        
    blocks[x] = entry;
    }
    I hope that was all that was missing.

    P.S.: Don't worry about lists too much, they're basically like arrays, but dynamic, so you can add items to them and remove them again. That's a really simple explanations on how they work, but arrays, of course, do the trick, too.
     
  5. Offline

    RE3ELL

    Thanks for the excellent explanation. I´ll try your code asap.

    Update:
    Works fine now. Thank you for your help.
    Now i only need to get this database (sqlite) queries to work :p

    Update nr. 2:
    Everything seems to work now. Thank you guys for the excellent help.
     
Thread Status:
Not open for further replies.

Share This Page