Skip to content

Commit

Permalink
Fixed crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jan 14, 2021
1 parent 64d1fbb commit c1a214f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/willfp/eco/util/recipes/EcoShapedRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

public final class EcoShapedRecipe extends PluginDependent implements Registerable {
/**
* Recipe parts.
Expand Down Expand Up @@ -85,6 +87,15 @@ public void register() {
this.getPlugin().getRecipeManager().register(this);
}

@Override
public String toString() {
return "EcoShapedRecipe{"
+ "parts=" + Arrays.toString(parts)
+ ", key='" + key + '\''
+ ", output=" + output
+ '}';
}

/**
* Create a new recipe builder.
*
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/willfp/eco/util/recipes/RecipeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.willfp.eco.util.internal.PluginDependent;
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -38,16 +39,16 @@ public void prepareCraftListener(@NotNull final PrepareItemCraftEvent event) {
return;
}

EcoShapedRecipe internalRecipe = this.getPlugin().getRecipeManager().getShapedRecipe(recipe.getKey().getKey());
ItemStack[] matrix = event.getInventory().getMatrix();
EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix);

if (internalRecipe == null) {
if (matched == null) {
event.getInventory().setResult(new ItemStack(Material.AIR));
return;
}

ItemStack[] matrix = event.getInventory().getMatrix();

if (internalRecipe.test(matrix)) {
event.getInventory().setResult(internalRecipe.getOutput());
if (matched.test(matrix)) {
event.getInventory().setResult(matched.getOutput());
} else {
event.getInventory().setResult(new ItemStack(Material.AIR));
}
Expand All @@ -70,19 +71,20 @@ public void craftListener(@NotNull final CraftItemEvent event) {
return;
}

EcoShapedRecipe internalRecipe = this.getPlugin().getRecipeManager().getShapedRecipe(recipe.getKey().getKey());
ItemStack[] matrix = event.getInventory().getMatrix();
EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix);

if (internalRecipe == null) {
if (matched == null) {
event.getInventory().setResult(new ItemStack(Material.AIR));
event.setCancelled(true);
return;
}

ItemStack[] matrix = event.getInventory().getMatrix();

if (internalRecipe.test(matrix)) {
event.getInventory().setResult(internalRecipe.getOutput());
event.setCancelled(true);
if (matched.test(matrix)) {
event.getInventory().setResult(matched.getOutput());
} else {
event.getInventory().setResult(new ItemStack(Material.AIR));
event.setCancelled(true);
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/willfp/eco/util/recipes/RecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -55,6 +56,17 @@ void register(@NotNull final EcoShapedRecipe recipe) {
Bukkit.getServer().addRecipe(displayedRecipe);
}

/**
* Get recipe matching matrix.
*
* @param matrix The matrix to test.
* @return The match, or null if not found.
*/
@Nullable
public EcoShapedRecipe getMatch(@NotNull final ItemStack[] matrix) {
return registry.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null);
}

/**
* Get shaped recipe by key.
*
Expand Down

0 comments on commit c1a214f

Please sign in to comment.