OSDN Git Service

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