OSDN Git Service

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