Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.0.1
version=2.0.2
5 changes: 3 additions & 2 deletions src/main/java/fr/traqueur/recipes/api/RecipeType.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 14 additions & 6 deletions src/main/java/fr/traqueur/recipes/api/RecipesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -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<>();
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -190,7 +191,7 @@ public List<ItemRecipe> getRecipes() {
* Get the plugin instance
* @return The plugin instance
*/
public JavaPlugin getPlugin() {
public Plugin getPlugin() {
return plugin;
}

Expand All @@ -201,4 +202,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);
}
}
}
11 changes: 8 additions & 3 deletions src/main/java/fr/traqueur/recipes/api/hook/Hook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
18 changes: 16 additions & 2 deletions src/main/java/fr/traqueur/recipes/impl/PrepareCraftListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
Expand Down Expand Up @@ -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];
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ".";
Expand Down Expand Up @@ -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."))
Expand All @@ -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."))
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/fr/traqueur/recipes/impl/hook/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
},
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,19 +15,16 @@ 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.
* @param sign The sign that represents the ingredient in the recipe.
*/
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;
}

/**
Expand All @@ -44,20 +42,27 @@ 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());
}

/**
* {@inheritDoc}
*/
@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public boolean isSimilar(ItemStack item) {
public RecipeChoice choice() {
return new RecipeChoice.MaterialChoice(material);
}

@Override
public String toString() {
return this.id;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.0.1
version=2.0.2
Loading