-
Notifications
You must be signed in to change notification settings - Fork 497
/
WorldPropertyIsOre.java
executable file
·86 lines (75 loc) · 3.12 KB
/
WorldPropertyIsOre.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
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* BuildCraft is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL. Please check the contents
* of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt */
package buildcraft.core.properties;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.oredict.OreDictionary;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class WorldPropertyIsOre extends WorldProperty {
private final HashSet<Integer> ores = new HashSet<>();
private final int harvestLevel;
public WorldPropertyIsOre(int harvestLevel) {
initBlockHarvestTools();
this.harvestLevel = harvestLevel;
for (String oreName : OreDictionary.getOreNames()) {
if (oreName.startsWith("ore")) {
List<ItemStack> oreStacks = OreDictionary.getOres(oreName);
if (oreStacks.size() > 0) {
Block block = Block.getBlockFromItem(oreStacks.get(0).getItem());
if (block == null) {
continue;
}
int meta = oreStacks.get(0).getItemDamage();
if (meta >= 16 || meta < 0) {
meta = 0;
}
IBlockState state = block.getStateFromMeta(meta);
if ("pickaxe".equals(block.getHarvestTool(state)) && block.getHarvestLevel(state) <= harvestLevel) {
ores.add(OreDictionary.getOreID(oreName));
}
}
}
}
}
private void initBlockHarvestTools() {
// Make sure the static code block in the ForgeHooks class is run
new ForgeHooks();
}
@Override
public boolean get(IBlockAccess blockAccess, IBlockState state, BlockPos pos) {
Block block = state.getBlock();
if (block == null) {
return false;
} else {
// Workaround for lit redstone ore
if (state.getBlock() == Blocks.lit_redstone_ore) {
return block.getHarvestLevel(state) <= harvestLevel;
}
List<ItemStack> toCheck = new ArrayList<>();
toCheck.add(new ItemStack(block, 1, block.getMetaFromState(state)));
if (block.hasTileEntity(state) && blockAccess instanceof World) {
toCheck.addAll(block.getDrops((World) blockAccess, pos, state, 0));
}
for (ItemStack stack : toCheck) {
if (stack.getItem() != null) {
for (int id : OreDictionary.getOreIDs(stack)) {
if (ores.contains(id)) {
return true;
}
}
}
}
}
return false;
}
}