Jump to content

Can't fix item dupe issue with EntityItem


MCZaphelon

Recommended Posts

Hey all. I've been messing around with a pickaxe that "auto-smelts" iron and gold (i.e directly drops the ingot instead of ore) and have run into an annoying issue.

When mining an ore that is able to get "smelted", the first few drops are fine. After some time though, the ingots start duping into stacks of about 2-4. I noted in Block#spawnAsEntity that !worldIn.restoringBlockSnapshots is the check used to stop this from happening. I have passed this in a few different places in the onBlockDestroyed method of my pickaxe's item class, but to no avail. I did however have it seemingly working fine using user.entityDropItem but that drops the item at the player's feet which is not ideal.

My class file ItemVOHMagmaPickaxe below:
 

Spoiler

package mczaphelon.voh.item;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Nullable;

import mczaphelon.voh.init.VOHEnchantments;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ItemVOHMagmaPickaxe extends ItemVOHPickaxe{

	private Map<Block, ItemStack> smeltableBlocks = new HashMap<Block, ItemStack>();
	private Map<Block, Item> extractableBlocks = new HashMap<Block, Item>();
	
	public ItemVOHMagmaPickaxe(String name, ToolMaterial material) {
		super(name, material);
		this.addConversion(Blocks.IRON_ORE, new ItemStack(Items.IRON_INGOT));
		this.addExtraction(Blocks.IRON_ORE, Items.IRON_NUGGET);
		this.addConversion(Blocks.GOLD_ORE, new ItemStack(Items.GOLD_INGOT));
		this.addExtraction(Blocks.GOLD_ORE, Items.GOLD_NUGGET);
	}
	
	//allows anyone to add a smelting "recipe" to the magma pickaxe
	public void addConversion(Block input, ItemStack output) {
		smeltableBlocks.put(input, output);
	}
	
	public Map<Block, ItemStack> getConversions(){
		return this.smeltableBlocks;
	}
	
	//allows adding of extra Extraction enchantment drops
	public void addExtraction(Block input, Item output) {
		extractableBlocks.put(input, output);
	}
	
	public Map<Block, Item> getExtractions(){
		return this.extractableBlocks;
	}
	
	public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase user)
    {
        Block targetBlock = state.getBlock();
		
		if (!worldIn.isRemote && smeltableBlocks.containsKey(targetBlock)) {
        	this.dropItemAtBlockLocation(worldIn, pos, smeltableBlocks.get(targetBlock));
        	worldIn.setBlockToAir(pos);
        }
		
		if (!worldIn.isRemote && extractableBlocks.containsKey(targetBlock)) {
			
			int extractionLevel = EnchantmentHelper.getEnchantmentLevel(VOHEnchantments.extraction, stack);
			
			if (extractionLevel > 0) {
				
				Random rand = new Random();
				int amount = extractionLevel/2 + rand.nextInt(extractionLevel + 1);
				
				this.dropItemAtBlockLocation(worldIn, pos, new ItemStack(extractableBlocks.get(targetBlock), amount));
			}
		}
		
		if (!worldIn.isRemote && (double)state.getBlockHardness(worldIn, pos) != 0.0D)
        {
            stack.damageItem(1, user);
        }

        return true;
    }
	
	@Nullable
	public EntityItem dropItemAtBlockLocation(World world, BlockPos pos, ItemStack stack)
	{
		if (stack.isEmpty())
        {
            return null;
        }
        else
        {
        	double d0 = (double)(world.rand.nextFloat() * 0.5F) + 0.25D;
            double d1 = (double)(world.rand.nextFloat() * 0.5F) + 0.25D;
            double d2 = (double)(world.rand.nextFloat() * 0.5F) + 0.25D;
        	
            EntityItem entityitem = new EntityItem(world, (double)pos.getX() + d0, (double)pos.getY() + d1, (double)pos.getZ() + d2, stack);
            entityitem.setDefaultPickupDelay();
            world.spawnEntity(entityitem);
            
            return entityitem;
        }
	}
	
	public float getDestroySpeed(ItemStack stack, IBlockState state)
    {
        Block block = state.getBlock();
        return block == Blocks.OBSIDIAN ? this.efficiency*4.0F : super.getDestroySpeed(stack, state);
    }
	
	@SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
    {
		tooltip.add("Automatically smelts ore");
		tooltip.add("Mines obsidian much faster");
		tooltip.add("Right-click to light fire");
		tooltip.add("");
    }
	
	public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
        pos = pos.offset(facing);
        ItemStack itemstack = player.getHeldItem(hand);

        if (!player.canPlayerEdit(pos, facing, itemstack))
        {
            return EnumActionResult.FAIL;
        }
        else
        {
            if (worldIn.isAirBlock(pos))
            {
                worldIn.playSound(player, pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, itemRand.nextFloat() * 0.4F + 0.8F);
                worldIn.setBlockState(pos, Blocks.FIRE.getDefaultState(), 11);
            }

            if (player instanceof EntityPlayerMP)
            {
                CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)player, pos, itemstack);
            }

            return EnumActionResult.SUCCESS;
        }
    }
}

 

 

Edited by MCZaphelon

I'm a casual modder and gamer! I'll always do my best to be nice, and to help with code issues even though there are better modders out there. If you're trying to help me, I have a good understanding of Java and programming techniques.

 

Please do not PM me on this website, instead, head over to the Minecraft Forums and PM me there (same account name), as I check that more. Remember, I'm only as good as the errors I know how to fix.

Link to comment
Share on other sites

2 minutes ago, Animefan8888 said:

@MCZaphelon You should only spawn entities on the Server. IE is !world.isRemote

My current code checks for that. It's not a ghost-dupe, the dupes are physically there and can be picked up unlike what happens without checking for !world.isRemote

I'm a casual modder and gamer! I'll always do my best to be nice, and to help with code issues even though there are better modders out there. If you're trying to help me, I have a good understanding of Java and programming techniques.

 

Please do not PM me on this website, instead, head over to the Minecraft Forums and PM me there (same account name), as I check that more. Remember, I'm only as good as the errors I know how to fix.

Link to comment
Share on other sites

3 minutes ago, MCZaphelon said:

My current code checks for that. It's not a ghost-dupe, the dupes are physically there and can be picked up unlike what happens without checking for !world.isRemote 

Is onBlockDestroyed being called twice?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Hmm, I don't see how. I'm not calling super or anything. Plus, that wouldn't explain the inconsistency (dropping correct amount at the start but then starts duping after a few blocks mined) and the fact that it works with entityDropItem. I feel like it's something to do with the stuff about "restoring blockstates", but I don't understand enough about that to really know.

I'll check back in the morning, it's pretty late here.

I'm a casual modder and gamer! I'll always do my best to be nice, and to help with code issues even though there are better modders out there. If you're trying to help me, I have a good understanding of Java and programming techniques.

 

Please do not PM me on this website, instead, head over to the Minecraft Forums and PM me there (same account name), as I check that more. Remember, I'm only as good as the errors I know how to fix.

Link to comment
Share on other sites

10 hours ago, diesieben07 said:

You cannot re-use ItemStack instances like that. You must clone the stack before spawning it in the world (ItemStack#copy).

Thanks a ton! That worked perfectly.

Edited by MCZaphelon

I'm a casual modder and gamer! I'll always do my best to be nice, and to help with code issues even though there are better modders out there. If you're trying to help me, I have a good understanding of Java and programming techniques.

 

Please do not PM me on this website, instead, head over to the Minecraft Forums and PM me there (same account name), as I check that more. Remember, I'm only as good as the errors I know how to fix.

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.