OSDN Git Service

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