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
@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* Specifies that the annotated class is an entry point for a Terra addon.
*/
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Addon { public @interface Addon {
/**
* @return The ID of the addon.
*/
String value(); String value();
} }
@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* Optional annotation that specifies the author of an addon.
*/
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
public @interface Author { public @interface Author {
/**
* @return Name of the addon author.
*/
String value(); String value();
} }
@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* Optional annotation that specifies dependencies of an addon.
*/
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Depends { public @interface Depends {
/**
* @return All addons this addon is dependent upon.
*/
String[] value(); String[] value();
} }
@@ -5,8 +5,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* Optional annotation that specifies the version of an addon.
*/
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Version { public @interface Version {
/**
* @return Version of the addon.
*/
String value(); String value();
} }
@@ -5,6 +5,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/**
* Specifies that a field is a target for dependency injection.
*/
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface Inject { public @interface Inject {
@@ -12,11 +12,11 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface Priority { 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; 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; int LOWEST = Integer.MIN_VALUE;
/** /**
@@ -30,7 +30,6 @@ public class CheckedRegistry<T> implements TypeLoader<T> {
* @throws DuplicateEntryException If an entry with the same identifier is already present. * @throws DuplicateEntryException If an entry with the same identifier is already present.
*/ */
public void add(String identifier, T value) throws DuplicateEntryException { 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); registry.addChecked(identifier, value);
} }
@@ -3,6 +3,7 @@ package com.dfsek.terra.registry;
import com.dfsek.tectonic.exception.LoadException; import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader; import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.loading.TypeLoader; import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.HashMap; 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 identifier Identifier to assign value.
* @param value Object to increment * @param value Value to add.
* @return True if tree was overwritten.
*/ */
public boolean add(String name, T value) { public boolean add(String identifier, T value) {
boolean exists = objects.containsKey(name); boolean exists = objects.containsKey(identifier);
objects.put(name, value); objects.put(identifier, value);
return exists; return exists;
} }
public void addChecked(String name, T value) { /**
if(objects.containsKey(name)) throw new IllegalArgumentException("Value is already defined in registry."); * Add a value to this registry, checking whether it is present first.
add(name, value); *
* @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. * @param identifier Identifier of value.
* @return Whether the registry contains the object. * @return Whether the registry contains the value.
*/ */
public boolean contains(String name) { public boolean contains(String identifier) {
return objects.containsKey(name); return objects.containsKey(identifier);
} }
/** /**
* Get an object from the registry, * Get a value from the registry.
* *
* @param id ID of object to get * @param identifier Identifier of value.
* @return Object * @return Value matching the identifier, {@code null} if no value is present.
*/ */
public T get(String id) { public T get(String identifier) {
return objects.get(id); return objects.get(identifier);
} }
public void forEach(Consumer<T> consumer) { public void forEach(Consumer<T> consumer) {
@@ -70,7 +70,7 @@ public class FloraRegistry extends TerraRegistry<Flora> {
@Override @Override
public Flora get(String id) { public Flora get(String identifier) {
return super.get(id); return super.get(identifier);
} }
} }
@@ -14,9 +14,9 @@ public class PaletteRegistry extends TerraRegistry<Palette<BlockData>> {
@Override @Override
public Palette<BlockData> get(String id) { public Palette<BlockData> get(String identifier) {
if(id.startsWith("BLOCK:")) if(identifier.startsWith("BLOCK:"))
return new SinglePalette<>(main.getWorldHandle().createBlockData(id.substring(6))); // Return single palette for BLOCK: shortcut. return new SinglePalette<>(main.getWorldHandle().createBlockData(identifier.substring(6))); // Return single palette for BLOCK: shortcut.
return super.get(id); return super.get(identifier);
} }
} }
@@ -45,8 +45,8 @@ public class TreeRegistry extends TerraRegistry<Tree> {
} }
@Override @Override
public boolean add(String name, Tree value) { public boolean add(String identifier, Tree value) {
return super.add(name, value); return super.add(identifier, value);
} }
private final class FractalTreeHolder implements Tree { private final class FractalTreeHolder implements Tree {
@@ -1,5 +1,8 @@
package com.dfsek.terra.registry.exception; package com.dfsek.terra.registry.exception;
/**
* Thrown when a duplicate entry is found in a registry.
*/
public class DuplicateEntryException extends Exception { public class DuplicateEntryException extends Exception {
private static final long serialVersionUID = -7199021672428288780L; private static final long serialVersionUID = -7199021672428288780L;
@@ -9,6 +9,7 @@ import com.dfsek.terra.addons.loading.pre.AddonPool;
import com.dfsek.terra.addons.loading.pre.PreLoadAddon; import com.dfsek.terra.addons.loading.pre.PreLoadAddon;
import com.dfsek.terra.api.core.TerraPlugin; import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.registry.TerraRegistry; import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -31,11 +32,11 @@ public class AddonRegistry extends TerraRegistry<TerraAddon> {
} }
@Override @Override
public boolean add(String name, TerraAddon addon) { public boolean add(String identifier, TerraAddon addon) {
if(contains(name)) throw new IllegalArgumentException("Addon " + name + " is already registered."); if(contains(identifier)) throw new IllegalArgumentException("Addon " + identifier + " is already registered.");
addon.initialize(); addon.initialize();
main.getLogger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor()); main.getLogger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor());
return super.add(name, addon); return super.add(identifier, addon);
} }
@Override @Override
@@ -92,7 +93,7 @@ public class AddonRegistry extends TerraRegistry<TerraAddon> {
} }
try { try {
addChecked(loadedAddon.getName(), loadedAddon); addChecked(loadedAddon.getName(), loadedAddon);
} catch(IllegalArgumentException e) { } catch(DuplicateEntryException e) {
valid = false; valid = false;
main.getLogger().severe("Duplicate addon ID; addon with ID " + loadedAddon.getName() + " is already loaded."); 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()); main.getLogger().severe("Existing addon class: " + get(loadedAddon.getName()).getClass().getCanonicalName());
@@ -3,9 +3,11 @@ package com.dfsek.terra.bukkit.listeners;
import com.dfsek.terra.api.core.TerraPlugin; import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.core.event.EventListener; 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.Global;
import com.dfsek.terra.api.core.event.annotations.Priority;
import com.dfsek.terra.api.core.event.events.config.ConfigPackPreLoadEvent; import com.dfsek.terra.api.core.event.events.config.ConfigPackPreLoadEvent;
import com.dfsek.terra.bukkit.world.BukkitAdapter; import com.dfsek.terra.bukkit.world.BukkitAdapter;
import com.dfsek.terra.bukkit.world.BukkitTree; import com.dfsek.terra.bukkit.world.BukkitTree;
import com.dfsek.terra.registry.exception.DuplicateEntryException;
import org.bukkit.TreeType; import org.bukkit.TreeType;
public class TerraListener implements EventListener { public class TerraListener implements EventListener {
@@ -16,9 +18,13 @@ public class TerraListener implements EventListener {
} }
@Global @Global
@Priority(Priority.LOWEST)
public void injectTrees(ConfigPackPreLoadEvent event) { public void injectTrees(ConfigPackPreLoadEvent event) {
for(TreeType value : TreeType.values()) { 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.
}
} }
} }
} }