Advertisement
Gamebuster

RepairRecipe.class (2)

Jun 25th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package com.gamebuster19901.supernatural.crafting;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.annotation.Nullable;
  7.  
  8. import net.minecraft.inventory.InventoryCrafting;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.item.crafting.IRecipe;
  12. import net.minecraft.world.World;
  13.  
  14.  
  15.  
  16. public class RepairRecipe implements IRecipe{
  17.     protected final Item itemToRepair;//the item type that will be repaired
  18.     protected final Item[] repairMaterial;//the materials that will repair the item
  19.     private ItemStack output;
  20.    
  21.     public RepairRecipe(Item itemToRepair, Item repairMaterial){
  22.         this.itemToRepair = itemToRepair;
  23.         this.repairMaterial = new Item[]{repairMaterial};
  24.     }
  25.    
  26.     public RepairRecipe(Item itemToRepair, ItemStack repairMaterial){
  27.         this.itemToRepair = itemToRepair;
  28.         this.repairMaterial = new Item[]{repairMaterial.getItem()};
  29.     }
  30.    
  31.     public RepairRecipe(Item itemToRepair, List repairMaterial){
  32.         this.itemToRepair = itemToRepair;
  33.         this.repairMaterial = new Item[repairMaterial.size()];
  34.         for (int j = 0; j < repairMaterial.size(); j++){
  35.             if (repairMaterial.get(j) instanceof Item){
  36.                 this.repairMaterial[j] = (Item) repairMaterial.get(j);
  37.             }
  38.             else if (repairMaterial.get(j) instanceof ItemStack){
  39.                 this.repairMaterial[j] = ((ItemStack) repairMaterial.get(j)).getItem();
  40.             }
  41.             else{
  42.                 throw new IllegalArgumentException(repairMaterial.get(j).getClass().getName() + " is not an item or itemstack (" + j + ")");
  43.             }
  44.         }
  45.     }
  46.  
  47.     @Override
  48.     public boolean matches(InventoryCrafting inv, World worldIn) {
  49.         List<ItemStack> list = new ArrayList<ItemStack>();
  50.         int amt = 0;
  51.         int amtItemsToRepairFound = 0;
  52.         int amtrepairable = 0;
  53.         int durability;
  54.         ItemStack repairedItem = null;
  55.        
  56.         for (int i = 0; i < inv.getSizeInventory(); ++i){
  57.             ItemStack itemstack = inv.getStackInSlot(i);
  58.             if (itemstack != null){
  59.                 list.add(itemstack);
  60.                 for (Item item : repairMaterial){
  61.                     if (itemstack.getItem().equals(item) && !itemstack.getItem().equals(itemToRepair)){
  62.                         amt += itemstack.stackSize;
  63.                         System.out.println(itemstack.stackSize);
  64.                     }
  65.                 }
  66.                 if (itemstack.getItem().equals(itemToRepair)){
  67.                     repairedItem = itemstack;
  68.                     durability = itemstack.getItemDamage();
  69.                     amtItemsToRepairFound++;
  70.                     if (amtItemsToRepairFound != 1){
  71.                         return false;
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         if (repairedItem == null){
  77.  
  78.             return false;
  79.         }
  80.         if (repairedItem.getItemDamage() - amt >= 0 && amt > 0){
  81.             this.output = new ItemStack(itemToRepair, 1, repairedItem.getItemDamage() - amt);
  82.             return true;
  83.         }
  84.  
  85.         return false;
  86.     }
  87.    
  88.     @Override
  89.     public ItemStack getCraftingResult(InventoryCrafting inv){
  90.         System.out.println("CraftingResult");
  91.         List<ItemStack> list = new ArrayList<ItemStack>();
  92.         int amt = 0;//how many repair items are in the crafting grid
  93.         int amtrepairable = 0;//how many repairable items are in the grid
  94.         int durability;//current durability of the item to repair
  95.         ItemStack repairedItem = null;
  96.        
  97.         for (int i = 0; i < inv.getSizeInventory(); ++i){//loop slots in crafting inventory
  98.             ItemStack itemstack = inv.getStackInSlot(i);
  99.             if (itemstack != null){//if slot is not empty
  100.                 list.add(itemstack);//add item to list
  101.                 for (Item item : repairMaterial){
  102.                     if (itemstack.getItem().equals(item) && !itemstack.getItem().equals(itemToRepair)){ //if the item is a repair material and is not the item being repaired
  103.                         amt += itemstack.stackSize;
  104.                         System.out.println(itemstack.stackSize); //debug
  105.                     }
  106.                 }
  107.                 if (itemstack.getItem().equals(itemToRepair)){
  108.                     repairedItem = itemstack;
  109.                     durability = itemstack.getItemDamage();
  110.                 }
  111.             }
  112.         }
  113.             return new ItemStack(itemToRepair, 1, repairedItem.getItemDamage() - amt);
  114.     }
  115.  
  116.     @Override
  117.     public int getRecipeSize() {
  118.         System.out.println(2);//debug
  119.         return 0;
  120.     }
  121.  
  122.     @Override
  123.     public ItemStack getRecipeOutput() {
  124.         System.out.println(3);//debug
  125.         return output;
  126.     }
  127.  
  128.     @Override
  129.     public ItemStack[] getRemainingItems(InventoryCrafting inv) {
  130.         System.out.println(4);//debug
  131.         ItemStack[] arrayStacks = new ItemStack[inv.getSizeInventory()];
  132.         for (int i = 0; i < arrayStacks.length; ++i){
  133.             inv.setInventorySlotContents(i, null); //all items in the crafting grid were used, even ones in a stack
  134.         }
  135.         return arrayStacks;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement