aboutsummaryrefslogtreecommitdiff
path: root/src/Java/powercrystals/minefactoryreloaded/api/ReplacementBlock.java
blob: c67e8a39a2cf4d8f7ddd94c08ee3ae5d1ce8269d (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
package powercrystals.minefactoryreloaded.api;

import net.minecraft.block.Block;
import net.minecraft.item.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class ReplacementBlock
{
	protected byte _hasMeta;
	protected int _meta;
	protected final Block _block;
	protected final NBTTagCompound _tileTag;
	
	/**
	 * Called to replace a block in the world.
	 * @param world The world object
	 * @param x The X coord
	 * @param y The Y coord
	 * @param z The Z coord
	 * @param stack The ItemStack being used to replace the block (may be null)
	 * @return True if the block was set successfully
	 */
	public boolean replaceBlock(World world, int x, int y, int z, ItemStack stack)
	{
		int meta = getMeta(world, x, y, z, stack);
		if (world.setBlock(x, y, z, _block, meta, 3))
		{
			if (hasTag(stack) && _block.hasTileEntity(meta))
			{
				TileEntity tile = world.getTileEntity(x, y, z);
				if (tile != null)
					tile.readFromNBT(getTag(world, x, y, z, stack));
			}
			return true;
		}
		return false;
	}

	/**
	 * Called to get the metadata of the replacement block in the world.
	 * @param world The world object
	 * @param x The X coord
	 * @param y The Y coord
	 * @param z The Z coord
	 * @param stack The ItemStack being used to replace the block (may be null)
	 * @return The metadata of the block
	 */
	protected int getMeta(World world, int x, int y, int z, ItemStack stack)
	{
		int m = 0;
		if (_hasMeta > 0)
		{
			if (_hasMeta > 1)
				return _meta;
			m = stack.getItemDamage();
			Item item = stack.getItem();
			if (item instanceof ItemBlock)
				m = ((ItemBlock)item).getMetadata(m);
		}
		return m;
	}
	
	/**
	 * Called to set the metdata of this ReplacementBlock to a fixed value
	 * @param meta The metadata of the block 
	 * @return This instance
	 */
	public ReplacementBlock setMeta(int meta)
	{
		if (meta >= 0)
		{
			_hasMeta = 2;
			_meta = meta;
		}
		return this;
	}
	
	/**
	 * Called to set the metdata of this ReplacementBlock to a value read from an ItemStack
	 * @param meta The metadata of the block 
	 * @return This instance
	 */
	public ReplacementBlock setMeta(boolean hasMeta)
	{
		_hasMeta = (byte) (hasMeta ? 1 : 0);
		return this;
	}
	
	/**
	 * Called to get the NBTTagCompound a TileEntity will read its state from
	 * @param world The world object
	 * @param x The X coord
	 * @param y The Y coord
	 * @param z The Z coord
	 * @param stack The ItemStack being used to replace the block (may be null)
	 * @return The NBTTagCompound a TileEntity will read its state from
	 */
	protected NBTTagCompound getTag(World world, int x, int y, int z, ItemStack stack)
	{
		return _tileTag;
	}
	
	/**
	 * Called to see if a TileEntity should have its state set
	 * @param stack The ItemStack being used to replace the block (may be null)
	 * @return True if the TileEntity should have its state set
	 */
	protected boolean hasTag(ItemStack stack)
	{
		return _tileTag != null;
	}
	
	public ReplacementBlock(Item block)
	{
		this(Block.getBlockFromItem(block));
	}
	
	public ReplacementBlock(Item block, NBTTagCompound tag)
	{
		this(Block.getBlockFromItem(block), tag);
	}
	
	public ReplacementBlock(Block block)
	{
		this(block, null);
	}
	
	public ReplacementBlock(Block block, NBTTagCompound tag)
	{
		_block = block;
		_tileTag = tag;
	}
}