Skip to content

Commit

Permalink
workaround Spigot recipe handling bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 5, 2021
1 parent 8564ec9 commit 9b310f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Expand Up @@ -57,7 +57,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// <--[tag]
// @attribute <EntityTag.dark_duration>
// @returns DurationTag
// @mechanism EntityTag.direction
// @mechanism EntityTag.dark_duration
// @group attributes
// @description
// Returns the duration remaining before a glow squid starts glowing.
Expand Down
Expand Up @@ -390,14 +390,20 @@ public static boolean isAllowedChoice(ItemScriptContainer script, RecipeChoice c

private static ItemStack AIR = new ItemStack(Material.AIR);

public static boolean shouldDenyCraft(ItemStack[] items, Recipe recipe) {
public enum DenyCraftReason {
ALLOWED,
IMPOSSIBLE,
NOT_ALLOWED
}

public static DenyCraftReason shouldDenyCraft(ItemStack[] items, Recipe recipe) {
int width = items.length == 9 ? 3 : 2;
int shapeStartX = 0, shapeStartY = 0;
if (recipe instanceof ShapedRecipe) {
String[] shape = ((ShapedRecipe) recipe).getShape();
if (shape.length != width || shape[0].length() != width) {
if (shape.length > width || shape[0].length() > width) {
return false; // Already impossible regardless
return DenyCraftReason.ALLOWED; // Already impossible regardless
}
loopStart:
for (shapeStartX = 0; shapeStartX <= width - shape[0].length(); shapeStartX++) {
Expand Down Expand Up @@ -442,7 +448,7 @@ else if (recipe instanceof ShapedRecipe) {
int x = i % width - shapeStartX;
int y = i / width - shapeStartY;
if (x < 0 || y < 0) {
return true;
return DenyCraftReason.IMPOSSIBLE;
}
String[] shape = ((ShapedRecipe) recipe).getShape();
if (y < shape.length && x < shape[y].length()) {
Expand All @@ -460,7 +466,24 @@ else if (recipe instanceof CookingRecipe) {
allowed = true; // Shouldn't be possible?
}
if (!allowed) {
return true;
return DenyCraftReason.NOT_ALLOWED;
}
}
return DenyCraftReason.ALLOWED;
}

public static boolean hasAlternateValidRecipe(Recipe recipe, ItemStack[] items) {
// Workaround for Spigot bug with the wrong recipe ID getting grabbed
if (recipe instanceof ShapedRecipe) {
ItemStack result = recipe.getResult();
if (isItemscript(result)) {
for (Recipe altRecipe : Bukkit.getRecipesFor(result)) {
if (altRecipe instanceof ShapedRecipe) {
if (shouldDenyCraft(items, altRecipe) == DenyCraftReason.ALLOWED) {
return true;
}
}
}
}
}
return false;
Expand All @@ -473,7 +496,7 @@ public void onCraftPrepared(PrepareItemCraftEvent event) {
return;
}
ItemStack[] items = event.getInventory().getMatrix();
if (shouldDenyCraft(items, recipe)) {
if (shouldDenyCraft(items, recipe) != DenyCraftReason.ALLOWED && !hasAlternateValidRecipe(recipe, items)) {
event.getInventory().setResult(null);
}
}
Expand All @@ -482,7 +505,7 @@ public void onCraftPrepared(PrepareItemCraftEvent event) {
public void onItemCrafted(CraftItemEvent event) {
Recipe recipe = event.getRecipe();
ItemStack[] items = event.getInventory().getMatrix();
if (shouldDenyCraft(items, recipe)) {
if (shouldDenyCraft(items, recipe) != DenyCraftReason.ALLOWED && !hasAlternateValidRecipe(recipe, items)) {
event.setCancelled(true);
}
}
Expand All @@ -495,7 +518,7 @@ public void onItemCooked(BlockCookEvent event) {
}
ItemStack[] stacks = new ItemStack[] { event.getSource() };
for (Recipe recipe : Bukkit.getRecipesFor(event.getResult())) {
if (recipe instanceof CookingRecipe && !shouldDenyCraft(stacks, recipe)) {
if (recipe instanceof CookingRecipe && shouldDenyCraft(stacks, recipe) == DenyCraftReason.ALLOWED) {
return;
}
}
Expand Down

0 comments on commit 9b310f0

Please sign in to comment.