Refactoring and removed serialization system

This commit is contained in:
Auxilor
2021-04-03 14:19:11 +01:00
parent f96716966d
commit 2db011ae10
13 changed files with 174 additions and 303 deletions

View File

@@ -1,26 +1,28 @@
package com.willfp.eco.util.bukkit.scheduling;
package com.willfp.eco.internal.bukkit.events;
import com.willfp.eco.util.bukkit.scheduling.RunnableFactory;
import com.willfp.eco.util.bukkit.scheduling.RunnableTask;
import com.willfp.eco.util.plugin.EcoPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
public abstract class EcoBukkitRunnable extends BukkitRunnable {
public abstract class EcoRunnableTask extends BukkitRunnable implements RunnableTask {
/**
* The linked {@link EcoPlugin} to associate runnables with.
*/
private final EcoPlugin plugin;
/**
* Creates a new {@link EcoBukkitRunnable}.
* Creates a new {@link EcoRunnableTask}.
* <p>
* Cannot be instantiated normally, use {@link RunnableFactory}.
*
* @param plugin The {@link EcoPlugin} to associate runnables with.
*/
@ApiStatus.Internal
EcoBukkitRunnable(@NotNull final EcoPlugin plugin) {
public EcoRunnableTask(@NotNull final EcoPlugin plugin) {
this.plugin = plugin;
}
@@ -33,67 +35,37 @@ public abstract class EcoBukkitRunnable extends BukkitRunnable {
return this.plugin;
}
/**
* Run the task.
*
* @return The created {@link BukkitTask}.
*/
@Override
@NotNull
public final synchronized BukkitTask runTask() {
return super.runTask(plugin);
}
/**
* Run the task asynchronously.
*
* @return The created {@link BukkitTask}
*/
@Override
@NotNull
public final synchronized BukkitTask runTaskAsynchronously() {
return super.runTaskAsynchronously(plugin);
}
/**
* Run the task after a specified number of ticks.
*
* @param delay The number of ticks to wait.
* @return The created {@link BukkitTask}
*/
@Override
@NotNull
public final synchronized BukkitTask runTaskLater(final long delay) {
return super.runTaskLater(plugin, delay);
}
/**
* Run the task asynchronously after a specified number of ticks.
*
* @param delay The number of ticks to wait.
* @return The created {@link BukkitTask}
*/
@Override
@NotNull
public final synchronized BukkitTask runTaskLaterAsynchronously(final long delay) {
return super.runTaskLaterAsynchronously(plugin, delay);
}
/**
* Run the task repeatedly on a timer.
*
* @param delay The delay before the task is first ran (in ticks).
* @param period The ticks elapsed before the task is ran again.
* @return The created {@link BukkitTask}
*/
@Override
@NotNull
public final synchronized BukkitTask runTaskTimer(final long delay, final long period) {
return super.runTaskTimer(plugin, delay, period);
}
/**
* Run the task repeatedly on a timer asynchronously.
*
* @param delay The delay before the task is first ran (in ticks).
* @param period The ticks elapsed before the task is ran again.
* @return The created {@link BukkitTask}
*/
@Override
@NotNull
public final synchronized BukkitTask runTaskTimerAsynchronously(final long delay, final long period) {
return super.runTaskTimerAsynchronously(plugin, delay, period);

View File

@@ -1,11 +1,10 @@
package com.willfp.eco.internal.config;
import com.willfp.eco.util.SerializationUtils;
import com.willfp.eco.util.StringUtils;
import com.willfp.eco.util.config.Config;
import com.willfp.eco.util.serialization.EcoSerializable;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import org.jetbrains.annotations.NotNull;
@@ -17,7 +16,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
@SuppressWarnings({"unchecked", "unused", "DeprecatedIsStillUsed"})
@SuppressWarnings({"unchecked", "unused"})
public abstract class ConfigWrapper<T extends ConfigurationSection> implements Config {
/**
* The linked {@link MemorySection} where values are physically stored.
@@ -58,89 +57,29 @@ public abstract class ConfigWrapper<T extends ConfigurationSection> implements C
return new ArrayList<>(config.getKeys(deep));
}
@Override
public void set(@NotNull final String path,
@NotNull final EcoSerializable<?> object) {
Config serializedConfig = object.serialize();
for (String key : serializedConfig.getKeys(true)) {
Object raw = serializedConfig.getRaw(key);
config.set(path + "." + key, raw);
cache.put(path + "." + key, raw);
}
}
@Override
@Nullable
public Object getRaw(@NotNull final String path) {
return config.get(path);
}
@Override
@NotNull
public <T extends EcoSerializable<T>> T get(@NotNull final String path,
@NotNull final Class<T> clazz) {
T object = getOrNull(path, clazz);
if (object == null) {
throw new NullPointerException("Object cannot be null!");
} else {
return object;
}
}
@Override
@Nullable
public <T extends EcoSerializable<T>> T getOrNull(@NotNull final String path,
@NotNull final Class<T> clazz) {
if (cache.containsKey(path)) {
return (T) cache.get(path);
} else {
Config section = getSubsectionOrNull(path);
if (section == null) {
return null;
}
cache.put(path, SerializationUtils.deserialize(section, clazz));
return getOrNull(path, clazz);
}
}
@Override
@NotNull
@Deprecated
public ConfigurationSection getSection(@NotNull final String path) {
ConfigurationSection section = getSectionOrNull(path);
if (section == null) {
throw new NullPointerException("Section cannot be null!");
} else {
return section;
}
}
@Override
@Nullable
@Deprecated
public ConfigurationSection getSectionOrNull(@NotNull final String path) {
if (cache.containsKey(path)) {
return (ConfigurationSection) cache.get(path);
} else {
cache.put(path, config.getConfigurationSection(path));
return getSectionOrNull(path);
}
}
@Override
@NotNull
public Config getSubsection(@NotNull final String path) {
return new ConfigSection(this.getSection(path));
Config subsection = getSubsectionOrNull(path);
Validate.notNull(subsection);
return subsection;
}
@Override
@Nullable
public Config getSubsectionOrNull(@NotNull final String path) {
ConfigurationSection section = this.getSectionOrNull(path);
if (section == null) {
return null;
if (cache.containsKey(path)) {
return (Config) cache.get(path);
} else {
cache.put(path, new ConfigSection(Objects.requireNonNull(config.getConfigurationSection(path))));
return getSubsectionOrNull(path);
}
return new ConfigSection(section);
}
@Override

View File

@@ -1,49 +0,0 @@
package com.willfp.eco.util;
import com.willfp.eco.util.config.Config;
import com.willfp.eco.util.serialization.Deserializer;
import com.willfp.eco.util.serialization.EcoSerializable;
import com.willfp.eco.util.serialization.NoRegisteredDeserializerException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
@UtilityClass
public class SerializationUtils {
/**
* All registered deserializers.
*/
private static final Map<String, Deserializer<?>> REGISTRY = new HashMap<>();
/**
* Register deserializer.
*
* @param clazz The serializable class.
* @param deserializer The deserializer.
* @param <T> The object type.
*/
public <T extends EcoSerializable<T>> void registerDeserializer(@NotNull final Class<T> clazz,
@NotNull final Deserializer<T> deserializer) {
REGISTRY.put(clazz.getName(), deserializer);
}
/**
* Deserialize object.
*
* @param config The config.
* @param clazz The class.
* @param <T> The object type.
*/
@SneakyThrows
public <T extends EcoSerializable<T>> T deserialize(@NotNull final Config config,
@NotNull final Class<T> clazz) {
if (!REGISTRY.containsKey(clazz.getName())) {
throw new NoRegisteredDeserializerException("No deserializer registered for " + clazz.getName());
}
return clazz.cast(REGISTRY.get(clazz.getName()).deserialize(config));
}
}

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.util.bukkit.scheduling;
import com.willfp.eco.internal.bukkit.events.EcoRunnableTask;
import com.willfp.eco.util.internal.PluginDependent;
import com.willfp.eco.util.plugin.EcoPlugin;
import org.jetbrains.annotations.NotNull;
@@ -8,7 +9,7 @@ import java.util.function.Consumer;
public class RunnableFactory extends PluginDependent {
/**
* Factory class to produce {@link EcoBukkitRunnable}s associated with an {@link EcoPlugin}.
* Factory class to produce {@link RunnableTask}s associated with an {@link EcoPlugin}.
*
* @param plugin The plugin that this factory creates runnables for.
*/
@@ -17,13 +18,13 @@ public class RunnableFactory extends PluginDependent {
}
/**
* Create an {@link EcoBukkitRunnable}.
* Create a {@link RunnableTask}.
*
* @param consumer Lambda of the code to run, where the parameter represents the instance of the runnable.
* @return The created {@link EcoBukkitRunnable}.
* @return The created {@link RunnableTask}.
*/
public EcoBukkitRunnable create(@NotNull final Consumer<EcoBukkitRunnable> consumer) {
return new EcoBukkitRunnable(this.getPlugin()) {
public RunnableTask create(@NotNull final Consumer<RunnableTask> consumer) {
return new EcoRunnableTask(this.getPlugin()) {
@Override
public void run() {
consumer.accept(this);

View File

@@ -0,0 +1,54 @@
package com.willfp.eco.util.bukkit.scheduling;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
public interface RunnableTask extends Runnable {
/**
* Run the task.
*
* @return The created {@link BukkitTask}.
*/
@NotNull BukkitTask runTask();
/**
* Run the task asynchronously.
*
* @return The created {@link BukkitTask}
*/
@NotNull BukkitTask runTaskAsynchronously();
/**
* Run the task after a specified number of ticks.
*
* @param delay The number of ticks to wait.
* @return The created {@link BukkitTask}
*/
@NotNull BukkitTask runTaskLater(long delay);
/**
* Run the task asynchronously after a specified number of ticks.
*
* @param delay The number of ticks to wait.
* @return The created {@link BukkitTask}
*/
@NotNull BukkitTask runTaskLaterAsynchronously(long delay);
/**
* Run the task repeatedly on a timer.
*
* @param delay The delay before the task is first ran (in ticks).
* @param period The ticks elapsed before the task is ran again.
* @return The created {@link BukkitTask}
*/
@NotNull BukkitTask runTaskTimer(long delay, long period);
/**
* Run the task repeatedly on a timer asynchronously.
*
* @param delay The delay before the task is first ran (in ticks).
* @param period The ticks elapsed before the task is ran again.
* @return The created {@link BukkitTask}
*/
@NotNull BukkitTask runTaskTimerAsynchronously(long delay, long period);
}

View File

@@ -1,7 +1,5 @@
package com.willfp.eco.util.config;
import com.willfp.eco.util.serialization.EcoSerializable;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -30,15 +28,6 @@ public interface Config {
@NotNull
List<String> getKeys(boolean deep);
/**
* Set an object in config.
*
* @param path The path.
* @param object The object.
*/
void set(@NotNull String path,
@NotNull EcoSerializable<?> object);
/**
* Get an object from config.
* Default implementations call {@link org.bukkit.configuration.file.YamlConfiguration#get(String)}.
@@ -49,48 +38,6 @@ public interface Config {
@Nullable
Object getRaw(@NotNull String path);
/**
* Get an object from config.
*
* @param path The path.
* @param clazz The class of the object.
* @param <T> The type of the object.
* @return The object.
*/
@NotNull <T extends EcoSerializable<T>> T get(@NotNull String path,
@NotNull Class<T> clazz);
/**
* Get an object from config.
*
* @param path The path.
* @param clazz The class of the object.
* @param <T> The type of the object.
* @return The object, or null if not found.
*/
@Nullable <T extends EcoSerializable<T>> T getOrNull(@NotNull String path,
@NotNull Class<T> clazz);
/**
* Get bukkit configuration section from config.
*
* @param path The key to check.
* @return The configuration section. Throws NPE if not found.
*/
@NotNull
@Deprecated
ConfigurationSection getSection(@NotNull String path);
/**
* Get bukkit configuration section from config.
*
* @param path The key to check.
* @return The configuration section, or null if not found.
*/
@Nullable
@Deprecated
ConfigurationSection getSectionOrNull(@NotNull String path);
/**
* Get subsection from config.
*

View File

@@ -2,7 +2,7 @@ package com.willfp.eco.util.recipe;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.willfp.eco.util.recipe.recipes.EcoShapedRecipe;
import com.willfp.eco.util.recipe.recipes.ShapedCraftingRecipe;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
@@ -18,7 +18,7 @@ public class Recipes {
/**
* Registry of all recipes.
*/
private static final BiMap<NamespacedKey, EcoShapedRecipe> RECIPES = HashBiMap.create();
private static final BiMap<NamespacedKey, ShapedCraftingRecipe> RECIPES = HashBiMap.create();
/**
@@ -26,7 +26,7 @@ public class Recipes {
*
* @param recipe The recipe.
*/
public void register(@NotNull final EcoShapedRecipe recipe) {
public void register(@NotNull final ShapedCraftingRecipe recipe) {
RECIPES.forcePut(recipe.getKey(), recipe);
Bukkit.getServer().removeRecipe(recipe.getKey());
@@ -57,7 +57,7 @@ public class Recipes {
* @return The match, or null if not found.
*/
@Nullable
public EcoShapedRecipe getMatch(@NotNull final ItemStack[] matrix) {
public ShapedCraftingRecipe getMatch(@NotNull final ItemStack[] matrix) {
return RECIPES.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null);
}
@@ -68,8 +68,8 @@ public class Recipes {
* @return The shaped recipe, or null if not found.
*/
@Nullable
public EcoShapedRecipe getShapedRecipe(@NotNull final NamespacedKey key) {
EcoShapedRecipe recipe = RECIPES.get(key);
public ShapedCraftingRecipe getShapedRecipe(@NotNull final NamespacedKey key) {
ShapedCraftingRecipe recipe = RECIPES.get(key);
if (recipe != null) {
return recipe;
}

View File

@@ -0,0 +1,66 @@
package com.willfp.eco.util.recipe.recipes;
import com.willfp.eco.util.recipe.parts.RecipePart;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public interface CraftingRecipe {
/**
* Get item material at a specific index.
*
* @param index The index to check.
* @return The material.
*/
Material getMaterialAtIndex(int index);
/**
* Get "real" item at specific index.
*
* @param index The index to check.
* @return The item.
*/
ItemStack getDisplayedAtIndex(int index);
/**
* Test matrix against recipe.
*
* @param matrix The matrix to check.
* @return If the recipe matches.
*/
boolean test(@NotNull ItemStack[] matrix);
/**
* Register the recipe.
*/
void register();
/**
* The recipe parts.
*
* @return The parts.
*/
RecipePart[] getParts();
/**
* Get the recipe key.
*
* @return The key.
*/
NamespacedKey getKey();
/**
* Get the displayed recipe key.
*
* @return The key.
*/
NamespacedKey getDisplayedKey();
/**
* Get the recipe output.
*
* @return The output.
*/
ItemStack getOutput();
}

View File

@@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public final class EcoShapedRecipe extends PluginDependent {
public final class ShapedCraftingRecipe extends PluginDependent implements CraftingRecipe {
/**
* Recipe parts.
*/
@@ -38,10 +38,10 @@ public final class EcoShapedRecipe extends PluginDependent {
@Getter
private final ItemStack output;
private EcoShapedRecipe(@NotNull final EcoPlugin plugin,
@NotNull final String key,
@NotNull final RecipePart[] parts,
@NotNull final ItemStack output) {
private ShapedCraftingRecipe(@NotNull final EcoPlugin plugin,
@NotNull final String key,
@NotNull final RecipePart[] parts,
@NotNull final ItemStack output) {
super(plugin);
this.parts = parts;
@@ -50,32 +50,17 @@ public final class EcoShapedRecipe extends PluginDependent {
this.output = output;
}
/**
* Get item material at a specific index.
*
* @param index The index to check.
* @return The material.
*/
@Override
public Material getMaterialAtIndex(final int index) {
return parts[index].getDisplayed().getType();
}
/**
* Get "real" item at specific index.
*
* @param index The index to check.
* @return The item.
*/
@Override
public ItemStack getDisplayedAtIndex(final int index) {
return parts[index].getDisplayed();
}
/**
* Test matrix against recipe.
*
* @param matrix The matrix to check.
* @return If the recipe matches.
*/
@Override
public boolean test(@NotNull final ItemStack[] matrix) {
boolean matches = true;
for (int i = 0; i < 9; i++) {
@@ -87,9 +72,7 @@ public final class EcoShapedRecipe extends PluginDependent {
return matches;
}
/**
* Register the recipe.
*/
@Override
public void register() {
Recipes.register(this);
}
@@ -190,14 +173,14 @@ public final class EcoShapedRecipe extends PluginDependent {
*
* @return The built recipe.
*/
public EcoShapedRecipe build() {
public ShapedCraftingRecipe build() {
for (int i = 0; i < 9; i++) {
if (recipeParts[i] == null) {
recipeParts[i] = new EmptyRecipePart();
}
}
return new EcoShapedRecipe(plugin, key.toLowerCase(), recipeParts, output);
return new ShapedCraftingRecipe(plugin, key.toLowerCase(), recipeParts, output);
}
}
}

View File

@@ -1,14 +0,0 @@
package com.willfp.eco.util.serialization;
import com.willfp.eco.util.config.Config;
import org.jetbrains.annotations.NotNull;
public abstract class Deserializer<T extends EcoSerializable<T>> {
/**
* Deserialize a config into an object.
*
* @param config The config.
* @return The object.
*/
public abstract T deserialize(@NotNull Config config);
}

View File

@@ -1,14 +0,0 @@
package com.willfp.eco.util.serialization;
import com.willfp.eco.util.config.Config;
import org.jetbrains.annotations.NotNull;
public interface EcoSerializable<T> {
/**
* Serialize an object into a config.
*
* @return The config.
*/
@NotNull
Config serialize();
}

View File

@@ -1,14 +0,0 @@
package com.willfp.eco.util.serialization;
import org.jetbrains.annotations.NotNull;
public class NoRegisteredDeserializerException extends Exception {
/**
* Create new NoRegisteredDeserializerException.
*
* @param message The message.
*/
public NoRegisteredDeserializerException(@NotNull final String message) {
super(message);
}
}