From 605cc5cf3d946d4582af31c972392e94b0870737 Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 25 Nov 2024 21:44:07 +0000 Subject: [PATCH] start recipes --- .../oribuin/fishing/api/recipe/Recipe.java | 30 ++++++++ .../fishing/api/recipe/RecipeItem.java | 76 +++++++++++++++++++ .../api/recipe/type/AugmentRecipeItem.java | 63 +++++++++++++++ .../api/recipe/type/MaterialRecipeItem.java | 53 +++++++++++++ .../xyz/oribuin/fishing/augment/Augment.java | 1 - .../fishing/augment/AugmentRegistry.java | 11 +++ .../fishing/storage/util/PersistKeys.java | 3 + 7 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 src/main/java/xyz/oribuin/fishing/api/recipe/Recipe.java create mode 100644 src/main/java/xyz/oribuin/fishing/api/recipe/RecipeItem.java create mode 100644 src/main/java/xyz/oribuin/fishing/api/recipe/type/AugmentRecipeItem.java create mode 100644 src/main/java/xyz/oribuin/fishing/api/recipe/type/MaterialRecipeItem.java diff --git a/src/main/java/xyz/oribuin/fishing/api/recipe/Recipe.java b/src/main/java/xyz/oribuin/fishing/api/recipe/Recipe.java new file mode 100644 index 0000000..1ffeb71 --- /dev/null +++ b/src/main/java/xyz/oribuin/fishing/api/recipe/Recipe.java @@ -0,0 +1,30 @@ +package xyz.oribuin.fishing.api.recipe; + +import xyz.oribuin.fishing.api.economy.Cost; + +import java.util.List; + +public interface Recipe { + + /** + * The result of the recipe + * + * @return The result of the recipe + */ + T result(); + + /** + * The list of ingredients for the recipe to be crafted + * + * @return The list of ingredients + */ + List> ingredients(); + + /** + * The cost of the recipe + * + * @return The cost of the recipe + */ + Cost cost(); + +} diff --git a/src/main/java/xyz/oribuin/fishing/api/recipe/RecipeItem.java b/src/main/java/xyz/oribuin/fishing/api/recipe/RecipeItem.java new file mode 100644 index 0000000..0d28b00 --- /dev/null +++ b/src/main/java/xyz/oribuin/fishing/api/recipe/RecipeItem.java @@ -0,0 +1,76 @@ +package xyz.oribuin.fishing.api.recipe; + +import dev.rosewood.rosegarden.config.CommentedConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import xyz.oribuin.fishing.api.config.Configurable; + + + private final Class type; // This is the type of the ite; + protected T item; + protected int amount; + + public RecipeItem(Class type) { + this.type = type; + } + + /** + * Check if the item is the same as the recipe item + * + * @param item The item to check + * + * @return If the item is the same as the recipe item + */ + public abstract boolean check(ItemStack item); + + /** + * Load the settings from the configuration file + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to load + */ + @Override + public void loadSettings(@NotNull CommentedConfigurationSection config) { + this.amount = config.getInt("amount", 1); + } + + /** + * Save the configuration file for the configurable class + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to save + */ + @Override + public void saveSettings(@NotNull CommentedConfigurationSection config) { + config.set("amount", this.amount); + } + + /** + * Get the item of the recipe item + * + * @return The item of the recipe item + */ + public final T item() { + return this.item; + } + + /** + * Get the amount of the recipe item + * + * @return The amount of the recipe item + */ + public final Class type() { + return this.type; + } + + /** + * Get the amount of the recipe item + * + * @return The amount of the recipe item + */ + public final int amount() { + return this.amount; + } + +} diff --git a/src/main/java/xyz/oribuin/fishing/api/recipe/type/AugmentRecipeItem.java b/src/main/java/xyz/oribuin/fishing/api/recipe/type/AugmentRecipeItem.java new file mode 100644 index 0000000..3cf5345 --- /dev/null +++ b/src/main/java/xyz/oribuin/fishing/api/recipe/type/AugmentRecipeItem.java @@ -0,0 +1,63 @@ +package xyz.oribuin.fishing.api.recipe.type; + +import dev.rosewood.rosegarden.config.CommentedConfigurationSection; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; +import xyz.oribuin.fishing.api.recipe.RecipeItem; +import xyz.oribuin.fishing.augment.Augment; +import xyz.oribuin.fishing.augment.AugmentRegistry; +import xyz.oribuin.fishing.storage.util.PersistKeys; + +public class AugmentRecipeItem extends RecipeItem { + + public AugmentRecipeItem() { + super(Augment.class); + } + + /** + * Check if the item is the same as the recipe item + * + * @param item The item to check + * + * @return If the item is the same as the recipe item + */ + @Override + public boolean check(ItemStack item) { + if (item == null) return false; + + PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer(); + String augmentType = container.get(PersistKeys.AUGMENT_TYPE, PersistentDataType.STRING); + + return augmentType != null && augmentType.equalsIgnoreCase(this.item.name()); + } + + + /** + * Load the settings from the configuration file + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to load + */ + @Override + public void loadSettings(@NotNull CommentedConfigurationSection config) { + super.loadSettings(config); + + this.item = AugmentRegistry.from(config.getString("item")); + } + + /** + * Save the configuration file for the configurable class + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to save + */ + @Override + public void saveSettings(@NotNull CommentedConfigurationSection config) { + super.saveSettings(config); + + config.set("item", this.item.name()); + } + +} diff --git a/src/main/java/xyz/oribuin/fishing/api/recipe/type/MaterialRecipeItem.java b/src/main/java/xyz/oribuin/fishing/api/recipe/type/MaterialRecipeItem.java new file mode 100644 index 0000000..a8d4a4c --- /dev/null +++ b/src/main/java/xyz/oribuin/fishing/api/recipe/type/MaterialRecipeItem.java @@ -0,0 +1,53 @@ +package xyz.oribuin.fishing.api.recipe.type; + +import dev.rosewood.rosegarden.config.CommentedConfigurationSection; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import xyz.oribuin.fishing.api.recipe.RecipeItem; +import xyz.oribuin.fishing.util.FishUtils; + +public class MaterialRecipeItem extends RecipeItem { + + public MaterialRecipeItem() { + super(Material.class); + } + + /** + * Check if the item is the same as the recipe item + * + * @param item The item to check + * + * @return If the item is the same as the recipe item + */ + @Override + public boolean check(ItemStack item) { + return item != null && item.getType() == this.item(); + } + + /** + * Load the settings from the configuration file + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to load + */ + @Override + public void loadSettings(@NotNull CommentedConfigurationSection config) { + super.loadSettings(config); + + this.item = FishUtils.getEnum(this.type(), config.getString("item"), Material.BEDROCK); + } + /** + * Save the configuration file for the configurable class + * I would recommend always super calling this method to save any settings that could be implemented + * + * @param config The configuration file to save + */ + @Override + public void saveSettings(@NotNull CommentedConfigurationSection config) { + super.saveSettings(config); + + config.set("item", this.item().name()); + } + +} diff --git a/src/main/java/xyz/oribuin/fishing/augment/Augment.java b/src/main/java/xyz/oribuin/fishing/augment/Augment.java index 826346d..7fc3be6 100644 --- a/src/main/java/xyz/oribuin/fishing/augment/Augment.java +++ b/src/main/java/xyz/oribuin/fishing/augment/Augment.java @@ -25,7 +25,6 @@ public abstract class Augment extends FishEventHandler implements Listener, Conf protected int requiredLevel; protected String permission; - /** * Create a new augment instance with a name and description * diff --git a/src/main/java/xyz/oribuin/fishing/augment/AugmentRegistry.java b/src/main/java/xyz/oribuin/fishing/augment/AugmentRegistry.java index 9499131..722ad00 100644 --- a/src/main/java/xyz/oribuin/fishing/augment/AugmentRegistry.java +++ b/src/main/java/xyz/oribuin/fishing/augment/AugmentRegistry.java @@ -76,6 +76,17 @@ public static Map from(ItemStack itemStack) { return result; } + /** + * Get an augment from the registry by its name + * + * @param name The name of the augment + * + * @return The augment + */ + public static Augment from(String name) { + return augments.get(name); + } + /** * Save the augments to the itemstack meta * diff --git a/src/main/java/xyz/oribuin/fishing/storage/util/PersistKeys.java b/src/main/java/xyz/oribuin/fishing/storage/util/PersistKeys.java index e3a1985..4ee1414 100644 --- a/src/main/java/xyz/oribuin/fishing/storage/util/PersistKeys.java +++ b/src/main/java/xyz/oribuin/fishing/storage/util/PersistKeys.java @@ -12,6 +12,9 @@ public class PersistKeys { public static NamespacedKey FISH_TYPE = new NamespacedKey(PLUGIN, "fish_type"); public static NamespacedKey FISH_TIER = new NamespacedKey(PLUGIN, "fish_tier"); + // Augment Namespaces + public static final NamespacedKey AUGMENT_TYPE = new NamespacedKey(PLUGIN, "augment_type"); + // Totem Namespaces public static final NamespacedKey TOTEM_OWNER = new NamespacedKey(PLUGIN, "totem_owner"); public static final NamespacedKey TOTEM_OWNERNAME = new NamespacedKey(PLUGIN, "totem_ownername");