Reworked EcoEnchant constructor
This commit is contained in:
@@ -2,6 +2,7 @@ package com.willfp.ecoenchants;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.ProtocolManager;
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
|
||||
import com.willfp.ecoenchants.extensions.loader.EcoExtensionLoader;
|
||||
import com.willfp.ecoenchants.extensions.loader.ExtensionLoader;
|
||||
import com.willfp.ecoenchants.util.internal.Loader;
|
||||
@@ -11,7 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
/**
|
||||
* The Main class for EcoEnchants
|
||||
*/
|
||||
public class EcoEnchantsPlugin extends JavaPlugin {
|
||||
public class EcoEnchantsPlugin extends JavaPlugin implements EnchantmentRegisterer {
|
||||
/**
|
||||
* Instance of EcoEnchants
|
||||
*/
|
||||
|
||||
@@ -2,10 +2,17 @@ package com.willfp.ecoenchants.config;
|
||||
|
||||
import com.willfp.ecoenchants.EcoEnchantsPlugin;
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchant;
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
@@ -17,23 +24,23 @@ public abstract class EnchantmentYamlConfig {
|
||||
public YamlConfiguration config;
|
||||
protected File configFile;
|
||||
private final File directory;
|
||||
private final Class<?> source;
|
||||
private final EnchantmentRegisterer registerer;
|
||||
private final EcoEnchant.EnchantmentType type;
|
||||
|
||||
/**
|
||||
* Create new config yml
|
||||
*
|
||||
* @param name The config name
|
||||
* @param plugin The class of the main class of plugin or extension
|
||||
* @param type The enchantment type
|
||||
* @param name The config name
|
||||
* @param registerer The class of the main class of plugin or extension
|
||||
* @param type The enchantment type
|
||||
*/
|
||||
public EnchantmentYamlConfig(String name, Class<?> plugin, EcoEnchant.EnchantmentType type) {
|
||||
public EnchantmentYamlConfig(String name, EnchantmentRegisterer registerer, EcoEnchant.EnchantmentType type) {
|
||||
this.name = name;
|
||||
this.source = plugin;
|
||||
this.registerer = registerer;
|
||||
this.type = type;
|
||||
|
||||
File basedir = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), "enchants/");
|
||||
if(!basedir.exists()) basedir.mkdirs();
|
||||
if (!basedir.exists()) basedir.mkdirs();
|
||||
|
||||
File dir = new File(basedir, type.getName() + "/");
|
||||
if (!dir.exists()) {
|
||||
@@ -58,7 +65,7 @@ public abstract class EnchantmentYamlConfig {
|
||||
private void saveResource() {
|
||||
String resourcePath = "/enchants/" + type.getName() + "/" + name + ".yml";
|
||||
|
||||
InputStream in = source.getResourceAsStream(resourcePath);
|
||||
InputStream in = registerer.getResourceAsStream(resourcePath);
|
||||
|
||||
File outFile = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), resourcePath);
|
||||
int lastIndex = resourcePath.lastIndexOf('/');
|
||||
@@ -79,7 +86,8 @@ public abstract class EnchantmentYamlConfig {
|
||||
out.close();
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException ignored) {}
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private void createFile() {
|
||||
@@ -91,13 +99,13 @@ public abstract class EnchantmentYamlConfig {
|
||||
config.load(configFile);
|
||||
|
||||
String resourcePath = "/enchants/" + type.getName() + "/" + name + ".yml";
|
||||
InputStream newIn = source.getResourceAsStream(resourcePath);
|
||||
InputStream newIn = registerer.getResourceAsStream(resourcePath);
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
|
||||
YamlConfiguration newConfig = new YamlConfiguration();
|
||||
newConfig.load(reader);
|
||||
|
||||
if(newConfig.getKeys(true).equals(config.getKeys(true)))
|
||||
if (newConfig.getKeys(true).equals(config.getKeys(true)))
|
||||
return;
|
||||
|
||||
newConfig.getKeys(true).forEach((s -> {
|
||||
@@ -107,7 +115,7 @@ public abstract class EnchantmentYamlConfig {
|
||||
}));
|
||||
|
||||
config.getKeys(true).forEach((s -> {
|
||||
if(!newConfig.getKeys(true).contains(s)) {
|
||||
if (!newConfig.getKeys(true).contains(s)) {
|
||||
config.set(s, null);
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
|
||||
import com.willfp.ecoenchants.enchantments.EcoEnchants;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
|
||||
import com.willfp.ecoenchants.util.internal.Logger;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
@@ -22,8 +23,8 @@ import java.util.Set;
|
||||
public class EnchantmentConfig extends EnchantmentYamlConfig {
|
||||
private final String name;
|
||||
|
||||
public EnchantmentConfig(String name, Class<?> plugin, EcoEnchant.EnchantmentType type) {
|
||||
super(name, plugin, type);
|
||||
public EnchantmentConfig(String name, EnchantmentRegisterer registerer, EcoEnchant.EnchantmentType type) {
|
||||
super(name, registerer, type);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin;
|
||||
import com.willfp.ecoenchants.config.ConfigManager;
|
||||
import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
|
||||
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
|
||||
import com.willfp.ecoenchants.enchantments.util.Watcher;
|
||||
import com.willfp.ecoenchants.util.StringUtils;
|
||||
@@ -57,7 +58,6 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new EcoEnchant that exists within the base plugin
|
||||
*
|
||||
@@ -67,7 +67,7 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
|
||||
*/
|
||||
@ApiStatus.Internal
|
||||
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Prerequisite... prerequisites) {
|
||||
this(key, type, EcoEnchantsPlugin.class, prerequisites);
|
||||
this(key, type, EcoEnchantsPlugin.getInstance(), prerequisites);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,15 +75,15 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
|
||||
*
|
||||
* @param key The key name of the enchantment
|
||||
* @param type The type of the enchantment
|
||||
* @param plugin The Main class of the {@link org.bukkit.plugin.Plugin} or {@link com.willfp.ecoenchants.extensions.Extension} that the enchantment was created by
|
||||
* @param registerer The Main class of the {@link org.bukkit.plugin.Plugin} or {@link com.willfp.ecoenchants.extensions.Extension} that the enchantment was created by
|
||||
* @param prerequisites Optional {@link Prerequisite}s that must be met
|
||||
*/
|
||||
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Class<?> plugin, Prerequisite... prerequisites) {
|
||||
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, EnchantmentRegisterer registerer, Prerequisite... prerequisites) {
|
||||
super(NamespacedKey.minecraft(key));
|
||||
|
||||
this.type = type;
|
||||
this.permissionName = key.replaceAll("_", "");
|
||||
ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, plugin, this.type));
|
||||
ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, registerer, this.type));
|
||||
this.config = ConfigManager.getEnchantmentConfig(this.permissionName);
|
||||
|
||||
if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable." + permissionName) == null) {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.willfp.ecoenchants.enchantments.util;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Exists to simplify enchantment registration
|
||||
*
|
||||
* Prevents requiring direct class referencing on the front-end of enchantment creation
|
||||
*
|
||||
* @see com.willfp.ecoenchants.extensions.Extension
|
||||
*/
|
||||
public interface EnchantmentRegisterer {
|
||||
default InputStream getResourceAsStream(String location) {
|
||||
return this.getClass().getResourceAsStream(location);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.willfp.ecoenchants.extensions;
|
||||
|
||||
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -8,17 +9,23 @@ import org.jetbrains.annotations.NotNull;
|
||||
* Extensions are a way of interfacing with EcoEnchants
|
||||
* Syntactically similar to Bukkit Plugins.
|
||||
*/
|
||||
public abstract class Extension {
|
||||
public abstract class Extension implements EnchantmentRegisterer {
|
||||
/**
|
||||
* Metadata containing version and name
|
||||
*/
|
||||
private ExtensionMetadata metadata = null;
|
||||
|
||||
/**
|
||||
* Instance of the extension
|
||||
*/
|
||||
private Extension instance;
|
||||
|
||||
/**
|
||||
* Method to validate metadata and enable extension
|
||||
*/
|
||||
public final void enable() {
|
||||
Validate.notNull(metadata, "Metadata cannot be null!");
|
||||
instance = this;
|
||||
this.onEnable();
|
||||
}
|
||||
|
||||
@@ -39,6 +46,14 @@ public abstract class Extension {
|
||||
*/
|
||||
protected abstract void onDisable();
|
||||
|
||||
/**
|
||||
* Get instance of the extension
|
||||
* @return The instance
|
||||
*/
|
||||
public final Extension getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the metadata of the extension
|
||||
* <p>
|
||||
|
||||
Reference in New Issue
Block a user