From cce307035b981f2fcb14cf8e33c63be373ee8b28 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Thu, 24 Feb 2022 15:07:02 -0600 Subject: [PATCH] More implementations --- .idea/vcs.xml | 6 ++ .../configurations/ConfigFactory.java | 4 +- .../configurations/Configurations.java | 1 - .../simplexdev/configurations/Group.java | 81 ++++++++++++++----- .../simplexdev/configurations/Node.java | 18 ++++- .../simplexdev/configurations/Section.java | 2 +- .../simplexdev/configurations/Utils.java | 32 +++++--- .../simplexdev/configurations/api/IGroup.java | 14 ++-- .../simplexdev/configurations/api/INode.java | 2 +- .../configurations/api/ISection.java | 3 + .../simplexdev/configurations/api/Json.java | 5 ++ .../configurations/api/Serializable.java | 1 + .../simplexdev/configurations/api/TOML.java | 5 ++ .../configurations/{ => api}/Yaml.java | 29 +++---- 14 files changed, 141 insertions(+), 62 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/io/github/simplexdev/configurations/api/Json.java create mode 100644 src/main/java/io/github/simplexdev/configurations/api/TOML.java rename src/main/java/io/github/simplexdev/configurations/{ => api}/Yaml.java (61%) diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/io/github/simplexdev/configurations/ConfigFactory.java b/src/main/java/io/github/simplexdev/configurations/ConfigFactory.java index fc023b1..b5084ce 100644 --- a/src/main/java/io/github/simplexdev/configurations/ConfigFactory.java +++ b/src/main/java/io/github/simplexdev/configurations/ConfigFactory.java @@ -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 bigIntegerNode(String name, BigInteger value) { + public INode doubleNode(String name, Double value) { return new Node<>(name, value); } } diff --git a/src/main/java/io/github/simplexdev/configurations/Configurations.java b/src/main/java/io/github/simplexdev/configurations/Configurations.java index deb4288..fdadf8e 100644 --- a/src/main/java/io/github/simplexdev/configurations/Configurations.java +++ b/src/main/java/io/github/simplexdev/configurations/Configurations.java @@ -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; diff --git a/src/main/java/io/github/simplexdev/configurations/Group.java b/src/main/java/io/github/simplexdev/configurations/Group.java index 4fd6525..346c624 100644 --- a/src/main/java/io/github/simplexdev/configurations/Group.java +++ b/src/main/java/io/github/simplexdev/configurations/Group.java @@ -16,7 +16,7 @@ public class Group implements IGroup { private final List> 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; diff --git a/src/main/java/io/github/simplexdev/configurations/Node.java b/src/main/java/io/github/simplexdev/configurations/Node.java index 91ed074..3acd2d9 100644 --- a/src/main/java/io/github/simplexdev/configurations/Node.java +++ b/src/main/java/io/github/simplexdev/configurations/Node.java @@ -7,7 +7,7 @@ public class Node implements INode { private final T value; /** - * @param name The name of the entry. + * @param name The name of the entry. * @param value The value of the entry */ public Node(String name, T value) { @@ -36,9 +36,19 @@ public class Node implements INode { return (getName() + ": " + getValue().toString()); } - // Not sure how to deserialize here. + // I think this might work @Override - public INode 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); } } diff --git a/src/main/java/io/github/simplexdev/configurations/Section.java b/src/main/java/io/github/simplexdev/configurations/Section.java index 1e428cc..473537b 100644 --- a/src/main/java/io/github/simplexdev/configurations/Section.java +++ b/src/main/java/io/github/simplexdev/configurations/Section.java @@ -10,9 +10,9 @@ import java.util.ArrayList; import java.util.List; public class Section implements ISection { + private static final List> ungroupedNodes = new ArrayList<>(); private final String name; private final List groupList; - private static final List> ungroupedNodes = new ArrayList<>(); public Section(String name) { this.name = name; diff --git a/src/main/java/io/github/simplexdev/configurations/Utils.java b/src/main/java/io/github/simplexdev/configurations/Utils.java index 6e0691f..b0bbbbc 100644 --- a/src/main/java/io/github/simplexdev/configurations/Utils.java +++ b/src/main/java/io/github/simplexdev/configurations/Utils.java @@ -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; diff --git a/src/main/java/io/github/simplexdev/configurations/api/IGroup.java b/src/main/java/io/github/simplexdev/configurations/api/IGroup.java index 75636a9..66f640a 100644 --- a/src/main/java/io/github/simplexdev/configurations/api/IGroup.java +++ b/src/main/java/io/github/simplexdev/configurations/api/IGroup.java @@ -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 createGroup(String name, INode... nodes); +public interface IGroup extends Serializable { + 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{ */ @Nullable Collection> getNodes(); + IGroup addNodes(INode... nodes); + IGroup removeNodes(INode... nodes); + IGroup addNode(INode node); + IGroup removeNode(INode node); } diff --git a/src/main/java/io/github/simplexdev/configurations/api/INode.java b/src/main/java/io/github/simplexdev/configurations/api/INode.java index 9f50568..d6ac004 100644 --- a/src/main/java/io/github/simplexdev/configurations/api/INode.java +++ b/src/main/java/io/github/simplexdev/configurations/api/INode.java @@ -1,6 +1,6 @@ package io.github.simplexdev.configurations.api; -public interface INode extends Serializable> { +public interface INode extends Serializable { /** * @return The name of the configuration entry. */ diff --git a/src/main/java/io/github/simplexdev/configurations/api/ISection.java b/src/main/java/io/github/simplexdev/configurations/api/ISection.java index d196045..f6a614a 100644 --- a/src/main/java/io/github/simplexdev/configurations/api/ISection.java +++ b/src/main/java/io/github/simplexdev/configurations/api/ISection.java @@ -7,10 +7,13 @@ import org.jetbrains.annotations.Nullable; public interface ISection extends Serializable { @NotNull String getName(); + @Nullable INode getNode(String name); + @Contract(pure = true) void setNode(String name, T node); + @Nullable IGroup getGroup(String name); } diff --git a/src/main/java/io/github/simplexdev/configurations/api/Json.java b/src/main/java/io/github/simplexdev/configurations/api/Json.java new file mode 100644 index 0000000..4c20697 --- /dev/null +++ b/src/main/java/io/github/simplexdev/configurations/api/Json.java @@ -0,0 +1,5 @@ +package io.github.simplexdev.configurations.api; + +public abstract class Json { + +} diff --git a/src/main/java/io/github/simplexdev/configurations/api/Serializable.java b/src/main/java/io/github/simplexdev/configurations/api/Serializable.java index 955c053..01dcd05 100644 --- a/src/main/java/io/github/simplexdev/configurations/api/Serializable.java +++ b/src/main/java/io/github/simplexdev/configurations/api/Serializable.java @@ -2,5 +2,6 @@ package io.github.simplexdev.configurations.api; public interface Serializable { String serialize(); + T deserialize(String serializedInput); } diff --git a/src/main/java/io/github/simplexdev/configurations/api/TOML.java b/src/main/java/io/github/simplexdev/configurations/api/TOML.java new file mode 100644 index 0000000..0c55c14 --- /dev/null +++ b/src/main/java/io/github/simplexdev/configurations/api/TOML.java @@ -0,0 +1,5 @@ +package io.github.simplexdev.configurations.api; + +public abstract class TOML { + +} diff --git a/src/main/java/io/github/simplexdev/configurations/Yaml.java b/src/main/java/io/github/simplexdev/configurations/api/Yaml.java similarity index 61% rename from src/main/java/io/github/simplexdev/configurations/Yaml.java rename to src/main/java/io/github/simplexdev/configurations/api/Yaml.java index c607d47..d7d1cef 100644 --- a/src/main/java/io/github/simplexdev/configurations/Yaml.java +++ b/src/main/java/io/github/simplexdev/configurations/api/Yaml.java @@ -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; }