From e0025e5ac53b769e00fdf3e37b3206479cd7a719 Mon Sep 17 00:00:00 2001 From: Traqueur <54551467+Traqueur-dev@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:00:59 +0200 Subject: [PATCH 1/2] Feat/improve (#25) --- gradle.properties | 2 +- .../fr/traqueur/recipes/api/RecipesAPI.java | 7 ++++++ .../recipes/impl/PrepareCraftListener.java | 18 +++++++++++++-- .../ingredients/ItemStackIngredient.java | 7 ++++++ .../ingredients/MaterialIngredient.java | 5 ++++ .../StrictItemStackIngredient.java | 7 ++++++ .../domains/ingredients/TagIngredient.java | 5 ++++ .../fr/traqueur/recipes/impl/hook/Hooks.java | 7 +++--- .../impl/hook/hooks/ItemsAdderIngredient.java | 23 +++++++++++-------- .../impl/hook/hooks/OraxenIngredient.java | 5 ++++ src/main/resources/version.properties | 2 +- 11 files changed, 72 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index 516314f..fb7cb53 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=2.0.1 \ No newline at end of file +version=2.0.2 \ No newline at end of file diff --git a/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java b/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java index 317a6c9..7c746da 100644 --- a/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java +++ b/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java @@ -201,4 +201,11 @@ public JavaPlugin getPlugin() { public boolean isDebug() { return debug; } + + public void debug(String message, Object... args) { + String formattedMessage = String.format(message, args); + if (debug) { + this.plugin.getLogger().info(formattedMessage); + } + } } \ No newline at end of file diff --git a/src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java b/src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java index 903f406..62dd6f7 100644 --- a/src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java +++ b/src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java @@ -76,7 +76,10 @@ public void onSmelt(BlockCookEvent event) { .findFirst() .ifPresent(recipe -> { if(!isSimilar(item, itemRecipe.ingredients()[0])) { + this.api.debug("The smelting recipe %s is not good.", itemRecipe.getKey()); event.setCancelled(true); + } else { + this.api.debug("The smelting recipe %s is good.", itemRecipe.getKey()); } }); } @@ -112,6 +115,7 @@ public void onSmithingTransform(PrepareSmithingEvent event) { if (!itemRecipe.getKey() .equals(recipe.getKey())) continue; + this.api.debug("The recipe %s is a smithing recipe.", itemRecipe.getKey()); Ingredient templateIngredient = itemRecipe.ingredients()[0]; Ingredient baseIngredient = itemRecipe.ingredients()[1]; Ingredient additionIngredient = itemRecipe.ingredients()[2]; @@ -121,9 +125,11 @@ && isSimilar(base, baseIngredient) && isSimilar(addition, additionIngredient); if(!isSimilar) { + this.api.debug("The smithing recipe %s is not good.", itemRecipe.getKey()); event.setResult(new ItemStack(Material.AIR)); return; } + this.api.debug("The smithing recipe %s is good.", itemRecipe.getKey()); } } @@ -153,11 +159,13 @@ public void onPrepareCraft(PrepareItemCraftEvent event) { for (ItemRecipe itemRecipe : itemRecipes) { if(recipe instanceof ShapedRecipe shapedRecipe && itemRecipe.recipeType() == RecipeType.CRAFTING_SHAPED) { if (!shapedRecipe.getKey().equals(itemRecipe.getKey())) continue; + this.api.debug("The recipe %s is a shaped recipe.", itemRecipe.getKey()); this.checkGoodShapedRecipe(itemRecipe, event); } if(recipe instanceof ShapelessRecipe shapelessRecipe && itemRecipe.recipeType() == RecipeType.CRAFTING_SHAPELESS) { if(!shapelessRecipe.getKey().equals(itemRecipe.getKey())) continue; + this.api.debug("The recipe %s is a shapeless recipe.", itemRecipe.getKey()); this.checkGoodShapelessRecipe(itemRecipe, event); } } @@ -169,18 +177,19 @@ public void onPrepareCraft(PrepareItemCraftEvent event) { * @param event the event */ private void checkGoodShapedRecipe(ItemRecipe itemRecipe, PrepareItemCraftEvent event) { - AtomicBoolean isSimilar = new AtomicBoolean(true); ItemStack[] matrix = event.getInventory().getMatrix(); matrix = Arrays.stream(matrix).filter(stack -> stack != null && stack.getType() != Material.AIR).toArray(ItemStack[]::new); String[] pattern = Arrays.stream(itemRecipe.pattern()).map(s -> s.split("")).flatMap(Arrays::stream).toArray(String[]::new); for (int i = 0; i < matrix.length; i++) { + AtomicBoolean isSimilar = new AtomicBoolean(true); ItemStack stack = matrix[i]; char sign = pattern[i].charAt(0); Arrays.stream(itemRecipe.ingredients()).filter(ingredient -> ingredient.sign() == sign).findFirst().ifPresent(ingredient -> { isSimilar.set(ingredient.isSimilar(stack)); }); if(!isSimilar.get()) { + this.api.debug("The shaped recipe %s is not good.", itemRecipe.getKey()); event.getInventory().setResult(new ItemStack(Material.AIR)); return; } @@ -203,13 +212,18 @@ private void checkGoodShapelessRecipe(ItemRecipe itemRecipe, PrepareItemCraftEve return ingredient.isSimilar(stack); }); if (!found) { + this.api.debug("Ingredient %s not found in the matrix.", ingredient.toString()); isSimilar.set(false); break; } + this.api.debug("Ingredient %s found in the matrix.", ingredient.toString()); } - if (!isSimilar.get()) { + if (!isSimilar.get() || matrix.size() != itemIngredients.length) { + this.api.debug("The shapeless recipe %s is not good.", itemRecipe.getKey()); event.getInventory().setResult(new ItemStack(Material.AIR)); + return; } + this.api.debug("The shapeless recipe %s is good.", itemRecipe.getKey()); } } diff --git a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/ItemStackIngredient.java b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/ItemStackIngredient.java index 7d4ad39..3d62181 100644 --- a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/ItemStackIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/ItemStackIngredient.java @@ -79,4 +79,11 @@ private boolean similarMeta(ItemMeta sourceMeta, ItemMeta ingredientMeta) { public RecipeChoice choice() { return new RecipeChoice.MaterialChoice(this.item.getType()); } + + @Override + public String toString() { + return "ItemStackIngredient{" + + "item=" + item + + '}'; + } } diff --git a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/MaterialIngredient.java b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/MaterialIngredient.java index 2cb9244..b6b1362 100644 --- a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/MaterialIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/MaterialIngredient.java @@ -48,4 +48,9 @@ public boolean isSimilar(ItemStack item) { public RecipeChoice choice() { return new RecipeChoice.MaterialChoice(this.material); } + + @Override + public String toString() { + return this.material.toString(); + } } diff --git a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/StrictItemStackIngredient.java b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/StrictItemStackIngredient.java index 55aa955..de925c0 100644 --- a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/StrictItemStackIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/StrictItemStackIngredient.java @@ -40,4 +40,11 @@ public boolean isSimilar(ItemStack item) { public RecipeChoice choice() { return new RecipeChoice.ExactChoice(this.item); } + + @Override + public String toString() { + return "StrictItemStackIngredient{" + + "item=" + item + + '}'; + } } diff --git a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/TagIngredient.java b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/TagIngredient.java index 7a8f352..cce6e36 100644 --- a/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/TagIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/domains/ingredients/TagIngredient.java @@ -49,4 +49,9 @@ public boolean isSimilar(ItemStack item) { public RecipeChoice choice() { return new RecipeChoice.MaterialChoice(this.tag); } + + @Override + public String toString() { + return this.tag.getKey().toString(); + } } diff --git a/src/main/java/fr/traqueur/recipes/impl/hook/Hooks.java b/src/main/java/fr/traqueur/recipes/impl/hook/Hooks.java index 9b42086..081fb70 100644 --- a/src/main/java/fr/traqueur/recipes/impl/hook/Hooks.java +++ b/src/main/java/fr/traqueur/recipes/impl/hook/Hooks.java @@ -23,10 +23,11 @@ public Ingredient getIngredient(String data, Character sign) { @Override public ItemStack getItemStack(String data) { - if(!CustomStack.isInRegistry(data)) { - throw new IllegalArgumentException("The item " + data + " is not registered in ItemsAdder."); + CustomStack stack = CustomStack.getInstance(data); + if (stack == null) { + throw new IllegalArgumentException("ItemsAdder item with id " + data + " not found"); } - return CustomStack.getInstance(data).getItemStack(); + return stack.getItemStack(); } }, /** diff --git a/src/main/java/fr/traqueur/recipes/impl/hook/hooks/ItemsAdderIngredient.java b/src/main/java/fr/traqueur/recipes/impl/hook/hooks/ItemsAdderIngredient.java index e92fe02..b7ec83d 100644 --- a/src/main/java/fr/traqueur/recipes/impl/hook/hooks/ItemsAdderIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/hook/hooks/ItemsAdderIngredient.java @@ -4,6 +4,7 @@ import fr.traqueur.recipes.api.domains.Ingredient; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.RecipeChoice; +import org.checkerframework.checker.units.qual.C; /** * This class is an implementation of the BaseIngredient class. @@ -14,8 +15,8 @@ public class ItemsAdderIngredient extends Ingredient { /** * The CustomStack object that represents the item from ItemsAdder. */ - private final CustomStack customStack; + private final String data; /** * Constructor of the class. * @param data The id of the item from ItemsAdder. @@ -23,10 +24,7 @@ public class ItemsAdderIngredient extends Ingredient { */ public ItemsAdderIngredient(String data, Character sign) { super(sign); - this.customStack = CustomStack.getInstance(data); - if(this.customStack == null) { - throw new IllegalArgumentException("The item " + data + " is not registered in ItemsAdder."); - } + this.data = data; } /** @@ -44,8 +42,7 @@ public ItemsAdderIngredient(String data) { public boolean isSimilar(ItemStack ingredient) { CustomStack item = CustomStack.byItemStack(ingredient); if (item == null) return false; - if (!item.getNamespacedID().equals(this.customStack.getNamespacedID())) return false; - return true; + return item.getNamespacedID().equals(this.getCustomStack().getNamespacedID()); } /** @@ -53,11 +50,19 @@ public boolean isSimilar(ItemStack ingredient) { */ @Override public RecipeChoice choice() { - return new RecipeChoice.MaterialChoice(this.customStack.getItemStack().getType()); + return new RecipeChoice.MaterialChoice(this.getCustomStack().getItemStack().getType()); + } + + private CustomStack getCustomStack() { + CustomStack customStack = CustomStack.getInstance(data); + if(customStack == null) { + throw new IllegalArgumentException("The item " + data + " is not registered in ItemsAdder."); + } + return customStack; } @Override public String toString() { - return this.customStack.getNamespacedID(); + return this.getCustomStack().getNamespacedID(); } } diff --git a/src/main/java/fr/traqueur/recipes/impl/hook/hooks/OraxenIngredient.java b/src/main/java/fr/traqueur/recipes/impl/hook/hooks/OraxenIngredient.java index 287e7b1..c63f601 100644 --- a/src/main/java/fr/traqueur/recipes/impl/hook/hooks/OraxenIngredient.java +++ b/src/main/java/fr/traqueur/recipes/impl/hook/hooks/OraxenIngredient.java @@ -74,4 +74,9 @@ public boolean isSimilar(ItemStack item) { public RecipeChoice choice() { return new RecipeChoice.MaterialChoice(material); } + + @Override + public String toString() { + return this.id; + } } diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties index 516314f..fb7cb53 100644 --- a/src/main/resources/version.properties +++ b/src/main/resources/version.properties @@ -1 +1 @@ -version=2.0.1 \ No newline at end of file +version=2.0.2 \ No newline at end of file From a485e947c6230649f1bd629db1c263dab935a34a Mon Sep 17 00:00:00 2001 From: Traqueur <54551467+Traqueur-dev@users.noreply.github.com> Date: Sat, 26 Jul 2025 09:34:57 +0200 Subject: [PATCH 2/2] Feat/improve (#27) * fix: ia hook (#23) * feat: improve * feat: remove some useless usage of variable --- .../java/fr/traqueur/recipes/api/RecipeType.java | 5 +++-- .../java/fr/traqueur/recipes/api/RecipesAPI.java | 13 +++++++------ .../java/fr/traqueur/recipes/api/hook/Hook.java | 11 ++++++++--- .../impl/domains/recipes/RecipeConfiguration.java | 12 +++++------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/main/java/fr/traqueur/recipes/api/RecipeType.java b/src/main/java/fr/traqueur/recipes/api/RecipeType.java index 5cfe8ed..bbe6186 100644 --- a/src/main/java/fr/traqueur/recipes/api/RecipeType.java +++ b/src/main/java/fr/traqueur/recipes/api/RecipeType.java @@ -1,6 +1,7 @@ package fr.traqueur.recipes.api; import org.bukkit.NamespacedKey; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import java.util.List; @@ -47,7 +48,7 @@ public enum RecipeType { /** * The plugin that registered this enum. */ - private static JavaPlugin plugin; + private static Plugin plugin; /** * The maximum number of ingredients that can be used in this recipe. @@ -83,7 +84,7 @@ public NamespacedKey getNamespacedKey(String key) { * Registers the plugin that is using this enum. * @param plugin the plugin */ - public static void registerPlugin(JavaPlugin plugin) { + public static void registerPlugin(Plugin plugin) { RecipeType.plugin = plugin; } diff --git a/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java b/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java index 7c746da..46161ce 100644 --- a/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java +++ b/src/main/java/fr/traqueur/recipes/api/RecipesAPI.java @@ -6,6 +6,7 @@ import fr.traqueur.recipes.impl.domains.recipes.RecipeConfiguration; import fr.traqueur.recipes.impl.updater.Updater; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -29,7 +30,7 @@ public final class RecipesAPI { /** * The plugin instance */ - private final JavaPlugin plugin; + private final Plugin plugin; /** * If the debug mode is enabled @@ -46,7 +47,7 @@ public final class RecipesAPI { * @param plugin The plugin instance * @param debug If the debug mode is enabled */ - public RecipesAPI(JavaPlugin plugin, boolean debug) { + public RecipesAPI(Plugin plugin, boolean debug) { this(plugin, debug, true); } @@ -56,7 +57,7 @@ public RecipesAPI(JavaPlugin plugin, boolean debug) { * @param debug If the debug mode is enabled * @param enableYmlSupport If the yml support is enabled */ - public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport) { + public RecipesAPI(Plugin plugin, boolean debug, boolean enableYmlSupport) { this.debug = debug; this.plugin = plugin; this.recipes = new ArrayList<>(); @@ -77,7 +78,7 @@ public RecipesAPI(JavaPlugin plugin, boolean debug, boolean enableYmlSupport) { if(this.debug) { Hook.HOOKS.stream() - .filter(hook -> hook.isEnable(plugin)) + .filter(Hook::isEnable) .forEach(hook -> this.plugin.getLogger().info("Hook enabled: " + hook.getPluginName())); Updater.update("RecipesAPI"); @@ -134,7 +135,7 @@ private void addConfiguredRecipes(File recipeFolder) { */ private void loadRecipe(File file) { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file); - var recipe = new RecipeConfiguration(this.plugin, file.getName().replace(".yml", ""), configuration) + var recipe = new RecipeConfiguration(file.getName().replace(".yml", ""), configuration) .build(); this.addRecipe(recipe); } @@ -190,7 +191,7 @@ public List getRecipes() { * Get the plugin instance * @return The plugin instance */ - public JavaPlugin getPlugin() { + public Plugin getPlugin() { return plugin; } diff --git a/src/main/java/fr/traqueur/recipes/api/hook/Hook.java b/src/main/java/fr/traqueur/recipes/api/hook/Hook.java index 921e35a..a199b18 100644 --- a/src/main/java/fr/traqueur/recipes/api/hook/Hook.java +++ b/src/main/java/fr/traqueur/recipes/api/hook/Hook.java @@ -2,6 +2,7 @@ import fr.traqueur.recipes.api.domains.Ingredient; import fr.traqueur.recipes.impl.hook.Hooks; +import org.bukkit.Bukkit; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; @@ -42,12 +43,16 @@ static void addHook(Hook hook) { /** * Check if the plugin is enabled - * @param plugin The plugin which use the API * @return If the plugin is enabled */ - default boolean isEnable(JavaPlugin plugin) { - return plugin.getServer().getPluginManager().getPlugin(getPluginName()) != null; + default boolean isEnable() { + return Bukkit.getPluginManager().getPlugin(getPluginName()) != null; } + /** + * Get the ItemStack from a result part + * @param resultPart The result part to get the ItemStack from + * @return The ItemStack from the result part + */ ItemStack getItemStack(String resultPart); } diff --git a/src/main/java/fr/traqueur/recipes/impl/domains/recipes/RecipeConfiguration.java b/src/main/java/fr/traqueur/recipes/impl/domains/recipes/RecipeConfiguration.java index 3554323..b399bc8 100644 --- a/src/main/java/fr/traqueur/recipes/impl/domains/recipes/RecipeConfiguration.java +++ b/src/main/java/fr/traqueur/recipes/impl/domains/recipes/RecipeConfiguration.java @@ -80,22 +80,20 @@ public class RecipeConfiguration implements Recipe { /** * The constructor of the recipe. - * @param plugin the plugin of the recipe. * @param name the name of the recipe. * @param configuration the configuration of the recipe. */ - public RecipeConfiguration(JavaPlugin plugin, String name, YamlConfiguration configuration) { - this(plugin, name, "", configuration); + public RecipeConfiguration(String name, YamlConfiguration configuration) { + this(name, "", configuration); } /** * The constructor of the recipe. - * @param plugin the plugin of the recipe. * @param name the name of the recipe. * @param path the path of the recipe. * @param configuration the configuration of the recipe. */ - public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConfiguration configuration) { + public RecipeConfiguration(String name, String path, YamlConfiguration configuration) { this.name = name.replace(".yml", ""); if(!path.endsWith(".") && !path.isEmpty()) { path += "."; @@ -140,7 +138,7 @@ public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConf yield new ItemStackIngredient(this.getItemStack(data[1]), sign); } default -> Hook.HOOKS.stream() - .filter(hook -> hook.isEnable(plugin)) + .filter(Hook::isEnable) .filter(hook -> hook.getPluginName().equalsIgnoreCase(data[0])) .findFirst() .orElseThrow(() -> new IllegalArgumentException("The data " + data[0] + " isn't valid.")) @@ -163,7 +161,7 @@ public RecipeConfiguration(JavaPlugin plugin, String name, String path, YamlConf case "material" -> new ItemStack(this.getMaterial(resultParts[1])); case "item", "base64" -> this.getItemStack(resultParts[1]); default -> Hook.HOOKS.stream() - .filter(hook -> hook.isEnable(plugin)) + .filter(Hook::isEnable) .filter(hook -> hook.getPluginName().equalsIgnoreCase(resultParts[0])) .findFirst() .orElseThrow(() -> new IllegalArgumentException("The result " + strItem + " isn't valid."))