Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.advancedcombat.common.recipe;
- import com.advancedcombat.init.ACRecipes;
- import com.google.common.collect.Lists;
- import net.minecraft.enchantment.Enchantment;
- import net.minecraft.enchantment.EnchantmentHelper;
- import net.minecraft.inventory.CraftingInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.IRecipeSerializer;
- import net.minecraft.item.crafting.SpecialRecipe;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.world.World;
- import javax.annotation.Nonnull;
- import java.util.List;
- import java.util.Map;
- import static com.advancedcombat.common.item.EnchantmentItem.*;
- /** Specifies the recipe needed to enchant items with an upgrade item. */
- public class EnchantmentItemRecipe extends SpecialRecipe {
- public EnchantmentItemRecipe(ResourceLocation location) {
- super(location);
- }
- public ItemStack getUpgradableStack(List<ItemStack> stacks) {
- ItemStack stack = stacks.get(0);
- if (enchant.canEnchant(stack)) {
- Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(stack);
- for (Enchantment i : enchantments.keySet()) {
- if (i == enchant) {
- int lvl = enchantments.get(i);
- if (lvl >= enchant.getMaxLevel()) {
- return ItemStack.EMPTY;
- }
- }
- }
- return stack.copy();
- }
- return ItemStack.EMPTY;
- }
- @Override
- public boolean matches(CraftingInventory inventory, @Nonnull World world) {
- List<ItemStack> stacks = Lists.newArrayList();
- for (int i = 0; i < inventory.getContainerSize(); i++) {
- if (!inventory.getItem(i).isEmpty()) {
- stacks.add(inventory.getItem(i));
- }
- }
- // 2 things in this recipe.
- if (stacks.size() == 2) {
- return !getUpgradableStack(stacks).isEmpty();
- }
- return false;
- }
- @Override
- @Nonnull
- public ItemStack assemble(CraftingInventory inventory) {
- List<ItemStack> stacks = Lists.newArrayList();
- for (int i = 0; i < inventory.getContainerSize(); i++) {
- if (!inventory.getItem(i).isEmpty()) {
- stacks.add(inventory.getItem(i));
- }
- }
- ItemStack stack = this.getUpgradableStack(stacks);
- Map<Enchantment, Integer> map = EnchantmentHelper.getEnchantments(stack);
- if (stack.isEmpty()) return ItemStack.EMPTY;
- // Check for the enchantment.
- for (Enchantment i : map.keySet()) {
- if (i == enchant && map.get(i) >= enchant.getMaxLevel()) {
- int newLevel = map.get(i) + 1;
- map.put(i, newLevel);
- EnchantmentHelper.setEnchantments(map, stack);
- return stack;
- }
- }
- if (enchant.getMinLevel() <= 0) {
- map.put(enchant, 1);
- EnchantmentHelper.setEnchantments(map, stack);
- return stack;
- } else {
- return ItemStack.EMPTY;
- }
- }
- @Override
- public boolean canCraftInDimensions(int width, int height) {
- return width >= 2 && height >= 2;
- }
- @Override
- @Nonnull
- public IRecipeSerializer<?> getSerializer() {
- return ACRecipes.ENCHANTMENT_UPGRADE.get();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement