summaryrefslogtreecommitdiff
path: root/ihl/tunneling_shield/MultiBlockSpacerBlock.java
blob: 79e595587cfd43c633e5ae9f92284cec06830830 (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
package ihl.tunneling_shield;

import ic2.core.IC2;
import ic2.core.IHasGui;
import ic2.core.audio.PositionSpec;
import ic2.core.item.tool.ItemToolWrench;
import ihl.utils.IHLUtils;

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

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;

public class MultiBlockSpacerBlock extends Block {
	
	public static Set<IMultiBlock> teList = new HashSet<IMultiBlock>();
	
	public MultiBlockSpacerBlock() 
	{
		super(Material.iron);
	}

	/**
	* The type of render function that is called for this block
	*/
	@Override
	public int getRenderType()
	{
	return -2;
	}
	
	@Override
	public ArrayList<ItemStack> getDrops(World world,int x, int y, int z, int meta, int flag)
	{
		return new ArrayList<ItemStack>();
	}
	@Override
	public void onBlockDestroyedByExplosion(World world, int x, int y, int z, Explosion explosion)
	{
		super.onBlockDestroyedByExplosion(world, x, y, z, explosion);
		this.checkIfMachinePart(world, x, y, z);
	}

	@Override
	public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int meta)
	{
		super.onBlockDestroyedByPlayer(world, x, y, z, meta);
		this.checkIfMachinePart(world, x, y, z);
	}
	
	private boolean checkIfMachinePart(World world, int x, int y, int z)
	{
		if(!teList.isEmpty())
		{
			Iterator<IMultiBlock> i = MultiBlockSpacerBlock.teList.iterator();
			while(i.hasNext())
			{
				IMultiBlock te = i.next();
				if(te!=null && !te.getIsInvalid())
				{
					if(te.isAPartOfStructure(x, y, z, true))
					{
						return true;
					}
				}
				else
				{
					teList.remove(i);
				}
			}
		}
		return false;
	}
	
	private IMultiBlock getLinkedTileEntity(World world, int x, int y, int z)
	{
		if(!teList.isEmpty())
		{
			Iterator<IMultiBlock> i = MultiBlockSpacerBlock.teList.iterator();
			while(i.hasNext())
			{
				IMultiBlock te = i.next();
				if(te!=null && !te.getIsInvalid())
				{
					if(te.isAPartOfStructure(x, y, z, false))
					{
						return te;
					}
				}
				else
				{
					teList.remove(i);
				}
			}
		}
		return null;
	}
	
	/**
	* Is this block (a) opaque and (B) a full 1m cube? This determines whether or not to render the shared face of two
	* adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
	*/
	@Override
	public boolean isOpaqueCube()
	{
		return false;
	}

	/**
	* If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
	*/
	@Override
	public boolean renderAsNormalBlock()
	{
		return false;
	}
	
	@Override
	public void onBlockAdded(World world, int x, int y,int z)
	{	
		world.scheduleBlockUpdate(x, y, z, this, this.tickRate(world));
	}
	
	@Override
    public void onNeighborBlockChange(World world, int x, int y,int z, Block block)
    {
		this.updateTick(world, x, y, z, world.rand);
	}
	
@Override
public void updateTick(World world, int x, int y,int z, Random rand)
{
	world.scheduleBlockUpdate(x, y, z, this, this.tickRate(world));
	if(!world.isRemote)
	{
		boolean setToAir=true;
		if(teList.size()>0)
		{
			if(!teList.isEmpty())
			{
				Iterator<IMultiBlock> i = MultiBlockSpacerBlock.teList.iterator();
				while(i.hasNext())
				{
					IMultiBlock te = i.next();
					if(te!=null && !te.getIsInvalid())
					{
						if(te.isAPartOfStructure(x, y, z, false))
						{
							setToAir=false;
						}
					}
					else
					{
						teList.remove(i);
					}
				}
			}
		}
		if(setToAir)
		{
			world.setBlockToAir(x, y, z);
		}
	}
}
	
	@Override
	public int tickRate(World world)
	{
		return 200;
	}
	
	@Override
	public boolean onBlockActivated(World world,int x,int y,int z,EntityPlayer entityPlayer,int i,float pos_x,float pos_y,float pos_z)
	{
    	IMultiBlock te = this.getLinkedTileEntity(world, x, y, z);
		ItemStack currentIS = entityPlayer.inventory.getCurrentItem();
		if(currentIS!=null)
		{
			if(currentIS.getItem() instanceof ItemToolWrench)
			{
				if(((ItemToolWrench)currentIS.getItem()).canTakeDamage(currentIS, 10))
				{
                    if (IC2.platform.isRendering())
                    {
                        IC2.audioManager.playOnce(entityPlayer, PositionSpec.Hand, "Tools/wrench.ogg", true, 1.0F);
                    }
                    else if(IC2.platform.isSimulating())
                    {
                    	if(te instanceof DriverTileEntity)
                    	{
                    		DriverTileEntity dte =(DriverTileEntity) te;
                    		if(dte.hasShield)
                    		{
                    			if(dte.advancedShield)
                    			{
                					world.spawnEntityInWorld(new EntityItem(world, x, y, z, IHLUtils.getThisModItemStack("advancedShieldAssemblyUnitBlock")));
                    			}
                    			else
                    			{
                					world.spawnEntityInWorld(new EntityItem(world, x, y, z, IHLUtils.getThisModItemStack("IHLShieldAssemblyUnitBlock")));
                    			}
                    		}
                    		dte.isAPartOfStructure(x, y, z, true);
        					world.setBlockToAir(x, y, z);
                    	}
    					((ItemToolWrench)currentIS.getItem()).damage(currentIS, 10, entityPlayer);
                    }
				}
				return true;
			}
		}
		return te instanceof IHasGui ? (IC2.platform.isSimulating() ? IC2.platform.launchGui(entityPlayer, (IHasGui)te) : true) : false;
    }
}