diff --git a/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/Reg.java b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/Reg.java new file mode 100644 index 0000000..1f2bce2 --- /dev/null +++ b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/Reg.java @@ -0,0 +1,23 @@ +package io.github.hyscript7.ascendancy.api.registry; + +import java.util.List; +import java.util.Optional; + +/** +*The Registry which binds a unified key to a generic object +*/ +public interface Reg { + /** + *Adds an object to the register + */ + void register(E value) throws RegIdentifierCollision; + /** + *Returns all registered elements + */ + List getAll(); + /** + *Returns the element associated with the provided id + *Implementation detail: Must log a warning when a lookup fails + */ + Optional get(RegIdentifier id); +} \ No newline at end of file diff --git a/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifiable.java b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifiable.java new file mode 100644 index 0000000..23f0f8a --- /dev/null +++ b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifiable.java @@ -0,0 +1,8 @@ +package io.github.hyscript7.ascendancy.api.registry; + +public interface RegIdentifiable{ + /** + * Must return the identifier for the object implementing this method + */ + RegIdentifier getIdentifier(); +} \ No newline at end of file diff --git a/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifier.java b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifier.java new file mode 100644 index 0000000..2450c1e --- /dev/null +++ b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifier.java @@ -0,0 +1,47 @@ +package io.github.hyscript7.ascendancy.api.registry; + +public record RegIdentifier(String namespace, String path) +{ + public RegIdentifier { + if (namespace == null || namespace.isBlank()) { + throw new IllegalArgumentException("Namespace cannot be empty"); + } + if (path == null || path.isBlank()) { + throw new IllegalArgumentException("Path cannot be empty"); + } + } + + public static RegIdentifier of(String namespace, String path) { + return new RegIdentifier(namespace, path); + } + + public static RegIdentifier of(org.bukkit.plugin.Plugin plugin, String path) { + if (plugin == null) { + throw new IllegalArgumentException("Plugin cannot be null"); + } + return new RegIdentifier(plugin.getName(), path); + } + /** + *Parses a stringified id into 2 parts using a colon as a separator [namespace]:[path] + *Then it returns them as a an instance of this class + */ + public static RegIdentifier parse(String value) { + if (value == null || value.isBlank()) { + throw new IllegalArgumentException("Identifier cannot be null/blank"); + } + + String[] split = value.split(":", 2); + if (split.length != 2) { + throw new IllegalArgumentException("Invalid identifier format: " + value); + } + + return new RegIdentifier(split[0], split[1]); + } + /** + * This is the reverse of the parse method, where it combines the 2 id segments together + */ + @Override + public String toString() { + return namespace + ":" + path; + } +} \ No newline at end of file diff --git a/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifierCollision.java b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifierCollision.java new file mode 100644 index 0000000..bef89a5 --- /dev/null +++ b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifierCollision.java @@ -0,0 +1,8 @@ +package io.github.hyscript7.ascendancy.api.registry; + +/** +*Thrown when an object by the same identifier already exists in the registry +*/ +public class RegIdentifierCollision extends Exception +{ +} \ No newline at end of file