OSDN Git Service

903c7f3e64115807fa52916768a8f9280a3278e4
[chemicraft/chemicraft.git] / common / chemicraft / system / ChemiCraftCraftingManager.java
1 package chemicraft.system;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import net.minecraft.src.IInventory;
7 import net.minecraft.src.ItemStack;
8 import chemicraft.ChemiCraftAPI;
9 import chemicraft.inventory.InventoryChemicalCombination;
10 import chemicraft.inventory.InventoryChemicalCraftingMaterial;
11 import chemicraft.inventory.InventoryChemicalCraftingNBT;
12 import chemicraft.inventory.InventoryChemicalCraftingResult;
13 import chemicraft.inventory.InventoryDecomposition;
14 import chemicraft.util.ChemicalNBTRecipe;
15
16 public class ChemiCraftCraftingManager {
17
18         public ItemStack[] getDecompositionResults(IInventory par1IInventory){
19                 //Setting to Flag.
20                 ((InventoryDecomposition)par1IInventory).setFlag(false);
21
22                 //variable of results.
23                 ItemStack[] results = null;
24
25                 //Just loop of amount of added recipe.
26                 for(int i1 = 0;i1 < ChemiCraftAPI.instance.getDecompositionMaterial().size();i1++){
27                         //Check of null of the Material Slot.
28                         if(par1IInventory.getStackInSlot(16) == null){ this.clearResults(par1IInventory, 16); return new ItemStack[16];}
29                         //Recipe the match?
30                         boolean match = true;
31                         //Reset the results.
32                         results  = new ItemStack[ChemiCraftAPI.instance.getDecompositionResult().get(i1).length];
33                         //Recipe ID & Damage.
34                         //Material ID & Damage.
35                         int materialID = par1IInventory.getStackInSlot(16).itemID;
36                         int materialDamage = par1IInventory.getStackInSlot(16).getItemDamage();
37                         int recipeID = ChemiCraftAPI.instance.getDecompositionMaterial().get(i1).itemID;
38                         int recipeDamage = ChemiCraftAPI.instance.getDecompositionMaterial().get(i1).getItemDamage();
39
40                         //ID check.
41                         if(materialID != recipeID){ match = false;}
42                         //Damage check.
43                         if(materialDamage != recipeDamage){ match = false;}
44
45                         //if "match == true"
46                         if(match){
47                                 //Just loop of recipe size of recipe index i1.
48                                 for(int i = 0;i < ChemiCraftAPI.instance.getDecompositionResult().get(i1).length;i++){
49                                         //Assignment to the results.
50                                         results[i] = new ItemStack(
51                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].itemID,
52                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].stackSize,
53                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].getItemDamage()
54                                                         );
55                                 }
56                                 //return the results.
57                                 return results;
58                         }
59                 }
60                 //return the null.
61                 return null;
62         }
63
64
65
66         public ItemStack getChemicalCombinationResult(IInventory par1IInventory){
67                 //Setting to flag.
68                 ((InventoryChemicalCombination) par1IInventory).setFlag(false);
69
70                 //variable of result.
71                 ItemStack result = null;
72
73                 //Just loop of amount of added recipe.
74                 for(int i = 0;i < ChemiCraftAPI.instance.getChemicalCombinationMaterial().size();i++){
75
76                         //Recipe the match?
77                         boolean match = true;
78                         //materialID & recipeID
79                         ArrayList<Integer> materialID = new ArrayList<Integer>();
80                         ArrayList<Integer> materialDamage = new ArrayList<Integer>();
81                         ArrayList<Integer> recipeID = new ArrayList<Integer>();
82                         ArrayList<Integer> recipeDamage = new ArrayList<Integer>();
83
84                         //Just loop of recipe size of recipe index i.
85                         for(int j = 0;j < ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i).length;j++){
86                                 //Adding the recipe ID & Damage.
87                                 recipeID.add(ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i)[j].itemID);
88                                 recipeDamage.add(ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i)[j].getItemDamage());
89                         }
90                         //Just loop of 16.
91                         for(int j = 0;j < 16;j++){
92                                 //Null check
93                                 if(par1IInventory.getStackInSlot(j) != null){
94                                         //Adding the materialID & Damage.
95                                         materialID.add(par1IInventory.getStackInSlot(j).itemID);
96                                         materialDamage.add(par1IInventory.getStackInSlot(j).getItemDamage());
97                                 }
98                         }
99
100                         //trimToSize & sorting.
101                         Collections.sort(materialID);
102                         Collections.sort(materialDamage);
103                         Collections.sort(recipeID);
104                         Collections.sort(recipeDamage);
105                         materialID.trimToSize();
106                         materialDamage.trimToSize();
107                         recipeID.trimToSize();
108                         recipeDamage.trimToSize();
109
110                         //Size check.
111                         if(materialID.size() != recipeID.size()){ match = false;}
112                         //Just loop of materialID.size().
113                         for(int j = 0;j < materialID.size();j++){
114                                 if(match){
115                                         //ID check.
116                                         if(materialID.get(j) != recipeID.get(j).hashCode()){ match = false;}
117                                         //Damage check.
118                                         if(materialDamage.get(j) != recipeDamage.get(j).hashCode()){ match = false;}
119                                 }
120                         }
121
122                         //if "match == true"
123                         if(match){
124                                 //Assignment to the result.
125                                 result =
126                                                 new ItemStack(
127                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).itemID,
128                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).stackSize,
129                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).getItemDamage()
130                                                                 );
131                                 //return the result.
132                                 return result;
133                         }
134                 }
135                 //return the null.
136                 return null;
137         }
138
139
140
141         public ChemicalNBTRecipe chemicalCrafting(InventoryChemicalCraftingMaterial par1IInventory, InventoryChemicalCraftingResult par2IInventory, InventoryChemicalCraftingNBT par3IInventory){
142                 ChemiCraftAPI api = ChemiCraftAPI.instance;
143                 ChemicalNBTRecipe returnObj = null;
144                 for(int i = 0;i < api.getMaterialRecipe().size();i++){
145                         ItemStack result = api.getMaterialRecipe().get(i).match(par1IInventory);
146                         ItemStack resultArg = null;
147                         if(result != null){
148                                 resultArg = new ItemStack(result.itemID, result.stackSize, result.getItemDamage());
149                                 if(api.getMaterialRecipe().get(i).nbtMatch(par1IInventory) != null){
150                                         ItemStack[] nbtInv = new ItemStack[par3IInventory.getSizeInventory()];
151                                         for(int j = 0;j < par3IInventory.getSizeInventory();j++){
152                                                 nbtInv[j] = par3IInventory.getStackInSlot(j);
153                                         }
154                                         api.getMaterialRecipe().get(i).nbtMatch(par1IInventory).setNBT(nbtInv, resultArg);
155                                 }
156                                 returnObj = api.getMaterialRecipe().get(i).nbtMatch(par1IInventory);
157                         }
158                         par2IInventory.setInventorySlotContents(0, resultArg);
159                 }
160                 return returnObj;
161         }
162
163
164
165         private void clearResults(IInventory par1IInventory, int par2){
166                 ((InventoryDecomposition)par1IInventory).setFlag(false);
167                 for(int i = 0;i < par2;i++){
168                         par1IInventory.setInventorySlotContents(i, null);
169                 }
170         }
171
172 }