summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/processing/chemistry/ElectricEvaporatorTileEntity.java
blob: cdf35245c701ba9ef21ebefac450f54b472e9ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package ihl.processing.chemistry;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.item.IC2Items;
import ic2.api.network.INetworkTileEntityEventListener;
import ic2.core.ContainerBase;
import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.audio.AudioSource;
import ic2.core.block.invslot.InvSlot;
import ic2.core.block.invslot.InvSlot.Access;
import ic2.core.block.invslot.InvSlotUpgrade;
import ic2.core.upgrade.IUpgradableBlock;
import ic2.core.upgrade.IUpgradeItem;
import ic2.core.upgrade.UpgradableProperty;
import ihl.IHLMod;
import ihl.utils.IHLInvSlotDischarge;
import ihl.utils.IHLUtils;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;

public class ElectricEvaporatorTileEntity extends EvaporatorTileEntity implements IEnergySink, IUpgradableBlock, INetworkTileEntityEventListener
{
    public final InvSlotUpgrade upgradeSlot;
    private int tier;
    public int maxStorage;
    private int defaultMaxStorage;
    private double energy;
    public final int defaultEnergyConsume;
    public final int defaultOperationLength;
    public final int defaultTier;
    public int energyConsume;
    public AudioSource audioSource;
	private int updateChecksum=0;
	private boolean addedToEnergyNet=false;

	public ElectricEvaporatorTileEntity()
    {
    	super();
        this.defaultEnergyConsume = this.energyConsume = 8;
        this.defaultOperationLength = this.maxProgress = 400;
        this.energy=0D;
        this.tier = this.defaultTier = 1;
        this.maxStorage = this.defaultMaxStorage = defaultEnergyConsume * defaultOperationLength;
    	this.fuelSlot = new IHLInvSlotDischarge(this, 1, Access.IO, this.tier, InvSlot.InvSide.BOTTOM);
        this.upgradeSlot = new InvSlotUpgrade(this, "upgrade", 4, 4);
    }
    
    @Override
	public void onLoaded()
    {
        super.onLoaded();
        if (IC2.platform.isSimulating()&&!this.addedToEnergyNet)
        {
            MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
            this.addedToEnergyNet = true;
        }
    }
    
	@Override
    public List<String> getNetworkedFields()
    {
		List<String> fields = super.getNetworkedFields();
		fields.add("tier");
		fields.add("maxStorage");
		return fields;
    }
    
	public void setOverclockRates()
    {
		int speedUp=0;
		int tierUp=0;
		int capacityUp=0;
		int checksum=0;
		for(int i=0;i<this.upgradeSlot.size();i++)
		{
			if(this.upgradeSlot.get(i)!=null)
			{
				if(this.upgradeSlot.get(i).getItemDamage()==IC2Items.getItem("overclockerUpgrade").getItemDamage())
				{
					speedUp+=this.upgradeSlot.get(i).stackSize;
				}
				if(this.upgradeSlot.get(i).getItemDamage()==IC2Items.getItem("transformerUpgrade").getItemDamage())
				{
					tierUp+=this.upgradeSlot.get(i).stackSize;
				}
				if(this.upgradeSlot.get(i).getItemDamage()==IC2Items.getItem("energyStorageUpgrade").getItemDamage())
				{
					capacityUp+=this.upgradeSlot.get(i).stackSize;
				}
			}
		}
		checksum=speedUp*4096+tierUp*64+capacityUp;
		if(this.updateChecksum!=checksum)
		{
			this.maxProgress=(short)Math.max(this.defaultOperationLength*Math.pow(0.7D, speedUp),2D);
			this.maxStorage=this.defaultMaxStorage + capacityUp*10000;
	        IC2.network.get().updateTileEntityField(this, "maxStorage");
			this.energyConsume=(int) Math.min(this.defaultEnergyConsume*Math.pow(1.6D, speedUp),this.maxStorage);
			this.tier=this.defaultTier+tierUp;
			IC2.network.get().updateTileEntityField(this, "tier");
	        this.updateChecksum=checksum;
		};
    }
	
	@Override
	public void onUnloaded()
    {
        super.onUnloaded();

        if (IC2.platform.isRendering() && this.audioSource != null)
        {
            IC2.audioManager.removeSources(this);
            this.audioSource = null;
        }
        
        if (IC2.platform.isSimulating()&&this.addedToEnergyNet)
        {
            MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
            this.addedToEnergyNet = false;
        }
    }
    
    @Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
        this.energy=nbttagcompound.getDouble("energy");
    }

    @Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setDouble("energy", this.energy);
    }
    
	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
		return this.getFacing()!=(short)side && side!=0 && side!=1;
	}
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
		if(this.outputSlot.get()!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.outputSlot.get()));
		if(this.fuelSlot.get()!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.fuelSlot.get()));
		if(this.emptyFluidItemsSlot.get()!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.emptyFluidItemsSlot.get()));
		if(this.upgradeSlot.get(0)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.upgradeSlot.get(0)));
		if(this.upgradeSlot.get(1)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.upgradeSlot.get(1)));
		if(this.upgradeSlot.get(2)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.upgradeSlot.get(2)));
		if(this.upgradeSlot.get(3)!=null)this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.xCoord, this.yCoord+1, this.zCoord, this.upgradeSlot.get(3)));
		return new ItemStack(IHLMod.electricEvaporatorBlock,1);
	}
    
    public int getGUIEnergy(int i)
    {
    	if(this.energy<Float.MAX_VALUE)
    	{
    		return Math.round((float)this.energy/this.maxStorage*i);
    	}
    	else
    	{
    		return Math.round((float)(this.energy/this.maxStorage)*i);
    	}
    }

    @Override
	public boolean enableUpdateEntity()
    {
        return true;
    }
    @Override
	public void updateEntityClient()
    {
    if(((IHLInvSlotDischarge)this.fuelSlot).tier!=this.tier)
    {
    	((IHLInvSlotDischarge)this.fuelSlot).tier=this.tier;
    }
    }
    @Override
	public void updateEntityServer()
    {
        if(((IHLInvSlotDischarge)this.fuelSlot).tier!=this.tier)
        {
        	((IHLInvSlotDischarge)this.fuelSlot).tier=this.tier;
        }
    	if(IC2.platform.isSimulating())
    	{
        this.setOverclockRates();

    	if(this.getDemandedEnergy() > 1.0D)
    	{
    		double amount = ((IHLInvSlotDischarge)this.fuelSlot).discharge(this.getDemandedEnergy(), false);
    		this.energy += amount;
    	}
        if (this.needsFluid())
        {
       		IHLUtils.handleFluidSlotsBehaviour(null, fluidItemsSlot, emptyFluidItemsSlot, fluidTank);
        }
        
        if (this.canOperate() && this.energy >= this.energyConsume)
        {
            this.setActive(true);

            if (this.progress == 0)
            {
                IC2.network.get().initiateTileEntityEvent(this, 0, true);
            }
            ++this.progress;
            this.energy -= this.energyConsume;
            if (this.progress >= this.maxProgress)
            {
                this.operate();
                this.progress = 0;
                IC2.network.get().initiateTileEntityEvent(this, 2, true);
            }
        }
        else
        {
            if (this.progress != 0 && this.getActive())
            {
                IC2.network.get().initiateTileEntityEvent(this, 1, true);
            }
            if (!this.canOperate())
            {
                this.progress = 0;
            }
            this.setActive(false);
        }
        for (int i = 0; i < this.upgradeSlot.size(); ++i)
        {
            ItemStack stack = this.upgradeSlot.get(i);

            if (stack != null && stack.getItem() instanceof IUpgradeItem)
            {
            	((IUpgradeItem)stack.getItem()).onTick(stack, this);
            }
        }
    	}
    }
    
    @Override
	public void onNetworkEvent(int event)
    {
        if (this.audioSource == null && this.getStartSoundFile() != null)
        {
            this.audioSource = IC2.audioManager.createSource(this, this.getStartSoundFile());
        }

        switch (event)
        {
            case 0:
                if (this.audioSource != null)
                {
                    this.audioSource.play();
                }

                break;

            case 1:
                if (this.audioSource != null)
                {
                    this.audioSource.stop();
                }

                break;

            case 2:
                if (this.audioSource != null)
                {
                    this.audioSource.stop();
                }
                
        }
    }

    public String getStartSoundFile()
    {
        return "Machines/Electro Furnace/ElectroFurnaceLoop.ogg";
    }

    /**
     * Returns the name of the inventory
     */
    @Override
	public String getInventoryName()
    {
        return "Electric evaporator";
    }

    @Override
	public ContainerBase<ElectricEvaporatorTileEntity> getGuiContainer(EntityPlayer entityPlayer)
    {
        return new ElectricEvaporatorContainer(entityPlayer, this);
    }
    
    @Override
	@SideOnly(Side.CLIENT)
    public GuiScreen getGui(EntityPlayer entityPlayer, boolean isAdmin)
    {
        return new ElectricEvaporatorGui(new ElectricEvaporatorContainer(entityPlayer, this));
    }

    @Override
    public boolean getGui(EntityPlayer player)
    {
			return this instanceof IHasGui ? (IC2.platform.isSimulating() ? IC2.platform.launchGui(player, this) : true) : false;
    }
    
    @Override
	public int mX()
	{
		switch(this.getFacing())
		{
		case 4:
		return -1;
		case 5:
		return 1;
		default:
		return 0;
		}
	}
    
    @Override
	public int mZ()
	{
		switch(this.getFacing())
		{
		case 2:
		return -1;
		case 3:
		return 1;
		default:
		return 0;
		}
	}
    
	@Override
	public double getEnergy() 
	{
		return this.energy;
	}

	@Override
	public boolean useEnergy(double amount) {
        if (this.energy >= amount)
        {
            this.energy -= amount;
            return true;
        }
        else
        {
            return false;
        }
	}

	public void setEnergy(double i) {
		this.energy=i;
	}

	@Override
	public double getDemandedEnergy()
	{
		 return this.maxStorage - this.energy;
	}
	
	@Override
	public int getSinkTier()
	{
		return this.tier;
	}
	
	@Override
	public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage)
	{
        if (this.energy  >= this.maxStorage)
        {
            return amount;
        }
        else
        {
            this.energy += amount;
            return 0.0D;
        }
	}

	@Override
	public boolean acceptsEnergyFrom(TileEntity emitter,
			ForgeDirection direction) {
		return true;
	}

	public ItemStack getOutput(int arg0) 
	{
		return this.outputSlot.get(arg0);
	}

	public int getOutputSize() {
		return this.outputSlot.size();
	}

	public void setOutput(int arg0, ItemStack arg1) 
	{
		this.outputSlot.put(arg0, arg1);
	}
	
	@Override
	public Set<UpgradableProperty> getUpgradableProperties() 
	{
		Set<UpgradableProperty> properties = new HashSet<UpgradableProperty>();
		properties.add(UpgradableProperty.ItemProducing);
		properties.add(UpgradableProperty.EnergyStorage);
		properties.add(UpgradableProperty.Transformer);
		return properties;
	}
}