/
ItemBlockMetaExtra.java
77 lines (61 loc) · 2.54 KB
/
ItemBlockMetaExtra.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
package slimeknights.tmechworks.items;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.StringUtils;
import slimeknights.mantle.item.ItemBlockMeta;
import slimeknights.tmechworks.blocks.RedstoneMachine;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collection;
public class ItemBlockMetaExtra extends ItemBlockMeta {
private String extra = "";
public ItemBlockMetaExtra(Block block, String... extraData) {
super(block);
if (extraData.length == 0) {
this.extra = "facing=inv";
} else {
Arrays.sort(extraData);
this.extra = StringUtils.join(extraData, ',');
}
}
@SideOnly(Side.CLIENT)
@Override
public void registerItemModels() {
final Item item = this;
final ResourceLocation loc = block.getRegistryName();
for (Comparable o : (Collection<Comparable>) mappingProperty.getAllowedValues()) {
int meta = block.getMetaFromState(block.getDefaultState().withProperty(mappingProperty, o));
String name = mappingProperty.getName(o);
String[] props = (extra + "," + mappingProperty.getName() + "=" + name).split(",");
Arrays.sort(props);
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(loc, StringUtils.join(props, ',')));
}
}
@Nullable
@Override
public net.minecraftforge.common.capabilities.ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
Block block = getBlock();
if(!(block instanceof RedstoneMachine))
return super.initCapabilities(stack, nbt);
if(nbt == null) {
nbt = new NBTTagCompound();
stack.setTagCompound(nbt);
} else if(nbt.hasKey("BlockEntityTag"))
return null;
NBTTagCompound tags = new NBTTagCompound();
((RedstoneMachine)block).setDefaultNBT(tags);
nbt.setTag("BlockEntityTag", tags);
return null;
}
}