-
Notifications
You must be signed in to change notification settings - Fork 81
/
ModelRegisterUtil.java
130 lines (110 loc) · 4.68 KB
/
ModelRegisterUtil.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
122
123
124
125
126
127
128
129
130
package com.progwml6.natura.common;
import javax.annotation.Nonnull;
import com.progwml6.natura.Natura;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StringUtils;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.item.ItemBlockMeta;
/** This class basically is what you want when you got models to register */
@SideOnly(Side.CLIENT)
public final class ModelRegisterUtil
{
public static final String VARIANT_INVENTORY = "inventory";
// Regular ITEM MODELS //
/** Registers the item-meta combo in the itemstack with the given location for the inventory-variant */
public static void registerItemModel(ItemStack itemStack, ResourceLocation name)
{
if (!itemStack.isEmpty() && name != null)
{
// tell the loader to load the model
ModelLoader.registerItemVariants(itemStack.getItem(), name);
// tell the game which model to use for this item-meta combination
ModelLoader.setCustomModelResourceLocation(itemStack.getItem(), itemStack.getMetadata(), new ModelResourceLocation(name, VARIANT_INVENTORY));
}
}
/** Registers the given item with its registry name for all metadata values for the inventory variant */
public static ResourceLocation registerItemModel(Item item)
{
ResourceLocation itemLocation = null;
if (item != null)
{
itemLocation = item.getRegistryName();
}
if (itemLocation != null)
{
itemLocation = registerIt(item, itemLocation);
}
return itemLocation;
}
/** Registers the item of the given block with its registry name for all metadata values for the inventory variant */
public static ResourceLocation registerItemModel(Block block)
{
return registerItemModel(Item.getItemFromBlock(block));
}
/** Registers an itemblockmeta model for the blocks inventory variant. */
public static void registerItemBlockMeta(Block block)
{
if (block != null)
{
Item item = Item.getItemFromBlock(block);
if (item instanceof ItemBlockMeta)
{
((ItemBlockMeta) item).registerItemModels();
}
else
{
Natura.log.error("Trying to register an ItemBlockMeta model for a non itemblockmeta block: " + block.getRegistryName());
}
}
}
/** Registers the item with the given metadata and its registry name for the inventory variant */
public static void registerItemModel(Item item, int meta)
{
registerItemModel(item, meta, VARIANT_INVENTORY);
}
/** Registers the given item with the given meta and its registry name for the given variant */
public static void registerItemModel(Item item, int meta, String variant)
{
if (item != null)
{
registerItemModel(item, meta, item.getRegistryName(), variant);
}
}
/** Registers the given item/meta combination with the model at the given location, and the given variant */
public static void registerItemModel(Item item, int meta, ResourceLocation location, String variant)
{
if (item != null && !StringUtils.isNullOrEmpty(variant))
{
//ModelLoader.registerItemVariants(item, location);
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), variant));
}
}
private static ResourceLocation registerIt(Item item, final ResourceLocation location)
{
// plop it in.
// This here is needed for the model to be found ingame when the game looks for a model to render an Itemstack
// we use an ItemMeshDefinition because it allows us to do it no matter what metadata we use
ModelLoader.setCustomMeshDefinition(item, new ItemMeshDefinition()
{
@Nonnull
@Override
public ModelResourceLocation getModelLocation(@Nonnull ItemStack stack)
{
return new ModelResourceLocation(location, VARIANT_INVENTORY);
}
});
// We have to readd the default variant if we have custom variants, since it wont be added otherwise and therefore not loaded
ModelLoader.registerItemVariants(item, location);
return location;
}
private ModelRegisterUtil()
{
}
}