BloodMagic/src/main/java/WayofTime/alchemicalWizardry/common/block/BlockFilledSocket.java

118 lines
3.9 KiB
Java
Raw Normal View History

package WayofTime.alchemicalWizardry.common.block;
2014-10-13 20:33:20 +00:00
import WayofTime.alchemicalWizardry.api.items.interfaces.ArmourUpgrade;
import WayofTime.alchemicalWizardry.common.tileEntity.TESocket;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
2015-07-29 15:30:24 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2015-07-29 15:30:24 +00:00
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
2014-10-13 20:33:20 +00:00
import java.util.Random;
2015-07-29 15:30:24 +00:00
public class BlockFilledSocket extends BlockContainer
{
2015-07-29 15:30:24 +00:00
public BlockFilledSocket()
{
super(Material.rock);
setHardness(2.0F);
setResistance(5.0F);
//func_111022_d("AlchemicalWizardry:blocks");
}
@Override
2015-07-29 15:30:24 +00:00
public boolean onBlockActivated(World world, BlockPos blockPos, IBlockState blockState, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ)
{
2015-07-29 15:30:24 +00:00
TESocket tileEntity = (TESocket) world.getTileEntity(blockPos);
if (tileEntity == null || player.isSneaking())
{
return false;
}
ItemStack playerItem = player.getCurrentEquippedItem();
if (tileEntity.getStackInSlot(0) == null && playerItem != null)
{
if (playerItem.getItem() instanceof ArmourUpgrade)
{
ItemStack newItem = playerItem.copy();
newItem.stackSize = 1;
--playerItem.stackSize;
tileEntity.setInventorySlotContents(0, newItem);
2015-06-14 14:33:01 +00:00
return true;
}
2015-06-14 14:33:01 +00:00
else
{
return false;
}
}
else if (tileEntity.getStackInSlot(0) != null && playerItem == null)
{
player.inventory.addItemStackToInventory(tileEntity.getStackInSlot(0));
tileEntity.setInventorySlotContents(0, null);
tileEntity.setActive();
2015-06-14 14:33:01 +00:00
return true;
}
2015-07-29 15:30:24 +00:00
world.markBlockForUpdate(blockPos);
2015-06-14 14:33:01 +00:00
return false;
}
@Override
2015-07-29 18:35:00 +00:00
public void breakBlock(World world, BlockPos blockPos, IBlockState blockState)
{
2015-07-29 18:35:00 +00:00
dropItems(world, blockPos);
super.breakBlock(world, blockPos, blockState);
}
2015-07-29 18:35:00 +00:00
private void dropItems(World world, BlockPos blockPos)
{
Random rand = new Random();
2015-07-29 18:35:00 +00:00
TileEntity tileEntity = world.getTileEntity(blockPos);
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 rx = rand.nextFloat() * 0.8F + 0.1F;
float ry = rand.nextFloat() * 0.8F + 0.1F;
float rz = rand.nextFloat() * 0.8F + 0.1F;
2015-07-29 18:35:00 +00:00
EntityItem entityItem = new EntityItem(world, blockPos.getX() + rx, blockPos.getY() + ry, blockPos.getZ() + rz, 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 = rand.nextGaussian() * factor + 0.2F;
entityItem.motionZ = rand.nextGaussian() * factor;
world.spawnEntityInWorld(entityItem);
item.stackSize = 0;
}
}
}
@Override
public TileEntity createNewTileEntity(World world, int meta)
{
return new TESocket();
}
}