Posted April 11, 20187 yr So I'm following a tutorial on how to create a TileEntity, but for some reason I got an error with the method heldItem: package tutuicraft3.sapphirecraft.blocks; import javax.annotation.Nullable; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import tutuicraft3.sapphirecraft.Main; public class MSapphire_Infusor extends BlockTileEntity<TileEntityInfusor> { public MSapphire_Infusor() { super("msapphire_infusor", Material.IRON); } @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { if (!world.isRemote) { TileEntityInfusor tile = getTileEntity(world, pos); IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); if (!player.isSneaking()) { if (heldItem.isEmpty()) { player.setHeldItem(hand, itemHandler.extractItem(0, 64, false)); } else { player.setHeldItem(hand, itemHandler.insertItem(0, heldItem, false)); } tile.markDirty(); } else { ItemStack stack = itemHandler.getStackInSlot(0); if (!stack.isEmpty()) { String localized = Main.proxy.localize(stack.getUnlocalizedName() + ".name"); player.sendMessage(new TextComponentString(stack.getCount() + "x " + localized)); } else { player.sendMessage(new TextComponentString("Empty")); } } } return true; } @Override public void breakBlock(World world, BlockPos pos, IBlockState state) { TileEntityInfusor tile = getTileEntity(world, pos); IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH); ItemStack stack = itemHandler.getStackInSlot(0); if (!stack.isEmpty()) { EntityItem item = new EntityItem(world, pos.getX(), pos.getY(), pos.getZ(), stack); world.spawnEntity(item); } super.breakBlock(world, pos, state); } @Override public Class<TileEntityInfusor> getTileEntityClass() { return TileEntityInfusor.class; } @Nullable @Override public TileEntityInfusor createTileEntity(World world, IBlockState state) { return new TileEntityInfusor(); } } I was wondering if someone could help me, please. Thanks in advance. Edited April 12, 20187 yr by Arthur Wesley
April 11, 20187 yr You should not be using setHeldItem at all. This is how I do something similar: https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/block/BlockTanner.java#L121-L148 Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable. If you think this is the case, JUST REPORT ME. Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice. Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked. DO NOT PM ME WITH PROBLEMS. No help will be given.
April 11, 20187 yr Author 5 minutes ago, Draco18s said: You should not be using setHeldItem at all. This is how I do something similar: https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/farming/block/BlockTanner.java#L121-L148 Oh, thanks, that make things more clear.
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.