TechReborn/src/main/java/techreborn/items/tools/ItemWrench.java

206 lines
6.9 KiB
Java
Raw Normal View History

2016-02-26 22:46:55 +01:00
package techreborn.items.tools;
import me.modmuss50.jsonDestroyer.api.ITexturedItem;
2016-02-27 10:15:14 +01:00
import net.minecraft.block.BlockDynamicLiquid;
import net.minecraft.block.BlockStaticLiquid;
import net.minecraft.block.state.IBlockState;
2016-03-14 14:28:33 +01:00
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
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;
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;
import reborncore.common.util.RebornPermissions;
2016-03-28 14:35:59 +02:00
import techreborn.blocks.fluid.BlockFluidBase;
import techreborn.blocks.storage.BlockEnergyStorage;
2016-02-26 22:46:55 +01:00
import techreborn.client.TechRebornCreativeTabMisc;
import techreborn.compat.CompatManager;
2016-05-08 12:42:59 +02:00
import techreborn.init.ModSounds;
2016-02-28 01:49:50 +01:00
import techreborn.items.ItemTR;
2016-03-14 14:28:33 +01:00
import techreborn.lib.ModInfo;
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-10-08 21:46:16 +02:00
public class ItemWrench extends ItemTR implements ITexturedItem {
2016-10-08 21:46:16 +02:00
public ItemWrench() {
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))) {
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) {
return result;
}
}
2016-10-08 21:46:16 +02:00
if (world.isAirBlock(pos)) {
return EnumActionResult.FAIL;
}
TileEntity tile = world.getTileEntity(pos);
2016-10-08 21:46:16 +02:00
if (tile == null) {
return EnumActionResult.FAIL;
}
if (!player.isSneaking()) {
2016-10-08 21:46:16 +02:00
if (tile instanceof TileMachineBase) {
if (side != EnumFacing.DOWN && side != EnumFacing.UP) {
((TileMachineBase) tile).setFacing(side);
return EnumActionResult.SUCCESS;
}
}
IBlockState state = world.getBlockState(pos);
if(state.getBlock() instanceof BlockEnergyStorage){
2016-12-07 14:22:53 +01:00
EnumFacing facing = state.getValue(BlockEnergyStorage.FACING);
if(facing.getOpposite() == side){
facing = side;
} else {
facing = side.getOpposite();
}
world.setBlockState(pos, state.withProperty(BlockEnergyStorage.FACING, facing));
return EnumActionResult.SUCCESS;
}
}
2016-11-19 13:50:08 +01:00
return super.onItemUseFirst(player, world, pos, side, hitX, hitY, hitZ, hand);
}
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))) {
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);
if (result == EnumActionResult.SUCCESS) {
return result;
}
}
2016-10-08 21:46:16 +02:00
if (world.isAirBlock(pos)) {
return EnumActionResult.FAIL;
}
TileEntity tile = world.getTileEntity(pos);
2016-10-08 21:46:16 +02:00
if (tile == null) {
return EnumActionResult.FAIL;
}
2016-10-08 21:46:16 +02:00
if (!world.isRemote) {
if (player.isSneaking()) {
List<ItemStack> items = new ArrayList<>();
2016-10-08 21:46:16 +02:00
if (tile instanceof IInventory) {
IInventory inventory = (IInventory) tile;
2016-10-08 21:46:16 +02:00
for (int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack itemStack = inventory.getStackInSlot(i);
2016-11-19 13:50:08 +01:00
if (itemStack != ItemStack.EMPTY) {
if (itemStack.getCount() > 0) {
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)) {
items.add(itemStack.copy());
}
}
}
}
2016-10-08 21:46:16 +02:00
if (tile instanceof IWrenchable) {
if (((IWrenchable) tile).wrenchCanRemove(player)) {
ItemStack itemStack = ((IWrenchable) tile).getWrenchDrop(player);
2016-10-08 21:46:16 +02:00
if (itemStack == null) {
return EnumActionResult.FAIL;
}
items.add(itemStack);
}
2016-10-08 21:46:16 +02:00
if (!items.isEmpty()) {
for (ItemStack itemStack : items) {
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-10-08 21:46:16 +02:00
if (itemStack.hasTagCompound()) {
entityItem.getEntityItem()
2016-10-08 21:46:16 +02:00
.setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
}
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);
}
}
}
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) {
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 2);
}
return EnumActionResult.SUCCESS;
}
}
}
return EnumActionResult.FAIL;
2016-10-08 21:46:16 +02:00
} else {
return EnumActionResult.FAIL;
}
}
2016-10-08 21:46:16 +02:00
@Override
public String getTextureName(int damage) {
return "techreborn:items/tool/wrench";
}
2016-10-08 21:46:16 +02:00
@Override
public int getMaxMeta() {
return 1;
}
2016-10-08 21:46:16 +02:00
@Override
@SideOnly(Side.CLIENT)
public ModelResourceLocation getModel(ItemStack stack, EntityPlayer player,
int useRemaining) {
return new ModelResourceLocation(ModInfo.MOD_ID + ":" + getUnlocalizedName(stack).substring(5), "inventory");
}
2016-10-08 21:46:16 +02:00
@SideOnly(Side.CLIENT)
public boolean isFull3D() {
return true;
}
2016-02-26 22:46:55 +01:00
}