BiomesOPlenty/common/biomesoplenty/items/ItemBOPBucket.java

252 lines
6.1 KiB
Java
Raw Normal View History

package biomesoplenty.items;
import java.util.List;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumMovingObjectType;
import net.minecraft.util.Icon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import biomesoplenty.BiomesOPlenty;
2013-07-09 04:10:57 +00:00
import biomesoplenty.api.Fluids;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2013-05-31 10:34:02 +00:00
public class ItemBOPBucket extends Item
{
2013-05-31 10:34:02 +00:00
private int isFull;
2013-10-28 04:20:08 +00:00
private static final String[] bucketTypes = new String[] {"amethyst_empty", "liquid_poison", "amethyst_spring_water", "honey"};
2013-05-31 10:34:02 +00:00
@SideOnly(Side.CLIENT)
private Icon[] textures;
2013-05-31 10:34:02 +00:00
public ItemBOPBucket(int i)
{
super(i);
this.setHasSubtypes(true);
this.setCreativeTab(BiomesOPlenty.tabBiomesOPlenty);
}
2013-05-31 10:34:02 +00:00
@Override
2013-07-09 05:14:10 +00:00
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player)
2013-05-31 10:34:02 +00:00
{
float f = 1.0F;
2013-07-09 05:14:10 +00:00
double d0 = player.prevPosX + (player.posX - player.prevPosX) * f;
double d1 = player.prevPosY + (player.posY - player.prevPosY) * f + 1.62D - player.yOffset;
double d2 = player.prevPosZ + (player.posZ - player.prevPosZ) * f;
isFull = getLiquidIDFromMeta(itemstack.getItemDamage());
2013-05-31 10:34:02 +00:00
boolean flag = isFull == 0;
2013-07-09 05:14:10 +00:00
if (itemstack.getItemDamage() == 0)
2013-05-31 10:34:02 +00:00
{
2013-07-09 05:14:10 +00:00
MovingObjectPosition pos = this.getMovingObjectPositionFromPlayer(world, player, true);
if (pos == null)
{
return itemstack;
}
else
{
int blockID = world.getBlockId(pos.blockX, pos.blockY, pos.blockZ);
int meta = world.getBlockMetadata(pos.blockX, pos.blockY, pos.blockZ);
if ((blockID == Fluids.springWater.get().blockID) && meta == 0)
{
if (player.capabilities.isCreativeMode)
{
world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
return itemstack;
}
if (--itemstack.stackSize <= 0)
{
world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
return new ItemStack(Fluids.bopBucket.get(), 1, 2);
}
if (!player.inventory.addItemStackToInventory(new ItemStack(Fluids.bopBucket.get(), 1, 2)))
{
world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
player.dropPlayerItem(new ItemStack(Fluids.bopBucket.get(), 1, 2));
}
return itemstack;
}
else
{
return itemstack;
}
}
}
else
{
MovingObjectPosition pos = this.getMovingObjectPositionFromPlayer(world, player, flag);
if (pos == null)
{
return itemstack;
}
else if (pos.typeOfHit == EnumMovingObjectType.TILE)
2013-05-31 10:34:02 +00:00
{
2013-07-09 05:14:10 +00:00
int i = pos.blockX;
int j = pos.blockY;
int k = pos.blockZ;
if (!world.canMineBlock(player, i, j, k))
return itemstack;
if (isFull < 0)
return new ItemStack(Item.bucketEmpty);
if (pos.sideHit == 0)
2013-05-31 10:34:02 +00:00
{
2013-07-09 05:14:10 +00:00
--j;
}
2013-05-31 10:34:02 +00:00
2013-07-09 05:14:10 +00:00
if (pos.sideHit == 1)
{
++j;
}
2013-05-31 10:34:02 +00:00
2013-07-09 05:14:10 +00:00
if (pos.sideHit == 2)
{
--k;
}
2013-05-31 10:34:02 +00:00
2013-07-09 05:14:10 +00:00
if (pos.sideHit == 3)
{
++k;
}
if (pos.sideHit == 4)
{
--i;
}
if (pos.sideHit == 5)
{
++i;
}
if (!player.canPlayerEdit(i, j, k, pos.sideHit, itemstack))
return itemstack;
if (this.tryPlaceContainedLiquid(world, d0, d1, d2, i, j, k) && !player.capabilities.isCreativeMode)
{
if (itemstack.getItemDamage() != 2)
return new ItemStack(Item.bucketEmpty);
else
return new ItemStack(Fluids.bopBucket.get(), 1, 0);
2013-05-31 10:34:02 +00:00
}
}
}
2013-07-09 05:14:10 +00:00
return itemstack;
2013-05-31 10:34:02 +00:00
}
/**
* Attempts to place the liquid contained inside the bucket.
*/
public boolean tryPlaceContainedLiquid(World par1World, double par2, double par4, double par6, int par8, int par9, int par10)
{
if (isFull <= 0)
return false;
else if (!par1World.isAirBlock(par8, par9, par10) && par1World.getBlockMaterial(par8, par9, par10).isSolid())
return false;
else
{
2013-07-09 04:10:57 +00:00
if (par1World.provider.isHellWorld && isFull == Fluids.springWater.get().blockID)
2013-05-31 10:34:02 +00:00
{
par1World.playSoundEffect(par2 + 0.5D, par4 + 0.5D, par6 + 0.5D, "random.fizz", 0.5F, 2.6F + (par1World.rand.nextFloat() - par1World.rand.nextFloat()) * 0.8F);
for (int l = 0; l < 8; ++l)
{
par1World.spawnParticle("largesmoke", par8 + Math.random(), par9 + Math.random(), par10 + Math.random(), 0.0D, 0.0D, 0.0D);
}
}
else
{
par1World.setBlock(par8, par9, par10, isFull, 0, 3);
}
return true;
}
}
2013-07-22 07:44:39 +00:00
@Override
public boolean hasContainerItem()
{
return true;
}
2013-07-22 07:44:39 +00:00
@Override
public ItemStack getContainerItemStack(ItemStack itemstack)
{
return itemstack.getItemDamage() == 2 ? new ItemStack(Fluids.bopBucket.get(), 1, 0) : new ItemStack(Item.bucketEmpty, 1);
}
2013-05-31 10:34:02 +00:00
private static int getLiquidIDFromMeta(int meta)
{
switch (meta)
{
case 1:
2013-07-09 05:14:10 +00:00
return Fluids.liquidPoison.get().blockID;
2013-05-31 10:34:02 +00:00
case 2:
2013-07-09 05:14:10 +00:00
return Fluids.springWater.get().blockID;
2013-10-28 04:20:08 +00:00
case 3:
return Fluids.honey.get().blockID;
2013-05-31 10:34:02 +00:00
default:
2013-07-09 04:10:57 +00:00
return Fluids.liquidPoison.get().blockID;
2013-05-31 10:34:02 +00:00
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
{
for (int i = 0; i < bucketTypes.length; ++i)
{
par3List.add(new ItemStack(par1, 1, i));
}
}
@Override
public String getUnlocalizedName(ItemStack itemStack)
{
2013-05-31 10:34:02 +00:00
int meta = itemStack.getItemDamage();
if (meta < 0 || meta >= bucketTypes.length) {
meta = 0;
}
2013-06-09 20:48:05 +00:00
return super.getUnlocalizedName() + "." + bucketTypes[meta];
}
2013-05-31 10:34:02 +00:00
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconRegister)
{
textures = new Icon[bucketTypes.length];
2013-05-31 10:34:02 +00:00
for (int i = 0; i < bucketTypes.length; ++i) {
textures[i] = iconRegister.registerIcon("biomesoplenty:" + "bucket_" + bucketTypes[i]);
2013-05-31 10:34:02 +00:00
}
}
@Override
public Icon getIconFromDamage(int meta)
{
return textures[meta];
}
}