Reworked extension versioning and naming

This commit is contained in:
Auxilor 2020-11-27 12:50:45 +00:00
parent fbe1cd7a5f
commit 1e5c76d72b
3 changed files with 81 additions and 26 deletions

View File

@ -1,24 +1,85 @@
package com.willfp.ecoenchants.extensions;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
* Extensions are a way of interfacing with EcoEnchants
* Syntactically similar to Bukkit Plugins.
*/
public abstract class Extension {
/**
* Create new Extension
* Metadata containing version and name
*/
public Extension() {
onEnable();
private ExtensionMetadata metadata = null;
/**
* Method to validate metadata and enable extension
*/
public final void enable() {
Validate.notNull(metadata, "Metadata cannot be null!");
this.onEnable();
}
/**
* Method to disable extension
*/
public final void disable() {
this.onDisable();
}
/**
* Called on enabling Extension
*/
public abstract void onEnable();
protected abstract void onEnable();
/**
* Called when Extension is disabled
*/
public abstract void onDisable();
protected abstract void onDisable();
/**
* Set the metadata of the extension
*
* Must be called before enabling
* @param metadata The metadata to set
*/
public final void setMetadata(ExtensionMetadata metadata) {
this.metadata = metadata;
}
/**
* Get the name of the extension
* @return The name of the metadata attached to the extension
*/
public final String getName() {
Validate.notNull(metadata, "Metadata cannot be null!");
return this.metadata.name;
}
/**
* Get the version of the extension
* @return The version of the metadata attached to the extension
*/
public final String getVersion() {
Validate.notNull(metadata, "Metadata cannot be null!");
return this.metadata.version;
}
/**
* Wrapper for the string and version of the extension
* Contains versions and name
* Designed for internal use
*/
@ApiStatus.Internal
static final class ExtensionMetadata {
private final @NotNull String version;
private final @NotNull String name;
ExtensionMetadata(@NotNull String version, @NotNull String name) {
this.version = version;
this.name = name;
}
}
}

View File

@ -1,6 +1,7 @@
package com.willfp.ecoenchants.extensions;
import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.util.Logger;
import com.willfp.ecoenchants.util.tuplets.Pair;
import org.bukkit.Bukkit;
@ -12,19 +13,14 @@ import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
* Class containing method to load extensions
*/
public class ExtensionManager {
private static final Map<Extension, Pair<String, String>> extensions = new HashMap<>();
private static final Set<Extension> extensions = new HashSet<>();
/**
* Load all extensions
@ -58,14 +54,12 @@ public class ExtensionManager {
} catch (MalformedURLException e) {
e.printStackTrace();
}
URL[] urls = {url};
ClassLoader cl = new URLClassLoader(urls, EcoEnchantsPlugin.class.getClassLoader());
ClassLoader cl = new URLClassLoader(new URL[]{url}, EcoEnchantsPlugin.class.getClassLoader());
InputStream ymlIn = cl.getResourceAsStream("extension.yml");
URL extensionYmlUrl = cl.getResource("extension.yml");
if (extensionYmlUrl == null || ymlIn == null) {
if (ymlIn == null) {
throw new MalformedExtensionException("No extension.yml found in " + extensionJar.getName());
}
@ -81,6 +75,7 @@ public class ExtensionManager {
String mainClass = extensionYml.getString("main");
String name = extensionYml.getString("name");
String version = extensionYml.getString("version");
Extension.ExtensionMetadata metadata = new Extension.ExtensionMetadata(name, version);
Class<?> cls;
Object object = null;
@ -95,17 +90,16 @@ public class ExtensionManager {
throw new MalformedExtensionException(extensionJar.getName() + " is invalid");
Extension extension = (Extension) object;
extension.onEnable();
extensions.put(extension, new Pair<>(name, version));
extension.setMetadata(metadata);
extension.enable();
extensions.add(extension);
}
/**
* Unload all extensions
*/
public static void unloadExtensions() {
extensions.forEach(((extension, s) -> {
extension.onDisable();
}));
extensions.forEach(Extension::onDisable);
extensions.clear();
}
@ -118,10 +112,10 @@ public class ExtensionManager {
}
/**
* Get Map of all loaded extensions and their names
* @return {@link Map} of {@link Extension}s and their names
* Get set of all loaded extensions
* @return {@link Set} of {@link Extension}s
*/
public static Map<Extension, Pair<String, String>> getLoadedExtensions() {
public static Set<Extension> getLoadedExtensions() {
return extensions;
}
}

View File

@ -273,8 +273,8 @@ public class Loader {
Logger.info("&cNo extensions found");
} else {
Logger.info("Extensions Loaded:");
ExtensionManager.getLoadedExtensions().forEach((extension, pair) -> {
Logger.info("- " + pair.getFirst() + " v" + pair.getSecond());
ExtensionManager.getLoadedExtensions().forEach((extension) -> {
Logger.info("- " + extension.getName() + " v" + extension.getVersion());
});
}
Logger.info("");