de.doncarnage.minecraft.jailquarry.data.QuarryData.java Source code

Java tutorial

Introduction

Here is the source code for de.doncarnage.minecraft.jailquarry.data.QuarryData.java

Source

/*
 *     JailQuarry
 *   Copyright (C) 2014 Viciouss <http://www.doncarnage.de>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 3 of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *   USA
 */

package de.doncarnage.minecraft.jailquarry.data;

import de.doncarnage.minecraft.jailquarry.data.groups.GroupManager;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.SerializableAs;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * This class provides access to the quarry and group data.
 */
@SerializableAs("Groups")
public class QuarryData {

    private Map<String, Quarry> quarries;
    private YamlConfiguration quarryData;
    private Path file;
    private GroupManager groupManager;

    /**
     * Creates a {@link QuarryData} instance from the given file.
     *
     * @param file the file to be loaded
     */
    public QuarryData(Path file) {
        Validate.notNull(file, "File may not be null.");
        this.file = file;
        this.quarryData = YamlConfiguration.loadConfiguration(file.toFile());
        quarries = new HashMap<>();
        loadData();
    }

    private void loadData() {

        ConfigurationSection quarrySection = quarryData.getConfigurationSection("quarries");
        if (quarrySection != null) {
            Map<String, Object> loadedQuarries = quarrySection.getValues(true);
            for (String key : loadedQuarries.keySet()) {
                quarries.put(key, (Quarry) loadedQuarries.get(key));
            }
        }

        GroupManager groupManager = (GroupManager) quarryData.get("groups");
        if (groupManager == null) {
            groupManager = new GroupManager();
        }
        setGroupManager(groupManager);
    }

    /**
     * Save the configuration to disk, only allowing one thread at a time.
     */
    public synchronized void save() {
        try {
            quarryData.set("quarries", quarries);
            quarryData.set("groups", groupManager);
            quarryData.save(file.toFile());
        } catch (IOException e) {
            throw new RuntimeException("Quarry data could not be saved!");
        }
    }

    public GroupManager getGroupManager() {
        return groupManager;
    }

    public void setGroupManager(GroupManager groupManager) {
        this.groupManager = groupManager;
    }

    public Map<String, Quarry> getQuarries() {
        return quarries;
    }

}