Skip to content
Open
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,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);
}

}
44 changes: 44 additions & 0 deletions core/src/main/java/io/github/hyscript7/ascendancy/BaseReg.java
Original file line number Diff line number Diff line change
@@ -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<E extends RegIdentifiable> implements Reg<E> {
ConcurrentHashMap<RegIdentifier,E> 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<E> getAll() {

return(new ArrayList<>(storiage.values()));
}

@Override
public Optional<E> get(RegIdentifier id) {
Optional<E> entry = Optional.ofNullable(storiage.get(id));
if (entry.isEmpty()){
log.warn("Entity of an id \"" + id + "\" does not exist.");
}
return(entry);
}

}