Revert "Reworked EcoEnchant constructor"

This reverts commit e3e15ace
This commit is contained in:
Auxilor 2020-12-11 19:48:14 +00:00
parent 05bd8f31be
commit dbfa253fce
6 changed files with 22 additions and 144 deletions

View File

@ -2,7 +2,6 @@ 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;
@ -12,7 +11,7 @@ import org.bukkit.plugin.java.JavaPlugin;
/** /**
* The Main class for EcoEnchants * The Main class for EcoEnchants
*/ */
public class EcoEnchantsPlugin extends JavaPlugin implements EnchantmentRegisterer { public class EcoEnchantsPlugin extends JavaPlugin {
/** /**
* Instance of EcoEnchants * Instance of EcoEnchants
*/ */

View File

@ -2,18 +2,10 @@ 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 com.willfp.ecoenchants.util.internal.Logger;
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.BufferedReader; import java.io.*;
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;
/** /**
@ -25,50 +17,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 EnchantmentRegisterer registerer; private final Class<?> source;
private final EcoEnchant.EnchantmentType type; private final EcoEnchant.EnchantmentType type;
private final boolean legacy;
private Class<?> legacyRegisterer;
/** /**
* Create new config yml * Create new config yml
* *
* @param name The config name * @param name The config name
* @param registerer The class of the main class of plugin or extension * @param plugin The class of the main class of plugin or extension
* @param type The enchantment type * @param type The enchantment type
*/ */
public EnchantmentYamlConfig(String name, EnchantmentRegisterer registerer, EcoEnchant.EnchantmentType type) { public EnchantmentYamlConfig(String name, Class<?> plugin, EcoEnchant.EnchantmentType type) {
this.name = name; this.name = name;
this.registerer = registerer; this.source = plugin;
this.type = type; this.type = type;
legacy = false;
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() + "/");
if (!dir.exists()) {
dir.mkdirs();
}
this.directory = dir;
init();
}
@Deprecated
public EnchantmentYamlConfig(String name, Class<?> registerer, EcoEnchant.EnchantmentType type) {
this.name = name;
this.legacyRegisterer = registerer;
this.registerer = null;
this.type = type;
legacy = true;
Logger.warn("Config " + name + " was created in legacy mode!");
Logger.warn("Update your extensions");
File basedir = new File(EcoEnchantsPlugin.getInstance().getDataFolder(), "enchants/");
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()) {
@ -93,12 +58,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; InputStream in = source.getResourceAsStream(resourcePath);
if(legacy) {
in = legacyRegisterer.getResourceAsStream(resourcePath);
} else {
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('/');
@ -119,8 +79,7 @@ public abstract class EnchantmentYamlConfig {
out.close(); out.close();
in.close(); in.close();
} }
} catch (IOException ignored) { } catch (IOException ignored) {}
}
} }
private void createFile() { private void createFile() {
@ -132,20 +91,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;
if(legacy) {
newIn = legacyRegisterer.getResourceAsStream(resourcePath);
} else {
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 -> {
@ -155,7 +107,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,7 +6,6 @@ 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;
@ -23,8 +22,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, EnchantmentRegisterer registerer, EcoEnchant.EnchantmentType type) { public EnchantmentConfig(String name, Class<?> plugin, EcoEnchant.EnchantmentType type) {
super(name, registerer, type); super(name, plugin, type);
this.name = name; this.name = name;
} }

View File

@ -4,13 +4,11 @@ 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;
import com.willfp.ecoenchants.util.interfaces.ObjectCallable; import com.willfp.ecoenchants.util.interfaces.ObjectCallable;
import com.willfp.ecoenchants.util.interfaces.Registerable; import com.willfp.ecoenchants.util.interfaces.Registerable;
import com.willfp.ecoenchants.util.internal.Logger;
import com.willfp.ecoenchants.util.optional.Prerequisite; import com.willfp.ecoenchants.util.optional.Prerequisite;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -59,6 +57,7 @@ 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
* *
@ -68,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.getInstance(), prerequisites); this(key, type, EcoEnchantsPlugin.class, prerequisites);
} }
/** /**
@ -76,55 +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 registererClass The Main class of the {@link org.bukkit.plugin.Plugin} or {@link com.willfp.ecoenchants.extensions.Extension} that the enchantment was created by * @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 prerequisites Optional {@link Prerequisite}s that must be met
*
* @deprecated Class-based enchantment registration was deprecated in 5.6.0. Base your extensions off of {@link EnchantmentRegisterer}
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "5.7.0")
protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Class<?> registererClass, Prerequisite... prerequisites) {
super(NamespacedKey.minecraft(key));
Logger.warn("Enchantment " + key + " was created in legacy mode!");
Logger.warn("Update your extensions");
this.type = type;
this.permissionName = key.replaceAll("_", "");
ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, registererClass, this.type));
this.config = ConfigManager.getEnchantmentConfig(this.permissionName);
if (Bukkit.getPluginManager().getPermission("ecoenchants.fromtable." + permissionName) == null) {
Permission permission = new Permission(
"ecoenchants.fromtable." + permissionName,
"Allows getting " + permissionName + " from an Enchanting Table",
PermissionDefault.TRUE
);
permission.addParent(Objects.requireNonNull(Bukkit.getPluginManager().getPermission("ecoenchants.fromtable.*")), true);
Bukkit.getPluginManager().addPermission(permission);
}
if (!Prerequisite.areMet(prerequisites))
return;
this.update();
EcoEnchants.addNewEcoEnchant(this);
}
/**
* Create a new EcoEnchant that exists within an extension or external plugin
*
* @param key The key name of the enchantment
* @param type The type of the enchantment
* @param registerer Instance of the main class of the {@link com.willfp.ecoenchants.extensions.Extension} or {@link EnchantmentRegisterer} 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, EnchantmentRegisterer registerer, Prerequisite... prerequisites) { protected EcoEnchant(String key, EcoEnchant.EnchantmentType type, Class<?> plugin, 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, registerer, this.type)); ConfigManager.addEnchantmentConfig(new EnchantmentConfig(this.permissionName, plugin, 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

@ -1,16 +0,0 @@
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,6 +1,5 @@
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;
@ -9,23 +8,17 @@ 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 implements EnchantmentRegisterer { public abstract class Extension {
/** /**
* Metadata containing version and name * Metadata containing version and name
*/ */
private ExtensionMetadata metadata = null; private ExtensionMetadata metadata = null;
/**
* Instance of the extension
*/
private static 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();
} }
@ -46,14 +39,6 @@ public abstract class Extension implements EnchantmentRegisterer {
*/ */
protected abstract void onDisable(); protected abstract void onDisable();
/**
* Get instance of the extension
* @return The instance
*/
public static Extension getInstance() {
return instance;
}
/** /**
* Set the metadata of the extension * Set the metadata of the extension
* <p> * <p>