Solved Splitting a List

Discussion in 'Plugin Development' started by cfil360, Aug 25, 2013.

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

    cfil360

    i am trying to split my config file, but i am having trouble. My code is below.
    Code:java
    1. List<String> list = getConfig().getStringList("Items");
    2. String[] splited = list.split(",");
    3. for(String items : list) {
    4. String name = list.split(",")[0];;
    5. String amount = list.split(",")[1];
    6. String price = list.split(",")[2];
     
  2. Offline

    Stoux

    Could you explain what you try to achieve by "Splitting" a list, because it doesn't make much sense to me. Also what is your YML file (for reference).
     
  3. Offline

    cfil360

    Stoux my yaml file is the default config. I am trying to retrieve the item,amount,price from the config. All 3 of those are on the same line of the list and i am trying to separate them.
     
  4. Offline

    Stoux

    I have a really hard time figuring out why you should do it like this. If you just save it in a more logical way in the YML, there's no need for splitting.
    Code:
    items:
      axe:
        price: 10
        amount: 2
      sword:
        price: 12
        amount: 8
    If you do it like this you can easily access any value of an item. First you get the ConfigurationSection "items". Once you have that you get all the keys. Each key is an item, and for each item you can the do config.getInt(key + ".price") which will return the price.
     
  5. Offline

    cfil360

    My client wants a shop plugin, so i am allowing the potential to have a ton of items in the shop
     
  6. Offline

    jayfella

    Reading configuration files should be on on server start, not in-game, in which case, it doesnt really matter very much on how long it takes to read the file (or multiple files). It may take 5 or 10 seconds, but its startup time, so it doesnt matter. This is one of the few areas of the plugin where you should be encouraged to push any lengthy work, (your exact situation) because in-game performance will not be affected. They shouldnt be read from whilst the server is live. My point is that you should be able to use yaml as intended, at the expense of a large file, rather than trying to roll your own smaller file-size implementation.

    If and when you write to the file, it should be done asynchronously, so again, who cares if it takes half a second, your internal representation will already be updated with the correct data. Just lock the file as to avoid any potential problems. I'm not sure if marking the method synchronized will be enough.
     
  7. Offline

    cfil360

    jayfella Here is my entire code. I just created a method that is run in the onEnable() method. It was just too hard to read when it was in onEnable();
    Code:java
    1. package me.connor.shop;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.DyeColor;
    9. import org.bukkit.World;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.material.Wool;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Main extends JavaPlugin {
    18. Inventory inv;
    19. private ItemStack c, s, a;
    20.  
    21.  
    22. public void onEnable() {
    23. saveDefaultConfig();
    24. getCommand("shop").setExecutor(new ShopCommands(this));
    25. Inv();
    26. }
    27.  
    28. public void onDisable() {
    29. reloadConfig();
    30. saveConfig();
    31. }
    32.  
    33. public void Inv() {
    34. inv = Bukkit.getServer().createInventory(null, 27, "Items");
    35. ItemStack item;
    36. List<String> list = this.getConfig().getStringList("items");
    37. for(String s : list) {
    38. String both[] = s.split(",");
    39. String name = both[0]; // item name
    40. String amount = both[1]; // quantity of an item
    41. String price = both[2]; // price of item
    42. item = createItem(DyeColor.LIME, ChatColor.GREEN + name, amount, price);
    43. if(!inv.contains(item)){
    44. inv.setItem(inv.firstEmpty(), item);
    45. }
    46. }
    47. }
    48.  
    49. private ItemStack createItem(DyeColor dc, String name, String price, String amount) {
    50. ItemStack i = new Wool(dc).toItemStack(1);
    51. ItemMeta im = i.getItemMeta();
    52. im.setDisplayName(name);
    53. im.setLore(Arrays.asList(ChatColor.GRAY + "$Quantity" + amount));
    54. im.setLore(Arrays.asList(ChatColor.GRAY + "$Costs" + price));
    55. i.setItemMeta(im);
    56. return i;
    57. }
    58.  
    59. public void showWorld(Player p) {
    60. p.openInventory(inv);
    61. }
    62.  
    63. }
    64.  
     
Thread Status:
Not open for further replies.

Share This Page