Java Code Examples for net.minecraft.util.math.RayTraceResult#getBlockPos()

The following examples show how to use net.minecraft.util.math.RayTraceResult#getBlockPos() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RayTraceUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nullable
public static BlockPos getTargetedPosition(World world, Entity entity, double maxDistance, boolean sneakToOffset)
{
    RayTraceResult trace = fi.dy.masa.malilib.util.RayTraceUtils
            .getRayTraceFromEntity(world, entity, RayTraceFluidHandling.NONE, false, maxDistance);

    if (trace.typeOfHit != RayTraceResult.Type.BLOCK)
    {
        return null;
    }

    BlockPos pos = trace.getBlockPos();

    // Sneaking puts the position adjacent the targeted block face, not sneaking puts it inside the targeted block
    if (sneakToOffset == entity.isSneaking())
    {
        pos = pos.offset(trace.sideHit);
    }

    return pos;
}
 
Example 2
Source File: SchematicEditUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static boolean breakSchematicBlocks(Minecraft mc)
{
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    RayTraceWrapper wrapper = RayTraceUtils.getSchematicWorldTraceWrapperIfClosest(mc.world, entity, 20);

    if (wrapper != null)
    {
        RayTraceResult trace = wrapper.getRayTraceResult();
        BlockPos pos = trace.getBlockPos();
        EnumFacing playerFacingH = mc.player.getHorizontalFacing();
        EnumFacing direction = fi.dy.masa.malilib.util.PositionUtils.getTargetedDirection(trace.sideHit, playerFacingH, pos, trace.hitVec);

        // Center region
        if (direction == trace.sideHit)
        {
            direction = direction.getOpposite();
        }

        BlockPos posEnd = getReplacementBoxEndPos(pos, direction);

        return setSchematicBlockStates(pos, posEnd, Blocks.AIR.getDefaultState());
    }

    return false;
}
 
Example 3
Source File: SchematicEditUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean rebuildAcceptReplacement(Minecraft mc)
{
    WorldSchematic schematicWorld = SchematicWorldHandler.getSchematicWorld();
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    RayTraceResult trace = fi.dy.masa.malilib.util.RayTraceUtils.getRayTraceFromEntity(mc.world, entity, RayTraceFluidHandling.ANY, false, 5);

    if (schematicWorld != null && trace != null && trace.typeOfHit == RayTraceResult.Type.BLOCK)
    {
        BlockPos pos = trace.getBlockPos();
        IBlockState stateOriginal = schematicWorld.getBlockState(pos);
        IBlockState stateClient = mc.world.getBlockState(pos).getActualState(mc.world, pos);

        if (stateOriginal != stateClient)
        {
            if (setAllIdenticalSchematicBlockStates(pos, stateOriginal, stateClient))
            {
                InfoUtils.showGuiOrInGameMessage(MessageType.SUCCESS, "litematica.message.schematic_rebuild.accepted_replacement");
                return true;
            }
        }
    }

    return false;
}
 
Example 4
Source File: SchematicPlacementManager.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void setPositionOfCurrentSelectionToRayTrace(Minecraft mc, double maxDistance)
{
    SchematicPlacement schematicPlacement = this.getSelectedSchematicPlacement();

    if (schematicPlacement != null)
    {
        Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
        RayTraceResult trace = fi.dy.masa.malilib.util.RayTraceUtils.getRayTraceFromEntity(mc.world, entity, RayTraceFluidHandling.NONE, false, maxDistance);

        if (trace.typeOfHit != RayTraceResult.Type.BLOCK)
        {
            return;
        }

        BlockPos pos = trace.getBlockPos();

        // Sneaking puts the position inside the targeted block, not sneaking puts it against the targeted face
        if (mc.player.isSneaking() == false)
        {
            pos = pos.offset(trace.sideHit);
        }

        this.setPositionOfCurrentSelectionTo(pos, mc);
    }
}
 
Example 5
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ActionResult<ItemStack> tryPickUpFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, true);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		FluidActionResult result = FluidUtil.tryPickUpFluid(itemstack, player, world, clickPos, mop.sideHit);
		if (result.isSuccess()) {
			ItemHandlerHelper.giveItemToPlayer(player, result.getResult());
			itemstack.shrink(1);
			return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 6
Source File: SchematicEditUtils.java    From litematica with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static boolean breakAllIdenticalSchematicBlocks(Minecraft mc)
{
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    RayTraceWrapper wrapper = RayTraceUtils.getSchematicWorldTraceWrapperIfClosest(mc.world, entity, 20);

    // The state can be null in 1.13+
    if (wrapper != null)
    {
        RayTraceResult trace = wrapper.getRayTraceResult();
        BlockPos pos = trace.getBlockPos();
        IBlockState stateOriginal = SchematicWorldHandler.getSchematicWorld().getBlockState(pos);

        return setAllIdenticalSchematicBlockStates(pos, stateOriginal, Blocks.AIR.getDefaultState());
    }

    return false;
}
 
Example 7
Source File: EasyPlaceUtils.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Nullable
private static HitPosition getAdjacentClickPosition(final BlockPos targetPos, Minecraft mc)
{
    World world = mc.world;
    double reach = Math.max(6, mc.playerController.getBlockReachDistance());
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    RayTraceResult traceVanilla = fi.dy.masa.malilib.util.RayTraceUtils.getRayTraceFromEntity(world, entity, RayTraceFluidHandling.NONE, false, reach);

    if (traceVanilla != null && traceVanilla.typeOfHit == RayTraceResult.Type.BLOCK)
    {
        BlockPos posVanilla = traceVanilla.getBlockPos();

        // If there is a block in the world right behind the targeted schematic block, then use
        // that block as the click position
        if (PlacementUtils.isReplaceable(world, posVanilla, false) == false &&
            targetPos.equals(posVanilla.offset(traceVanilla.sideHit)))
        {
            return HitPosition.of(posVanilla, traceVanilla.hitVec, traceVanilla.sideHit);
        }
    }

    for (EnumFacing side : fi.dy.masa.malilib.util.PositionUtils.ALL_DIRECTIONS)
    {
        BlockPos posSide = targetPos.offset(side);

        if (PlacementUtils.isReplaceable(world, posSide, false) == false)
        {
            Vec3d hitPos = getHitPositionForSidePosition(posSide, side);
            return HitPosition.of(posSide, hitPos, side.getOpposite());
        }
    }

    return null;
}
 
Example 8
Source File: ToolUtils.java    From litematica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void setToolModeBlockState(ToolMode mode, boolean primary, Minecraft mc)
{
    IBlockState state = Blocks.AIR.getDefaultState();
    double reach = mc.playerController.getBlockReachDistance();
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    RayTraceWrapper wrapper = RayTraceUtils.getGenericTrace(mc.world, entity, reach, true);

    if (wrapper != null)
    {
        RayTraceResult trace = wrapper.getRayTraceResult();

        if (trace != null)
        {
            BlockPos pos = trace.getBlockPos();

            if (wrapper.getHitType() == HitType.SCHEMATIC_BLOCK)
            {
                state = SchematicWorldHandler.getSchematicWorld().getBlockState(pos);
            }
            else if (wrapper.getHitType() == HitType.VANILLA)
            {
                state = mc.world.getBlockState(pos).getActualState(mc.world, pos);
            }
        }
    }

    if (primary)
    {
        mode.setPrimaryBlock(state);
    }
    else
    {
        mode.setSecondaryBlock(state);
    }
}
 
Example 9
Source File: CraftEventFactory.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
public static ProjectileHitEvent callProjectileHitEvent(Entity entity, RayTraceResult position) {
    Block hitBlock = null;
    if (position.typeOfHit == RayTraceResult.Type.BLOCK) {
        BlockPos blockposition = position.getBlockPos();
        hitBlock = entity.getBukkitEntity().getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
    }

    ProjectileHitEvent event = new ProjectileHitEvent((Projectile) entity.getBukkitEntity(), position.entityHit == null ? null : position.entityHit.getBukkitEntity(), hitBlock);
    entity.world.getServer().getPluginManager().callEvent(event);
    return event;
}
 
Example 10
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ActionResult<ItemStack> tryPlaceFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, false);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null || mop.typeOfHit != RayTraceResult.Type.BLOCK) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		BlockPos targetPos = clickPos.offset(mop.sideHit);
		if (player.canPlayerEdit(targetPos, mop.sideHit, itemstack)) {
			FluidActionResult result = FluidUtil.tryPlaceFluid(player, world, targetPos, itemstack, fluidStack);
			if (result.isSuccess() && !player.capabilities.isCreativeMode) {
				player.addStat(StatList.getObjectUseStats(this));
				itemstack.shrink(1);
				ItemStack emptyStack = new ItemStack(GTItems.testTube);
				if (itemstack.isEmpty()) {
					return ActionResult.newResult(EnumActionResult.SUCCESS, emptyStack);
				} else {
					ItemHandlerHelper.giveItemToPlayer(player, emptyStack);
					return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
				}
			}
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 11
Source File: ScannerBehavior.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Pair<BlockPos, IBlockState> getHitBlock(EntityPlayer entityPlayer) {
    RayTraceResult result = RayTracer.retrace(entityPlayer);
    if (result.typeOfHit == Type.BLOCK) {
        BlockPos blockPos = result.getBlockPos();
        IBlockState blockState = entityPlayer.world.getBlockState(blockPos);
        if (blockState.getBlock() instanceof IScannableBlock) {
            return Pair.of(blockPos, blockState);
        }
    }
    return null;
}
 
Example 12
Source File: WrenchOverlayRenderer.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SubscribeEvent
public static void onDrawBlockHighlight(DrawBlockHighlightEvent event) {
    EntityPlayer player = event.getPlayer();
    World world = player.world;
    RayTraceResult target = event.getTarget();

    if (target.typeOfHit != RayTraceResult.Type.BLOCK) {
        return; //magically, draw block highlight is called not only for blocks (see forge issues)
    }

    BlockPos pos = target.getBlockPos();
    IBlockState blockState = world.getBlockState(pos);
    TileEntity tileEntity = world.getTileEntity(pos);
    ItemStack heldItem = player.getHeldItem(EnumHand.MAIN_HAND);

    if (tileEntity != null && shouldDrawOverlayForItem(heldItem, tileEntity) && useGridForRayTraceResult(target)) {
        EnumFacing facing = target.sideHit;
        GlStateManager.enableBlend();
        GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
        GlStateManager.glLineWidth(2.0F);
        GlStateManager.disableTexture2D();
        GlStateManager.depthMask(false);

        if (world.getWorldBorder().contains(pos)) {
            double d3 = player.lastTickPosX + (player.posX - player.lastTickPosX) * (double) event.getPartialTicks();
            double d4 = player.lastTickPosY + (player.posY - player.lastTickPosY) * (double) event.getPartialTicks();
            double d5 = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * (double) event.getPartialTicks();
            AxisAlignedBB box = blockState.getSelectedBoundingBox(world, pos).grow(0.002D).offset(-d3, -d4, -d5);
            RenderGlobal.drawSelectionBoundingBox(box, 0.0F, 0.0F, 0.0F, 0.4F);
            drawOverlayLines(facing, box);
        }

        GlStateManager.depthMask(true);
        GlStateManager.enableTexture2D();
        GlStateManager.disableBlend();

        event.setCanceled(true);
    }
}
 
Example 13
Source File: ParticleHandlerUtil.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Cuboid6 getBoundingBox(IBlockState blockState, World world, RayTraceResult target) {
    BlockPos blockPos = target.getBlockPos();
    if (target instanceof CuboidRayTraceResult) {
        return ((CuboidRayTraceResult) target).cuboid6.copy().add(blockPos);
    }
    return new Cuboid6(blockState.getBoundingBox(world, blockPos)).add(blockPos);
}
 
Example 14
Source File: WorldHelper.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the first hittable blockpos between an entity and a target position
 */
public static BlockPos getTargetBlockTowards(World world, Entity ent, BlockPos target)
{
	Vec3d start = ent.getPositionVector().add(0D, ent.getEyeHeight(), 0D);
	Vec3d end = new Vec3d(target);
	RayTraceResult result = world.rayTraceBlocks(start, end, false, false, true);
	return (result == null) ? null : result.getBlockPos();
}
 
Example 15
Source File: WorldHelper.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the first hittable blockpos along a random vector in a cone based
 * on an entity's line of sight
 */
public static BlockPos getConeOfSightTargetBlock(World world, Entity ent, double dist)
{
	Vec3d start = ent.getPositionVector().add(0D, ent.getEyeHeight(), 0D);
	
	Vec3d LOSvec = ent.getLookVec();
	LOSvec = LOSvec.rotatePitch((world.rand.nextFloat()-0.5F)*MathExtender.PI_HALVED);
	LOSvec = LOSvec.rotateYaw((world.rand.nextFloat()-0.5F)*MathExtender.PI_QUARTERED);
	LOSvec = LOSvec.scale(dist);
	Vec3d end = start.add(LOSvec);
	RayTraceResult result = world.rayTraceBlocks(start, end, false, false, true);
	return (result == null) ? null : result.getBlockPos();
}
 
Example 16
Source File: BlockHighlightModule.java    From seppuku with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void render3D(EventRender3D event) {
    final Minecraft mc = Minecraft.getMinecraft();
    final RayTraceResult ray = mc.objectMouseOver;
    if(ray.typeOfHit == RayTraceResult.Type.BLOCK) {

        final BlockPos blockpos = ray.getBlockPos();
        final IBlockState iblockstate = mc.world.getBlockState(blockpos);

        if (iblockstate.getMaterial() != Material.AIR && mc.world.getWorldBorder().contains(blockpos)) {
            final Vec3d interp = MathUtil.interpolateEntity(mc.player, mc.getRenderPartialTicks());
            RenderUtil.drawBoundingBox(iblockstate.getSelectedBoundingBox(mc.world, blockpos).grow(0.0020000000949949026D).offset(-interp.x, -interp.y, -interp.z), 1.5f, 0xFF9900EE);
        }
    }
}
 
Example 17
Source File: SchematicEditUtils.java    From litematica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Nullable
private static ReplacementInfo getTargetInfo(Minecraft mc)
{
    ItemStack stack = mc.player.getHeldItemMainhand();

    if ((stack.isEmpty() == false && (stack.getItem() instanceof ItemBlock || stack.getItem() instanceof ItemBlockSpecial)) ||
        (stack.isEmpty() && ToolMode.REBUILD.getPrimaryBlock() != null))
    {
        WorldSchematic world = SchematicWorldHandler.getSchematicWorld();
        Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
        RayTraceWrapper traceWrapper = RayTraceUtils.getGenericTrace(mc.world, entity, 20, true);

        if (world != null && traceWrapper != null &&
            traceWrapper.getHitType() == RayTraceWrapper.HitType.SCHEMATIC_BLOCK)
        {
            RayTraceResult trace = traceWrapper.getRayTraceResult();
            EnumFacing side = trace.sideHit;
            Vec3d hitVec = trace.hitVec;
            int meta = stack.getItem().getMetadata(stack.getMetadata());
            BlockPos pos = trace.getBlockPos();
            IBlockState stateOriginal = world.getBlockState(pos);
            IBlockState stateNew = Blocks.AIR.getDefaultState();

            if (stack.getItem() instanceof ItemBlock)
            {
                stateNew = ((ItemBlock) stack.getItem()).getBlock().getStateForPlacement(world, pos.offset(side),
                                side, (float) hitVec.x, (float) hitVec.y, (float) hitVec.z, meta, mc.player);
            }
            else if (stack.getItem() instanceof ItemBlockSpecial)
            {
                stateNew = ((IMixinItemBlockSpecial) stack.getItem()).getBlock().getStateForPlacement(world, pos.offset(side),
                                side, (float) hitVec.x, (float) hitVec.y, (float) hitVec.z, 0, mc.player);
            }
            else if (ToolMode.REBUILD.getPrimaryBlock() != null)
            {
                stateNew = ToolMode.REBUILD.getPrimaryBlock();
            }

            return new ReplacementInfo(pos, side, hitVec, stateOriginal, stateNew);
        }
    }

    return null;
}
 
Example 18
Source File: OverlayRenderer.java    From litematica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void renderSchematicRebuildTargetingOverlay(float partialTicks)
{
    RayTraceWrapper traceWrapper = null;
    Entity entity = fi.dy.masa.malilib.util.EntityUtils.getCameraEntity();
    Color4f color = null;
    boolean direction = false;

    if (Hotkeys.SCHEMATIC_REBUILD_BREAK_ALL.getKeybind().isKeybindHeld())
    {
        traceWrapper = RayTraceUtils.getGenericTrace(this.mc.world, entity, 20, true);
        color = Configs.Colors.REBUILD_BREAK_OVERLAY_COLOR.getColor();
    }
    else if (Hotkeys.SCHEMATIC_REBUILD_BREAK_DIRECTION.getKeybind().isKeybindHeld())
    {
        traceWrapper = RayTraceUtils.getGenericTrace(this.mc.world, entity, 20, true);
        color = Configs.Colors.REBUILD_BREAK_OVERLAY_COLOR.getColor();
        direction = true;
    }
    else if (Hotkeys.SCHEMATIC_REBUILD_REPLACE_ALL.getKeybind().isKeybindHeld())
    {
        traceWrapper = RayTraceUtils.getGenericTrace(this.mc.world, entity, 20, true);
        color = Configs.Colors.REBUILD_REPLACE_OVERLAY_COLOR.getColor();
    }
    else if (Hotkeys.SCHEMATIC_REBUILD_REPLACE_DIRECTION.getKeybind().isKeybindHeld())
    {
        traceWrapper = RayTraceUtils.getGenericTrace(this.mc.world, entity, 20, true);
        color = Configs.Colors.REBUILD_REPLACE_OVERLAY_COLOR.getColor();
        direction = true;
    }

    if (traceWrapper != null && traceWrapper.getHitType() == RayTraceWrapper.HitType.SCHEMATIC_BLOCK)
    {
        RayTraceResult trace = traceWrapper.getRayTraceResult();
        BlockPos pos = trace.getBlockPos();

        GlStateManager.depthMask(false);
        GlStateManager.disableLighting();
        GlStateManager.disableCull();
        GlStateManager.disableTexture2D();
        fi.dy.masa.malilib.render.RenderUtils.setupBlend();

        if (direction)
        {
            fi.dy.masa.malilib.render.RenderUtils.renderBlockTargetingOverlay(
                    entity, pos, trace.sideHit, trace.hitVec, color, partialTicks);
        }
        else
        {
            fi.dy.masa.malilib.render.RenderUtils.renderBlockTargetingOverlaySimple(
                    entity, pos, trace.sideHit, color, partialTicks);
        }

        GlStateManager.enableTexture2D();
        //GlStateManager.enableDepth();
        GlStateManager.disableBlend();
        GlStateManager.enableCull();
        GlStateManager.depthMask(true);
    }
}
 
Example 19
Source File: ItemNigari.java    From TofuCraftReload with MIT License 4 votes vote down vote up
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
	if (worldIn.isRemote)
		return new ActionResult<ItemStack>(EnumActionResult.PASS, ItemStack.EMPTY);
	RayTraceResult var4 = this.rayTrace(worldIn, playerIn, true);
	ItemStack itemStackIn = playerIn.getHeldItem(handIn);

	if (var4 == null) {
		return new ActionResult<ItemStack>(EnumActionResult.PASS, itemStackIn);
	} else {
		if (var4.typeOfHit == RayTraceResult.Type.BLOCK) {
			BlockPos targetPos = var4.getBlockPos();
			Block var11 = worldIn.getBlockState(targetPos).getBlock();
			Block var13 = null;

			if (var11 == BlockLoader.SOYMILK) {
				var13 = BlockLoader.KINUTOFU;
			} else if (var11 == BlockLoader.SOYMILKHELL) {
				var13 = BlockLoader.TOFUHELL;
			} else if (var11 == BlockLoader.ZUNDASOYMILK) {
				var13 = BlockLoader.TOFUZUNDA;
			}

			if (var13 != null) {
				worldIn.playSound(playerIn, targetPos.add(0.5, 0.5, 0.5),
						var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getBreakSound(),
						SoundCategory.BLOCKS,
						(var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getVolume()
								+ 1.0F) / 2.0F,
						var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getPitch()
								* 0.8F);

				worldIn.setBlockState(targetPos, var13.getDefaultState());

				if (!playerIn.capabilities.isCreativeMode) {
					itemStackIn.shrink(1);

					ItemStack container = new ItemStack(this.getContainerItem());

					if (itemStackIn.getCount() <= 0) {
						return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, container);
					}

					if (!playerIn.inventory.addItemStackToInventory(container)) {
						playerIn.dropItem(container, false);
					}
				}
			}
		}
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStackIn);
	}
}
 
Example 20
Source File: ItemRiceSeed.java    From TofuCraftReload with MIT License 4 votes vote down vote up
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
{
    ItemStack itemstack = playerIn.getHeldItem(handIn);
    RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

    if (raytraceresult == null)
    {
        return new ActionResult<ItemStack>(EnumActionResult.PASS, itemstack);
    }
    else
    {
        if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK)
        {
            BlockPos blockpos = raytraceresult.getBlockPos();

            if (!worldIn.isBlockModifiable(playerIn, blockpos) || !playerIn.canPlayerEdit(blockpos.offset(raytraceresult.sideHit), raytraceresult.sideHit, itemstack))
            {
                return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
            }

            BlockPos blockpos1 = blockpos.up();
            IBlockState iblockstate = worldIn.getBlockState(blockpos);

            if (iblockstate.getMaterial() == Material.WATER && ((Integer)iblockstate.getValue(BlockLiquid.LEVEL)).intValue() == 0 && worldIn.isAirBlock(blockpos1))
            {
                // special case for handling block placement with water lilies
                net.minecraftforge.common.util.BlockSnapshot blocksnapshot = net.minecraftforge.common.util.BlockSnapshot.getBlockSnapshot(worldIn, blockpos1);
                worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState());
                if (net.minecraftforge.event.ForgeEventFactory.onPlayerBlockPlace(playerIn, blocksnapshot, net.minecraft.util.EnumFacing.UP, handIn).isCanceled())
                {
                    blocksnapshot.restore(true, false);
                    return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
                }

                worldIn.setBlockState(blockpos1, BlockLoader.RICECROP.getDefaultState(), 11);

                if (playerIn instanceof EntityPlayerMP)
                {
                    CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)playerIn, blockpos1, itemstack);
                }

                if (!playerIn.capabilities.isCreativeMode)
                {
                    itemstack.shrink(1);
                }

                playerIn.addStat(StatList.getObjectUseStats(this));
                worldIn.playSound(playerIn, blockpos, SoundEvents.BLOCK_WATERLILY_PLACE, SoundCategory.BLOCKS, 1.0F, 1.0F);
                return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
            }
        }

        return new ActionResult<ItemStack>(EnumActionResult.FAIL, itemstack);
    }
}