Skip to content

Instantly share code, notes, and snippets.

@dlsf
Last active August 27, 2019 16:52
Show Gist options
  • Save dlsf/e6f67e0cca74fca5c40d6d1071e99291 to your computer and use it in GitHub Desktop.
Save dlsf/e6f67e0cca74fca5c40d6d1071e99291 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) Daniel Scherf - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Written by Daniel Scherf <munita.gavai@gmail.com>, 2019
*/
package net.seliba.dailyquest.configuration;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import jdk.internal.jline.internal.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Class which allows to easily
* manage a Bukkit configuration file
*/
public class Config extends YamlConfiguration {
private String name;
private JavaPlugin javaPlugin;
private File file;
/**
* The default constructor to
* initialize a new configuration
* and create it if needed
* @param name The name of the configuration file followed by the file extension
* @param javaPlugin The instance of the current JavaPlugin instance
*/
public Config(String name, JavaPlugin javaPlugin) {
this.name = name;
this.javaPlugin = javaPlugin;
// Reload the configuration
reload();
}
/**
* Reload the configuration file
* and load the YamlConfiguration again
*/
private void reload() {
file = new File(javaPlugin.getDataFolder(), name);
try {
// Check if the file already exists
if (!file.exists())
// Create a new file and check whether it was successful
if (!file.createNewFile())
// Throw an exception
throw new RuntimeException("Could not create " + name);
// Load the configuration from the file
load(file);
} catch (InvalidConfigurationException | IOException e) {
// Do nothing, just ignore it
}
}
/**
* Save the configuration to
* the provided file
*/
public void save() {
try {
// Save the content of the YamlConfiguration to the file
save(file);
} catch (IOException e) {
// Print the stack trace
e.printStackTrace();
}
}
/**
* Save a value to the configuration
* if it doesn't already exist
* @param path The path where the value should be stored
* @param value The value that should be stored
*/
public void setDefault(String path, Object value) {
// Set the value into the configuration if it doesn't already exist
if(!isSet(path)) {
set(path, value);
}
}
/**
* Save a location to the configuration file
* @param path The path where the value should be stored
* @param location The location that should be stored
*/
public void setLocation(String path, Location location) {
// Save all fields of the location in the configuration
set(path + ".world", Objects.requireNonNull(location.getWorld()).getName());
set(path + ".x", location.getX());
set(path + ".y", location.getY());
set(path + ".z", location.getZ());
set(path + ".yaw", location.getYaw());
set(path + ".pitch", location.getPitch());
}
/**
* Accesses a location from the configuration
* and returns a new Location instance
* @param path The path where the value is stored
* @return The Location loaded from the configuration
*/
public @Nullable Location getLocation(String path) {
// Build a new location from the provided path
return new Location(
Bukkit.getWorld(Objects.requireNonNull(getString(path + ".world"))),
getDouble(path + ".x"),
getDouble(path + ".y"),
getDouble(path + ".z"),
(float) getDouble(path + ".yaw"),
(float) getDouble(path + ".pitch")
);
}
/**
* Removes a location from the configuration
* @param path The path where the value is stored
*/
public void removeLocation(String path) {
// Set all location paths to null
set(path + ".world", null);
set(path + ".x", null);
set(path + ".y", null);
set(path + ".z", null);
set(path + ".yaw", null);
set(path + ".pitch", null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment