basic addon loading

This commit is contained in:
dfsek
2021-02-19 20:36:35 -07:00
parent 6d51da3118
commit 5e761c3e29
124 changed files with 483 additions and 220 deletions

View File

@@ -0,0 +1,56 @@
package com.dfsek.terra.registry;
import com.dfsek.terra.addons.addon.TerraAddon;
import com.dfsek.terra.addons.loading.AddonClassLoader;
import com.dfsek.terra.addons.loading.AddonLoadException;
import com.dfsek.terra.api.core.TerraPlugin;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
public class AddonRegistry extends TerraRegistry<TerraAddon> {
public boolean loadAll(TerraPlugin main) {
boolean valid = true;
File addonsFolder = new File(main.getDataFolder(), "addons");
addonsFolder.mkdirs();
for(File jar : addonsFolder.listFiles(file -> file.getName().endsWith(".jar"))) {
try {
main.getLogger().info("Loading Addon(s) from: " + jar.getName());
load(jar, main);
} catch(IOException | AddonLoadException e) {
e.printStackTrace();
valid = false;
}
}
return valid;
}
public void load(File file, TerraPlugin main) throws AddonLoadException, IOException {
Set<Class<? extends TerraAddon>> addonClasses = AddonClassLoader.fetchAddonClasses(file);
for(Class<? extends TerraAddon> addonClass : addonClasses) {
Constructor<? extends TerraAddon> constructor;
try {
constructor = addonClass.getConstructor();
} catch(NoSuchMethodException e) {
throw new AddonLoadException("Addon class has no valid constructor: " + addonClass.getCanonicalName(), e);
}
TerraAddon addon;
try {
addon = constructor.newInstance();
} catch(InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new AddonLoadException("Failed to instantiate addon: " + addonClass.getCanonicalName(), e);
}
try {
addChecked(addon.getName(), addon);
} catch(IllegalArgumentException e) {
throw new AddonLoadException("Duplicate addon ID; addon with ID " + addon.getName() + " is already loaded.");
}
main.getLogger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor());
addon.initialize();
}
}
}

View File

@@ -29,7 +29,7 @@ public class ConfigRegistry extends TerraRegistry<ConfigPack> {
valid = false;
}
}
for(File zip : packsFolder.listFiles(file -> file.getName().endsWith(".zip") || file.getName().endsWith(".jar") || file.getName().endsWith(".com.dfsek.terra"))) {
for(File zip : packsFolder.listFiles(file -> file.getName().endsWith(".zip") || file.getName().endsWith(".terra"))) {
try {
main.getDebugLogger().info("Loading ZIP archive: " + zip.getName());
load(new ZipFile(zip), main);

View File

@@ -36,6 +36,11 @@ public abstract class TerraRegistry<T> implements TypeLoader<T> {
return exists;
}
public void addChecked(String name, T value) {
if(objects.containsKey(name)) throw new IllegalArgumentException("Value is already defined in registry.");
objects.put(name, value);
}
/**
* Check if the registry contains an object.
*

View File

@@ -2,7 +2,7 @@ package com.dfsek.terra.registry.config;
import com.dfsek.tectonic.exception.LoadException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.biome.TerraBiome;
import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.registry.TerraRegistry;
import java.lang.reflect.Type;

View File

@@ -3,9 +3,9 @@ package com.dfsek.terra.registry.config;
import com.dfsek.terra.api.core.TerraPlugin;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.MaterialData;
import com.dfsek.terra.api.util.world.MaterialSet;
import com.dfsek.terra.api.world.flora.Flora;
import com.dfsek.terra.registry.TerraRegistry;
import com.dfsek.terra.util.MaterialSet;
import com.dfsek.terra.world.population.items.flora.ConstantFlora;
import java.util.Arrays;