Jump to content

[1.12.2 | SOLVED] Change item model depending on NBT


trexxet

Recommended Posts

I'm trying to implement a rifle, which model depends on if it has a magazine inserted or no. 
So, I'm doing the following:

Rifle class:

    private static final String MAGAZINE_KEY = "has_magazine";

    public ItemRifle () {
        super("rifle");
        setMaxStackSize(1);

        addPropertyOverride(new ResourceLocation(Reference.MODID, "empty"),
                new IItemPropertyGetter() {
                    @Override
                    public float apply (ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
                        return ItemRifle.getPropertyEmpty (stack, entityIn);
                    }
                });
    }

    public static float getPropertyEmpty (ItemStack stack, @Nullable EntityLivingBase entityIn) {
        if (entityIn == null) return 0.f;
        ItemStack activeItemStack = entityIn.getActiveItemStack();
        if (!activeItemStack.isEmpty() && activeItemStack.getItem() instanceof ItemRifle)
            return ItemRifle.hasMagazine(stack) ? 0.f : 1.f; // 0 - not empty
        return 0.f;
    }

    public static boolean hasMagazine (ItemStack stack) {
        if (stack.hasTagCompound()) {
            NBTTagCompound nbt = stack.getTagCompound();
            return nbt.hasKey(MAGAZINE_KEY) && nbt.getBoolean(MAGAZINE_KEY);
        }
        return false;
    }

 

Model of a rifle with magazine (model/item/rifle.json):

{
  "parent": "item/handheld",
  "textures": {"layer0": "ic2guns:items/rifle"},
  "overrides": [
    {"predicate": {"ic2guns:empty": 1}, "model": "ic2guns:item/rifle_empty"}
  ]
}

 

Model of an empty rifle (model/item/rifle_empty.json):

{
  "parent": "ic2guns:item/rifle",
  "textures": {
    "layer0": "ic2guns:items/rifle_empty"
  }
}


At this moment I literally have no code which would set any NBT tag to rifle, so it should use the texture of an empty rifle.
However, the base one (rifle with magazine) is rendered.
Have I forgotten to register anything, or I'm doing it in a totally wrong way?

Edited by trexxet
self-solved
Link to comment
Share on other sites

Oh, nevermind. This is correct:

    public static float getPropertyEmpty (ItemStack stack, @Nullable EntityLivingBase entityIn) {
        if (entityIn == null) return 0.f;
        if (!stack.isEmpty() && stack.getItem() instanceof ItemRifle)
            return ItemRifle.hasMagazine(stack) ? 0.f : 1.f; // 0 - not empty
        return 0.f;
    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.