Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<E extends RegIdentifiable> {
/**
*Adds an object to the register
*/
void register(E value) throws RegIdentifierCollision;
/**
*Returns all registered elements
*/
List<E> getAll();
/**
*Returns the element associated with the provided id
*Implementation detail: Must log a warning when a lookup fails
*/
Optional<E> get(RegIdentifier id);
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
{
}