-
Notifications
You must be signed in to change notification settings - Fork 497
/
HeuristicBlockDetection.java
99 lines (84 loc) · 3.13 KB
/
HeuristicBlockDetection.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
package buildcraft.builders;
import java.util.BitSet;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.blueprints.SchematicBlock;
import buildcraft.api.blueprints.SchematicFluid;
import buildcraft.builders.schematics.SchematicBlockCreative;
import buildcraft.builders.schematics.SchematicTileCreative;
import buildcraft.core.blueprints.SchematicRegistry;
public final class HeuristicBlockDetection {
private HeuristicBlockDetection() {
}
private static BitSet craftableBlockList = new BitSet(65536);
public static void start() {
// Initialize craftableBlockList
for (Object or : CraftingManager.getInstance().getRecipeList()) {
if (or instanceof IRecipe) {
IRecipe recipe = ((IRecipe) or);
if (recipe.getRecipeOutput() != null && recipe.getRecipeOutput().getItem() != null &&
recipe.getRecipeOutput().getItem() instanceof ItemBlock) {
int pos = recipe.getRecipeOutput().getItemDamage() & 15;
pos |= Block.getIdFromBlock(Block.getBlockFromItem(recipe.getRecipeOutput().getItem())) << 4;
if (pos >= 0 && pos < 65536) {
craftableBlockList.set(pos);
}
}
}
}
// Register fluids
for (Fluid f : FluidRegistry.getRegisteredFluids().values()) {
SchematicRegistry.INSTANCE.registerSchematicBlock(f.getBlock(), SchematicFluid.class, new FluidStack(f, 1000));
}
// Register blocks
for (Object o : Block.blockRegistry.getKeys()) {
Block block = (Block) Block.blockRegistry.getObject(o);
if (block == null) {
continue;
}
for (int meta = 0; meta < 16; meta++) {
if (!SchematicRegistry.INSTANCE.isSupported(block, meta)) {
if (block.hasTileEntity(meta)) {
// All tiles are registered as creative only.
// This is helpful for example for server admins.
SchematicRegistry.INSTANCE.registerSchematicBlock(block, meta, SchematicTileCreative.class);
continue;
}
boolean creativeOnly = false;
if (!canCraft(block, meta)) {
// Does it drop a different block type?
try {
if (block.getItemDropped(meta, null, 0) != Item.getItemFromBlock(block)) {
creativeOnly = true;
}
} catch (NullPointerException e) {
// The "null" for Random in getItemDropped stops blocks
// depending on an RNG for deciding the dropped item
// from being autodetected.
}
}
try {
if (creativeOnly) {
SchematicRegistry.INSTANCE.registerSchematicBlock(block, meta, SchematicBlockCreative.class);
} else {
SchematicRegistry.INSTANCE.registerSchematicBlock(block, meta, SchematicBlock.class);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
private static boolean canCraft(Block block, int meta) {
int pos = Block.getIdFromBlock(block) << 4 | meta;
return craftableBlockList.get(pos);
}
}