OSDN Git Service

修正・変更
[chemicraft/chemicraft.git] / common / pcc / chemicraft / tileentity / TileEntityElectrolysisTable.java
1 package pcc.chemicraft.tileentity;
2
3 import java.io.DataOutputStream;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Random;
7
8 import net.minecraft.block.Block;
9 import net.minecraft.item.ItemStack;
10 import net.minecraft.nbt.NBTTagCompound;
11 import net.minecraft.nbt.NBTTagList;
12 import net.minecraft.network.packet.Packet;
13 import net.minecraft.tileentity.TileEntity;
14 import pcc.chemicraft.ChemiCraftAPI;
15 import pcc.chemicraft.inventory.InventoryElectrolysisFuel;
16 import pcc.chemicraft.inventory.InventoryElectrolysisMaterial;
17 import pcc.chemicraft.inventory.InventoryElectrolysisResult;
18 import pcc.chemicraft.system.PacketHandler;
19
20 import com.google.common.io.ByteArrayDataInput;
21
22 public class TileEntityElectrolysisTable extends TileEntity
23 {
24
25         private InventoryElectrolysisMaterial invm = new InventoryElectrolysisMaterial();
26         private InventoryElectrolysisResult invr = new InventoryElectrolysisResult();
27         private InventoryElectrolysisFuel invf = new InventoryElectrolysisFuel();
28
29         /**
30          * 電力
31          */
32         private float heat = 0;
33
34         /**
35          * 素材の電力時間
36          */
37         private float burnTime = 0;
38
39         /**
40          * 電池の残り電力時間
41          */
42         private float fuelRestTime = 0; 
43
44         /**
45          * 最大熱量
46          */
47         private static final int MAX_HEAT = 1700;
48
49         /**
50          * 燃料が燃え始める最低熱量
51          */
52         private static final int MIN_HEAT = 400;
53
54         /**
55          * 燃料の燃焼速度比率・素材の燃焼速度比率の中心となる熱量
56          */
57         private static final int CENTER_HEAT = 1050;
58
59         /**
60          * 分解までの時間
61          */
62         private static final int MAX_BURN_TIME = 2000;
63
64         /**
65          * Random of instance.
66          */
67         public static final Random rand = new Random();
68
69         /**
70          * レシピのリスト
71          */
72         private static HashMap<ItemStack, ItemStack[]> recipeList = new HashMap<ItemStack, ItemStack[]>();
73
74         @Override
75         public void updateEntity()
76         {
77                 if (this.fuelRestTime >= 0) {
78                         this.burnTime += 10 * CENTER_HEAT / this.heat;
79                         this.fuelRestTime -= 10 * CENTER_HEAT / this.heat;
80                 }
81
82                 if (this.burnTime >= MAX_BURN_TIME) {
83                         Iterator<ItemStack> itMaterial = recipeList.keySet().iterator();
84                         while (itMaterial.hasNext()) {
85                                 ItemStack itemstackMaterial = itMaterial.next();
86                                 ItemStack materialItem = this.invm.getStackInSlot(0);
87                                 if (materialItem != null) {
88                                         if (this.isInItemCheck(itemstackMaterial)) {
89                                                 if (itemstackMaterial.itemID == materialItem.itemID && itemstackMaterial.getItemDamage() == materialItem.getItemDamage()) {
90                                                         this.inItem(itemstackMaterial);
91                                                         if (materialItem.stackSize > 1) {
92                                                                 this.invm.setInventorySlotContents(0, new ItemStack(materialItem.itemID, --materialItem.stackSize, materialItem.getItemDamage()));
93                                                         } else {
94                                                                 this.invm.setInventorySlotContents(0, null);
95                                                         }
96                                                         break;
97                                                 }
98                                         }
99                                 }
100                         }
101                         this.burnTime = 0;
102                 }
103
104                 boolean var1 = false;
105                 if (this.heat > MIN_HEAT) {
106                         Iterator<ItemStack> itMaterial = recipeList.keySet().iterator();
107                         while (itMaterial.hasNext()) {
108                                 ItemStack itemstackMaterial = itMaterial.next();
109                                 ItemStack materialItem = this.invm.getStackInSlot(0);
110                                 if (materialItem != null) {
111                                         if (this.isInItemCheck(itemstackMaterial)) {
112                                                 if (itemstackMaterial.itemID == materialItem.itemID && itemstackMaterial.getItemDamage() == materialItem.getItemDamage()) {
113                                                         var1 = true;
114                                                         if (this.fuelRestTime <= 0) {
115                                                                 Iterator<ItemStack> itFuel = ChemiCraftAPI.instance().getChemicalCellsList().keySet().iterator();
116                                                                 while (itFuel.hasNext()) {
117                                                                         ItemStack itemstackFuel = itFuel.next();
118                                                                         ItemStack fuelItem = this.invf.getStackInSlot(0);
119                                                                         if (fuelItem != null) {
120                                                                                 if (itemstackFuel.itemID == fuelItem.itemID && itemstackFuel.getItemDamage() == fuelItem.getItemDamage()) {
121                                                                                         this.fuelRestTime = ChemiCraftAPI.instance().getChemicalCellsList().get(itemstackFuel);
122                                                                                         if (fuelItem.stackSize > 1) {
123                                                                                                 this.invf.setInventorySlotContents(0, new ItemStack(fuelItem.itemID, --fuelItem.stackSize, fuelItem.getItemDamage()));
124                                                                                                 break;
125                                                                                         } else {
126                                                                                                 this.invf.setInventorySlotContents(0, null);
127                                                                                                 break;
128                                                                                         }
129                                                                                 }
130                                                                         }
131                                                                 }
132                                                         }
133                                                 }
134                                         }
135                                 } else {
136                                         this.burnTime = 0;
137                                 }
138                         }
139                         if (!var1) {
140                                 this.burnTime = 0;
141                         }
142                 }
143         }
144
145         @Override
146         public void readFromNBT(NBTTagCompound par1) {
147                 super.readFromNBT(par1);
148                 this.heat = par1.getFloat("Heat");
149                 this.burnTime = par1.getFloat("BurnTime");
150                 this.fuelRestTime = par1.getFloat("FuelRestTime");
151
152                 NBTTagList var2 = par1.getTagList("Items");
153                 for (int var3 = 0; var3 < var2.tagCount(); ++var3)
154                 {
155                         NBTTagCompound var4 = (NBTTagCompound)var2.tagAt(var3);
156                         int var5 = var4.getByte("Slot") & 255;
157
158                         if  (var5 >= 0 && var5 < this.invm.getSizeInventory())
159                         {
160                                 this.invm.setInventorySlotContents(var5, ItemStack.loadItemStackFromNBT(var4));
161                         }
162                 }
163
164                 NBTTagList var6 = par1.getTagList("Items2");
165                 for (int var3 = 0; var3 < var6.tagCount(); ++var3)
166                 {
167                         NBTTagCompound var7 = (NBTTagCompound)var6.tagAt(var3);
168                         int var8 = var7.getByte("Slot2") & 255;
169
170                         if  (var8 >= 0 && var8 < this.invr.getSizeInventory())
171                         {
172                                 this.invr.setInventorySlotContents(var8, ItemStack.loadItemStackFromNBT(var7));
173                         }
174                 }
175
176                 NBTTagList var9 = par1.getTagList("Items3");
177                 for (int var3 = 0; var3 < var9.tagCount(); ++var3)
178                 {
179                         NBTTagCompound var10 = (NBTTagCompound)var9.tagAt(var3);
180                         int var11 = var10.getByte("Slot3") & 255;
181
182                         if  (var11 >= 0 && var11 < this.invf.getSizeInventory())
183                         {
184                                 this.invf.setInventorySlotContents(var11, ItemStack.loadItemStackFromNBT(var10));
185                         }
186                 }
187         }
188
189         @Override
190         public void writeToNBT(NBTTagCompound par1) {
191                 super.writeToNBT(par1);
192                 par1.setFloat("Heat", this.heat);
193                 par1.setFloat("BurnTime", this.burnTime);
194                 par1.setFloat("FuelRestTime", this.fuelRestTime);
195
196                 NBTTagList var2 = new NBTTagList();
197                 for (int var3 = 0; var3 < this.invm.getSizeInventory(); ++var3)
198                 {
199                         if  (this.invm.getStackInSlot(var3) != null)
200                         {
201                                 NBTTagCompound var4 = new NBTTagCompound();
202                                 var4.setByte("Slot", (byte)var3);
203                                 this.invm.getStackInSlot(var3).writeToNBT(var4);
204                                 var2.appendTag(var4);
205                         }
206                 }
207                 par1.setTag("Items", var2);
208
209                 NBTTagList var5 = new NBTTagList();
210                 for (int var6 = 0; var6 < this.invr.getSizeInventory(); ++var6)
211                 {
212                         if  (this.invr.getStackInSlot(var6) != null)
213                         {
214                                 NBTTagCompound var7 = new NBTTagCompound();
215                                 var7.setByte("Slot2", (byte)var6);
216                                 this.invr.getStackInSlot(var6).writeToNBT(var7);
217                                 var5.appendTag(var7);
218                         }
219                 }
220                 par1.setTag("Items2", var5);
221
222                 NBTTagList var8 = new NBTTagList();
223                 for (int var9 = 0; var9 < this.invf.getSizeInventory(); ++var9)
224                 {
225                         if  (this.invf.getStackInSlot(var9) != null)
226                         {
227                                 NBTTagCompound var10 = new NBTTagCompound();
228                                 var10.setByte("Slot3", (byte)var9);
229                                 this.invf.getStackInSlot(var9).writeToNBT(var10);
230                                 var8.appendTag(var10);
231                         }
232                 }
233                 par1.setTag("Items3", var8);
234         }
235
236         @Override
237         public Packet getDescriptionPacket() {
238                 return PacketHandler.getPacket(this);
239         }
240
241         public void readPacket(ByteArrayDataInput data) {
242                 try {
243                         this.heat = data.readFloat();
244                         this.burnTime = data.readInt();
245                         this.fuelRestTime = data.readFloat();
246                         for(int i = 0;i < this.invm.getSizeInventory();i++){
247                                 int id = data.readInt();
248                                 int size = data.readByte();
249                                 int damage = data.readInt();
250                                 if (id != 0 && size != 0){
251                                         this.invm.setInventorySlotContents(i, new ItemStack(id, size, damage));
252                                 }else{
253                                         this.invm.setInventorySlotContents(i, null);
254                                 }
255                         }
256
257                         for(int i = 0;i < this.invr.getSizeInventory();i++){
258                                 int id = data.readInt();
259                                 int size = data.readByte();
260                                 int damage = data.readInt();
261                                 if (id != 0 && size != 0){
262                                         this.invr.setInventorySlotContents(i, new ItemStack(id, size, damage));
263                                 }else{
264                                         this.invr.setInventorySlotContents(i, null);
265                                 }
266                         }
267
268                         for(int i = 0;i < this.invf.getSizeInventory();i++){
269                                 int id = data.readInt();
270                                 int size = data.readByte();
271                                 int damage = data.readInt();
272                                 if (id != 0 && size != 0){
273                                         this.invf.setInventorySlotContents(i, new ItemStack(id, size, damage));
274                                 }else{
275                                         this.invf.setInventorySlotContents(i, null);
276                                 }
277                         }
278                 } catch (Exception e) {
279                         e.printStackTrace();
280                 }
281         }
282
283
284         public void writePacket(DataOutputStream dos){
285                 try {
286                         dos.writeFloat(this.heat);
287                         dos.writeFloat(this.burnTime);
288                         dos.writeFloat(this.fuelRestTime);
289                         for(int i = 0;i < this.invm.getSizeInventory();i++){
290                                 int id = 0;
291                                 int size = 0;
292                                 int damage  = 0;
293                                 ItemStack itemstack = this.invm.getStackInSlot(i);
294                                 if (itemstack != null){
295                                         id = itemstack.itemID;
296                                         size = itemstack.stackSize;
297                                         damage = itemstack.getItemDamage();
298                                         dos.writeInt(id);
299                                         dos.writeByte(size);
300                                         dos.writeInt(damage);
301                                 }else{
302                                         dos.writeInt(0);
303                                         dos.writeByte(0);
304                                         dos.writeInt(0);
305                                 }
306                         }
307
308                         for(int i = 0;i < this.invr.getSizeInventory();i++){
309                                 int id = 0;
310                                 int size = 0;
311                                 int damage  = 0;
312                                 ItemStack itemstack = this.invr.getStackInSlot(i);
313                                 if (itemstack != null){
314                                         id = itemstack.itemID;
315                                         size = itemstack.stackSize;
316                                         damage = itemstack.getItemDamage();
317                                         dos.writeInt(id);
318                                         dos.writeByte(size);
319                                         dos.writeInt(damage);
320                                 }else{
321                                         dos.writeInt(0);
322                                         dos.writeByte(0);
323                                         dos.writeInt(0);
324                                 }
325                         }
326
327                         for(int i = 0;i < this.invf.getSizeInventory();i++){
328                                 int id = 0;
329                                 int size = 0;
330                                 int damage  = 0;
331                                 ItemStack itemstack = this.invf.getStackInSlot(i);
332                                 if (itemstack != null){
333                                         id = itemstack.itemID;
334                                         size = itemstack.stackSize;
335                                         damage = itemstack.getItemDamage();
336                                         dos.writeInt(id);
337                                         dos.writeByte(size);
338                                         dos.writeInt(damage);
339                                 }else{
340                                         dos.writeInt(0);
341                                         dos.writeByte(0);
342                                         dos.writeInt(0);
343                                 }
344                         }
345                 } catch (Exception e) {
346                         e.printStackTrace();
347                 }
348         }
349
350         public float getBurnTime() {
351                 return this.burnTime;
352         }
353
354         public float getHeat() {
355                 return this.heat;
356         }
357
358         public InventoryElectrolysisMaterial getInvMaterial(){
359                 return this.invm;
360         }
361
362         public InventoryElectrolysisResult getInvResult(){
363                 return this.invr;
364         }
365
366         public InventoryElectrolysisFuel getInvFuel(){
367                 return this.invf;
368         }
369
370         public boolean isInItemCheck(ItemStack key) {
371                 ItemStack[] results = recipeList.get(key).clone();
372                 ItemStack[] containerResults = new ItemStack[this.invr.getSizeInventory()];
373                 for (int j = 0; j < this.invr.getSizeInventory(); j++) {
374                         containerResults[j] = this.invr.getStackInSlot(j);
375                 }
376
377                 for (int i = 0; i < results.length; i++) {
378                         int var1 = results[i].itemID;
379                         int var3 = results[i].getItemDamage();
380                         int var5 = results[i].stackSize;
381                         for (int j = 0; j < containerResults.length; j++) {
382                                 if (containerResults[j] == null) {
383                                         containerResults[j] = results[i];
384                                         results[i] = null;
385                                         break;
386                                 } else {
387                                         int var2 = containerResults[j].itemID;
388                                         int var4 = containerResults[j].getItemDamage();
389                                         int var6 = containerResults[j].stackSize;
390                                         int var7 = containerResults[j].getMaxStackSize();
391                                         if (var1 == var2 && var3 == var4) {
392                                                 if (var5 + var6 <= var7) {
393                                                         containerResults[j] = results[i];
394                                                         results[i] = null;
395                                                         break;
396                                                 } else {
397                                                         var5 -= var7 - var6;
398                                                 }
399                                         }
400                                 }
401                         }
402                 }
403                 for (int i = 0; i < results.length; i++) {
404                         if (results[i] != null) {
405                                 return false;
406                         }
407                 }
408                 return true;
409         }
410
411         public void inItem(ItemStack key) {
412                 ItemStack[] results = recipeList.get(key);
413
414                 for (int i = 0; i < results.length; i++) {
415                         ItemStack[] containerResults = new ItemStack[this.invr.getSizeInventory()];
416                         for (int j = 0; j < this.invr.getSizeInventory(); j++) {
417                                 containerResults[j] = this.invr.getStackInSlot(j);
418                         }
419                         int var1 = results[i].itemID;
420                         int var3 = results[i].getItemDamage();
421                         int var5 = results[i].stackSize;
422                         for (int j = 0; j < containerResults.length; j++) {
423                                 if (containerResults[j] == null) {
424                                         this.invr.setInventorySlotContents(j, results[i]);
425                                         break;
426                                 } else {
427                                         int var2 = containerResults[j].itemID;
428                                         int var4 = containerResults[j].getItemDamage();
429                                         int var6 = containerResults[j].stackSize;
430                                         int var7 = containerResults[j].getMaxStackSize();
431                                         if (var1 == var2 && var3 == var4) {
432                                                 if (var5 + var6 <= var7) {
433                                                         this.invr.setInventorySlotContents(j, new ItemStack(results[i].itemID, var5 + var6, results[i].getItemDamage()));
434                                                         break;
435                                                 } else {
436                                                         this.invr.setInventorySlotContents(j, new ItemStack(results[i].itemID, var7, results[i].getItemDamage()));
437                                                         var5 -= var7 - var6;
438                                                 }
439                                         }
440                                 }
441                         }
442                 }
443         }
444
445         public static void addRecipe(ItemStack material, ItemStack[] result) {
446                 recipeList.put(material, result);
447         }
448
449 }