OSDN Git Service

素材製作台&手榴弾完成版
[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                         materialID.trimToSize();
102                         materialDamage.trimToSize();
103                         recipeID.trimToSize();
104                         recipeDamage.trimToSize();
105                         Collections.sort(materialID);
106                         Collections.sort(materialDamage);
107                         Collections.sort(recipeID);
108                         Collections.sort(recipeDamage);
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                                 //ID check.
115                                 if(materialID.get(j) != recipeID.get(j)){ match = false;}
116                                 //Damage check.
117                                 if(materialDamage.get(j) != recipeDamage.get(j)){ match = false;}
118                         }
119
120                         //if "match == true"
121                         if(match){
122                                 //Assignment to the result.
123                                 result =
124                                                 new ItemStack(
125                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).itemID,
126                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).stackSize,
127                                                                 ChemiCraftAPI.instance.getChemicalCombinationResult().get(i).getItemDamage()
128                                                                 );
129                                 //return the result.
130                                 return result;
131                         }
132                 }
133                 //return the null.
134                 return null;
135         }
136
137
138
139         public ChemicalNBTRecipe chemicalCrafting(InventoryChemicalCraftingMaterial par1IInventory, InventoryChemicalCraftingResult par2IInventory, InventoryChemicalCraftingNBT par3IInventory){
140                 ChemiCraftAPI api = ChemiCraftAPI.instance;
141                 ChemicalNBTRecipe returnObj = null;
142                 for(int i = 0;i < api.getMaterialRecipe().size();i++){
143                         ItemStack result = api.getMaterialRecipe().get(i).match(par1IInventory);
144                         ItemStack resultArg = null;
145                         if(result != null){
146                                 resultArg = new ItemStack(result.itemID, result.stackSize, result.getItemDamage());
147                                 if(api.getMaterialRecipe().get(i).nbtMatch(par1IInventory) != null){
148                                         ItemStack[] nbtInv = new ItemStack[par3IInventory.getSizeInventory()];
149                                         for(int j = 0;j < par3IInventory.getSizeInventory();j++){
150                                                 nbtInv[j] = par3IInventory.getStackInSlot(j);
151                                         }
152                                         api.getMaterialRecipe().get(i).nbtMatch(par1IInventory).setNBT(nbtInv, resultArg);
153                                 }
154                                 returnObj = api.getMaterialRecipe().get(i).nbtMatch(par1IInventory);
155                         }
156                         par2IInventory.setInventorySlotContents(0, resultArg);
157                 }
158                 return returnObj;
159         }
160
161
162
163         private void clearResults(IInventory par1IInventory, int par2){
164                 ((InventoryDecomposition)par1IInventory).setFlag(false);
165                 for(int i = 0;i < par2;i++){
166                         par1IInventory.setInventorySlotContents(i, null);
167                 }
168         }
169
170 }