OSDN Git Service

c46e44bdd68eca41a9626145cdb58d8bebeea4f5
[chemicraft/chemicraft.git] / common / chemicraft / ChemiCraftCraftingManager.java
1 package chemicraft;
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
9 public class ChemiCraftCraftingManager {
10
11         public ItemStack[] getDecompositionResults(IInventory par1IInventory){
12                 //Setting to Flag.
13                 ((InventoryDecomposition)par1IInventory).setFlag(false);
14
15                 //variable of results.
16                 ItemStack[] results = null;
17
18                 //Just loop of amount of added recipe.
19                 for(int i1 = 0;i1 < ChemiCraftAPI.instance.getDecompositionMaterial().size();i1++){
20                         //Recipe the match?
21                         boolean match = true;
22                         //Reset the results.
23                         results  = new ItemStack[ChemiCraftAPI.instance.getDecompositionResult().get(i1).length];
24                         //Recipe ID & Damage.
25                         //Material ID & Damage.
26                         int materialID = par1IInventory.getStackInSlot(16).itemID;
27                         int materialDamage = par1IInventory.getStackInSlot(16).getItemDamage();
28                         int recipeID = ChemiCraftAPI.instance.getDecompositionMaterial().get(i1).itemID;
29                         int recipeDamage = ChemiCraftAPI.instance.getDecompositionMaterial().get(i1).getItemDamage();
30
31                         //Check of null of the Material Slot.
32                         if(par1IInventory.getStackInSlot(16) == null){ this.clearResults(par1IInventory, 16); return new ItemStack[16];}
33                         //ID check.
34                         if(materialID != recipeID){ match = false;}
35                         //Damage check.
36                         if(materialDamage != recipeDamage){ match = false;}
37
38                         //if "match == true"
39                         if(match){
40                                 //Just loop of recipe size of recipe index i1.
41                                 for(int i = 0;i < ChemiCraftAPI.instance.getDecompositionResult().get(i1).length;i++){
42                                         //Assignment to the results.
43                                         results[i] = new ItemStack(
44                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].itemID,
45                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].stackSize,
46                                                         ChemiCraftAPI.instance.getDecompositionResult().get(i1)[i].getItemDamage()
47                                                         );
48                                 }
49                                 //return the results.
50                                 return results;
51                         }
52                 }
53                 //return the null.
54                 return null;
55         }
56
57
58
59         public ItemStack getChemicalCombinationResult(IInventory par1IInventory){
60                 //Setting to flag.
61                 ((InventoryChemicalCombination) par1IInventory).setFlag(false);
62
63                 //variable of result.
64                 ItemStack result = null;
65
66                 //Just loop of amount of added recipe.
67                 for(int i = 0;i < ChemiCraftAPI.instance.getChemicalCombinationMaterial().size();i++){
68
69                         //Recipe the match?
70                         boolean match = true;
71                         //materialID & recipeID
72                         ArrayList<Integer> materialID = new ArrayList<Integer>();
73                         ArrayList<Integer> materialDamage = new ArrayList<Integer>();
74                         ArrayList<Integer> recipeID = new ArrayList<Integer>();
75                         ArrayList<Integer> recipeDamage = new ArrayList<Integer>();
76
77                         //Just loop of recipe size of recipe index i.
78                         for(int j = 0;j < ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i).length;j++){
79                                 //Adding the recipe ID & Damage.
80                                 recipeID.add(ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i)[j].itemID);
81                                 recipeDamage.add(ChemiCraftAPI.instance.getChemicalCombinationMaterial().get(i)[j].getItemDamage());
82                         }
83                         //Just loop of 16.
84                         for(int j = 0;j < 16;j++){
85                                 //Null check
86                                 if(par1IInventory.getStackInSlot(j) != null){
87                                         //Adding the materialID & Damage.
88                                         materialID.add(par1IInventory.getStackInSlot(j).itemID);
89                                         materialDamage.add(par1IInventory.getStackInSlot(j).getItemDamage());
90                                 }
91                         }
92
93                         //trimToSize & sorting.
94                         materialID.trimToSize();
95                         materialDamage.trimToSize();
96                         recipeID.trimToSize();
97                         recipeDamage.trimToSize();
98                         Collections.sort(materialID);
99                         Collections.sort(materialDamage);
100                         Collections.sort(recipeID);
101                         Collections.sort(recipeDamage);
102
103                         //Size check.
104                         if(materialID.size() != recipeID.size()){ match = false;}
105                         //Just loop of materialID.size().
106                         for(int j = 0;j < materialID.size();j++){
107                                 //ID check.
108                                 if(materialID.get(j) != recipeID.get(j)){ match = false;}
109                                 //Damage check.
110                                 if(materialDamage.get(j) != recipeDamage.get(j)){ match = false;}
111                         }
112
113                         //if "match == true"
114                         if(match){
115                                 //Assignment to the result.
116                                 result =
117                                                 new ItemStack(
118                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).itemID,
119                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).stackSize,
120                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).getItemDamage()
121                                                                 );
122                                 //return the result.
123                                 return result;
124                         }
125                 }
126                 //return the null.
127                 return null;
128         }
129
130
131
132         public ItemStack getMaterialResult(IInventory par1IInventory){
133                 ((InventoryMaterialCrafting) par1IInventory).setFlag(false);
134
135                 ItemStack result = null;
136
137                 for(int i = 0;i < ChemiCraftAPI.instance.getMaterialMaterials().size();i++){
138                         boolean match = true;
139
140                         ArrayList<Integer> materialID = new ArrayList<Integer>();
141                         ArrayList<Integer> materialDamage = new ArrayList<Integer>();
142                         ArrayList<Integer> recipeID = new ArrayList<Integer>();
143                         ArrayList<Integer> recipeDamage = new ArrayList<Integer>();
144                         ItemStack[] var1 = new ItemStack[3];
145
146                         //Just loop of recipe size of recipe index i.
147                         for(int j = 0;j < ChemiCraftAPI.instance.getMaterialMaterials().get(i).length;j++){
148                                 //Adding the recipe ID & Damage.
149                                 recipeID.add(ChemiCraftAPI.instance.getMaterialMaterials().get(i)[j].itemID);
150                                 recipeDamage.add(ChemiCraftAPI.instance.getMaterialMaterials().get(i)[j].getItemDamage());
151                         }
152                         //Just loop of 9.
153                         for(int j = 0;j < 9;j++){
154                                 //Null check
155                                 if(par1IInventory.getStackInSlot(j+3) != null){
156                                         //Adding the materialID & Damage.
157                                         materialID.add(par1IInventory.getStackInSlot(j+3).itemID);
158                                         materialDamage.add(par1IInventory.getStackInSlot(j+3).getItemDamage());
159                                 }
160                         }
161
162                         for(int j = 0;j < 3;j++){
163                                 var1[j] = par1IInventory.getStackInSlot(j);
164                         }
165
166                         //trimToSize & sorting.
167                         materialID.trimToSize();
168                         materialDamage.trimToSize();
169                         recipeID.trimToSize();
170                         recipeDamage.trimToSize();
171                         Collections.sort(materialID);
172                         Collections.sort(materialDamage);
173                         Collections.sort(recipeID);
174                         Collections.sort(recipeDamage);
175
176                         //Size check.
177                         if(materialID.size() != recipeID.size()){ match = false;}
178                         //Just loop of materialID.size().
179                         for(int j = 0;j < materialID.size();j++){
180                                 //ID check.
181                                 if(materialID.get(j) != recipeID.get(j)){ match = false;}
182                                 //Damage check.
183                                 if(materialDamage.get(j) != recipeDamage.get(j)){ match = false;}
184                         }
185
186                         //if "match == true"
187                         if(match){
188                                 //Assignment to the result.
189                                 result =
190                                                 new ItemStack(
191                                                                 ChemiCraftAPI.instance.getMaterialResult().get(i).itemID,
192                                                                 ChemiCraftAPI.instance.getMaterialResult().get(i).stackSize,
193                                                                 ChemiCraftAPI.instance.getMaterialResult().get(i).getItemDamage()
194                                                                 );
195
196                                 if(ChemiCraftAPI.instance.getMaterialNBTRecipe().get(i) != null){
197                                         ItemStack[] useItems = ChemiCraftAPI.instance.getMaterialNBTRecipe().get(i).setNBT(var1, result);
198                                         ((InventoryMaterialCrafting) par1IInventory).subtractStack(0, 3, useItems);
199                                 }
200
201                                 ((InventoryMaterialCrafting) par1IInventory).setFlag2(false);
202
203                                 //return the result.
204                                 return result;
205                         }
206                 }
207
208                 return null;
209         }
210
211
212
213         private void clearResults(IInventory par1IInventory, int par2){
214                 ((InventoryDecomposition)par1IInventory).setFlag(false);
215                 for(int i = 0;i < par2;i++){
216                         par1IInventory.setInventorySlotContents(i, null);
217                 }
218         }
219
220 }