summaryrefslogtreecommitdiff
path: root/ihl/flexible_cable/BatterySwitchUnitTileEntity.java
blob: f4fc6c1e801fbae48b73d6bb31cdf7300073fb35 (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
package ihl.flexible_cable;

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

import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySource;
import ic2.api.network.INetworkClientTileEntityEventListener;
import ic2.core.ExplosionIC2;
import ic2.core.IC2;
import ihl.utils.IHLUtils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;

public class BatterySwitchUnitTileEntity extends FlexibleCableHolderBaseTileEntity implements INetworkClientTileEntityEventListener, IEnergySource{

	public short progress;
	protected short operationLength=200;
	public boolean isGuiScreenOpened=false;
	protected final double energyConsume=128D;
    public double energy;
    public int maxStorage=65536;
	private boolean addedToEnergyNet=false;
	public byte mode = 0;
	private Set<BatterySwitchUnitTileEntity> batteryChain = new HashSet<BatterySwitchUnitTileEntity>();
	private BatterySwitchUnitTileEntity batteryChainMaster;
	private boolean chargingMode=true;
	
	public BatterySwitchUnitTileEntity()
	{
		super();
	}
	
    @Override
	public void onLoaded()
    {
        super.onLoaded();
        if (IC2.platform.isSimulating()&&!this.addedToEnergyNet)
        {
            MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
            this.addedToEnergyNet = true;
        }
    }
    
    @Override
	public void onUnloaded()
    {
        if (IC2.platform.isSimulating()&&this.addedToEnergyNet)
        {
            MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
            this.addedToEnergyNet = false;
        }
        super.onUnloaded();
    }
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer player)
	{
		return IHLUtils.getThisModItemStack("batterySwitchUnit");
	}
	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) 
	{
		return this.getFacing()!=(short)side;
	}
	

	@Override
	public void writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        nbt.setShort("progress", this.progress);
        nbt.setDouble("energy", this.energy);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        this.progress = nbt.getShort("progress");
        this.energy=nbt.getDouble("energy");
	}
	
	@Override
	public void onNetworkEvent(EntityPlayer player, int event) 
	{
		switch(event)
		{
		case 0:
			this.isGuiScreenOpened=false;
			break;
		}	
	}
	
    @Override
	public void updateEntityServer()
    {
    	{
            if(this.chargingMode && this.gridID!=-1 && this.energy<this.maxStorage && !this.batteryChain.isEmpty())
            {
            	if(this.getGrid().energy>0D)
            	{
            		if(this.getGrid().getSinkVoltage(this)/this.batteryChain.size()>410D)
            		{
                		this.energy+=energyConsume;
                		this.getGrid().drawEnergy(energyConsume, this);
                		if(this.getGrid().getSinkVoltage(this)/this.batteryChain.size()>500D)
                		{
                			this.createChainOfExplosions();
                		}
            		}
            	}
            }
            if(this.chargingMode && this.batteryChainMaster!=null && this.gridID==-1)
            {
                if(this.batteryChainMaster.gridID!=-1 && this.batteryChainMaster.energy>0D && this.energy<this.maxStorage && !this.batteryChainMaster.batteryChain.isEmpty())
                {
                	if(this.batteryChainMaster.getGrid().getSinkVoltage(this.batteryChainMaster)/this.batteryChainMaster.batteryChain.size()>410D)
                	{
                    	this.energy+=energyConsume;
                    	this.batteryChainMaster.drawEnergy(energyConsume);
                	}
                }
            }
            if(this.chargingMode && this.gridID!=-1 && MinecraftServer.getServer().getTickCounter() % 40==0)
            {
            	this.checkBatteryChain();
            }
        	if(this.energy>=this.maxStorage)
        	{
        		this.chargingMode=false;
        	}
            if(!this.chargingMode)
            {
            	this.energy-=0.01D;
            	if(this.energy<energyConsume)
            	{
            		this.chargingMode=true;
            	}
            }
    	}
    }
	
	private void createChainOfExplosions() 
	{
		Iterator<BatterySwitchUnitTileEntity> batteryChainIterator = this.batteryChain.iterator(); 
		while(batteryChainIterator.hasNext())
		{
			BatterySwitchUnitTileEntity bsu = batteryChainIterator.next();
	        ExplosionIC2 explosion = new ExplosionIC2(bsu.worldObj, null, bsu.xCoord+0.5D, bsu.yCoord+0.5D, bsu.zCoord+0.5D, 2F, 0.3F, ExplosionIC2.Type.Normal, null, 0);
	        explosion.doExplosion();
		}
	}

	private boolean checkBatteryChain() 
	{
		boolean allright=true;
		if(!this.batteryChain.isEmpty())
		{
			Iterator<BatterySwitchUnitTileEntity> sectionsIterator = this.batteryChain.iterator();
			while(sectionsIterator.hasNext())
			{
				BatterySwitchUnitTileEntity section = sectionsIterator.next();
				if(section==null || section.isInvalid())
				{
					allright=false;
				}
			}
		}
		else
		{
			allright=false;
		}
		if(allright)
		{
			return true;
		}
		else
		{
			this.batteryChain.clear();
			boolean checking = true;
			int x=xCoord;
			int z=zCoord;
			List<Integer> xs = new ArrayList<Integer>();
			List<Integer> zs = new ArrayList<Integer>();
			xs.add(xCoord);
			zs.add(zCoord);
			this.batteryChain.add(this);
			while(checking)
			{
				if(!xs.isEmpty() && !zs.isEmpty())
				{
					x=xs.remove(0);
					z=zs.remove(0);
				}
				else
				{
					checking=false;
					break;
				}
				int[] xz = new int[] {0,1,0,-1,0};
				for(int i=0;i<xz.length-1;i++)
				{
					TileEntity te = worldObj.getTileEntity(x+xz[i], yCoord, z+xz[i+1]);
					if(te instanceof BatterySwitchUnitTileEntity)
					{
						BatterySwitchUnitTileEntity section = (BatterySwitchUnitTileEntity)te;
						if(this.batteryChain.add(section))
						{
							xs.add(section.xCoord);
							zs.add(section.zCoord);
							section.batteryChain.add(this);
							section.batteryChainMaster=this;
						}
					}
				}
			}
			return true;
		}
	}

	@Override
	public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction) {
		return true;
	}

	@Override
	public double getOfferedEnergy() 
	{
		return this.chargingMode? 0 : this.energy;
	}

	@Override
	public void drawEnergy(double amount) 
	{
		this.energy-=amount;
	}
	
	@Override
	public double getEnergyAmountThisNodeWant()
	{
		return this.energy-this.maxStorage;
	}
	
	public double drawEnergyToGrid(double amount)
	{
		return 0d;
	}
	
	public double injectEnergy(double amount)
	{
		this.energy+=amount;
		return 0d;
	}

	@Override
	public int getSourceTier() {
		return 4;
	}
	
	@Override
	public double getMaxAllowableVoltage() 
	{
		return 64000D;
	}
	
	@Override
    public boolean shouldRenderInPass(int pass)
    {
        return pass==0;
    }

	@Override
	public String getInventoryName() {
		return null;
	}

	@Override
	public void injectEnergyInThisNode(double amount, double voltage) 
	{
		this.energy+=amount;
		if(voltage/this.batteryChain.size()>500D)
		{
			this.createChainOfExplosions();
		}
	}

}