package emasher.items;
import buildcraft.api.tools.IToolWrench;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import emasher.EngineersToolbox;
import net.minecraft.block.Block;
import net.minecraft.block.BlockButton;
import net.minecraft.block.BlockChest;
import net.minecraft.block.BlockLever;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import java.util.HashSet;
import java.util.Set;
public class ItemEngWrench extends Item implements IToolWrench {
private final Set<Class<? extends Block>> shiftRotations = new HashSet<Class<? extends Block>>();
@SideOnly( Side.CLIENT )
public IIcon textures[];
public ItemEngWrench() {
super();
this.setCreativeTab( EngineersToolbox.tabItems() );
this.setMaxStackSize( 1 );
this.setUnlocalizedName( "eng_wrench" );
shiftRotations.add( BlockLever.class );
shiftRotations.add( BlockButton.class );
shiftRotations.add( BlockChest.class );
}
@Override
@SideOnly( Side.CLIENT )
public void registerIcons( IIconRegister ir ) {
textures = new IIcon[3];
textures[0] = ir.registerIcon( "eng_toolbox:wrench0" );
textures[1] = ir.registerIcon( "eng_toolbox:wrench1" );
textures[2] = ir.registerIcon( "eng_toolbox:wrench2" );
}
@Override
@SideOnly( Side.CLIENT )
public IIcon getIconFromDamage( int damage ) {
return textures[damage];
}
@Override
public ItemStack onItemRightClick( ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer ) {
float f = 1.0F;
double d0 = par3EntityPlayer.prevPosX + ( par3EntityPlayer.posX - par3EntityPlayer.prevPosX ) * ( double ) f;
double d1 = par3EntityPlayer.prevPosY + ( par3EntityPlayer.posY - par3EntityPlayer.prevPosY ) * ( double ) f + 1.62D - ( double ) par3EntityPlayer.yOffset;
double d2 = par3EntityPlayer.prevPosZ + ( par3EntityPlayer.posZ - par3EntityPlayer.prevPosZ ) * ( double ) f;
MovingObjectPosition movingobjectposition = this.getMovingObjectPositionFromPlayer( par2World, par3EntityPlayer, true );
if( movingobjectposition == null ) {
if( par3EntityPlayer.isSneaking() && !par2World.isRemote ) {
par1ItemStack.setItemDamage( par1ItemStack.getItemDamage() + 1 );
if( par1ItemStack.getItemDamage() > 2 ) par1ItemStack.setItemDamage( 0 );
}
}
return par1ItemStack;
}
@Override
public boolean canWrench( EntityPlayer player, int x, int y, int z ) {
return true;
}
@Override
public void wrenchUsed( EntityPlayer player, int x, int y, int z ) {
player.swingItem();
}
private boolean isShiftRotation( Class<? extends Block> cls ) {
for( Class<? extends Block> shift : shiftRotations ) {
if( shift.isAssignableFrom( cls ) )
return true;
}
return false;
}
@Override
public boolean onItemUseFirst( ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ ) {
//int blockId = world.getBlockId(x, y, z);
//Block block = Block.blocksList[blockId];
Block block = world.getBlock( x, y, z );
if( block == null )
return false;
if( player.isSneaking() != isShiftRotation( block.getClass() ) )
return false;
if( block.rotateBlock( world, x, y, z, ForgeDirection.getOrientation( side ) ) ) {
player.swingItem();
return !world.isRemote;
}
return false;
}
@Override
public boolean doesSneakBypassUse( World world, int x, int y, int z, EntityPlayer player ) {
return true;
}
@Override
public boolean isItemTool( ItemStack par1ItemStack ) {
return true;
}
}