documentation

This commit is contained in:
dfsek
2021-02-21 22:14:38 -07:00
parent 8b196716a4
commit 5e940187d9
14 changed files with 80 additions and 36 deletions

View File

@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Specifies that the annotated class is an entry point for a Terra addon.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Addon {
/**
* @return The ID of the addon.
*/
String value();
}

View File

@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Optional annotation that specifies the author of an addon.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Author {
/**
* @return Name of the addon author.
*/
String value();
}

View File

@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Optional annotation that specifies dependencies of an addon.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Depends {
/**
* @return All addons this addon is dependent upon.
*/
String[] value();
}

View File

@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Optional annotation that specifies the version of an addon.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Version {
/**
* @return Version of the addon.
*/
String value();
}

View File

@@ -5,6 +5,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Specifies that a field is a target for dependency injection.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Inject {

View File

@@ -12,11 +12,11 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD)
public @interface Priority {
/**
* Highest possible priority. Listeners with this priority will always be invoked first.
* Highest possible priority. Listeners with this priority will always be invoked last.
*/
int HIGHEST = Integer.MAX_VALUE;
/**
* Lowest possible priority. Listeners with this priority will always be invoked last.
* Lowest possible priority. Listeners with this priority will always be invoked first.
*/
int LOWEST = Integer.MIN_VALUE;
/**

View File

@@ -30,7 +30,6 @@ public class CheckedRegistry<T> implements TypeLoader<T> {
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
public void add(String identifier, T value) throws DuplicateEntryException {
if(registry.contains(identifier)) throw new DuplicateEntryException("Entry \"" + identifier + "\" is already present in registry.");
registry.addChecked(identifier, value);
}

View File

@@ -3,6 +3,7 @@ package com.dfsek.terra.registry;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import java.lang.reflect.Type;
import java.util.HashMap;
@@ -24,41 +25,48 @@ public abstract class TerraRegistry<T> implements TypeLoader<T> {
}
/**
* Add an object to the registry with a name.
* Add a value to this registry.
*
* @param name Name of the tree.
* @param value Object to increment
* @return True if tree was overwritten.
* @param identifier Identifier to assign value.
* @param value Value to add.
*/
public boolean add(String name, T value) {
boolean exists = objects.containsKey(name);
objects.put(name, value);
public boolean add(String identifier, T value) {
boolean exists = objects.containsKey(identifier);
objects.put(identifier, value);
return exists;
}
public void addChecked(String name, T value) {
if(objects.containsKey(name)) throw new IllegalArgumentException("Value is already defined in registry.");
add(name, value);
/**
* Add a value to this registry, checking whether it is present first.
*
* @param identifier Identifier to assign value.
* @param value Value to add.
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
public void addChecked(String identifier, T value) throws DuplicateEntryException {
if(objects.containsKey(identifier))
throw new DuplicateEntryException("Value with identifier \"" + identifier + "\" is already defined in registry.");
add(identifier, value);
}
/**
* Check if the registry contains an object.
* Check if the registry contains a value.
*
* @param name Name of the object.
* @return Whether the registry contains the object.
* @param identifier Identifier of value.
* @return Whether the registry contains the value.
*/
public boolean contains(String name) {
return objects.containsKey(name);
public boolean contains(String identifier) {
return objects.containsKey(identifier);
}
/**
* Get an object from the registry,
* Get a value from the registry.
*
* @param id ID of object to get
* @return Object
* @param identifier Identifier of value.
* @return Value matching the identifier, {@code null} if no value is present.
*/
public T get(String id) {
return objects.get(id);
public T get(String identifier) {
return objects.get(identifier);
}
public void forEach(Consumer<T> consumer) {

View File

@@ -70,7 +70,7 @@ public class FloraRegistry extends TerraRegistry<Flora> {
@Override
public Flora get(String id) {
return super.get(id);
public Flora get(String identifier) {
return super.get(identifier);
}
}

View File

@@ -14,9 +14,9 @@ public class PaletteRegistry extends TerraRegistry<Palette<BlockData>> {
@Override
public Palette<BlockData> get(String id) {
if(id.startsWith("BLOCK:"))
return new SinglePalette<>(main.getWorldHandle().createBlockData(id.substring(6))); // Return single palette for BLOCK: shortcut.
return super.get(id);
public Palette<BlockData> get(String identifier) {
if(identifier.startsWith("BLOCK:"))
return new SinglePalette<>(main.getWorldHandle().createBlockData(identifier.substring(6))); // Return single palette for BLOCK: shortcut.
return super.get(identifier);
}
}

View File

@@ -45,8 +45,8 @@ public class TreeRegistry extends TerraRegistry<Tree> {
}
@Override
public boolean add(String name, Tree value) {
return super.add(name, value);
public boolean add(String identifier, Tree value) {
return super.add(identifier, value);
}
private final class FractalTreeHolder implements Tree {

View File

@@ -1,5 +1,8 @@
package com.dfsek.terra.registry.exception;
/**
* Thrown when a duplicate entry is found in a registry.
*/
public class DuplicateEntryException extends Exception {
private static final long serialVersionUID = -7199021672428288780L;

View File

@@ -9,6 +9,7 @@ import com.dfsek.terra.addons.loading.pre.AddonPool;
import com.dfsek.terra.addons.loading.pre.PreLoadAddon;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import java.io.File;
import java.io.IOException;
@@ -31,11 +32,11 @@ public class AddonRegistry extends TerraRegistry<TerraAddon> {
}
@Override
public boolean add(String name, TerraAddon addon) {
if(contains(name)) throw new IllegalArgumentException("Addon " + name + " is already registered.");
public boolean add(String identifier, TerraAddon addon) {
if(contains(identifier)) throw new IllegalArgumentException("Addon " + identifier + " is already registered.");
addon.initialize();
main.getLogger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor());
return super.add(name, addon);
return super.add(identifier, addon);
}
@Override
@@ -92,7 +93,7 @@ public class AddonRegistry extends TerraRegistry<TerraAddon> {
}
try {
addChecked(loadedAddon.getName(), loadedAddon);
} catch(IllegalArgumentException e) {
} catch(DuplicateEntryException e) {
valid = false;
main.getLogger().severe("Duplicate addon ID; addon with ID " + loadedAddon.getName() + " is already loaded.");
main.getLogger().severe("Existing addon class: " + get(loadedAddon.getName()).getClass().getCanonicalName());

View File

@@ -3,9 +3,11 @@ package com.dfsek.terra.bukkit.listeners;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventListener;
import com.dfsek.terra.api.core.event.annotations.Global;
import com.dfsek.terra.api.core.event.annotations.Priority;
import com.dfsek.terra.api.core.event.events.config.ConfigPackPreLoadEvent;
import com.dfsek.terra.bukkit.world.BukkitAdapter;
import com.dfsek.terra.bukkit.world.BukkitTree;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import org.bukkit.TreeType;
public class TerraListener implements EventListener {
@@ -16,9 +18,13 @@ public class TerraListener implements EventListener {
}
@Global
@Priority(Priority.LOWEST)
public void injectTrees(ConfigPackPreLoadEvent event) {
for(TreeType value : TreeType.values()) {
event.getPack().getTreeRegistry().add(BukkitAdapter.TREE_TRANSFORMER.translate(value), new BukkitTree(value, main));
try {
event.getPack().getTreeRegistry().add(BukkitAdapter.TREE_TRANSFORMER.translate(value), new BukkitTree(value, main));
} catch(DuplicateEntryException ignore) { // If another addon has already registered trees, do nothing.
}
}
}
}