/
NetherrackFurnaceBlock.java
310 lines (268 loc) · 9.81 KB
/
NetherrackFurnaceBlock.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
package mods.natura.blocks.tech;
import java.util.Random;
import mods.natura.Natura;
import mods.natura.common.NaturaTab;
import mods.natura.gui.NGuiHandler;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* Abstract for blocks with inventories.
*
* @author mDiyo
*/
public class NetherrackFurnaceBlock extends BlockContainer
{
protected Random rand = new Random();
public NetherrackFurnaceBlock(int id)
{
super(id, Material.rock);
}
/* Logic backend */
public TileEntity createNewTileEntity (World var1)
{
return null;
}
public TileEntity createTileEntity (World world, int metadata)
{
return new NetherrackFurnaceLogic();
}
public Integer getGui (World world, int x, int y, int z, EntityPlayer entityplayer)
{
return NGuiHandler.furnaceGui;
}
public Object getModInstance ()
{
return Natura.instance;
}
@Override
public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float clickX, float clickY, float clickZ)
{
if (player.isSneaking())
return false;
Integer integer = getGui(world, x, y, z, player);
if (integer == null || integer == -1)
{
return false;
}
else
{
if (!world.isRemote)
player.openGui(getModInstance(), integer, world, x, y, z);
return true;
}
}
/* Inventory */
@Override
public void breakBlock (World par1World, int x, int y, int z, int blockID, int meta)
{
TileEntity te = par1World.getBlockTileEntity(x, y, z);
if (te != null && te instanceof NetherrackFurnaceLogic)
{
NetherrackFurnaceLogic logic = (NetherrackFurnaceLogic) te;
for (int iter = 0; iter < logic.getSizeInventory(); ++iter)
{
ItemStack stack = logic.getStackInSlot(iter);
if (stack != null)
{
float jumpX = rand.nextFloat() * 0.8F + 0.1F;
float jumpY = rand.nextFloat() * 0.8F + 0.1F;
float jumpZ = rand.nextFloat() * 0.8F + 0.1F;
while (stack.stackSize > 0)
{
int itemSize = rand.nextInt(21) + 10;
if (itemSize > stack.stackSize)
{
itemSize = stack.stackSize;
}
stack.stackSize -= itemSize;
EntityItem entityitem = new EntityItem(par1World, (double) ((float) x + jumpX), (double) ((float) y + jumpY), (double) ((float) z + jumpZ), new ItemStack(stack.itemID,
itemSize, stack.getItemDamage()));
if (stack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound) stack.getTagCompound().copy());
}
float offset = 0.05F;
entityitem.motionX = (double) ((float) rand.nextGaussian() * offset);
entityitem.motionY = (double) ((float) rand.nextGaussian() * offset + 0.2F);
entityitem.motionZ = (double) ((float) rand.nextGaussian() * offset);
par1World.spawnEntityInWorld(entityitem);
}
}
}
}
super.breakBlock(par1World, x, y, z, blockID, meta);
}
/* Placement */
int side = -1;
//This class does not have an actual block placed in the world
@Override
public int onBlockPlaced (World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int meta)
{
this.side = side;
return meta;
}
@Override
public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase entityliving, ItemStack stack)
{
TileEntity logic = world.getBlockTileEntity(x, y, z);
if (logic instanceof NetherrackFurnaceLogic)
{
NetherrackFurnaceLogic direction = (NetherrackFurnaceLogic) logic;
if (entityliving == null)
{
direction.setDirection(0F, 0F, null);
}
else
{
direction.setDirection(entityliving.rotationYaw * 4F, entityliving.rotationPitch, entityliving);
}
}
if (logic instanceof NetherrackFurnaceLogic)
{
NetherrackFurnaceLogic inv = (NetherrackFurnaceLogic) logic;
if (stack.hasDisplayName())
{
inv.setGuiDisplayName(stack.getDisplayName());
}
}
}
public static boolean isActive (IBlockAccess world, int x, int y, int z)
{
TileEntity logic = world.getBlockTileEntity(x, y, z);
if (logic instanceof NetherrackFurnaceLogic)
{
return ((NetherrackFurnaceLogic) logic).getActive();
}
return false;
}
public int damageDropped (int meta)
{
return meta;
}
/* Light */
public int getLightValue (IBlockAccess world, int x, int y, int z)
{
TileEntity te = world.getBlockTileEntity(x, y, z);
if (te instanceof NetherrackFurnaceLogic)
{
return ((NetherrackFurnaceLogic) te).getActive() ? 15 : 0;
}
return lightValue[blockID];
}
/* Comparator */
public boolean hasComparatorInputOverride ()
{
return true;
}
public int getComparatorInputOverride (World par1World, int par2, int par3, int par4, int par5)
{
return Container.calcRedstoneFromInventory((IInventory) par1World.getBlockTileEntity(par2, par3, par4));
}
@SideOnly(Side.CLIENT)
public void randomDisplayTick (World world, int x, int y, int z, Random par5Random)
{
NetherrackFurnaceLogic logic = (NetherrackFurnaceLogic) world.getBlockTileEntity(x, y, z);
if (logic.getActive())
{
int l = world.getBlockMetadata(x, y, z);
float f = (float) x + 0.5F;
float f1 = (float) y + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
float f2 = (float) z + 0.5F;
float f3 = 0.52F;
float f4 = par5Random.nextFloat() * 0.6F - 0.3F;
if (l == 4)
{
world.spawnParticle("smoke", (double) (f - f3), (double) f1, (double) (f2 + f4), 0.0D, 0.0D, 0.0D);
world.spawnParticle("flame", (double) (f - f3), (double) f1, (double) (f2 + f4), 0.0D, 0.0D, 0.0D);
}
else if (l == 5)
{
world.spawnParticle("smoke", (double) (f + f3), (double) f1, (double) (f2 + f4), 0.0D, 0.0D, 0.0D);
world.spawnParticle("flame", (double) (f + f3), (double) f1, (double) (f2 + f4), 0.0D, 0.0D, 0.0D);
}
else if (l == 2)
{
world.spawnParticle("smoke", (double) (f + f4), (double) f1, (double) (f2 - f3), 0.0D, 0.0D, 0.0D);
world.spawnParticle("flame", (double) (f + f4), (double) f1, (double) (f2 - f3), 0.0D, 0.0D, 0.0D);
}
else if (l == 3)
{
world.spawnParticle("smoke", (double) (f + f4), (double) f1, (double) (f2 + f3), 0.0D, 0.0D, 0.0D);
world.spawnParticle("flame", (double) (f + f4), (double) f1, (double) (f2 + f3), 0.0D, 0.0D, 0.0D);
}
}
}
/* Textures */
@Override
@SideOnly(Side.CLIENT)
public Icon getIcon (int side, int meta)
{
return icons[(meta % 8) * 3 + getTextureIndex(side)];
}
@SideOnly(Side.CLIENT)
public int getTextureIndex (int side)
{
if (side == 0 || side == 1)
return 3;
if (side == 3)
return 0;
return 2;
}
@Override
@SideOnly(Side.CLIENT)
public Icon getBlockTexture (IBlockAccess world, int x, int y, int z, int side)
{
TileEntity logic = world.getBlockTileEntity(x, y, z);
int direction = (logic instanceof NetherrackFurnaceLogic) ? ((NetherrackFurnaceLogic) logic).getRenderDirection() : 0;
int meta = world.getBlockMetadata(x, y, z) % 8;
if (meta == 0)
{
if (side == direction)
{
if (((NetherrackFurnaceLogic) logic).getActive())
return icons[1];
else
return icons[0];
}
else if (side > 1)
{
return icons[2];
}
return icons[3];
}
return icons[0];
}
@SideOnly(Side.CLIENT)
public Icon[] icons;
@SideOnly(Side.CLIENT)
public String[] getTextureNames ()
{
String[] textureNames = { "nfurnace_off", "nfurnace_on", "nfurnace_side", "nfurnace_top" };
return textureNames;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons (IconRegister iconRegister)
{
String[] textureNames = getTextureNames();
this.icons = new Icon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
this.icons[i] = iconRegister.registerIcon("natura:" + textureNames[i]);
}
}
}