OSDN Git Service

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