[SOLVED] How do I count amount of keys in a config?

Discussion in 'Spigot Plugin Development' started by Quasindro, Jul 7, 2015.

  1. I want to create an automated system adding keys to the config. How would I create the IDs of every other entry?
     
  2. getKeys().size()
     
  3. Could you elaborate...?
     
  4. getConfig().getKeys(false).size()

    getKeys(false) is assuming you just want top-level keys, if you set it to true, then it will give you all keys.
     
  5. AKA getKeys(false) will only return that string's children, and not the children's children etc useful for kit names etc
     
  6. Code (Text):

    int id = SettingsManager.getUnlockables().<ConfigurationSection>get("").getKeys(false).size();
         
             clickedBlockLoc = block.getLocation();
         
             SettingsManager.getUnlockables().set("Test" + id + ".world", clickedBlockLoc.getWorld().getName());
             SettingsManager.getUnlockables().set("Test" + id + ".x", clickedBlockLoc.getBlockX());
             SettingsManager.getUnlockables().set("Test" + id + ".y", clickedBlockLoc.getBlockY());
             SettingsManager.getUnlockables().set("Test" + id + ".z", clickedBlockLoc.getBlockZ());
         
             createItem(p, Material.TRIPWIRE_HOOK, "Test" + id);
             return;
     
    It does this:
    Code (Text):

    Test1:
      world: world
    Test2:
      x: 210
    Test3:
      y: 64
    Test4:
      z:417
     
    How do I save the id once and write all the values to it? So it would be like:
    Code (Text):

    TestID:
      world: world
      x: x
      y: y
      z: z
     
     
    #6 Quasindro, Jul 8, 2015
    Last edited: Jul 10, 2015
  7. Is that code in a loop of any kind?
     
  8. Nope.
     
  9. Do you have any other code that is modifying the same config? The code that you put should be doing exactly what you want... id isn't being incremented
     
  10. That's everything that contains the Unlockables config.
    Code (Text):

    if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GOLD + "DoorSetter :^)")) {
             Block block = e.getClickedBlock();
           
             if (block.getData() == 8) {
               block = block.getRelative(BlockFace.DOWN);
             }
           
             int id = SettingsManager.getUnlockables().<ConfigurationSection>get("").getKeys(false).size();
           
             clickedBlockLoc = block.getLocation();
           
             SettingsManager.getUnlockables().set("Test" + id + ".world", clickedBlockLoc.getWorld().getName());
             SettingsManager.getUnlockables().set("Test" + id + ".x", clickedBlockLoc.getBlockX());
             SettingsManager.getUnlockables().set("Test" + id + ".y", clickedBlockLoc.getBlockY());
             SettingsManager.getUnlockables().set("Test" + id + ".z", clickedBlockLoc.getBlockZ());
           
             createItem(p, Material.TRIPWIRE_HOOK, "Test" + id);
             return;
           }
         
           if ((p.getItemInHand().getType().equals(Material.TRIPWIRE_HOOK)) && (SettingsManager.getUnlockables().contains(keyName))) {

             if (clickedBlockLoc.getWorld().getName().equals(SettingsManager.getUnlockables().get(keyName + ".world")) &&
               clickedBlockLoc.getBlockX() == SettingsManager.getUnlockables().<Integer>get(keyName + ".x").intValue() &&
               clickedBlockLoc.getBlockZ() == SettingsManager.getUnlockables().<Integer>get(keyName + ".z").intValue() &&
               (clickedBlockLoc.getBlockY() == SettingsManager.getUnlockables().<Integer>get(keyName + ".y").intValue() ||
               (clickedBlockLoc.getBlockY() - 1) == SettingsManager.getUnlockables().<Integer>get(keyName + ".y").intValue())) {
             
               final Block block = e.getClickedBlock();
               final byte data = block.getData();
             
               if (data == 8) {
               
                 final Block bottomBlock = block.getRelative(BlockFace.DOWN);
                 final byte bottomData = bottomBlock.getData();
               
                 if (bottomBlock.getType().equals(Material.IRON_DOOR_BLOCK) && bottomData < 4) {
                 
                   bottomBlock.setData((byte)(bottomData + 4));
                   p.playSound(clickedBlockLoc, Sound.DOOR_OPEN, 1.0F, 0.5F);
                 
                   new BukkitRunnable() {
                     public void run() {
                       bottomBlock.setData(bottomData);
                     }
                   }.runTaskLater(Main.getPlugin(), 20*1);
                 }
               } else if (data < 4) {
               
                 block.setData((byte)(data + 4));
                 p.playSound(clickedBlockLoc, Sound.DOOR_OPEN, 1.0F, 0.5F);
               
                 new BukkitRunnable() {
                   public void run() {
                     block.setData(data);
                   }
                 }.runTaskLater(Main.getPlugin(), 20*1);
               }
             } else {
               p.playSound(clickedBlockLoc, Sound.BAT_TAKEOFF, 1.0F, 2.0F);
               return;
             }
           } else {
             p.playSound(clickedBlockLoc, Sound.BAT_TAKEOFF, 1.0F, 2.0F);
             return;
           }
     
    SettingsManager class (kinda stolen from pogo's github):
    Code (Text):

    package me.quasindro.heaven;

    import java.io.File;
    import java.util.Set;

    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class SettingsManager {

       private static final SettingsManager
           configuration = new SettingsManager("config"),
           items = new SettingsManager("items"),
           blocks = new SettingsManager("blocks"),
           ores = new SettingsManager("ores"),
           descriptions = new SettingsManager("descriptions"),
           unlockables = new SettingsManager("unlockables");
       
       public static SettingsManager getConfig() {
         return configuration;
       }
       
       public static SettingsManager getItems() {
         return items;
       }
       
       public static SettingsManager getBlocks() {
         return blocks;
       }
       
       public static SettingsManager getOres() {
         return ores;
       }
       
       public static SettingsManager getDescriptions() {
         return descriptions;
       }
       
       public static SettingsManager getUnlockables() {
         return unlockables;
       }
       
       private File file;
       private FileConfiguration config;
       
       private SettingsManager(String filename) {
         if (!Main.getPlugin().getDataFolder().exists()) {
           Main.getPlugin().getDataFolder().mkdir();
         }
         
         file = new File(Main.getPlugin().getDataFolder(), filename + ".yml");
         
         if (!file.exists()) {
           try {
             file.createNewFile();
           } catch (Exception e) {
             e.printStackTrace();
           }
         }
         
         config = YamlConfiguration.loadConfiguration(file);
       }
       
       @SuppressWarnings("unchecked")
       public <T> T get(String path) {
         return (T) config.get(path);
       }
       
       public Set<String> getKeys() {
         return config.getKeys(false);
       }
       
       public void set(String path, Object value) {
         config.set(path, value);
         save();
       }
       
       public boolean contains(String path) {
         return config.contains(path);
       }
       
       public ConfigurationSection createSection(String path) {
         ConfigurationSection section = config.createSection(path);
         save();
         return section;
       }
       
       public void save() {
         try {
           config.save(file);
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
    }
     
     
    #13 Quasindro, Jul 10, 2015
    Last edited: Jul 10, 2015
  11. Problem solved, I have created a new method that would set the information without saving it instantly - after setting those four values I would save the config with uncovered save() method.