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..bd378f0 --- /dev/null +++ b/api/src/main/java/io/github/hyscript7/ascendancy/api/registry/RegIdentifierCollision.java @@ -0,0 +1,13 @@ +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 +{ + + public RegIdentifierCollision(String message) { + super(message); + } + +} \ No newline at end of file diff --git a/core/src/main/java/io/github/hyscript7/ascendancy/BaseReg.java b/core/src/main/java/io/github/hyscript7/ascendancy/BaseReg.java new file mode 100644 index 0000000..3b22a83 --- /dev/null +++ b/core/src/main/java/io/github/hyscript7/ascendancy/BaseReg.java @@ -0,0 +1,44 @@ +package io.github.hyscript7.ascendancy; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +import io.github.hyscript7.ascendancy.api.registry.Reg; +import io.github.hyscript7.ascendancy.api.registry.RegIdentifiable; +import io.github.hyscript7.ascendancy.api.registry.RegIdentifier; +import io.github.hyscript7.ascendancy.api.registry.RegIdentifierCollision; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class BaseReg implements Reg { + ConcurrentHashMap storiage; + @Override + public void register(E value) throws RegIdentifierCollision { + if (storiage.get(value.getIdentifier()) == null) { + storiage.put(value.getIdentifier(), value); + } + else + { + log.warn("An entry with an existing identidier was attempted to be made: " + value.getIdentifier()); + throw new RegIdentifierCollision(("An entry with the same identidier already exists: " + value.getIdentifier() )); + } + } + + @Override + public List getAll() { + + return(new ArrayList<>(storiage.values())); + } + + @Override + public Optional get(RegIdentifier id) { + Optional entry = Optional.ofNullable(storiage.get(id)); + if (entry.isEmpty()){ + log.warn("Entity of an id \"" + id + "\" does not exist."); + } + return(entry); + } + +}