Reworked EcoEnchant constructor

This commit is contained in:
Auxilor
2020-12-11 18:47:30 +00:00
parent f28b1be4da
commit e3e15ace8a
6 changed files with 63 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ package com.willfp.ecoenchants;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager; 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.EcoExtensionLoader;
import com.willfp.ecoenchants.extensions.loader.ExtensionLoader; import com.willfp.ecoenchants.extensions.loader.ExtensionLoader;
import com.willfp.ecoenchants.util.internal.Loader; import com.willfp.ecoenchants.util.internal.Loader;
@@ -11,7 +12,7 @@ import org.bukkit.plugin.java.JavaPlugin;
/** /**
* The Main class for EcoEnchants * The Main class for EcoEnchants
*/ */
public class EcoEnchantsPlugin extends JavaPlugin { public class EcoEnchantsPlugin extends JavaPlugin implements EnchantmentRegisterer {
/** /**
* Instance of EcoEnchants * Instance of EcoEnchants
*/ */

View File

@@ -2,10 +2,17 @@ package com.willfp.ecoenchants.config;
import com.willfp.ecoenchants.EcoEnchantsPlugin; import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; 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; import java.nio.charset.StandardCharsets;
/** /**
@@ -17,23 +24,23 @@ public abstract class EnchantmentYamlConfig {
public YamlConfiguration config; public YamlConfiguration config;
protected File configFile; protected File configFile;
private final File directory; private final File directory;
private final Class<?> source; private final EnchantmentRegisterer registerer;
private final EcoEnchant.EnchantmentType type; private final EcoEnchant.EnchantmentType type;
/** /**
* Create new config yml * Create new config yml
* *
* @param name The config name * @param name The config name
* @param plugin The class of the main class of plugin or extension * @param registerer The class of the main class of plugin or extension
* @param type The enchantment type * @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.name = name;
this.source = plugin; this.registerer = registerer;
this.type = type; this.type = type;
File basedir = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), "enchants/"); 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() + "/"); File dir = new File(basedir, type.getName() + "/");
if (!dir.exists()) { if (!dir.exists()) {
@@ -58,7 +65,7 @@ public abstract class EnchantmentYamlConfig {
private void saveResource() { private void saveResource() {
String resourcePath = "/enchants/" + type.getName() + "/" + name + ".yml"; 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); File outFile = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), resourcePath);
int lastIndex = resourcePath.lastIndexOf('/'); int lastIndex = resourcePath.lastIndexOf('/');
@@ -79,7 +86,8 @@ public abstract class EnchantmentYamlConfig {
out.close(); out.close();
in.close(); in.close();
} }
} catch (IOException ignored) {} } catch (IOException ignored) {
}
} }
private void createFile() { private void createFile() {
@@ -91,13 +99,13 @@ public abstract class EnchantmentYamlConfig {
config.load(configFile); config.load(configFile);
String resourcePath = "/enchants/" + type.getName() + "/" + name + ".yml"; 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)); BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
YamlConfiguration newConfig = new YamlConfiguration(); YamlConfiguration newConfig = new YamlConfiguration();
newConfig.load(reader); newConfig.load(reader);
if(newConfig.getKeys(true).equals(config.getKeys(true))) if (newConfig.getKeys(true).equals(config.getKeys(true)))
return; return;
newConfig.getKeys(true).forEach((s -> { newConfig.getKeys(true).forEach((s -> {
@@ -107,7 +115,7 @@ public abstract class EnchantmentYamlConfig {
})); }));
config.getKeys(true).forEach((s -> { config.getKeys(true).forEach((s -> {
if(!newConfig.getKeys(true).contains(s)) { if (!newConfig.getKeys(true).contains(s)) {
config.set(s, null); config.set(s, null);
} }
})); }));

View File

@@ -6,6 +6,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget; import com.willfp.ecoenchants.enchantments.meta.EnchantmentTarget;
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
import com.willfp.ecoenchants.util.internal.Logger; import com.willfp.ecoenchants.util.internal.Logger;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@@ -22,8 +23,8 @@ import java.util.Set;
public class EnchantmentConfig extends EnchantmentYamlConfig { public class EnchantmentConfig extends EnchantmentYamlConfig {
private final String name; private final String name;
public EnchantmentConfig(String name, Class<?> plugin, EcoEnchant.EnchantmentType type) { public EnchantmentConfig(String name, EnchantmentRegisterer registerer, EcoEnchant.EnchantmentType type) {
super(name, plugin, type); super(name, registerer, type);
this.name = name; this.name = name;
} }

View File

@@ -4,6 +4,7 @@ import com.willfp.ecoenchants.EcoEnchantsPlugin;
import com.willfp.ecoenchants.config.ConfigManager; import com.willfp.ecoenchants.config.ConfigManager;
import com.willfp.ecoenchants.config.configs.EnchantmentConfig; import com.willfp.ecoenchants.config.configs.EnchantmentConfig;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity; 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.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.Watcher; import com.willfp.ecoenchants.enchantments.util.Watcher;
import com.willfp.ecoenchants.util.StringUtils; import com.willfp.ecoenchants.util.StringUtils;
@@ -57,7 +58,6 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
private boolean enabled; private boolean enabled;
/** /**
* Create a new EcoEnchant that exists within the base plugin * 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 @ApiStatus.Internal
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Prerequisite... prerequisites) { 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 key The key name of the enchantment
* @param type The type 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 * @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)); super(NamespacedKey.minecraft(key));
this.type = type; this.type = type;
this.permissionName = key.replaceAll("_", ""); 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); this.config = ConfigManager.getEnchantmentConfig(this.permissionName);
if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable." + permissionName) == null) { if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable." + permissionName) == null) {

View File

@@ -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);
}
}

View File

@@ -1,5 +1,6 @@
package com.willfp.ecoenchants.extensions; package com.willfp.ecoenchants.extensions;
import com.willfp.ecoenchants.enchantments.util.EnchantmentRegisterer;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -8,17 +9,23 @@ import org.jetbrains.annotations.NotNull;
* Extensions are a way of interfacing with EcoEnchants * Extensions are a way of interfacing with EcoEnchants
* Syntactically similar to Bukkit Plugins. * Syntactically similar to Bukkit Plugins.
*/ */
public abstract class Extension { public abstract class Extension implements EnchantmentRegisterer {
/** /**
* Metadata containing version and name * Metadata containing version and name
*/ */
private ExtensionMetadata metadata = null; private ExtensionMetadata metadata = null;
/**
* Instance of the extension
*/
private Extension instance;
/** /**
* Method to validate metadata and enable extension * Method to validate metadata and enable extension
*/ */
public final void enable() { public final void enable() {
Validate.notNull(metadata, "Metadata cannot be null!"); Validate.notNull(metadata, "Metadata cannot be null!");
instance = this;
this.onEnable(); this.onEnable();
} }
@@ -39,6 +46,14 @@ public abstract class Extension {
*/ */
protected abstract void onDisable(); protected abstract void onDisable();
/**
* Get instance of the extension
* @return The instance
*/
public final Extension getInstance() {
return instance;
}
/** /**
* Set the metadata of the extension * Set the metadata of the extension
* <p> * <p>