diff --git a/APIs/ic2/api/tile/IWrenchable.java b/APIs/ic2/api/tile/IWrenchable.java new file mode 100644 index 00000000..944f4a15 --- /dev/null +++ b/APIs/ic2/api/tile/IWrenchable.java @@ -0,0 +1,61 @@ +package ic2.api.tile; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +/** + * Allows a tile entity to make use of the wrench's removal and rotation functions. + */ +public interface IWrenchable { + /** + * Determine if the wrench can be used to set the block's facing. + * Called before wrenchCanRemove(). + * + * @param entityPlayer player using the wrench, may be null + * @param side block's side the wrench was clicked on + * @return Whether the wrenching was done and the wrench should be damaged + */ + boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side); + + /** + * Get the block's facing. + * + * @return Block facing + */ + short getFacing(); + + /** + * Set the block's facing + * + * @param facing facing to set the block to + */ + void setFacing(short facing); + + /** + * Determine if the wrench can be used to remove the block. + * Called if wrenchSetFacing fails. + * + * @param entityPlayer player using the wrench, may be null + * @return Whether the wrenching was done and the wrench should be damaged + */ + boolean wrenchCanRemove(EntityPlayer entityPlayer); + + /** + * Determine the probability to drop the block as it is. + * The first entry in getBlockDropped will be replaced by blockid:meta if the drop is successful. + * + * @return Probability from 0 to 1 + */ + float getWrenchDropRate(); + + /** + * Determine the item the block will drop when the wrenching is successful. + * + * The ItemStack will be copied before creating the EntityItem. + * + * @param entityPlayer player using the wrench, may be null + * @return ItemStack to drop, may be null + */ + ItemStack getWrenchDrop(EntityPlayer entityPlayer); +} + diff --git a/src/com/builtbroken/assemblyline/item/ItemWrench.java b/src/com/builtbroken/assemblyline/item/ItemWrench.java index 943a2569..6a3240ee 100644 --- a/src/com/builtbroken/assemblyline/item/ItemWrench.java +++ b/src/com/builtbroken/assemblyline/item/ItemWrench.java @@ -1,9 +1,12 @@ package com.builtbroken.assemblyline.item; +import universalelectricity.api.vector.Vector3; +import ic2.api.tile.IWrenchable; import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.Configuration; import net.minecraftforge.common.ForgeDirection; @@ -13,6 +16,7 @@ import buildcraft.api.tools.IToolWrench; import com.builtbroken.assemblyline.AssemblyLine; import com.builtbroken.minecraft.DarkCore; import com.builtbroken.minecraft.IExtraInfo.IExtraItemInfo; +import com.builtbroken.minecraft.helpers.DarksHelper; import com.builtbroken.minecraft.prefab.ItemBasic; public class ItemWrench extends ItemBasic implements IToolWrench, IExtraItemInfo @@ -31,11 +35,36 @@ public class ItemWrench extends ItemBasic implements IToolWrench, IExtraItemInfo @Override public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { - if (Block.blocksList[world.getBlockId(x, y, z)].rotateBlock(world, x, y, z, ForgeDirection.getOrientation(side))) + TileEntity tile = world.getBlockTileEntity(x, y, z); + Block block = Block.blocksList[world.getBlockId(x, y, z)]; + if (block != null && block.rotateBlock(world, x, y, z, ForgeDirection.getOrientation(side))) { this.wrenchUsed(player, x, y, z); return true; } + if (tile instanceof IWrenchable && ((IWrenchable) tile).wrenchCanSetFacing(player, side)) + { + ForgeDirection direction = ForgeDirection.getOrientation(side); + short setSide = 0; + if (player.isSneaking()) + { + direction = direction.getOpposite(); + } + setSide = (short) direction.ordinal(); + if (setSide != ((IWrenchable) tile).getFacing()) + { + ((IWrenchable) tile).setFacing(setSide); + } + else if (((IWrenchable) tile).wrenchCanRemove(player)) + { + ItemStack output = ((IWrenchable) tile).getWrenchDrop(player); + if(output != null) + { + world.setBlockToAir(x, y, z); + DarksHelper.dropItemStack(world, new Vector3(x,y,z), output, false); + } + } + } return false; }