2016-02-26 22:46:55 +01:00
|
|
|
package techreborn.items.tools;
|
|
|
|
|
2016-02-27 10:15:14 +01:00
|
|
|
import net.minecraft.block.BlockDynamicLiquid;
|
|
|
|
import net.minecraft.block.BlockStaticLiquid;
|
2016-12-07 14:18:18 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2016-02-26 22:46:55 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.Blocks;
|
2016-02-27 10:15:14 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
|
|
|
import net.minecraft.item.ItemBlock;
|
2016-02-26 22:46:55 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-03-13 19:38:41 +01:00
|
|
|
import net.minecraft.util.EnumActionResult;
|
2016-03-15 09:13:05 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-13 19:38:41 +01:00
|
|
|
import net.minecraft.util.EnumHand;
|
2016-05-08 12:42:59 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2016-03-13 17:08:30 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-02-26 22:46:55 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-03-14 14:28:33 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-11-05 16:10:28 +01:00
|
|
|
import net.minecraftforge.server.permission.PermissionAPI;
|
|
|
|
import net.minecraftforge.server.permission.context.BlockPosContext;
|
2016-10-08 21:46:16 +02:00
|
|
|
import reborncore.common.IWrenchable;
|
2016-03-27 19:38:43 +02:00
|
|
|
import reborncore.common.tile.TileMachineBase;
|
2016-11-05 16:10:28 +01:00
|
|
|
import reborncore.common.util.RebornPermissions;
|
2016-03-28 14:35:59 +02:00
|
|
|
import techreborn.blocks.fluid.BlockFluidBase;
|
2016-12-07 14:18:18 +01:00
|
|
|
import techreborn.blocks.storage.BlockEnergyStorage;
|
2016-02-26 22:46:55 +01:00
|
|
|
import techreborn.client.TechRebornCreativeTabMisc;
|
2016-09-06 23:16:21 +02:00
|
|
|
import techreborn.compat.CompatManager;
|
2016-05-08 12:42:59 +02:00
|
|
|
import techreborn.init.ModSounds;
|
2016-12-12 08:03:51 +01:00
|
|
|
import techreborn.items.ItemTRNoDestroy;
|
2016-09-06 23:16:21 +02:00
|
|
|
import techreborn.utils.IC2WrenchHelper;
|
2016-03-28 14:35:59 +02:00
|
|
|
|
2016-03-27 21:10:49 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
2016-02-26 22:46:55 +01:00
|
|
|
|
|
|
|
/**
|
2016-03-07 21:33:08 +01:00
|
|
|
* Created by modmuss50 on 26/02/2016.
|
2016-02-26 22:46:55 +01:00
|
|
|
*/
|
2016-12-12 08:03:51 +01:00
|
|
|
public class ItemWrench extends ItemTRNoDestroy {
|
2016-08-10 01:29:47 +02:00
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
public ItemWrench() {
|
2016-08-10 01:29:47 +02:00
|
|
|
setCreativeTab(TechRebornCreativeTabMisc.instance);
|
|
|
|
setUnlocalizedName("techreborn.wrench");
|
|
|
|
setMaxStackSize(1);
|
|
|
|
}
|
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
@Override
|
2016-11-19 13:50:08 +01:00
|
|
|
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos,
|
2016-10-08 21:46:16 +02:00
|
|
|
EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
|
2016-11-25 14:25:51 +01:00
|
|
|
if (!PermissionAPI.hasPermission(player.getGameProfile(), RebornPermissions.WRENCH_BLOCK, new BlockPosContext(player, pos, world.getBlockState(pos), side))) {
|
2016-11-05 16:10:28 +01:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (CompatManager.isIC2Loaded) {
|
2016-11-19 13:50:08 +01:00
|
|
|
EnumActionResult result = IC2WrenchHelper.onItemUseFirst(player.getHeldItem(hand), player, world, pos, side, hitX, hitY, hitZ, hand);
|
2016-10-08 21:46:16 +02:00
|
|
|
if (result == EnumActionResult.SUCCESS) {
|
2016-09-06 23:16:21 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (world.isAirBlock(pos)) {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-10-08 21:46:16 +02:00
|
|
|
if (tile == null) {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
|
2016-12-07 14:18:18 +01:00
|
|
|
if (!player.isSneaking()) {
|
2016-10-08 21:46:16 +02:00
|
|
|
if (tile instanceof TileMachineBase) {
|
|
|
|
if (side != EnumFacing.DOWN && side != EnumFacing.UP) {
|
2016-08-10 01:29:47 +02:00
|
|
|
((TileMachineBase) tile).setFacing(side);
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 14:18:18 +01:00
|
|
|
IBlockState state = world.getBlockState(pos);
|
2016-12-12 08:03:51 +01:00
|
|
|
if (state.getBlock() instanceof BlockEnergyStorage) {
|
2016-12-07 14:22:53 +01:00
|
|
|
EnumFacing facing = state.getValue(BlockEnergyStorage.FACING);
|
2016-12-12 08:03:51 +01:00
|
|
|
if (facing.getOpposite() == side) {
|
2016-12-07 14:22:53 +01:00
|
|
|
facing = side;
|
2016-12-12 08:03:51 +01:00
|
|
|
} else {
|
2016-12-07 14:22:53 +01:00
|
|
|
facing = side.getOpposite();
|
|
|
|
}
|
|
|
|
world.setBlockState(pos, state.withProperty(BlockEnergyStorage.FACING, facing));
|
2016-12-07 14:18:18 +01:00
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
2016-08-10 01:29:47 +02:00
|
|
|
}
|
2016-11-19 13:50:08 +01:00
|
|
|
return super.onItemUseFirst(player, world, pos, side, hitX, hitY, hitZ, hand);
|
2016-08-10 01:29:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
@Override
|
2016-11-19 13:50:08 +01:00
|
|
|
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos,
|
2016-10-08 21:46:16 +02:00
|
|
|
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
|
2016-11-25 14:25:51 +01:00
|
|
|
if (!PermissionAPI.hasPermission(player.getGameProfile(), RebornPermissions.WRENCH_BLOCK, new BlockPosContext(player, pos, world.getBlockState(pos), facing))) {
|
2016-11-05 16:10:28 +01:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
if (CompatManager.isIC2Loaded) {
|
2016-11-19 13:50:08 +01:00
|
|
|
EnumActionResult result = IC2WrenchHelper.onItemUse(player.getHeldItem(hand), player, world, pos, hand, facing, hitX, hitY, hitZ);
|
2016-11-05 16:10:28 +01:00
|
|
|
if (result == EnumActionResult.SUCCESS) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (world.isAirBlock(pos)) {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
TileEntity tile = world.getTileEntity(pos);
|
2016-10-08 21:46:16 +02:00
|
|
|
if (tile == null) {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (!world.isRemote) {
|
|
|
|
if (player.isSneaking()) {
|
2016-08-10 01:29:47 +02:00
|
|
|
List<ItemStack> items = new ArrayList<>();
|
2016-10-08 21:46:16 +02:00
|
|
|
if (tile instanceof IInventory) {
|
2016-08-10 01:29:47 +02:00
|
|
|
IInventory inventory = (IInventory) tile;
|
2016-10-08 21:46:16 +02:00
|
|
|
for (int i = 0; i < inventory.getSizeInventory(); i++) {
|
2016-08-10 01:29:47 +02:00
|
|
|
ItemStack itemStack = inventory.getStackInSlot(i);
|
|
|
|
|
2016-11-19 13:50:08 +01:00
|
|
|
if (itemStack != ItemStack.EMPTY) {
|
|
|
|
if (itemStack.getCount() > 0) {
|
2016-08-10 01:29:47 +02:00
|
|
|
if (itemStack.getItem() instanceof ItemBlock)
|
|
|
|
|
|
|
|
if (!(((ItemBlock) itemStack.getItem()).block instanceof BlockFluidBase) || !(((ItemBlock) itemStack.getItem()).block instanceof BlockStaticLiquid)
|
2016-10-08 21:46:16 +02:00
|
|
|
|| !(((ItemBlock) itemStack.getItem()).block instanceof BlockDynamicLiquid)) {
|
2016-08-10 01:29:47 +02:00
|
|
|
items.add(itemStack.copy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (tile instanceof IWrenchable) {
|
|
|
|
if (((IWrenchable) tile).wrenchCanRemove(player)) {
|
2016-08-10 01:29:47 +02:00
|
|
|
ItemStack itemStack = ((IWrenchable) tile).getWrenchDrop(player);
|
2016-10-08 21:46:16 +02:00
|
|
|
if (itemStack == null) {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
items.add(itemStack);
|
|
|
|
}
|
2016-10-08 21:46:16 +02:00
|
|
|
if (!items.isEmpty()) {
|
|
|
|
for (ItemStack itemStack : items) {
|
2016-08-10 01:29:47 +02:00
|
|
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
|
|
|
float dX = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dY = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
float dZ = rand.nextFloat() * 0.8F + 0.1F;
|
|
|
|
|
|
|
|
EntityItem entityItem = new EntityItem(world, pos.getX() + dX, pos.getY() + dY,
|
2016-10-08 21:46:16 +02:00
|
|
|
pos.getZ() + dZ, itemStack.copy());
|
2016-08-10 01:29:47 +02:00
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
if (itemStack.hasTagCompound()) {
|
2016-08-10 01:29:47 +02:00
|
|
|
entityItem.getEntityItem()
|
2016-10-08 21:46:16 +02:00
|
|
|
.setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
|
2016-08-10 01:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float factor = 0.05F;
|
|
|
|
entityItem.motionX = rand.nextGaussian() * factor;
|
|
|
|
entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
|
|
|
|
entityItem.motionZ = rand.nextGaussian() * factor;
|
2016-10-08 21:46:16 +02:00
|
|
|
if (!world.isRemote) {
|
2016-11-19 13:50:08 +01:00
|
|
|
world.spawnEntity(entityItem);
|
2016-08-10 01:29:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
world.playSound(null, player.posX, player.posY,
|
2016-10-08 21:46:16 +02:00
|
|
|
player.posZ, ModSounds.dismantle,
|
|
|
|
SoundCategory.BLOCKS, 0.6F, 1F);
|
|
|
|
if (!world.isRemote) {
|
2016-08-10 01:29:47 +02:00
|
|
|
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 2);
|
|
|
|
}
|
|
|
|
return EnumActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return EnumActionResult.FAIL;
|
2016-10-08 21:46:16 +02:00
|
|
|
} else {
|
2016-08-10 01:29:47 +02:00
|
|
|
return EnumActionResult.FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 21:46:16 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public boolean isFull3D() {
|
2016-08-10 01:29:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-26 22:46:55 +01:00
|
|
|
}
|