Advertisement
Gamebuster

RepairRecipe.class

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