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.INode;
import io.github.simplexdev.configurations.api.ISection; import io.github.simplexdev.configurations.api.ISection;
import java.math.BigInteger;
public class ConfigFactory { public class ConfigFactory {
public ISection createNewSection(String name) { public ISection createNewSection(String name) {
ISection section = new Section(name); ISection section = new Section(name);
@ -32,7 +30,7 @@ public class ConfigFactory {
return new Node<>(name, value); 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); return new Node<>(name, value);
} }
} }

View File

@ -2,7 +2,6 @@ package io.github.simplexdev.configurations;
import io.github.simplexdev.configurations.api.ISection; import io.github.simplexdev.configurations.api.ISection;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -16,7 +16,7 @@ public class Group implements IGroup {
private final List<INode<?>> nodes = new ArrayList<>(); private final List<INode<?>> nodes = new ArrayList<>();
private final String name; private final String name;
private final ISection section; private final ISection section;
private IGroup group; private IGroup group = null;
public Group(ISection section, String name, INode<?>... nodes) { public Group(ISection section, String name, INode<?>... nodes) {
this.nodes.addAll(Arrays.asList(nodes)); this.nodes.addAll(Arrays.asList(nodes));
@ -24,47 +24,86 @@ public class Group implements IGroup {
this.name = name; 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.section = section;
this.name = name; 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.group = group;
this.name = name; 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) { * Creates an empty nested group.
IGroup group = new Group(section, name, nodes); *
* @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); 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; return group;
} }
/** /**
* Creates a group as a child of this group. * @return The name of this group.
* @param name The name of the nested group.
* @return The nested group.
*/ */
public IGroup emptyGroup(String name) { public String getName() {
IGroup group = new Group(section, name); return name;
nestedGroups.add(group);
return group;
} }
/** /**
* Gets a nested group. * Gets a nested group.
* Can return null if no nested group exists. * Can return null if no nested group exists.
*
* @param name The name of the nested group * @param name The name of the nested group
* @return The nested group. * @return The nested group.
*/ */
@Override public @Nullable IGroup getNestedGroup(String name) {
public @Nullable IGroup getGroup(String name) {
for (IGroup nested : nestedGroups) { for (IGroup nested : nestedGroups) {
if (nested.getName().equalsIgnoreCase(name)) { if (nested.getName().equalsIgnoreCase(name)) {
return nested; return nested;
@ -73,6 +112,12 @@ public class Group implements IGroup {
return null; 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 @Override
public @NotNull ISection getSection() { public @NotNull ISection getSection() {
return section; return section;

View File

@ -36,9 +36,19 @@ public class Node<T> implements INode<T> {
return (getName() + ": " + getValue().toString()); return (getName() + ": " + getValue().toString());
} }
// Not sure how to deserialize here. // I think this might work
@Override @Override
public INode<T> deserialize(String serializedInput) { public INode deserialize(String serializedInput) {
return null; 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; import java.util.List;
public class Section implements ISection { public class Section implements ISection {
private static final List<Node<?>> ungroupedNodes = new ArrayList<>();
private final String name; private final String name;
private final List<IGroup> groupList; private final List<IGroup> groupList;
private static final List<Node<?>> ungroupedNodes = new ArrayList<>();
public Section(String name) { public Section(String name) {
this.name = name; this.name = name;

View File

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

View File

@ -1,29 +1,29 @@
package io.github.simplexdev.configurations.api; package io.github.simplexdev.configurations.api;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collection; import java.util.Collection;
public interface IGroup extends Serializable<IGroup>{ public interface IGroup extends Serializable<IGroup> {
IGroup createGroup(String name, INode<?>... nodes); IGroup createNestedGroup(IGroup group, String name, INode<?>... nodes);
String getName(); String getName();
/** /**
* Gets the parent group that this group is in. * Gets the parent group that this group is in.
* This will return null if there is no parent group associated with this group. * 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. * @return The parent group this group is associated to. Returns null if there is no parent group.
*/ */
@Nullable @Nullable
IGroup getGroup(String name); IGroup getParentGroup();
/** /**
* Gets the parent section associated to this group. * Gets the parent section associated to this group.
* Groups cannot exist outside a parent section. * Groups cannot exist outside a parent section.
* Groups may be nested. * Groups may be nested.
*
* @return The parent section of this group. * @return The parent section of this group.
*/ */
@NotNull @NotNull
@ -34,8 +34,12 @@ public interface IGroup extends Serializable<IGroup>{
*/ */
@Nullable @Nullable
Collection<? extends INode<?>> getNodes(); Collection<? extends INode<?>> getNodes();
IGroup addNodes(INode<?>... nodes); IGroup addNodes(INode<?>... nodes);
IGroup removeNodes(INode<?>... nodes); IGroup removeNodes(INode<?>... nodes);
IGroup addNode(INode<?> node); IGroup addNode(INode<?> node);
IGroup removeNode(INode<?> node); IGroup removeNode(INode<?> node);
} }

View File

@ -1,6 +1,6 @@
package io.github.simplexdev.configurations.api; 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. * @return The name of the configuration entry.
*/ */

View File

@ -7,10 +7,13 @@ import org.jetbrains.annotations.Nullable;
public interface ISection extends Serializable<ISection> { public interface ISection extends Serializable<ISection> {
@NotNull @NotNull
String getName(); String getName();
@Nullable @Nullable
INode<?> getNode(String name); INode<?> getNode(String name);
@Contract(pure = true) @Contract(pure = true)
<T> void setNode(String name, T node); <T> void setNode(String name, T node);
@Nullable @Nullable
IGroup getGroup(String name); 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> { public interface Serializable<T> {
String serialize(); String serialize();
T deserialize(String serializedInput); 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.Configurations;
import io.github.simplexdev.configurations.api.INode; import io.github.simplexdev.configurations.Utils;
import io.github.simplexdev.configurations.api.ISection;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.io.*; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -26,29 +26,18 @@ public abstract class Yaml {
File dataFolder = plugin.getDataFolder(); File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists()) dataFolder.mkdir(); if (!dataFolder.exists()) dataFolder.mkdir();
this.dataFolder = dataFolder; this.dataFolder = dataFolder;
File file = new File(dataFolder, fileName); 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 { try {
file.createNewFile(); file.createNewFile();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (copyResource) {
plugin.saveResource(fileName, true);
}
this.configFile = file; this.configFile = file;
} }