Jump to content

[1.7.10] Duplicate item entity is spawning when it shouldn't


TrekkieCub314

Recommended Posts

So I have an item that places a block that can store one item inside it.  If I right-click on the block with the correct type of item and the block is empty, said item is deposited.  If I right-click on the block while the block is full, the item should drop from the block.

 

The problem I'm having is somehow I'm spawning two item entities.  I'm able to pick up one of these entities, but the other one doesn't do anything if I walk over it.  I changed the normal item drop code for my tile entity (I pulled it from another tile entity from a previous version of the project) to prevent any upward motion of the item when it's dropped.  The item that I can't pick up has no upward motion when spawned, but the one I can pick up drops like a normal item.

 

Only one entity drops when I break the block.

 

Can anyone tell me what I'm doing wrong that's causing the second item entity to spawn?

 

[spoiler=Block]

package com.trekkiecub.oddsandends.block;

import java.util.Random;

import com.trekkiecub.oddsandends.init.BlockInit;
import com.trekkiecub.oddsandends.init.ItemInit;
import com.trekkiecub.oddsandends.item.Item_HealingCrystal;
import com.trekkiecub.oddsandends.tileentity.TileEntity_HarpFiber;
import com.trekkiecub.oddsandends.tileentity.TileEntity_Sceptre;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class Block_Sceptre_Top extends BlockContainer{

public Block_Sceptre_Top(Material p_i45386_1_) {
	super(p_i45386_1_);
	// TODO Auto-generated constructor stub
}

@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
	// TODO Auto-generated method stub
	return new TileEntity_Sceptre();
}

@Override
public void breakBlock(World world, int x, int y, int z, Block block, int par6)
{
	Random rand = new Random();
	dropItems(world, x, y, z);

	float randX = rand.nextFloat() * 0.8F + 0.1F;
	float randY = rand.nextFloat() * 0.8F + 0.1F;
	float randZ = rand.nextFloat() * 0.8F + 0.1F;
	ItemStack item = new ItemStack(ItemInit.Sceptre, 1, 0);
	EntityItem entityItem = new EntityItem(world, x+randX, y+randY, z+randZ, item.copy());

/*		if (item.hasTagCompound())
	{
		entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
	}
*/	
	float factor = 0.05F;
	entityItem.motionX = rand.nextGaussian() * factor;
	entityItem.motionY = 0;
	entityItem.motionZ = rand.nextGaussian() * factor;
	world.spawnEntityInWorld(entityItem);
	super.breakBlock(world, x, y, z, block, par6);
}

private void dropItems(World world, int x, int y, int z)
{
	Random rand = new Random();
	TileEntity tileEntity = world.getTileEntity(x, y, z);

	if (!(tileEntity instanceof IInventory))
	{
		return;
	}
	IInventory inventory = (IInventory) tileEntity;

	for (int i = 0; i < inventory.getSizeInventory(); i++)
	{
		ItemStack item = inventory.getStackInSlot(i);

		if (item != null && item.stackSize > 0)
		{
			float randX = rand.nextFloat() * 0.8F + 0.1F;
			float randY = rand.nextFloat() * 0.8F + 0.1F;
			float randZ = rand.nextFloat() * 0.8F + 0.1F;

			EntityItem entityItem = new EntityItem(world, x+randX, y+randY, z+randZ, new ItemStack(item.getItem(), item.stackSize, item.getItemDamage()));

			if (item.hasTagCompound())
			{
				entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
			}

			float factor = 0.05F;
			entityItem.motionX = rand.nextGaussian() * factor;
			entityItem.motionY = 0;
			entityItem.motionZ = rand.nextGaussian() * factor;
			world.spawnEntityInWorld(entityItem);
		}
		inventory.setInventorySlotContents(i, null);
	}
}

public boolean isOpaqueCube()
    {
        return false;
    }

public boolean renderAsNormalBlock()
    {
        return false;
    }

    public int getRenderType()
    {
        return 1;
    }
    
    public void onNeighborBlockChange(World world, int x, int y, int z, Block block)
    {
    	Random rand = new Random();
    	if (!this.canBlockStay(world, x, y, z))
    	{
    		world.setBlockToAir(x, y, z);
    	}
    }
    public boolean canBlockStay(World world, int x, int y, int z)
    {
    	if (world.getBlock(x, y-1, z) == BlockInit.Sceptre_Bottom)
    	{
    		return true;
    	}
    	return false;
    }
    
    public Item getItemDropped(int p_149650_1_, Random p_149650_2_, int p_149650_3_)
    {
        return ItemInit.Sceptre;
    }
    
    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_)
    {
    	TileEntity_Sceptre thisTile = (TileEntity_Sceptre) world.getTileEntity(x, y, z);
    	if (thisTile != null)
    	{
    		ItemStack heldItem = player.getHeldItem();
    		if (heldItem != null && heldItem.stackSize > 0)
    		{
    			if (heldItem.getItem() instanceof Item_HealingCrystal)
    			{
    				if (thisTile.canAddPartial(heldItem))
    				{
    					player.setCurrentItemOrArmor(0, thisTile.mergeStack(heldItem));
    					return true;
    				}
    				else
    				{
    					this.dropItems(world, x, y, z);
    				}
    			}
    			else
    			{
    				this.dropItems(world, x, y, z);
    			}
    		}
    		else
    		{
    			this.dropItems(world, x, y, z);
    		}
    	}
        return false;
    }

}

 

 

 

[spoiler=Tile Entity]

package com.trekkiecub.oddsandends.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntity_Sceptre extends TileEntity implements IInventory{
private ItemStack itemStacks[];
private int cooldown = 0;

public TileEntity_Sceptre()
{
	itemStacks = new ItemStack[1];
}
@Override
public int getSizeInventory() {
	// TODO Auto-generated method stub
	return itemStacks.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	if (slot < 0 || slot >= this.getSizeInventory())
        return null;
    return this.itemStacks[slot];
}

@Override
public ItemStack decrStackSize(int slot, int amount) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
		if (stack.stackSize <= amount)
		{
			setInventorySlotContents(slot, null);
			markDirty();
		}
		else
		{
			stack = stack.splitStack(amount);
			if(stack.stackSize == 0)
			{
				setInventorySlotContents(slot, null);
			}
			this.markDirty();
		}
	}
	return stack;
}
public void updateEntity()
{
	if (!this.worldObj.isRemote)
    	{

    	}
    	super.updateEntity();
}
//
//  Merge the given item stack with the inventory slots either
//  until the stack is empty or the inventory runs out.
public ItemStack mergeStack(ItemStack stack)
{
	ItemStack returnThisStack = stack.copy();
	boolean addedItem = false;
	for (int i = 0; i < getSizeInventory() && returnThisStack.stackSize > 0; i++)
	{
		// This is the maximum amount we can deposit into each slot
		// based on both this tile entity and the number of items
		// Number of items can't go above the item's max stack size
		// Will never be zero: First is hard-coded, second breaks loop
		int maxPossibleStackSize = Math.min(this.getInventoryStackLimit(), returnThisStack.stackSize);
		if (itemStacks[i] == null)
		{
			setInventorySlotContents(i, returnThisStack.splitStack(maxPossibleStackSize));
			addedItem = true;
		}
		else if (itemStacks[i].getItem() == returnThisStack.getItem() && returnThisStack.isStackable())
		{
			if (itemStacks[i].getItemDamage() == returnThisStack.getItemDamage())
			{
				// Get max of both inventory limit and item stack limit, subtract current items
				// Current items can't be greater than stack limit since it's the same item
				maxPossibleStackSize = Math.min(this.getInventoryStackLimit(), returnThisStack.stackSize) - itemStacks[i].stackSize;
				if (maxPossibleStackSize > 0)
				{
					itemStacks[i].stackSize += maxPossibleStackSize;
					returnThisStack.splitStack(maxPossibleStackSize);
					addedItem = true;
				}
			}
		}
	}
	if (returnThisStack.stackSize == 0)
	{
		returnThisStack = null;
	}
	if (addedItem)
	{
		markDirty();
	}
	return returnThisStack;
}

public boolean canAddItem(ItemStack stack)
{
	for (int i = 0; i < getSizeInventory(); i++)
	{
		if (itemStacks[i] == null)
		{
			return true;
		}
	}
	return false;
}
public boolean canAddPartial(ItemStack stack)
{
	for (int i = 0; i < getSizeInventory(); i++)
	{
		if (itemStacks[i] == null)
		{
			return true;
		}
	}
	return false;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null)
	{
		setInventorySlotContents(slot, null);
	}
	return stack;
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	itemStacks[slot] = stack;
	if (slot < 0 || slot >= this.getSizeInventory())
	{
		return;
	}
	if (stack != null && stack.stackSize > getInventoryStackLimit())
	{
		stack.stackSize = getInventoryStackLimit();
	}

	if (stack != null && stack.stackSize == 0)
	{
		stack = null;
	}
	this.itemStacks[slot] = stack;
	this.markDirty();
}

@Override
public String getInventoryName() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean hasCustomInventoryName() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public int getInventoryStackLimit() {
	// TODO Auto-generated method stub
	return 1;
}

@Override
public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public void openInventory() {
	// TODO Auto-generated method stub

}

@Override
public void closeInventory() {
	// TODO Auto-generated method stub

}

@Override
public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
	// TODO Auto-generated method stub
	return false;
}

@Override
    public void readFromNBT(NBTTagCompound tag)
    {
        super.readFromNBT(tag);
        NBTTagList nbttaglist = tag.getTagList("Items", 10);
        
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
        	NBTTagCompound tag1 = nbttaglist.getCompoundTagAt(i);
        	int j = tag1.getByte("Slot") & 255;
        	
        	setInventorySlotContents(j, ItemStack.loadItemStackFromNBT(tag1));
        }
        
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        super.writeToNBT(tag);
    	
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < getSizeInventory(); ++i)
        {
            if (getStackInSlot(i) != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                getStackInSlot(i).writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }
        tag.setTag("Items", nbttaglist);
    }
       
    @Override
public Packet getDescriptionPacket() {
	return null;
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
}

}

 

 

Link to comment
Share on other sites

Is Forge for 1.10 complete?  I ask because when I tried to update to 1.8, Forge wasn't sufficiently complete to allow new villagers, at the very least.

It is mostly done, if not it is complete.

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

Is Forge for 1.10 complete?

 

Has been for months.

There's always updates, yes, but it's fully functional.

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.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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