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

Hi. I'm having a bit of a problem comparing 2 ItemStacks, which is a bit annoying.

 

Explanation:

I'm making a TileEntity, which is spawning a new ItemStack when a specific item is drop on it.

I'm trying to make the base of it, but I stuck at comparing.

 

The error:

When I'm comparing two items, It return false ,although it should return true.

I made a debug part, where it returns the required Item and the Item that dropped on the block.

Here is the result when I drop the correct item:

 

-----/--/-----

1xitem.housestoneessentials:hardBrick@0

1xitem.housestoneessentials:hardBrick@0

-----/--/-----

 

I don't know why it is doing this, but I'm sure I did something wrong....

 

Code:

Mentf (the block mentioned above):

 

package com.sqbika.housestoneessentials.block;

 

import com.sqbika.housestoneessentials.TileEntity.TileEntityMentf;

import com.sqbika.housestoneessentials.creativetab.CreativeTabHSE;

import com.sqbika.housestoneessentials.creativetab.CreativeTabLSI;

import com.sqbika.housestoneessentials.init.ModItems;

import com.sqbika.housestoneessentials.reference.RenderIDS;

import com.sqbika.housestoneessentials.utility.LogHelper;

import com.sqbika.housestoneessentials.utility.NBTHelper;

import net.minecraft.block.ITileEntityProvider;

import net.minecraft.client.particle.EntityDiggingFX;

import net.minecraft.entity.DataWatcher;

import net.minecraft.entity.Entity;

import net.minecraft.entity.item.EntityItem;

import net.minecraft.init.Blocks;

import net.minecraft.init.Items;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.world.World;

 

public class BlockMentf extends BlockHSE implements ITileEntityProvider {

 

    public BlockMentf() {

        super();

        this.setHardness(3.0F);

        this.setCreativeTab(CreativeTabLSI.LSI_TAB);

        this.setBlockName("mentf");

        this.setBlockBounds(0.1F,0f,0.1f,0.9f,0.4f,0.9f);

    }

 

    protected DataWatcher dataWatcher;

 

    @Override

    public TileEntity createNewTileEntity(World world, int tokmind1) {

        return new TileEntityMentf();

    }

 

    @Override

    public int getRenderType() {

        return RenderIDS.mentf;

    }

 

    @Override

    public boolean isOpaqueCube() {

        return false;

    }

 

    public boolean renderAsNormalBlock() {

        return false;

    }

 

    public void onEntityCollidedWithBlock(World world, int x, int y, int z, Entity entity) {

        //String Entity = String.valueOf(entity);

 

        if (entity instanceof EntityItem) {

            if (!world.isRemote) {

                ItemStack itemStack = new ItemStack(ModItems.hardBrick);

                if (((EntityItem) entity).getEntityItem().equals(itemStack)) {

                    entity.setDead();

                    EntityItem entityitem = new EntityItem(world, x, y + 2, z, new ItemStack(ModItems.lifeessence));

                    world.spawnEntityInWorld(entityitem);

                    System.out.println(entity);

                    return;

                } else {

                    System.out.println("-----/--/-----");

                    System.out.println(((EntityItem) entity).getEntityItem());

                    System.out.println(itemStack);

                    System.out.println("-----/--/-----");

                }

            }

        } else if (entity instanceof EntityDiggingFX) {

            return;

        } else {

            System.out.println("Not an Item entity. Ignored. Debug:");

            System.out.print(entity);

        }

    }

 

 

}

 

 

 

SOLUTION:

You don't have to "convert" it to an item, just get the Item using the getItem() method for the purpose of the comparison.

 

You only want to compare item stacks if you want to ensure the stack itself is equal -- meaning same number of items in the stack as well as it being same item.

 

Note totally sure, but I think in your case the code could be:

((EntityItem) entity).getEntityItem().getItem().equals(itemStack.getItem())

 

Note:

I'm using Pahimar's modding structure cause I'm a beginner >.<  ;D

You are comparing the itemstacks, not the items.  The NBT data is what is screwing it up.

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

  • Author

Ahh! Soo.. I need to convert the ItemStack to Item? Cause I cannot call the EntityItem to be able to do an if () {} with my ModItems.hardBrick :\

You don't have to "convert" it to an item, just get the Item using the getItem() method for the purpose of the comparison.

 

You only want to compare item stacks if you want to ensure the stack itself is equal -- meaning same number of items in the stack as well as it being same item.

 

Note totally sure, but I think in your case the code could be:

((EntityItem) entity).getEntityItem().getItem().equals(itemStack.getItem())

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.