OSDN Git Service

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