Помогите Парсинг конфига

  • Автор темы Автор темы Playrek
  • Дата начала Дата начала
Статус
Закрыто для дальнейших ответов.

Playrek

Активный участник
Пользователь
Баллы
61
Имеется следующий конфиг (YAML):
Код:
maps:
- teams:
  - x: 0
    y: 5
    z: 0
  - x: 0
    y: 2
    z: 0
  spawners:
  - x: 0
    y: 5
    z: 0
    type: iron
  - x: 0
    y: 2
    z: 0
    type: bronze

Вопрос:
Как нормально получить значения из массивов, например координаты базы или спавнера?
Используется Bukkit FileConfiguration.
 
Код:
maps:
  teams:
    x1: 0
    y1: 5
    z1: 0
    x2: 0
    y2: 2
    z2: 0
  spawners:
    1:
      x: 0
      y: 2
      z: 0
      type: 'IRON'
    2:
      x: 0
      y: 2
      z: 0
      type: 'GOLD'


Код:
Double x1 = plugin.getConfig().getDouble("maps.teams.x1");
Double x2 = plugin.getConfig().getDouble("maps.teams.x2");
Double y1 = plugin.getConfig().getDouble("maps.teams.y1");
Double y2 = plugin.getConfig().getDouble("maps.teams.y2");
Double z1 = plugin.getConfig().getDouble("maps.teams.z1");
Double z2 = plugin.getConfig().getDouble("maps.teams.z2");
ConfigurationSection s = main.getConfig().getConfigurationSection("maps.spawners.1");
Double x = s.getDouble("x");
Double y = s.getDouble("y");
Double z = s.getDouble("z");
Material m = Material.valueOf(s.getString("type"));
 
Последнее редактирование:
Код:
maps:
  teams:
    x1: 0
    y1: 5
    z1: 0
    x2: 0
    y2: 2
    z2: 0
  spawners:
    1:
      x: 0
      y: 2
      z: 0
      type: 'IRON'
    2:
      x: 0
      y: 2
      z: 0
      type: 'GOLD'


Код:
Double x1 = plugin.getConfig().getDouble("maps.teams.x1");
Double x2 = plugin.getConfig().getDouble("maps.teams.x2");
Double y1 = plugin.getConfig().getDouble("maps.teams.y1");
Double y2 = plugin.getConfig().getDouble("maps.teams.y2");
Double z1 = plugin.getConfig().getDouble("maps.teams.z1");
Double z2 = plugin.getConfig().getDouble("maps.teams.z2");
ConfigurationSection s = main.getConfig().getConfigurationSection("maps.spawners.1");
Double x = s.getDouble("x");
Double y = s.getDouble("y");
Double z = s.getDouble("z");
Material m = Material.valueOf(s.getString("type"));
Проблема в том, что нужно перебрать эти значения, а не просто взять. Чтобы без изменений в коде можно было сделать, например, не 2, а 4 спавнера.
 
Тогда вбей всё в стринглист, в чём проблема?
Код:
maps:
  teams:
    - '0.0;5.0;0.0'
    - '0.0;2.0;0.0'
  spawners:
    - '0.0;5.0;0.0;IRON'
    - '0.0;2.0;0.0;GOLD'


Код:
for (String s : plugin.getConfig().getStringList("maps.spawners")) {
Double x = s.split(";")[0];
Double x = s.split(";")[1];
Double x = s.split(";")[2];
Material m = Material.valueOf(s.split(";")[3]);
}
 
Или используй config.getConfigurationSection("maps.spawners").getKeys(false), чтобы названия всех спавнеров
 
Держи немного екземпла)
 

Вложения

  • upload_2020-1-31_5-47-0.png
    upload_2020-1-31_5-47-0.png
    110,8 КБ · Просмотры: 8
  • upload_2020-1-31_5-47-9.png
    upload_2020-1-31_5-47-9.png
    28,6 КБ · Просмотры: 7
Всем спасибо. Сделал через стринглист и getKeys(false) .
Код:
ConfigurationSection cs = config.getConfigurationSection("bedwars.maps");

        for (String mapsStr : cs.getKeys(false)) {
            ConfigurationSection map = cs.getConfigurationSection(mapsStr);

            List<String> list = map.getStringList("spawners");

            for (String spawner : list) {
                String[] splitList = spawner.split(";");

                int x = Integer.parseInt(splitList[0]);
                int y = Integer.parseInt(splitList[1]);
                int z = Integer.parseInt(splitList[2]);
                String world = splitList[3];
                String typeStr = splitList[4];

                Material type = null;

                if (typeStr.equalsIgnoreCase("BRONZE")) {
                    type = Material.CLAY_BRICK;
                } else if (typeStr.equalsIgnoreCase("IRON")) {
                    type = Material.IRON_INGOT;
                } else {
                    getLogger().info("Incorrect config");
                    return;
                }

                Material finalType = type;
                getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                    @Override
                    public void run() {
                        getServer().getWorlds().get(0).dropItemNaturally(
                                new Location(getServer().getWorld(world), x, y, z),
                                new ItemStack(finalType, 1));
                    }
                }, 0L, 20L);
            }
        }
 
Статус
Закрыто для дальнейших ответов.
Назад
Верх