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