Jump to content

[SOLVED] Item comparison


Sqbika

Recommended Posts

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

Link to comment
Share on other sites

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/

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.

Announcements



×
×
  • Create New...

Important Information

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