Skip to content

Commit

Permalink
don't consume bookbinder inputs if the output is worse than the input (
Browse files Browse the repository at this point in the history
…#3925)

Co-authored-by: Jeroen <j.teriele1@students.uu.nl>
  • Loading branch information
iTwins and Jeroen authored Nov 21, 2023
1 parent 6e01add commit 52f5a14
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) {

// Just return if no enchantments exist. This shouldn't ever happen. :NotLikeThis:
if (enchantments.size() > 0) {
if (hasIllegalEnchants(storedItemEnchantments) || hasIllegalEnchants(storedTargetEnchantments)) {
return null;
}

ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);

EnchantmentStorageMeta enchantMeta = (EnchantmentStorageMeta) book.getItemMeta();
Expand All @@ -70,6 +74,11 @@ protected MachineRecipe findNextRecipe(BlockMenu menu) {
return null;
}

// If the output is the same as one of the inputs: don't consume items
if (enchantMeta.getStoredEnchants().equals(storedItemEnchantments) || enchantMeta.getStoredEnchants().equals(storedTargetEnchantments)) {
return null;
}

book.setItemMeta(enchantMeta);

MachineRecipe recipe = new MachineRecipe(25 * (enchantments.size() / this.getSpeed()), new ItemStack[] { target, item }, new ItemStack[] { book });
Expand Down Expand Up @@ -97,6 +106,19 @@ private boolean isCompatible(@Nullable ItemStack item) {
return item != null && item.getType() == Material.ENCHANTED_BOOK;
}

private boolean hasIllegalEnchants(@Nullable Map<Enchantment, Integer> enchantments) {
if (enchantments == null) {
return false;
}

for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
if (bypassVanillaMaxLevel.getValue() && entry.getValue() > customMaxLevel.getValue() || !bypassVanillaMaxLevel.getValue() && entry.getValue() > entry.getKey().getMaxLevel()) {
return true;
}
}
return false;
}

@Override
public ItemStack getProgressBar() {
return new ItemStack(Material.IRON_CHESTPLATE);
Expand Down

0 comments on commit 52f5a14

Please sign in to comment.