mirror of
https://github.com/SimplexDevelopment/Configurations.git
synced 2025-07-01 22:56:42 +00:00
Initial commit
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.INode;
|
||||
import io.github.simplexdev.configurations.api.ISection;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class ConfigFactory {
|
||||
public ISection createNewSection(String name) {
|
||||
ISection section = new Section(name);
|
||||
Configurations.availableSections.add(section);
|
||||
return section;
|
||||
}
|
||||
|
||||
public INode<String> stringNode(String name, String value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
|
||||
public INode<Boolean> booleanNode(String name, boolean value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
|
||||
public INode<Long> longNode(String name, long value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
|
||||
public INode<Float> floatNode(String name, float value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
|
||||
public INode<Integer> integerNode(String name, int value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
|
||||
public INode<BigInteger> bigIntegerNode(String name, BigInteger value) {
|
||||
return new Node<>(name, value);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.ISection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class Configurations {
|
||||
public static final ConfigFactory factory = new ConfigFactory();
|
||||
public static final List<ISection> availableSections = new ArrayList<>();
|
||||
|
||||
private final String pluginName;
|
||||
private final Plugin plugin;
|
||||
|
||||
public Configurations(@NotNull Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
this.pluginName = plugin.getName();
|
||||
}
|
||||
|
||||
public String getPluginName() {
|
||||
return pluginName;
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
119
src/main/java/io/github/simplexdev/configurations/Group.java
Normal file
119
src/main/java/io/github/simplexdev/configurations/Group.java
Normal file
@ -0,0 +1,119 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.IGroup;
|
||||
import io.github.simplexdev.configurations.api.INode;
|
||||
import io.github.simplexdev.configurations.api.ISection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class Group implements IGroup {
|
||||
private final List<IGroup> nestedGroups = new ArrayList<>();
|
||||
private final List<INode<?>> nodes = new ArrayList<>();
|
||||
private final String name;
|
||||
private final ISection section;
|
||||
private IGroup group;
|
||||
|
||||
public Group(ISection section, String name, INode<?>... nodes) {
|
||||
this.nodes.addAll(Arrays.asList(nodes));
|
||||
this.section = section;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Group(ISection section, String name) {
|
||||
this.section = section;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Group(ISection section, IGroup group, String name) {
|
||||
this.section = section;
|
||||
this.group = group;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup createGroup(String name, INode<?>... nodes) {
|
||||
IGroup group = new Group(section, name, nodes);
|
||||
nestedGroups.add(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a group as a child of this group.
|
||||
* @param name The name of the nested group.
|
||||
* @return The nested group.
|
||||
*/
|
||||
public IGroup emptyGroup(String name) {
|
||||
IGroup group = new Group(section, name);
|
||||
nestedGroups.add(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a nested group.
|
||||
* Can return null if no nested group exists.
|
||||
* @param name The name of the nested group
|
||||
* @return The nested group.
|
||||
*/
|
||||
@Override
|
||||
public @Nullable IGroup getGroup(String name) {
|
||||
for (IGroup nested : nestedGroups) {
|
||||
if (nested.getName().equalsIgnoreCase(name)) {
|
||||
return nested;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ISection getSection() {
|
||||
return section;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Collection<? extends INode<?>> getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup addNodes(INode<?>... nodes) {
|
||||
this.nodes.addAll(Arrays.asList(nodes));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup removeNodes(INode<?>... nodes) {
|
||||
this.nodes.removeAll(Arrays.asList(nodes));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup addNode(INode<?> node) {
|
||||
nodes.add(node);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup removeNode(INode<?> node) {
|
||||
nodes.remove(node);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGroup deserialize(String serializedInput) {
|
||||
return getSection().getGroup(serializedInput);
|
||||
}
|
||||
}
|
44
src/main/java/io/github/simplexdev/configurations/Node.java
Normal file
44
src/main/java/io/github/simplexdev/configurations/Node.java
Normal file
@ -0,0 +1,44 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.INode;
|
||||
|
||||
public class Node<T> implements INode<T> {
|
||||
private final String name;
|
||||
private final T value;
|
||||
|
||||
/**
|
||||
* @param name The name of the entry.
|
||||
* @param value The value of the entry
|
||||
*/
|
||||
public Node(String name, T value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The name of the entry.
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value of the entry.
|
||||
*/
|
||||
@Override
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize() {
|
||||
return (getName() + ": " + getValue().toString());
|
||||
}
|
||||
|
||||
// Not sure how to deserialize here.
|
||||
@Override
|
||||
public INode<T> deserialize(String serializedInput) {
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.IGroup;
|
||||
import io.github.simplexdev.configurations.api.INode;
|
||||
import io.github.simplexdev.configurations.api.ISection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Section implements ISection {
|
||||
private final String name;
|
||||
private final List<IGroup> groupList;
|
||||
private static final List<Node<?>> ungroupedNodes = new ArrayList<>();
|
||||
|
||||
public Section(String name) {
|
||||
this.name = name;
|
||||
this.groupList = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public INode<?> getNode(String name) {
|
||||
for (INode<?> nodes : ungroupedNodes) {
|
||||
if (nodes.getName().equalsIgnoreCase(name)) {
|
||||
return nodes;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setNode(String name, T value) {
|
||||
ungroupedNodes.forEach(a -> {
|
||||
if (a.getName().equalsIgnoreCase(name)) {
|
||||
ungroupedNodes.remove(a);
|
||||
}
|
||||
ungroupedNodes.add(new Node<>(name, value));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable IGroup getGroup(String name) {
|
||||
for (IGroup group : groupList) {
|
||||
if (group.getName().equalsIgnoreCase(name)) {
|
||||
return group;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addGroup(IGroup group) {
|
||||
groupList.add(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ISection deserialize(String serializedInput) {
|
||||
for (ISection section : Configurations.availableSections) {
|
||||
if (section.getName().equalsIgnoreCase(serializedInput)) {
|
||||
return section;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
45
src/main/java/io/github/simplexdev/configurations/Utils.java
Normal file
45
src/main/java/io/github/simplexdev/configurations/Utils.java
Normal file
@ -0,0 +1,45 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
public final class Utils {
|
||||
Utils() {}
|
||||
|
||||
public boolean checkForInt(String input) {
|
||||
try {
|
||||
Integer.parseInt(input);
|
||||
return true;
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkForFloat(String input) {
|
||||
if (!input.endsWith("F")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
Float.parseFloat(input.split("F")[0]);
|
||||
return true;
|
||||
} catch (NumberFormatException ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkForBoolean(String input) {
|
||||
if (input.equalsIgnoreCase("true")) {
|
||||
return true;
|
||||
}
|
||||
return input.equalsIgnoreCase("false");
|
||||
}
|
||||
|
||||
public boolean checkForLong(String input) {
|
||||
if (!input.endsWith("L")) return false;
|
||||
|
||||
try {
|
||||
Long.parseLong(input.split("L")[0]);
|
||||
return true;
|
||||
} catch (NumberFormatException ignored) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
63
src/main/java/io/github/simplexdev/configurations/Yaml.java
Normal file
63
src/main/java/io/github/simplexdev/configurations/Yaml.java
Normal file
@ -0,0 +1,63 @@
|
||||
package io.github.simplexdev.configurations;
|
||||
|
||||
import io.github.simplexdev.configurations.api.IGroup;
|
||||
import io.github.simplexdev.configurations.api.INode;
|
||||
import io.github.simplexdev.configurations.api.ISection;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public abstract class Yaml {
|
||||
private final Plugin plugin;
|
||||
private final File dataFolder;
|
||||
private final File configFile;
|
||||
private final Utils utils;
|
||||
|
||||
private final Map<ISection, ArrayList<IGroup>> groupsPerSection = new HashMap<>();
|
||||
private final Map<IGroup, ArrayList<INode<?>>> nodesPerGroup = new HashMap<>();
|
||||
|
||||
public Yaml(Plugin plugin, String fileName, boolean copyResource) {
|
||||
this.plugin = plugin;
|
||||
this.utils = new Utils();
|
||||
|
||||
File dataFolder = plugin.getDataFolder();
|
||||
if (!dataFolder.exists()) dataFolder.mkdir();
|
||||
this.dataFolder = dataFolder;
|
||||
File file = new File(dataFolder, fileName);
|
||||
if (copyResource) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
InputStream inputStream = plugin.getResource(fileName);
|
||||
FileOutputStream outputStream = new FileOutputStream(file);
|
||||
byte[] buffer = inputStream.readAllBytes();
|
||||
outputStream.write(buffer);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.configFile = file;
|
||||
}
|
||||
|
||||
public ISection getSection(String name) {
|
||||
for (ISection section : Configurations.availableSections) {
|
||||
if (section.getName().equalsIgnoreCase(name)) {
|
||||
return section;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package io.github.simplexdev.configurations.api;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface IGroup extends Serializable<IGroup>{
|
||||
IGroup createGroup(String name, INode<?>... nodes);
|
||||
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Gets the parent group that this group is in.
|
||||
* This will return null if there is no parent group associated with this group.
|
||||
* @param name
|
||||
* @return The parent group this group is associated to. Returns null if there is no parent group.
|
||||
*/
|
||||
@Nullable
|
||||
IGroup getGroup(String name);
|
||||
|
||||
/**
|
||||
* Gets the parent section associated to this group.
|
||||
* Groups cannot exist outside a parent section.
|
||||
* Groups may be nested.
|
||||
* @return The parent section of this group.
|
||||
*/
|
||||
@NotNull
|
||||
ISection getSection();
|
||||
|
||||
/**
|
||||
* @return The nodes contained within this group.
|
||||
*/
|
||||
@Nullable
|
||||
Collection<? extends INode<?>> getNodes();
|
||||
IGroup addNodes(INode<?>... nodes);
|
||||
IGroup removeNodes(INode<?>... nodes);
|
||||
IGroup addNode(INode<?> node);
|
||||
IGroup removeNode(INode<?> node);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package io.github.simplexdev.configurations.api;
|
||||
|
||||
public interface INode<T> extends Serializable<INode<T>> {
|
||||
/**
|
||||
* @return The name of the configuration entry.
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* @return The value of the configuration entry.
|
||||
*/
|
||||
T getValue();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package io.github.simplexdev.configurations.api;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface ISection extends Serializable<ISection> {
|
||||
@NotNull
|
||||
String getName();
|
||||
@Nullable
|
||||
INode<?> getNode(String name);
|
||||
@Contract(pure = true)
|
||||
<T> void setNode(String name, T node);
|
||||
@Nullable
|
||||
IGroup getGroup(String name);
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package io.github.simplexdev.configurations.api;
|
||||
|
||||
public interface Serializable<T> {
|
||||
String serialize();
|
||||
T deserialize(String serializedInput);
|
||||
}
|
6
src/main/resources/plugin.yml
Normal file
6
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,6 @@
|
||||
name: Configurations
|
||||
version: '${version}'
|
||||
main: io.github.simplexdev.configurations.Configurations
|
||||
api-version: 1.18
|
||||
authors: [ SimplexDevelopment ]
|
||||
description: A better config api for paper
|
Reference in New Issue
Block a user