Advertisement
jayhillx

[Advanced Combat] EnchantmentItemRecipe

Jan 2nd, 2022
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package com.advancedcombat.common.recipe;
  2.  
  3. import com.advancedcombat.init.ACRecipes;
  4. import com.google.common.collect.Lists;
  5. import net.minecraft.enchantment.Enchantment;
  6. import net.minecraft.enchantment.EnchantmentHelper;
  7. import net.minecraft.inventory.CraftingInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.item.crafting.IRecipeSerializer;
  10. import net.minecraft.item.crafting.SpecialRecipe;
  11. import net.minecraft.util.ResourceLocation;
  12. import net.minecraft.world.World;
  13.  
  14. import javax.annotation.Nonnull;
  15. import java.util.List;
  16. import java.util.Map;
  17.  
  18. import static com.advancedcombat.common.item.EnchantmentItem.*;
  19.  
  20. /** Specifies the recipe needed to enchant items with an upgrade item. */
  21. public class EnchantmentItemRecipe extends SpecialRecipe {
  22.  
  23.     public EnchantmentItemRecipe(ResourceLocation location) {
  24.         super(location);
  25.     }
  26.  
  27.     public ItemStack getUpgradableStack(List<ItemStack> stacks) {
  28.         ItemStack stack = stacks.get(0);
  29.  
  30.         if (enchant.canEnchant(stack)) {
  31.             Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(stack);
  32.  
  33.             for (Enchantment i : enchantments.keySet()) {
  34.                 if (i == enchant) {
  35.                     int lvl = enchantments.get(i);
  36.  
  37.                     if (lvl >= enchant.getMaxLevel()) {
  38.                         return ItemStack.EMPTY;
  39.                     }
  40.                 }
  41.             }
  42.             return stack.copy();
  43.         }
  44.         return ItemStack.EMPTY;
  45.     }
  46.  
  47.     @Override
  48.     public boolean matches(CraftingInventory inventory, @Nonnull World world) {
  49.         List<ItemStack> stacks = Lists.newArrayList();
  50.  
  51.         for (int i = 0; i < inventory.getContainerSize(); i++) {
  52.             if (!inventory.getItem(i).isEmpty()) {
  53.                 stacks.add(inventory.getItem(i));
  54.             }
  55.         }
  56.  
  57.         // 2 things in this recipe.
  58.         if (stacks.size() == 2) {
  59.             return !getUpgradableStack(stacks).isEmpty();
  60.         }
  61.         return false;
  62.     }
  63.  
  64.     @Override
  65.     @Nonnull
  66.     public ItemStack assemble(CraftingInventory inventory) {
  67.         List<ItemStack> stacks = Lists.newArrayList();
  68.  
  69.         for (int i = 0; i < inventory.getContainerSize(); i++) {
  70.             if (!inventory.getItem(i).isEmpty()) {
  71.                 stacks.add(inventory.getItem(i));
  72.             }
  73.         }
  74.  
  75.         ItemStack stack = this.getUpgradableStack(stacks);
  76.         Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(stack);
  77.  
  78.         if (stack.isEmpty()) return ItemStack.EMPTY;
  79.  
  80.         // Check for the enchantment.
  81.         for (Enchantment i : map.keySet()) {
  82.             if (i == enchant && map.get(i) >= enchant.getMaxLevel()) {
  83.                 int newLevel = map.get(i) + 1;
  84.  
  85.                 map.put(i, newLevel);
  86.                 EnchantmentHelper.setEnchantments(map, stack);
  87.                 return stack;
  88.             }
  89.         }
  90.  
  91.         if (enchant.getMinLevel() <= 0) {
  92.             map.put(enchant, 1);
  93.             EnchantmentHelper.setEnchantments(map, stack);
  94.             return stack;
  95.         } else {
  96.             return ItemStack.EMPTY;
  97.         }
  98.     }
  99.  
  100.     @Override
  101.     public boolean canCraftInDimensions(int width, int height) {
  102.         return width >= 2 && height >= 2;
  103.     }
  104.  
  105.     @Override
  106.     @Nonnull
  107.     public IRecipeSerializer<?> getSerializer() {
  108.         return ACRecipes.ENCHANTMENT_UPGRADE.get();
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement