Search in sources :

Example 6 with ConfigurationSection

use of com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection in project Parties by AlessioDP.

the class PartiesYAMLDispatcher method getListFixed.

@Override
public Set<PartyImpl> getListFixed() {
    Set<PartyImpl> ret = new HashSet<>();
    ConfigurationSection node = database.getYaml().getConfigurationSection("parties");
    for (String key : node.getKeys(false)) {
        if (node.get(key + ".leader") == null) {
            ret.add(getPartyFromNode(node.getConfigurationSection(key)));
        }
    }
    return ret;
}
Also used : PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ConfigurationSection(com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection)

Example 7 with ConfigurationSection

use of com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection in project Parties by AlessioDP.

the class PartiesYAMLDispatcher method getListParties.

@Override
public LinkedHashSet<PartyImpl> getListParties(PartiesDatabaseManager.ListOrder order, int limit, int offset) {
    ConfigurationSection node = database.getYaml().getConfigurationSection("parties");
    LinkedHashSet<PartyImpl> ret = new LinkedHashSet<>();
    List<String> lowerCaseBlacklist = new ArrayList<>();
    for (String b : ConfigParties.ADDITIONAL_LIST_HIDDENPARTIES) lowerCaseBlacklist.add(CommonUtils.toLowerCase(b));
    TreeSet<? extends Pair<String, ?>> entries;
    if (order == PartiesDatabaseManager.ListOrder.NAME)
        entries = listByName(node, lowerCaseBlacklist);
    else if (order == PartiesDatabaseManager.ListOrder.MEMBERS)
        entries = listByMembers(node, lowerCaseBlacklist);
    else if (order == PartiesDatabaseManager.ListOrder.KILLS)
        entries = listByKills(node, lowerCaseBlacklist);
    else if (order == PartiesDatabaseManager.ListOrder.EXPERIENCE)
        entries = listByExperience(node, lowerCaseBlacklist);
    else
        throw new IllegalStateException("Cannot get the list of parties with the order" + order.name());
    // Limit and offset
    Iterator<? extends Pair<String, ?>> iterator = entries.iterator();
    int n = 0;
    for (int c = 0; iterator.hasNext() && n < limit; c++) {
        String uuid = iterator.next().getKey();
        if (c >= offset) {
            ret.add(getParty(UUID.fromString(uuid)));
            n++;
        }
    }
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ConfigurationSection(com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection) PartyImpl(com.alessiodp.parties.common.parties.objects.PartyImpl)

Example 8 with ConfigurationSection

use of com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection in project Parties by AlessioDP.

the class PartiesYAMLDispatcher method updateParty.

@Override
public void updateParty(PartyImpl party) {
    ConfigurationSection node = database.getYaml().getConfigurationSection("parties." + party.getId());
    if (node == null)
        node = database.getYaml().createSection("parties." + party.getId());
    // Save in map name -> id
    if (party.getName() != null) {
        ConfigurationSection mapNameToId = database.getYaml().getConfigurationSection("map-parties-by-name");
        if (mapNameToId == null)
            mapNameToId = database.getYaml().createSection("map-parties-by-name");
        if (node.get("name") != null && !node.getString("name", "").equals(CommonUtils.toLowerCase(party.getName())))
            // Remove old name
            mapNameToId.set(CommonUtils.toLowerCase(node.getString("name", "")), null);
        mapNameToId.set(CommonUtils.toLowerCase(party.getName()), party.getId().toString());
    }
    // Save party
    node.set("name", party.getName());
    node.set("tag", CommonUtils.getNoEmptyOr(party.getTag(), null));
    node.set("description", CommonUtils.getNoEmptyOr(party.getDescription(), null));
    node.set("motd", CommonUtils.getNoEmptyOr(party.getMotd(), null));
    node.set("color", party.getColor() != null ? party.getColor().getName() : null);
    node.set("kills", party.getKills() > 0 ? party.getKills() : null);
    node.set("password", CommonUtils.getNoEmptyOr(party.getTag(), null));
    node.set("protection", party.getProtection() ? true : null);
    node.set("experience", party.getExperience() > 0 ? party.getExperience() : null);
    // By default is true, so insert it only if false
    node.set("follow", party.isFollowEnabled() ? null : false);
    if (party.isOpenNullable() != null)
        node.set("isopen", party.isOpenNullable());
    node.set("home", party.getHomes().size() > 0 ? PartyHomeImpl.serializeMultiple(party.getHomes()) : null);
    node.set("leader", party.getLeader() != null ? party.getLeader().toString() : null);
    List<String> lt = new ArrayList<>();
    for (UUID uuid : party.getMembers()) lt.add(uuid.toString());
    node.set("members", lt);
    save();
}
Also used : ArrayList(java.util.ArrayList) UUID(java.util.UUID) ConfigurationSection(com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection)

Example 9 with ConfigurationSection

use of com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection in project Parties by AlessioDP.

the class PartiesYAMLUpgradeManager method upgradeDatabase.

@Override
protected void upgradeDatabase(int currentVersion) {
    if (currentVersion == 1) {
        // Upgrade from 2.6.X
        try {
            plugin.getLoggerManager().log(String.format(PartiesConstants.DEBUG_MIGRATE_YAML, currentVersion), true);
            HashMap<String, String> idParties = new HashMap<>();
            databaseFile.getYaml().set("parties-old", databaseFile.getYaml().get("parties"));
            databaseFile.getYaml().set("parties", null);
            ConfigurationSection oldPartiesNode = databaseFile.getYaml().getConfigurationSection("parties-old");
            oldPartiesNode.getKeys(false).forEach(key -> {
                String newUuid = UUID.randomUUID().toString();
                idParties.put(key, newUuid);
                ConfigurationSection newPartiesNode = databaseFile.getYaml().createSection("parties." + newUuid);
                newPartiesNode.set("name", key);
                newPartiesNode.set("leader", oldPartiesNode.get(key + ".leader"));
                newPartiesNode.set("description", oldPartiesNode.get(key + ".desc"));
                newPartiesNode.set("motd", oldPartiesNode.get(key + ".motd"));
                newPartiesNode.set("color", oldPartiesNode.get(key + ".color"));
                newPartiesNode.set("kills", oldPartiesNode.get(key + ".kills"));
                newPartiesNode.set("password", oldPartiesNode.get(key + ".password"));
                newPartiesNode.set("home", oldPartiesNode.get(key + ".home") != null ? ("default," + oldPartiesNode.get(key + ".home") + ",") : null);
                newPartiesNode.set("protection", oldPartiesNode.get(key + ".protection"));
                newPartiesNode.set("experience", oldPartiesNode.get(key + ".experience"));
                newPartiesNode.set("follow", oldPartiesNode.get(key + ".follow"));
                newPartiesNode.set("members", oldPartiesNode.get(key + ".members"));
            });
            databaseFile.getYaml().set("players-old", databaseFile.getYaml().get("players"));
            databaseFile.getYaml().set("players", null);
            ConfigurationSection oldPlayersNode = databaseFile.getYaml().getConfigurationSection("players-old");
            oldPlayersNode.getKeys(false).forEach(key -> {
                String party = oldPlayersNode.getString(key + ".party");
                ConfigurationSection newPlayersNode = databaseFile.getYaml().createSection("players." + key);
                newPlayersNode.set("party", party != null && !party.isEmpty() ? idParties.get(party) : null);
                newPlayersNode.set("rank", oldPlayersNode.get(key + ".rank"));
                newPlayersNode.set("spy", oldPlayersNode.get(key + ".spy"));
                newPlayersNode.set("mute", oldPlayersNode.get(key + ".mute"));
            });
            idParties.forEach((name, id) -> {
                databaseFile.getYaml().set("map-parties-by-name." + CommonUtils.toLowerCase(name), id);
            });
            databaseFile.getYaml().remove("parties-old");
            databaseFile.getYaml().remove("players-old");
            databaseFile.getYaml().set("database-version", PartiesConstants.VERSION_DATABASE_YAML);
            databaseFile.saveFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ConfigurationSection(com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection)

Aggregations

ConfigurationSection (com.alessiodp.core.common.addons.external.simpleyaml.configuration.ConfigurationSection)9 ArrayList (java.util.ArrayList)3 PartyImpl (com.alessiodp.parties.common.parties.objects.PartyImpl)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 MemorySection (com.alessiodp.core.common.addons.external.simpleyaml.configuration.MemorySection)1 ConfigOption (com.alessiodp.core.common.configuration.ConfigOption)1 ConfigurationFile (com.alessiodp.core.common.configuration.ConfigurationFile)1 PartiesPlugin (com.alessiodp.parties.common.PartiesPlugin)1 PartiesConstants (com.alessiodp.parties.common.configuration.PartiesConstants)1 PartyColorImpl (com.alessiodp.parties.common.parties.objects.PartyColorImpl)1 PartyRankImpl (com.alessiodp.parties.common.players.objects.PartyRankImpl)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1