mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-18 11:19:20 +00:00
Expand dynamic item containing multiple items with the (unlocalized) names and a way to register models for it easily.
This commit is contained in:
1 parent
68911993fb
commit
008bbfeeb5
2 files changed
+46
-39
No files matched your search
@@ -1,36 +0,0 @@
|
||||
package slimeknights.mantle.item;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Item representing a fixed amount of different items represented by meta.
|
||||
*/
|
||||
public class ItemMeta extends Item {
|
||||
|
||||
protected int maxMeta;
|
||||
|
||||
public ItemMeta(int maxMeta) {
|
||||
setHasSubtypes(true);
|
||||
this.maxMeta = maxMeta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubItems(Item itemIn, CreativeTabs tab, List subItems) {
|
||||
for(int i = 0; i <= maxMeta; i++) {
|
||||
subItems.add(new ItemStack(itemIn, 1, i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetadata(int damage) {
|
||||
if(damage < 0 || damage > maxMeta) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return damage;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,48 @@
|
||||
package slimeknights.mantle.item;
|
||||
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
import gnu.trove.procedure.TIntObjectProcedure;
|
||||
|
||||
import net.minecraft.client.resources.model.ModelBakery;
|
||||
import net.minecraft.client.resources.model.ModelResourceLocation;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.fml.common.Loader;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Item representing a dynamic amount of different items represented by meta.
|
||||
* Only returns valid metadatas. The validity is determined by a bitmask.
|
||||
* Current maximum is 64 metas - that should suffice for most applications.
|
||||
*/
|
||||
public class ItemMetaDynamic extends ItemMeta {
|
||||
public class ItemMetaDynamic extends Item {
|
||||
|
||||
private static int MAX = Long.SIZE;
|
||||
|
||||
protected int maxMeta;
|
||||
protected long availabilityMask;
|
||||
protected TIntObjectHashMap<String> names;
|
||||
|
||||
public ItemMetaDynamic() {
|
||||
super(-1);
|
||||
maxMeta = -1;
|
||||
names = new TIntObjectHashMap<String>();
|
||||
|
||||
this.setHasSubtypes(true);
|
||||
}
|
||||
|
||||
public ItemStack addMeta(int meta) {
|
||||
/**
|
||||
* Add a valid metadata.
|
||||
* @param meta The metadata value
|
||||
* @param name The name used for registering the itemmodel as well as the unlocalized name of the meta. The unlocalized name will be lowercased and stripped of whitespaces.
|
||||
* @return An itemstack representing the Item-Meta combination.
|
||||
*/
|
||||
public ItemStack addMeta(int meta, String name) {
|
||||
if(meta > MAX) {
|
||||
throw new IllegalArgumentException(String
|
||||
.format("Metadata too high, highest supported value is %d. Meta was %d", MAX, meta));
|
||||
@@ -31,9 +51,16 @@ public class ItemMetaDynamic extends ItemMeta {
|
||||
maxMeta = meta;
|
||||
setValid(meta);
|
||||
}
|
||||
names.put(meta, name);
|
||||
return new ItemStack(this, 1, meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUnlocalizedName(ItemStack stack) {
|
||||
int meta = stack.getMetadata(); // should call getMetadata below
|
||||
return super.getUnlocalizedName(stack) + "." + names.get(meta).toLowerCase(Locale.US).replaceAll(" ", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubItems(Item itemIn, CreativeTabs tab, List subItems) {
|
||||
for(int i = 0; i <= maxMeta; i++) {
|
||||
@@ -59,4 +86,20 @@ public class ItemMetaDynamic extends ItemMeta {
|
||||
}
|
||||
return ((availabilityMask >> meta) & 1) == 1;
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void registerItemModels() {
|
||||
final String resourceId = Loader.instance().activeModContainer().getModId().toLowerCase();
|
||||
final Item item = this;
|
||||
names.forEachEntry(new TIntObjectProcedure<String>() {
|
||||
@Override
|
||||
public boolean execute(int meta, String name) {
|
||||
name = String.format("%s:%s", resourceId, name);
|
||||
ModelBakery.addVariantName(item, name);
|
||||
// tell the game which model to use for this item-meta combination
|
||||
ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(name, "inventory"));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user