OSDN Git Service

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