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

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

import ic2.core.block.TileEntityInventory;
import ihl.IHLMod;
import ihl.flexible_cable.IHLGrid;
import ihl.flexible_cable.NodeEntity;
import ihl.interfaces.IEnergyNetNode;
import ihl.utils.IHLUtils;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.AxisAlignedBB;

public abstract class FlexibleCableHolderBaseTileEntity extends TileEntityInventory implements IEnergyNetNode{

	protected double connectionX;
	protected double connectionY;
	protected double connectionZ;
	protected int gridID=-1;
	protected final Set<NBTTagCompound> cableList;
	public boolean checkCables=true;

	public FlexibleCableHolderBaseTileEntity()
	{
		super();
		cableList=new HashSet<NBTTagCompound>();
	}
	
    @Override
	public void onLoaded()
    {
        super.onLoaded();
        if(gridID!=-1)
        {
        	IHLGrid grid = IHLMod.enet.getGrid(gridID);
        	grid.add(this);
        }
    }
    
	@SuppressWarnings("unchecked")
	@Override
	public void setFacing(short facing1)
	{
		short facing2 = (short) Math.max(facing1, 2);
		double range = 2D;
		AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(connectionX-range,connectionY-range,connectionZ-range,connectionX+range,connectionY+range,connectionZ+range);
		List<NodeEntity> nodeList = worldObj.getEntitiesWithinAABB(NodeEntity.class, searchArea);
		super.setFacing(facing2);
		setConnectionX(this.xCoord+0.5D);
		setConnectionY(this.yCoord+1.5D);
		setConnectionZ(this.zCoord+0.5D);
		if(!nodeList.isEmpty())
		{
			Iterator<NodeEntity> ei = nodeList.iterator();
        	while(ei.hasNext())
        	{
        		NodeEntity ne=(NodeEntity) ei.next();
        		if((ne.prevAnchorEntity==null||ne.nextAnchorEntity==null) && this.cableListContains(ne.getChainUniqueID()))
        		{
        			ne.setVirtualNodePos(connectionX, connectionY, connectionZ);
        		}
        	}
		}
	}
	
	protected boolean cableListContains(int chainUniqueID) {
		Iterator<NBTTagCompound> cli = this.getCableList().iterator();
		while(cli.hasNext())
		{
			NBTTagCompound c = cli.next();
			if(c.getInteger("chainUID")==chainUniqueID)
			{
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) 
	{
		return this.getFacing()!=(short)side;
	}
	

	@Override
	public void writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        NBTTagList cableNBTList = new NBTTagList();
        for(NBTTagCompound cable:this.cableList)
        {
        	cableNBTList.appendTag(cable);
        }
        nbt.setTag("cableList", cableNBTList);
        nbt.setDouble("connectionX", this.connectionX);
        nbt.setDouble("connectionY", this.connectionY);
        nbt.setDouble("connectionZ", this.connectionZ);
        nbt.setInteger("gridID", this.gridID);
        nbt.setBoolean("checkCables", this.checkCables);
	}

	@Override
	public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        NBTTagList cableNBTList=nbt.getTagList("cableList", 10);
        for(int i=0;i<cableNBTList.tagCount();i++)
        {
            this.cableList.add(cableNBTList.getCompoundTagAt(i));
        }
        this.setConnectionX(nbt.getDouble("connectionX"));
        this.setConnectionY(nbt.getDouble("connectionY"));
        this.setConnectionZ(nbt.getDouble("connectionZ"));
        this.gridID=nbt.getInteger("gridID");
		this.checkCables=nbt.getBoolean("checkCables");
	}
	
	@Override
	public void removeAttachedChains()
	{
		IHLUtils.removeChains(this,this.worldObj);
	}

	public void setConnectionX(double connectionX) {
		this.connectionX = connectionX;
	}

	public void setConnectionY(double connectionY) {
		this.connectionY = connectionY;
	}

	public void setConnectionZ(double connectionZ) {
		this.connectionZ = connectionZ;
	}
	
	@Override
	public double[] getPortPos(EntityLivingBase player) 
	{
		return new double[] {this.connectionX,this.connectionY,this.connectionZ};
	}

	@Override
	public boolean addCable(NBTTagCompound cable) 
	{
		return this.cableList.add(cable);
	}

	@Override
	public Set<NBTTagCompound> getCableList() {
		return cableList;
	}
	
	@Override
	public void setGrid(int newGridID)
	{
		if(newGridID!=-1)
		{
			this.gridID=newGridID;
			IHLMod.enet.getGrid(newGridID).add(this);
		}
		else
		{
			this.gridID=-1;
		}
	}
	
	@Override
	public int getGridID()
	{
		return this.gridID;
	}
	
	@Override
	public IHLGrid getGrid()
	{
		return IHLMod.enet.getGrid(gridID);
	}
	
	@Override
    public boolean shouldRenderInPass(int pass)
    {
        return pass==0;
    }
	
	@Override
	public void remove(NBTTagCompound cable) 
	{
		if(this.cableList.remove(cable))
		{
			IHLUtils.removeChain(cable, this);
		}
	}
	

	@Override
	public boolean isCableRemoved(int chainUniqueID) 
	{
		if(!checkCables)
		{
			return false;
		}
		for(NBTTagCompound cable:this.cableList)
		{
			if(cable.getInteger("chainUID")==chainUniqueID)
			{
				return false;
			}
		}
		return true;
	}
	
	@Override
	public void setCableCheck(boolean b) 
	{
		this.checkCables=b;
	}
}