OSDN Git Service

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