More implementations

This commit is contained in:
Paldiu 2022-02-24 15:07:02 -06:00
parent c69709a4a1
commit cce307035b
14 changed files with 141 additions and 62 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -3,8 +3,6 @@ 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);
@ -32,7 +30,7 @@ public class ConfigFactory {
return new Node<>(name, value);
}
public INode<BigInteger> bigIntegerNode(String name, BigInteger value) {
public INode<Double> doubleNode(String name, Double value) {
return new Node<>(name, value);
}
}

View File

@ -2,7 +2,6 @@ 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;

View File

@ -16,7 +16,7 @@ public class Group implements IGroup {
private final List<INode<?>> nodes = new ArrayList<>();
private final String name;
private final ISection section;
private IGroup group;
private IGroup group = null;
public Group(ISection section, String name, INode<?>... nodes) {
this.nodes.addAll(Arrays.asList(nodes));
@ -24,47 +24,86 @@ public class Group implements IGroup {
this.name = name;
}
public Group(ISection section, String name) {
/**
* Initializes this group as an empty group.
*
* @param section The parent section for this group.
* @param name The name of this group.
*/
public Group(@NotNull ISection section, @NotNull String name) {
this.section = section;
this.name = name;
}
public Group(ISection section, IGroup group, String name) {
this.section = section;
/**
* Initializes this Group as a nested group.
* It is important to note that the section assigned to this group will be the section assigned to the parent group.
* If this is a nested group of a nested group, it will search upwards until the parent group is discovered,
* and the parent section of that group will be assigned.
*
* @param group The parent group of this group.
* @param name The name of this group.
*/
public Group(@NotNull IGroup group, @NotNull String name) {
this.section = group.getSection();
this.group = group;
this.name = name;
}
public String getName() {
return name;
/**
* Creates a nested group
*
* @param name The name of the nested group
* @param nodes The node entries to include in the group
* @return The created nested group.
*/
@Override
public IGroup createNestedGroup(IGroup group, String name, INode<?>... nodes) {
IGroup f = new Group(this, name);
nestedGroups.add(f);
return f;
}
@Override
public IGroup createGroup(String name, INode<?>... nodes) {
IGroup group = new Group(section, name, nodes);
/**
* Creates an empty nested group.
*
* @param group The parent group.
* @param name The name of the nested group.
* @return The nested group.
*/
public IGroup emptyGroup(IGroup group, String name) {
IGroup f = new Group(group, name);
nestedGroups.add(group);
return f;
}
/**
* Gets the parent group. If there is no parent group, this will return null.
* Please remember to use proper null catching when implementing this.
*
* @return The parent group, or null if there is no parent.
*/
@Override
@Nullable
public IGroup getParentGroup() {
return group;
}
/**
* Creates a group as a child of this group.
* @param name The name of the nested group.
* @return The nested group.
* @return The name of this group.
*/
public IGroup emptyGroup(String name) {
IGroup group = new Group(section, name);
nestedGroups.add(group);
return group;
public String getName() {
return name;
}
/**
* 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) {
public @Nullable IGroup getNestedGroup(String name) {
for (IGroup nested : nestedGroups) {
if (nested.getName().equalsIgnoreCase(name)) {
return nested;
@ -73,6 +112,12 @@ public class Group implements IGroup {
return null;
}
/**
* Gets the parent section associated with this group.
* This will get the parent section regardless of nesting because sections are singular.
*
* @return The parent section of this group.
*/
@Override
public @NotNull ISection getSection() {
return section;

View File

@ -36,9 +36,19 @@ public class Node<T> implements INode<T> {
return (getName() + ": " + getValue().toString());
}
// Not sure how to deserialize here.
// I think this might work
@Override
public INode<T> deserialize(String serializedInput) {
return null;
public INode deserialize(String serializedInput) {
String[] split = serializedInput.split(":\t");
String name = split[0].trim();
String value = split[1].trim();
Utils utils = new Utils();
if (utils.checkForBoolean(value)) return new Node<>(name, Boolean.parseBoolean(value));
if (utils.checkForFloat(value)) return new Node<>(name, Float.parseFloat(value));
if (utils.checkForInt(value)) return new Node<>(name, Integer.parseInt(value));
if (utils.checkForLong(value)) return new Node<>(name, Long.parseLong(value));
if (utils.checkForDouble(value)) return new Node<>(name, Double.parseDouble(value));
return new Node<>(name, value);
}
}

View File

@ -10,9 +10,9 @@ import java.util.ArrayList;
import java.util.List;
public class Section implements ISection {
private static final List<Node<?>> ungroupedNodes = new ArrayList<>();
private final String name;
private final List<IGroup> groupList;
private static final List<Node<?>> ungroupedNodes = new ArrayList<>();
public Section(String name) {
this.name = name;

View File

@ -1,8 +1,12 @@
package io.github.simplexdev.configurations;
public final class Utils {
Utils() {}
import org.jetbrains.annotations.Contract;
public final class Utils {
public Utils() {
}
@Contract(pure = true)
public boolean checkForInt(String input) {
try {
Integer.parseInt(input);
@ -12,31 +16,41 @@ public final class Utils {
}
}
@Contract(pure = true)
public boolean checkForFloat(String input) {
if (!input.endsWith("F")) {
if (!input.endsWith("f") || !input.endsWith("F")) {
return false;
}
try {
Float.parseFloat(input.split("F")[0]);
Float.parseFloat(input);
return true;
} catch (NumberFormatException ignored) {
return false;
}
}
@Contract(pure = true)
public boolean checkForBoolean(String input) {
if (input.equalsIgnoreCase("true")) {
return true;
}
return input.equalsIgnoreCase("false");
return input.equalsIgnoreCase("true");
}
@Contract(pure = true)
public boolean checkForLong(String input) {
if (!input.endsWith("L")) return false;
try {
Long.parseLong(input.split("L")[0]);
Long.parseLong(input);
return true;
} catch (NumberFormatException ignored) {
return false;
}
}
@Contract(pure = true)
public boolean checkForDouble(String value) {
try {
Double.parseDouble(value);
return true;
} catch (NumberFormatException ignored) {
return false;

View File

@ -1,29 +1,29 @@
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);
IGroup createNestedGroup(IGroup group, 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);
IGroup getParentGroup();
/**
* 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
@ -34,8 +34,12 @@ public interface IGroup extends Serializable<IGroup>{
*/
@Nullable
Collection<? extends INode<?>> getNodes();
IGroup addNodes(INode<?>... nodes);
IGroup removeNodes(INode<?>... nodes);
IGroup addNode(INode<?> node);
IGroup removeNode(INode<?> node);
}

View File

@ -1,6 +1,6 @@
package io.github.simplexdev.configurations.api;
public interface INode<T> extends Serializable<INode<T>> {
public interface INode<T> extends Serializable<INode> {
/**
* @return The name of the configuration entry.
*/

View File

@ -7,10 +7,13 @@ 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);
}

View File

@ -0,0 +1,5 @@
package io.github.simplexdev.configurations.api;
public abstract class Json {
}

View File

@ -2,5 +2,6 @@ package io.github.simplexdev.configurations.api;
public interface Serializable<T> {
String serialize();
T deserialize(String serializedInput);
}

View File

@ -0,0 +1,5 @@
package io.github.simplexdev.configurations.api;
public abstract class TOML {
}

View File

@ -1,11 +1,11 @@
package io.github.simplexdev.configurations;
package io.github.simplexdev.configurations.api;
import io.github.simplexdev.configurations.api.IGroup;
import io.github.simplexdev.configurations.api.INode;
import io.github.simplexdev.configurations.api.ISection;
import io.github.simplexdev.configurations.Configurations;
import io.github.simplexdev.configurations.Utils;
import org.bukkit.plugin.Plugin;
import java.io.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ -26,29 +26,18 @@ public abstract class Yaml {
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();
}
if (copyResource) {
plugin.saveResource(fileName, true);
}
this.configFile = file;
}