ripped off omniwrench code for wrench functionality

This commit is contained in:
MachineMuse 2015-09-07 14:57:34 -06:00
parent c1d044c28d
commit bc6e9f920c
2 changed files with 131 additions and 8 deletions

View file

@ -44,14 +44,18 @@ class OmniWrenchModule(validItems: List[IModularItem]) extends PowerModuleBase(v
def onItemUse(itemStack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float) {}
def onItemUseFirst(stack: ItemStack, player: EntityPlayer, world: World, x: Int, y: Int, z: Int, side: Int, hitX: Float, hitY: Float, hitZ: Float): Boolean = {
world.blockExists(x, y, z) && world.canMineBlock(player, x, y, z) && (world.getBlock(x, y, z) match {
case block: Block if block.rotateBlock(world, x, y, z, ForgeDirection.getOrientation(side)) =>
block.onNeighborBlockChange(world, x, y, z, Blocks.air)
player.swingItem()
!world.isRemote
case _ =>
false
})
if (!StolenWrenchCode.onItemUseFirst(stack, player, world, x, y, z, side, hitX, hitY, hitZ)) {
world.blockExists(x, y, z) && world.canMineBlock(player, x, y, z) && (world.getBlock(x, y, z) match {
case block: Block if block.rotateBlock(world, x, y, z, ForgeDirection.getOrientation(side)) =>
block.onNeighborBlockChange(world, x, y, z, Blocks.air)
player.swingItem()
!world.isRemote
case _ =>
false
})
} else {
false
}
}
def onPlayerStoppedUsing(itemStack: ItemStack, world: World, player: EntityPlayer, par4: Int) {}

View file

@ -0,0 +1,119 @@
package net.machinemuse.powersuits.powermodule.tool;
import cofh.api.block.IDismantleable;
import cofh.lib.util.helpers.BlockHelper;
import cofh.lib.util.helpers.ServerHelper;
import cpw.mods.fml.common.eventhandler.Event;
import ic2.api.tile.IWrenchable;
import net.machinemuse.powersuits.common.ModCompatibility;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
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.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import java.util.List;
/**
* Created by MachineMuse on 9/7/2015.
*
* Unabashedly ripped off of the Prototype Omniwrench in Redstone Arsenal.
*
*/
public class StolenWrenchCode {
public static void useEnergy(ItemStack stack, boolean simulate) {
// TBI, maybe?
}
public static boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int hitSide, float hitX, float hitY, float hitZ) {
if (stack.getItemDamage() > 0) {
stack.setItemDamage(0);
}
// if (!player.capabilities.isCreativeMode && getEnergyStored(stack) < getEnergyPerUse(stack)) {
// return false;
// }
Block block = world.getBlock(x, y, z);
if (block == null) {
return false;
}
PlayerInteractEvent event = new PlayerInteractEvent(player, PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK, x, y, z, hitSide, world);
if (MinecraftForge.EVENT_BUS.post(event) || event.getResult() == Event.Result.DENY || event.useBlock == Event.Result.DENY || event.useItem == Event.Result.DENY) {
return false;
}
if (ModCompatibility.isCoFHCoreLoaded() && ServerHelper.isServerWorld(world) && player.isSneaking() && block instanceof IDismantleable
&& ((IDismantleable) block).canDismantle(player, world, x, y, z)) {
((IDismantleable) block).dismantleBlock(player, world, x, y, z, false);
if (!player.capabilities.isCreativeMode) {
useEnergy(stack, false);
}
return true;
}
// if (BlockHelper.canRotate(block)) {
// if (player.isSneaking()) {
// world.setBlockMetadataWithNotify(x, y, z, BlockHelper.rotateVanillaBlockAlt(world, block, x, y, z), 3);
// world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, block.stepSound.getBreakSound(), 1.0F, 0.6F);
// } else {
// world.setBlockMetadataWithNotify(x, y, z, BlockHelper.rotateVanillaBlock(world, block, x, y, z), 3);
// world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, block.stepSound.getBreakSound(), 1.0F, 0.8F);
// }
// if (!player.capabilities.isCreativeMode) {
// useEnergy(stack, false);
// }
// return ServerHelper.isServerWorld(world);
// } else if (!player.isSneaking() && block.rotateBlock(world, x, y, z, ForgeDirection.getOrientation(hitSide))) {
// if (!player.capabilities.isCreativeMode) {
// useEnergy(stack, false);
// }
// return ServerHelper.isServerWorld(world);
// }
TileEntity tile = world.getTileEntity(x, y, z);
if (ModCompatibility.isIndustrialCraftLoaded() && tile instanceof IWrenchable) {
IWrenchable wrenchable = (IWrenchable) tile;
if (player.isSneaking()) {
hitSide = BlockHelper.SIDE_OPPOSITE[hitSide];
}
if (wrenchable.wrenchCanSetFacing(player, hitSide)) {
if (ServerHelper.isServerWorld(world)) {
wrenchable.setFacing((short) hitSide);
}
} else if (wrenchable.wrenchCanRemove(player)) {
ItemStack dropBlock = wrenchable.getWrenchDrop(player);
if (dropBlock != null) {
world.setBlockToAir(x, y, z);
if (ServerHelper.isServerWorld(world)) {
List<ItemStack> drops = block.getDrops(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
if (drops.isEmpty()) {
drops.add(dropBlock);
} else {
drops.set(0, dropBlock);
}
for (ItemStack drop : drops) {
float f = 0.7F;
double x2 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
double y2 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
double z2 = world.rand.nextFloat() * f + (1.0F - f) * 0.5D;
EntityItem entity = new EntityItem(world, x + x2, y + y2, z + z2, drop);
entity.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(entity);
}
}
}
}
if (!player.capabilities.isCreativeMode) {
useEnergy(stack, false);
}
return ServerHelper.isServerWorld(world);
}
return false;
}
}