-
Notifications
You must be signed in to change notification settings - Fork 755
/
LavaTankBlock.java
361 lines (324 loc) · 11.6 KB
/
LavaTankBlock.java
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
package tconstruct.blocks;
import java.util.List;
import mantle.blocks.iface.IServantLogic;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import tconstruct.TConstruct;
import tconstruct.blocks.logic.LavaTankLogic;
import tconstruct.client.block.TankRender;
import tconstruct.library.TConstructRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class LavaTankBlock extends BlockContainer
{
public IIcon[] icons;
String texturePrefix = "";
public LavaTankBlock()
{
super(Material.rock);
setHardness(3F);
setResistance(20F);
setCreativeTab(TConstructRegistry.blockTab);
this.setBlockName("TConstruct.LavaTank");
setStepSound(Block.soundTypeGrass);
}
public LavaTankBlock(String prefix)
{
this();
texturePrefix = prefix;
}
public String[] getTextureNames ()
{
String[] textureNames = { "lavatank_side", "lavatank_top", "searedgague_top", "searedgague_side", "searedgague_bottom", "searedwindow_top", "searedwindow_side", "searedwindow_bottom" };
if (!texturePrefix.equals(""))
for (int i = 0; i < textureNames.length; i++)
textureNames[i] = texturePrefix + "_" + textureNames[i];
return textureNames;
}
@Override
public void registerBlockIcons (IIconRegister IIconRegister)
{
String[] textureNames = getTextureNames();
this.icons = new IIcon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
this.icons[i] = IIconRegister.registerIcon("tinker:" + textureNames[i]);
}
}
@Override
public int getRenderBlockPass ()
{
return 1;
}
@Override
public boolean isOpaqueCube ()
{
return false;
}
@Override
public boolean renderAsNormalBlock ()
{
return false;
}
@Override
public boolean shouldSideBeRendered (IBlockAccess world, int x, int y, int z, int side)
{
// if (side == 0 && world.getBlockMetadata(x, y, z) == 0)
// return super.shouldSideBeRendered(world, x, y, z, side);
Block bID = world.getBlock(x, y, z);
return bID == this ? false : super.shouldSideBeRendered(world, x, y, z, side);
// return true;
}
@Override
public int getLightValue (IBlockAccess world, int x, int y, int z)
{
TileEntity logic = world.getTileEntity(x, y, z);
if (logic != null && logic instanceof LavaTankLogic)
return ((LavaTankLogic) logic).getBrightness();
return 0;
}
/*
* @Override public int getRenderBlockPass() { return 1; }
*/
@Override
public int getRenderType ()
{
return TankRender.tankModelID;
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon (int side, int meta)
{
if (meta >= 3)
meta = 0;
if (meta == 0)
{
if (side == 0 || side == 1)
{
return icons[1];
}
else
{
return icons[0];
}
}
else
{
return icons[meta * 3 + getTextureIndex(side) - 1];
}
}
public int getTextureIndex (int side)
{
if (side == 0)
return 2;
if (side == 1)
return 0;
return 1;
}
@Override
public TileEntity createTileEntity (World world, int metadata)
{
return new LavaTankLogic();
}
@Override
public boolean onBlockActivated (World world, int i, int j, int k, EntityPlayer entityplayer, int par6, float par7, float par8, float par9)
{
ItemStack current = entityplayer.inventory.getCurrentItem();
if (current != null)
{
FluidStack liquid;
LavaTankLogic logic = (LavaTankLogic) world.getTileEntity(i, j, k);
if (current.getItem() == Items.lava_bucket)
{
liquid = new FluidStack(FluidRegistry.LAVA, TConstruct.blockLiquidValue);
}
else
{
liquid = FluidContainerRegistry.getFluidForFilledItem(current);
}
if (liquid != null)
{
int amount = logic.fill(ForgeDirection.UNKNOWN, liquid, false);
if (amount == liquid.amount)
{
logic.fill(ForgeDirection.UNKNOWN, liquid, true);
if (!entityplayer.capabilities.isCreativeMode)
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
return true;
}
else
return true;
}
else if (current.getItem() == Items.lava_bucket)
{
FluidTankInfo[] tanks = logic.getTankInfo(ForgeDirection.UNKNOWN);
FluidStack fillFluid = tanks[0].fluid;// getFluid();
ItemStack fillStack = new ItemStack(Items.bucket);
if (fillStack != null)
{
logic.drain(ForgeDirection.UNKNOWN, TConstruct.blockLiquidValue, true);
if (!entityplayer.capabilities.isCreativeMode)
{
if (current.stackSize == 1)
{
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, fillStack);
}
else
{
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
if (!entityplayer.inventory.addItemStackToInventory(fillStack))
{
entityplayer.dropPlayerItemWithRandomChoice(fillStack, false);
}
}
}
return true;
}
}
else if (FluidContainerRegistry.isBucket(current))
{
FluidTankInfo[] tanks = logic.getTankInfo(ForgeDirection.UNKNOWN);
FluidStack fillFluid = tanks[0].fluid;// getFluid();
ItemStack fillStack = FluidContainerRegistry.fillFluidContainer(fillFluid, current);
if (fillStack != null)
{
logic.drain(ForgeDirection.UNKNOWN, FluidContainerRegistry.getFluidForFilledItem(fillStack).amount, true);
if (!entityplayer.capabilities.isCreativeMode)
{
if (current.stackSize == 1)
{
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, fillStack);
}
else
{
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
if (!entityplayer.inventory.addItemStackToInventory(fillStack))
{
entityplayer.dropPlayerItemWithRandomChoice(fillStack, false);
}
}
}
return true;
}
else
{
return true;
}
}
}
return false;
}
public static ItemStack consumeItem (ItemStack stack)
{
if (stack.stackSize == 1)
{
if (stack.getItem().hasContainerItem())
return stack.getItem().getContainerItem(stack);
else
return null;
}
else
{
stack.splitStack(1);
return stack;
}
}
@Override
public TileEntity createNewTileEntity (World world, int test)
{
return createTileEntity(world, 0);
}
@Override
public void getSubBlocks (Item id, CreativeTabs tab, List list)
{
for (int iter = 0; iter < 3; iter++)
{
list.add(new ItemStack(id, 1, iter));
}
}
/* Data */
@Override
public int damageDropped (int meta)
{
return meta;
}
/* Updates */
public void onNeighborBlockChange (World world, int x, int y, int z, Block nBlockID)
{
TileEntity logic = world.getTileEntity(x, y, z);
if (logic instanceof IServantLogic)
{
((IServantLogic) logic).notifyMasterOfChange();
}
}
@Override
public boolean removedByPlayer (World world, EntityPlayer player, int x, int y, int z)
{
player.addExhaustion(0.025F);
int meta = world.getBlockMetadata(x, y, z);
ItemStack stack = new ItemStack(this, 1, meta);
LavaTankLogic logic = (LavaTankLogic) world.getTileEntity(x, y, z);
FluidStack liquid = logic.tank.getFluid();
if (liquid != null)
{
NBTTagCompound tag = new NBTTagCompound();
NBTTagCompound liquidTag = new NBTTagCompound();
liquid.writeToNBT(liquidTag);
tag.setTag("Fluid", liquidTag);
stack.setTagCompound(tag);
}
if (!player.capabilities.isCreativeMode || player.isSneaking())
dropTankBlock(world, x, y, z, stack);
return world.setBlockToAir(x, y, z);
}
protected void dropTankBlock (World world, int x, int y, int z, ItemStack stack)
{
if (!world.isRemote && world.getGameRules().getGameRuleBooleanValue("doTileDrops"))
{
float f = 0.7F;
double d0 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d1 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
double d2 = (double) (world.rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(world, (double) x + d0, (double) y + d1, (double) z + d2, stack);
entityitem.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(entityitem);
}
}
@Override
public void harvestBlock (World par1World, EntityPlayer par2EntityPlayer, int par3, int par4, int par5, int par6)
{
}
@Override
public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase living, ItemStack stack)
{
if (stack.hasTagCompound())
{
NBTTagCompound liquidTag = stack.getTagCompound().getCompoundTag("Fluid");
if (liquidTag != null)
{
FluidStack liquid = FluidStack.loadFluidStackFromNBT(liquidTag);
LavaTankLogic logic = (LavaTankLogic) world.getTileEntity(x, y, z);
logic.tank.setFluid(liquid);
}
}
}
}