Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

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;
    }

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.