summaryrefslogtreecommitdiff
path: root/ihl/flexible_cable/AnchorBlock.java
blob: 9d9c1dcf95217b294eaac921b27cb3fab764f5fb (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
package ihl.flexible_cable;
import ihl.IHLModInfo;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class AnchorBlock extends Block implements ITileEntityProvider {

	private IIcon blockIconSide;
	
	public AnchorBlock(String unlocalizedName1) 
	{
		super(Material.circuits);
		this.setBlockName(unlocalizedName1);
    	GameRegistry.registerBlock(this, unlocalizedName1);
	}
	
	@Override
	public boolean hasTileEntity(int metadata)
	{
	    return true;
	}
	
	@Override
	public void onBlockPreDestroy(World world, int x, int y, int z, int meta)
	{
		if(!world.isRemote)
		{
			TileEntity te = world.getTileEntity(x, y, z);
			if(te!=null && te instanceof AnchorTileEntity)
			{
				AnchorTileEntity ate = (AnchorTileEntity) te;
				ate.invalidate();
				for(short i=0;i<6;i++)
				{
					ate.energyNetNodes[i].removeAttachedChains();
				}
			}
		}
		super.onBlockPreDestroy(world, x, y, z, meta);
	}
		
	@Override
	public TileEntity createNewTileEntity(World arg0, int arg1) {
		return new AnchorTileEntity();
	}
	
	@Override
	public void	dropBlockAsItemWithChance(World world, int x, int y, int z, int meta, float chance, int flag){}
	
    @Override
	public AxisAlignedBB getCollisionBoundingBoxFromPool(World p_149668_1_, int p_149668_2_, int p_149668_3_, int p_149668_4_)
    {
        return null;
    }
	
    @Override
	public boolean isOpaqueCube()
    {
        return false;
    }

    @Override
	public boolean renderAsNormalBlock()
    {
        return false;
    }

    @Override
	public int getRenderType()
	{
		return -2;
	}

	@Override
	@SideOnly(Side.CLIENT)
	public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side) 
	{
		int facing=3;
		int mask[] = {
				0,1,2,3,4,5,
				1,0,3,2,4,5,
				3,2,0,1,4,5,
				2,3,1,0,4,5,
				2,3,5,4,0,1,
				2,3,4,5,1,0
				};
		TileEntity te = world.getTileEntity(x, y, z);
		if(te!=null)
		{
			AnchorTileEntity tebh = (AnchorTileEntity) te;
			facing=tebh.getFacing();
		}

		switch (mask[facing*6+side])
		{
		case 0:
			return this.blockIcon;
		case 1:
			return this.blockIconSide;
		case 2:
			return this.blockIconSide;
		case 3:
			return this.blockIconSide;
		case 4:
			return this.blockIconSide;
		case 5:
			return this.blockIconSide;
		default:
			return this.blockIconSide;
		}
	}
	
	@Override
	@SideOnly(Side.CLIENT)
	public IIcon getIcon(int side, int meta) 
	{
		
		switch(side)
		{
		case 0:
			return this.blockIconSide;
		case 1:
			return this.blockIconSide;
		case 2:
			return this.blockIconSide;
		case 3:
			return this.blockIcon;
		case 4:
			return this.blockIconSide;
		case 5:
			return this.blockIconSide;
		default:
			return this.blockIconSide;
		}
	}
	
	@Override
	@SideOnly(Side.CLIENT)
	public void registerBlockIcons(IIconRegister par1IconRegister)
	{
   		this.blockIcon = par1IconRegister.registerIcon(IHLModInfo.MODID + ":junctionBoxFront");
   		this.blockIconSide = par1IconRegister.registerIcon(IHLModInfo.MODID + ":junctionBox");
	}
	
    @Override
	public void setBlockBoundsBasedOnState(IBlockAccess iBlockAccess, int x, int y, int z)
    {
    	TileEntity te = iBlockAccess.getTileEntity(x, y, z);
    	if(te!=null && te instanceof AnchorTileEntity)
    	{
    		AnchorTileEntity ate = (AnchorTileEntity) te;
    		float portSize=0.1f;
    		float bbMinX = 1f;
    		float bbMaxX = 0f;
    		float bbMinY = 1f;
    		float bbMaxY = 0f;
    		float bbMinZ = 1f;
    		float bbMaxZ = 0f;
    		for(short i=0;i<6;i++)
    		{
    			if(ate.hasCableOnSide[i])
    			{
        			float pMinX = (float) (ate.energyNetNodes[i].getPortPos(null)[0]-portSize-x);
        			float pMaxX = (float) (ate.energyNetNodes[i].getPortPos(null)[0]+portSize-x);
        			float pMinY = (float) (ate.energyNetNodes[i].getPortPos(null)[1]-portSize-y);
        			float pMaxY = (float) (ate.energyNetNodes[i].getPortPos(null)[1]+portSize-y);
        			float pMinZ = (float) (ate.energyNetNodes[i].getPortPos(null)[2]-portSize-z);
        			float pMaxZ = (float) (ate.energyNetNodes[i].getPortPos(null)[2]+portSize-z);
    	    		bbMinX=Math.min(pMinX,bbMinX);
    	    		bbMaxX=Math.max(pMaxX,bbMaxX);
    	    		bbMinY=Math.min(pMinY,bbMinY);
    	    		bbMaxY=Math.max(pMaxY,bbMaxY);
    	    		bbMinZ=Math.min(pMinZ,bbMinZ);
    	    		bbMaxZ=Math.max(pMaxZ,bbMaxZ);
    			}
    		}
            this.setBlockBounds(bbMinX, bbMinY, bbMinZ, bbMaxX, bbMaxY, bbMaxZ);
    	}
    }
    /*
	@Override
	public boolean onBlockActivated(World world,int x,int y,int z,EntityPlayer player,int i,float pos_x,float pos_y,float pos_z){
		TileEntity te = world.getTileEntity(x,y,z);
		if(IC2.platform.isSimulating())
		{
			AnchorTileEntity ate = (AnchorTileEntity) te;
			short facing = IHLUtils.getFacingFromPlayerView(player, true);
			System.out.println(ate.energyNetNodes[facing].getGridID());
			System.out.println(ate.getOfferedEnergy());
		}
		return false;
	}*/

}