Add main configuration file.
This commit is contained in:
		@@ -0,0 +1,120 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ItemCase is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU 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 General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 | 
			
		||||
 */
 | 
			
		||||
package com.gmail.bleedobsidian.itemcase;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.nio.file.Files;
 | 
			
		||||
import java.nio.file.StandardCopyOption;
 | 
			
		||||
import org.bukkit.configuration.file.FileConfiguration;
 | 
			
		||||
import org.bukkit.configuration.file.YamlConfiguration;
 | 
			
		||||
import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * An object with a corresponding configuration file that can be loaded/saved.
 | 
			
		||||
 * 
 | 
			
		||||
 * @author Jesse Prescott (BleedObsidian)
 | 
			
		||||
 */
 | 
			
		||||
public class ConfigurationFile {
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * FileConfiguration handle.
 | 
			
		||||
     */
 | 
			
		||||
    protected FileConfiguration file;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * The name of this configuration file.
 | 
			
		||||
     */
 | 
			
		||||
    private String name;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @param name The name of this configuration file.
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigurationFile(String name) {
 | 
			
		||||
        
 | 
			
		||||
        // Set name.
 | 
			
		||||
        this.name = name;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Loads this configuration file from the given plugin's data folder, if it
 | 
			
		||||
     * does not exist, it is copied from the plugins jar.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param plugin JavaPlugin.
 | 
			
		||||
     * @throws IOException.
 | 
			
		||||
     */
 | 
			
		||||
    public void load(JavaPlugin plugin) throws IOException {
 | 
			
		||||
        
 | 
			
		||||
        // Create file reference.
 | 
			
		||||
        File fileReference = new File(plugin.getDataFolder(), this.name);
 | 
			
		||||
        
 | 
			
		||||
        // If the file doesn't exist...
 | 
			
		||||
        if(!fileReference.exists()) {
 | 
			
		||||
            
 | 
			
		||||
            // Copy default config from jar to data folder.
 | 
			
		||||
            this.copyDefault(plugin);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Attempt to load it.
 | 
			
		||||
        this.file = YamlConfiguration.loadConfiguration(fileReference);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Attempt to copy the default configuration file from the given plugin's
 | 
			
		||||
     * jar to the data folder.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param plugin JavaPlugin.
 | 
			
		||||
     * @throws IOException.
 | 
			
		||||
     */
 | 
			
		||||
    private void copyDefault(JavaPlugin plugin) throws IOException {
 | 
			
		||||
        
 | 
			
		||||
        // Create reference to output file.
 | 
			
		||||
        File outputFile = new File(plugin.getDataFolder(), this.name);
 | 
			
		||||
        
 | 
			
		||||
        // Attempt to create an input stream of the default file in the jar.
 | 
			
		||||
        InputStream inputStream = plugin.getResource(this.name);
 | 
			
		||||
        
 | 
			
		||||
        // Check if we succeeded.
 | 
			
		||||
        if(inputStream == null) {
 | 
			
		||||
            
 | 
			
		||||
            // Throw exception.
 | 
			
		||||
            throw new IOException("Failed to create input stream to default"
 | 
			
		||||
                    + " file.");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Create initial file.
 | 
			
		||||
        outputFile.getParentFile().mkdirs();
 | 
			
		||||
        
 | 
			
		||||
        // Copy file.
 | 
			
		||||
        Files.copy(inputStream, outputFile.toPath(), 
 | 
			
		||||
                StandardCopyOption.REPLACE_EXISTING);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Save this configuration file in the given plugin's data folder.
 | 
			
		||||
     * 
 | 
			
		||||
     * @param plugin Plugin.
 | 
			
		||||
     * @throws IOException.
 | 
			
		||||
     */
 | 
			
		||||
    public void save(JavaPlugin plugin) throws IOException {
 | 
			
		||||
        
 | 
			
		||||
        // Create reference to output file.
 | 
			
		||||
        File outputFile = new File(plugin.getDataFolder(), this.name);
 | 
			
		||||
        
 | 
			
		||||
        // Attempt to save file.
 | 
			
		||||
        this.file.save(outputFile);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -16,7 +16,11 @@
 | 
			
		||||
package com.gmail.bleedobsidian.itemcase;
 | 
			
		||||
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.Itemcase.ItemcaseListener;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.configurations.ConfigFile;
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.managers.ItemcaseManager;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.logging.Logger;
 | 
			
		||||
import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -32,6 +36,11 @@ public final class ItemCaseCore extends JavaPlugin {
 | 
			
		||||
     */
 | 
			
		||||
    public static ItemCaseCore instance;
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Main ItemCase configuration file.
 | 
			
		||||
     */
 | 
			
		||||
    private final ConfigFile configFile = new ConfigFile();
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Custom plugin console logger.
 | 
			
		||||
     */
 | 
			
		||||
@@ -51,11 +60,34 @@ public final class ItemCaseCore extends JavaPlugin {
 | 
			
		||||
        // Start metrics.
 | 
			
		||||
        PluginMetrics metrics = new PluginMetrics(this);
 | 
			
		||||
        
 | 
			
		||||
        // Attempt to load configuration file.
 | 
			
		||||
        try {
 | 
			
		||||
            
 | 
			
		||||
            // Load configuration file.
 | 
			
		||||
            this.configFile.load(this);
 | 
			
		||||
            
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            
 | 
			
		||||
            // Display error.
 | 
			
		||||
            this.consoleLogger.severe("Failed to load configuration file.", e);
 | 
			
		||||
            
 | 
			
		||||
            // Stop loading.
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // Register ItemcaseListener.
 | 
			
		||||
        this.getServer().getPluginManager().registerEvents(
 | 
			
		||||
                new ItemcaseListener(), this);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
     * @return Main ItemCase configuration file.
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigFile getConfigFile() {
 | 
			
		||||
        return this.configFile;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return Custom plugin console logger.
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,83 @@
 | 
			
		||||
/*
 | 
			
		||||
 * ItemCase is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU 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 General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 | 
			
		||||
 */
 | 
			
		||||
package com.gmail.bleedobsidian.itemcase.configurations;
 | 
			
		||||
 | 
			
		||||
import com.gmail.bleedobsidian.itemcase.ConfigurationFile;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import org.bukkit.Material;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The main configuration file for ItemCase. 
 | 
			
		||||
 * 
 | 
			
		||||
 * @author Jesse Prescott (BleedObsidian)
 | 
			
		||||
 */
 | 
			
		||||
public class ConfigFile extends ConfigurationFile {
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Constructor.
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigFile() {
 | 
			
		||||
        
 | 
			
		||||
        // The name of this configuration file.
 | 
			
		||||
        super("config.yml");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return The chosen locale to use.
 | 
			
		||||
     */
 | 
			
		||||
    public String getLocale() {
 | 
			
		||||
        return this.file.getString("Locale");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return If players can create Itemcases by sneaking and right-clicking.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean canSneakCreate() {
 | 
			
		||||
        return !this.file.getBoolean("Options.Disable-Sneak-Create");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return If orders can timeout.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean canTimeout() {
 | 
			
		||||
        return !this.file.getBoolean("Order.Disable-Timeout");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return The number of seconds required to pass for an order to timeout.
 | 
			
		||||
     */
 | 
			
		||||
    public int getTimeout() {
 | 
			
		||||
        return this.file.getInt("Order.Timeout");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * @return An array list of materials that can be used as Itemcases.
 | 
			
		||||
     */
 | 
			
		||||
    public ArrayList<Material> getMaterials() {
 | 
			
		||||
        
 | 
			
		||||
        // Create array list.
 | 
			
		||||
        ArrayList<Material> materials = new ArrayList<>();
 | 
			
		||||
        
 | 
			
		||||
        // Loop through all material IDs in config.
 | 
			
		||||
        this.file.getStringList("Materials").forEach((id) -> {
 | 
			
		||||
            
 | 
			
		||||
            // Add material to list.
 | 
			
		||||
            materials.add(Material.getMaterial(id));
 | 
			
		||||
        });
 | 
			
		||||
        
 | 
			
		||||
        // Return list.
 | 
			
		||||
        return materials;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								src/main/resources/config.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/main/resources/config.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#-------------------- ItemCase Configuration File --------------------#
 | 
			
		||||
# This file contains all of the ItemCase configuration settings that  #
 | 
			
		||||
# can be altered.                                                     #
 | 
			
		||||
#---------------------------------------------------------------------#
 | 
			
		||||
 | 
			
		||||
# Locale: 
 | 
			
		||||
#     - en-us
 | 
			
		||||
Locale: en-us
 | 
			
		||||
 | 
			
		||||
# General Options
 | 
			
		||||
Options:
 | 
			
		||||
    
 | 
			
		||||
    # Disable ItemCase creation via sneaking and right clicking.
 | 
			
		||||
    Disable-Sneak-Create: false
 | 
			
		||||
    
 | 
			
		||||
# Order options
 | 
			
		||||
Order:
 | 
			
		||||
    
 | 
			
		||||
    # Disable automatic order timeout for itemcase shops.
 | 
			
		||||
    Disable-Timeout: false
 | 
			
		||||
    
 | 
			
		||||
    # The time in seconds before an order automatically times out.
 | 
			
		||||
    Timeout: 15
 | 
			
		||||
   
 | 
			
		||||
# Materials that can be used for ItemCases.
 | 
			
		||||
# https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html
 | 
			
		||||
Materials:
 | 
			
		||||
    - 'STEP'                # Slabs.
 | 
			
		||||
    - 'WOOD_STEP'           # Wooden Slabs.
 | 
			
		||||
    - 'PURPUR_SLAB'         # Purpur Slabs.
 | 
			
		||||
		Reference in New Issue
	
	Block a user