-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockHandler.java
121 lines (112 loc) · 3.67 KB
/
BlockHandler.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
package buildcraft.api.builder;
import buildcraft.core.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class BlockHandler {
/**
* By default we will ignore all blocks with Tile Entities.
*
* We will also skip any blocks that drop actual items like Ore blocks.
*/
public boolean canSaveBlockToSchematic(World world, int x, int y, int z) {
if (world.isAirBlock(x, y, z)) {
return false;
}
int blockId = world.getBlockId(x, y, z);
Block block = Block.blocksList[blockId];
if (block == null) {
return false;
}
int meta = world.getBlockMetadata(x, y, z);
try {
if (block.idDropped(meta, null, 0) != blockId) {
return false;
}
} catch (NullPointerException ex) {
return false;
}
return !block.hasTileEntity(meta);
}
/**
* It is assumed that Blueprints always face North on save.
*
* Tile Entities should store some NBT data in the BlockSchematic.blockData
* tag.
*/
public BlockSchematic saveBlockToSchematic(World world, int x, int y, int z) {
int blockId = world.getBlockId(x, y, z);
Block block = Block.blocksList[blockId];
if (block == null) {
return null;
}
BlockSchematic schematic = new BlockSchematic(block.getUnlocalizedName());
schematic.metadata = world.getBlockMetadata(x, y, z);
return schematic;
}
/**
* This will only work for simple blocks without tile entities and will in
* fact break on Ore blocks as well. Which is why those blocks can't be
* saved by default.
*/
public List<ItemStack> getCostForSchematic(BlockSchematic schematic) {
List<ItemStack> cost = new ArrayList<ItemStack>();
Block block = null; // TODO: replace with mapping -> id code
if (block != null) {
cost.add(new ItemStack(block.idDropped(schematic.metadata, Utils.RANDOM, 0), 1, block.damageDropped(schematic.metadata)));
}
return cost;
}
/**
* Called when items are consumed for this block, an array containing all
* the items listed as a cost is passed in. Use them as you see fit.
*
* If the function returns false, the block is not placed. You should not
* modify any ItemStack until you have determined that everything you
* require is present.
*/
public boolean consumeItems(ItemStack[] stacks) {
for (int i = 0; i < stacks.length; i++) {
stacks[i] = Utils.consumeItem(stacks[i]);
}
return true;
}
/**
* Can the block be placed currently or is it waiting on some other block to
* be placed first?
*/
public boolean canPlaceNow(World world, int x, int y, int z, ForgeDirection blueprintOrientation, BlockSchematic schematic) {
return true;
}
/**
* Should this place the block or should the block already be placed and
* this just initializes it?
*
* The ForgeDirection parameter can be use to determine the orientation of
* the blueprint. Blueprints are always saved facing North. This function
* will have to rotate the block accordingly.
*/
public void readBlockFromSchematic(World world, int x, int y, int z, ForgeDirection blueprintOrientation, BlockSchematic schematic) {
}
/**
* Checks if the block matches the schematic.
*/
public boolean doesBlockMatchSchematic(World world, int x, int y, int z, ForgeDirection blueprintOrientation, BlockSchematic schematic) {
int blockId = world.getBlockId(x, y, z);
Block block = Block.blocksList[blockId];
if (block == null) {
return false;
}
if (!schematic.blockName.equals(block.getUnlocalizedName())) {
return false;
}
return schematic.metadata == world.getBlockMetadata(x, y, z);
}
}