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

Hello, so I am trying to make an item lock that prevents you from dropping items so you don't accidentally lose them. I save NBT data to it, but I have a problem. If I try to save data to a block I've already made, which saves the NBT when you break it and shows it in the tooltip, it gives a null pointer exception.

Add information and getItemCount methods in my block class:

@Override
public void addInformation(ItemStack itemstack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
    tooltip.add("Makes cobblestone and compressed cobblestone with upgrades at a stack per tick. Uses LOADS of power when upgraded.");
    tooltip.add("Will auto-eject items.");
    NBTTagCompound tagCompound = itemstack.getTagCompound();
    if (tagCompound != null) {
        int energy = tagCompound.getInteger("energy");
       //Crashing Line int sizeIn = getItemCount(tagCompound, "itemsIn");
        int sizeOut = getItemCount(tagCompound, "itemsOut");
        String translated = I18n.format("message.tm.cobble_generator", energy, sizeIn, sizeOut);
        translated = COMPILE.matcher(translated).replaceAll("\u00a7");
        Collections.addAll(tooltip, StringUtils.split(translated, "\n"));
    }
}
private int getItemCount(NBTTagCompound tagCompound, String itemsIn2) {
    int sizeIn = 0;
    NBTTagCompound compoundIn = (NBTTagCompound) tagCompound.getTag(itemsIn2);
   //Crashing line NBTTagList itemsIn = compoundIn.getTagList("Items", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < itemsIn.tagCount(); i++) {
        NBTTagCompound itemTags = itemsIn.getCompoundTagAt(i);
        if (!new ItemStack(itemTags).isEmpty()) {
            sizeIn++;
        }
    }
    return sizeIn;
}

I think this is because of the "NBTTagCompound tagCompound = itemstack.getTagCompound();" line which gets all the NBT data, including the locking NBT boolean.

Is there anything I can do to fix this?

 

Note: This is a duplicate because I posted this in the wrong place. Sorry.

Edited by TesterTesting135
Solved

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.