Search in sources :

Example 1 with ItemUseContext

use of net.minecraft.item.ItemUseContext in project AgriCraft by AgriCraft.

the class ItemClipper method onItemUse.

@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
    World world = context.getWorld();
    if (world.isRemote()) {
        return ActionResultType.PASS;
    }
    BlockPos pos = context.getPos();
    ItemStack stack = context.getItem();
    PlayerEntity player = context.getPlayer();
    return AgriApi.getCrop(world, pos).map(crop -> {
        if (!crop.hasPlant() || !crop.getPlant().allowsClipping(crop.getGrowthStage(), stack, player)) {
            if (player != null) {
                player.sendMessage(AgriToolTips.MSG_CLIPPING_IMPOSSIBLE, player.getUniqueID());
            }
            return ActionResultType.FAIL;
        }
        if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Clip.Pre(crop, stack, player))) {
            return ActionResultType.FAIL;
        }
        List<ItemStack> drops = Lists.newArrayList();
        crop.getPlant().getClipProducts(drops::add, stack, crop.getGrowthStage(), crop.getStats(), world.getRandom());
        crop.setGrowthStage(crop.getPlant().getInitialGrowthStage());
        crop.getPlant().onClipped(crop, stack, player);
        AgriCropEvent.Clip.Post event = new AgriCropEvent.Clip.Post(crop, stack, drops, player);
        MinecraftForge.EVENT_BUS.post(event);
        event.getDrops().forEach(crop::dropItem);
        return ActionResultType.SUCCESS;
    }).orElse(ActionResultType.FAIL);
}
Also used : AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) IAgriClipperItem(com.infinityraider.agricraft.api.v1.content.items.IAgriClipperItem) ItemUseContext(net.minecraft.item.ItemUseContext) ItemStack(net.minecraft.item.ItemStack) MinecraftForge(net.minecraftforge.common.MinecraftForge) List(java.util.List) Lists(com.google.common.collect.Lists) AgriToolTips(com.infinityraider.agricraft.reference.AgriToolTips) AgriTabs(com.infinityraider.agricraft.content.AgriTabs) Names(com.infinityraider.agricraft.reference.Names) ItemBase(com.infinityraider.infinitylib.item.ItemBase) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent) Nonnull(javax.annotation.Nonnull) ActionResultType(net.minecraft.util.ActionResultType) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent) BlockPos(net.minecraft.util.math.BlockPos) List(java.util.List) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Example 2 with ItemUseContext

use of net.minecraft.item.ItemUseContext in project AgriCraft by AgriCraft.

the class ItemTrowel method onItemUse.

@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
    World world = context.getWorld();
    BlockPos pos = context.getPos();
    ItemStack stack = context.getItem();
    PlayerEntity player = context.getPlayer();
    return AgriApi.getCrop(world, pos).map(crop -> this.tryUseOnCrop(crop, stack, player)).orElseGet(() -> this.tryPlantOnSoil(world, pos, stack, player));
}
Also used : AgriNBT(com.infinityraider.agricraft.reference.AgriNBT) InfinityItemProperty(com.infinityraider.infinitylib.item.InfinityItemProperty) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) TileEntityCropSticks(com.infinityraider.agricraft.content.core.TileEntityCropSticks) NoPlant(com.infinityraider.agricraft.impl.v1.plant.NoPlant) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) CompoundNBT(net.minecraft.nbt.CompoundNBT) IAgriGrowthStage(com.infinityraider.agricraft.api.v1.crop.IAgriGrowthStage) IAgriGenome(com.infinityraider.agricraft.api.v1.genetics.IAgriGenome) ItemUseContext(net.minecraft.item.ItemUseContext) Dist(net.minecraftforge.api.distmarker.Dist) ItemStack(net.minecraft.item.ItemStack) AgriToolTips(com.infinityraider.agricraft.reference.AgriToolTips) Names(com.infinityraider.agricraft.reference.Names) ItemBase(com.infinityraider.infinitylib.item.ItemBase) IAgriTrowelItem(com.infinityraider.agricraft.api.v1.content.items.IAgriTrowelItem) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) AgriCraft(com.infinityraider.agricraft.AgriCraft) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) ImmutableSet(com.google.common.collect.ImmutableSet) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) World(net.minecraft.world.World) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) Blocks(net.minecraft.block.Blocks) MinecraftForge(net.minecraftforge.common.MinecraftForge) AgriTabs(com.infinityraider.agricraft.content.AgriTabs) ResourceLocation(net.minecraft.util.ResourceLocation) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent) ActionResultType(net.minecraft.util.ActionResultType) ClientWorld(net.minecraft.client.world.ClientWorld) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ClientWorld(net.minecraft.client.world.ClientWorld) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Example 3 with ItemUseContext

use of net.minecraft.item.ItemUseContext in project AgriCraft by AgriCraft.

the class ItemDynamicAgriSeed method onItemUse.

@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
    World world = context.getWorld();
    BlockPos pos = context.getPos();
    TileEntity tile = world.getTileEntity(pos);
    ItemStack stack = context.getItem();
    PlayerEntity player = context.getPlayer();
    // If crop sticks were clicked, attempt to plant the seed
    if (tile instanceof TileEntityCropSticks) {
        return this.attemptSeedPlant((TileEntityCropSticks) tile, stack, player);
    }
    // If a soil was clicked, check the block on top of the soil and handle accordingly
    return AgriApi.getSoil(world, pos).map(soil -> {
        BlockPos up = pos.up();
        TileEntity above = world.getTileEntity(up);
        // There are currently crop sticks on the soil, attempt to plant on the crop sticks
        if (above instanceof TileEntityCropSticks) {
            return this.attemptSeedPlant((TileEntityCropSticks) above, stack, player);
        }
        // There are currently no crop sticks, check if the place is suitable and plant the plant directly
        if (above == null && AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
            BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, up);
            if (newState != null && world.setBlockState(up, newState, 11)) {
                boolean success = AgriApi.getCrop(world, up).map(crop -> this.getGenome(context.getItem()).map(genome -> crop.plantGenome(genome, player)).map(result -> {
                    if (result) {
                        // consume item
                        if (player == null || !player.isCreative()) {
                            stack.shrink(1);
                        }
                    }
                    return result;
                }).orElse(false)).orElse(false);
                if (success) {
                    return ActionResultType.SUCCESS;
                } else {
                    world.setBlockState(up, Blocks.AIR.getDefaultState());
                }
            }
        }
        // Neither alternative option was successful, delegate the call to the super method
        return super.onItemUse(context);
    }).orElse(super.onItemUse(context));
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) NoPlant(com.infinityraider.agricraft.impl.v1.plant.NoPlant) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) CompoundNBT(net.minecraft.nbt.CompoundNBT) IAgriGenome(com.infinityraider.agricraft.api.v1.genetics.IAgriGenome) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) IWorldReader(net.minecraft.world.IWorldReader) ItemUseContext(net.minecraft.item.ItemUseContext) ITextComponent(net.minecraft.util.text.ITextComponent) Dist(net.minecraftforge.api.distmarker.Dist) ItemStack(net.minecraft.item.ItemStack) Names(com.infinityraider.agricraft.reference.Names) ItemBase(com.infinityraider.infinitylib.item.ItemBase) NonNullList(net.minecraft.util.NonNullList) BlockState(net.minecraft.block.BlockState) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) IAgriSeedItem(com.infinityraider.agricraft.api.v1.content.items.IAgriSeedItem) AgriCraft(com.infinityraider.agricraft.AgriCraft) IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) AgriApi(com.infinityraider.agricraft.api.v1.AgriApi) PlayerEntity(net.minecraft.entity.player.PlayerEntity) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Blocks(net.minecraft.block.Blocks) List(java.util.List) AgriTabs(com.infinityraider.agricraft.content.AgriTabs) ItemGroup(net.minecraft.item.ItemGroup) Optional(java.util.Optional) TileEntity(net.minecraft.tileentity.TileEntity) ActionResultType(net.minecraft.util.ActionResultType) BlockState(net.minecraft.block.BlockState) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Example 4 with ItemUseContext

use of net.minecraft.item.ItemUseContext in project BluePower by Qmunity.

the class TileDeployer method rightClick.

/**
 * Be sure to set up the fake player's hotbar with the right clicked items. starting with hotbar slot 0.
 * @param player
 * @param useItems this method will set the current selected slot of the fake player to 0, and move on to the next slot useItems - 1 times.
 *          So to use the first slot only, pass 1, to use the full hotbar, 9.
 * @return
 */
protected boolean rightClick(FakePlayer player, int useItems) {
    if (useItems > 9)
        throw new IllegalArgumentException("Hotbar is 9 items in width! You're trying " + useItems + "!");
    Direction faceDir = getFacingDirection();
    int dx = faceDir.getStepX();
    int dy = faceDir.getStepY();
    int dz = faceDir.getStepZ();
    int x = worldPosition.getX() + dx;
    int y = worldPosition.getY() + dy;
    int z = worldPosition.getZ() + dz;
    player.setPos(x + 0.5, y + 0.5 - player.getEyeHeight(), z + 0.5);
    player.xRot = faceDir.getStepY() * -90;
    switch(faceDir) {
        case NORTH:
            player.yRot = 180;
            break;
        case SOUTH:
            player.yRot = 0;
            break;
        case WEST:
            player.yRot = 90;
            break;
        case EAST:
            player.yRot = -90;
    }
    try {
        PlayerInteractEvent event = new PlayerInteractEvent.RightClickEmpty(player, Hand.MAIN_HAND);
        if (event.isCanceled())
            return false;
        Block block = level.getBlockState(new BlockPos(x, y, z)).getBlock();
        List<LivingEntity> detectedEntities = level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1));
        Entity entity = detectedEntities.isEmpty() ? null : detectedEntities.get(level.random.nextInt(detectedEntities.size()));
        if (entity != null) {
            for (int i = 0; i < useItems; i++) {
                player.inventory.selected = i;
                ItemStack stack = player.getMainHandItem();
                if (canDeployItem(stack) && stack.getItem().interactLivingEntity(stack, player, (LivingEntity) entity, Hand.MAIN_HAND).shouldSwing())
                    return true;
                if (entity instanceof AnimalEntity && ((AnimalEntity) entity).mobInteract(player, Hand.MAIN_HAND).shouldSwing())
                    return true;
            }
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack) && stack.getItem().onItemUseFirst(stack, new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            if (!level.isEmptyBlock(new BlockPos(x, y, z)) && block.use(level.getBlockState(new BlockPos(x, y, z)), level, new BlockPos(x, y, z), player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false)) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            boolean isGoingToShift = false;
            if (!stack.isEmpty()) {
                if (stack.getItem() == Items.SUGAR_CANE || stack.getItem() == Items.REDSTONE) {
                    isGoingToShift = true;
                }
            }
            int useX = isGoingToShift ? worldPosition.getX() : x;
            int useY = isGoingToShift ? worldPosition.getY() : y;
            int useZ = isGoingToShift ? worldPosition.getZ() : z;
            if (canDeployItem(stack) && stack.getItem().useOn(new ItemUseContext(player, Hand.MAIN_HAND, new BlockRayTraceResult(new Vector3d(dx, dy, dz), faceDir, new BlockPos(x, y, z), false))) == ActionResultType.SUCCESS)
                return true;
        }
        for (int i = 0; i < useItems; i++) {
            player.inventory.selected = i;
            ItemStack stack = player.getMainHandItem();
            if (canDeployItem(stack)) {
                ItemStack copy = stack.copy();
                // TODO Check this
                player.setItemInHand(Hand.MAIN_HAND, stack.getItem().use(level, player, Hand.MAIN_HAND).getObject());
                if (!copy.sameItem(stack))
                    return true;
            }
        }
        return false;
    } catch (Throwable e) {
        BluePower.log.error("Deployer crashed! Stacktrace: ");
        e.printStackTrace();
        return true;
    }
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) LivingEntity(net.minecraft.entity.LivingEntity) ItemEntity(net.minecraft.entity.item.ItemEntity) ItemUseContext(net.minecraft.item.ItemUseContext) PlayerInteractEvent(net.minecraftforge.event.entity.player.PlayerInteractEvent) AnimalEntity(net.minecraft.entity.passive.AnimalEntity) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Direction(net.minecraft.util.Direction) LivingEntity(net.minecraft.entity.LivingEntity) Vector3d(net.minecraft.util.math.vector.Vector3d) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

PlayerEntity (net.minecraft.entity.player.PlayerEntity)4 ItemStack (net.minecraft.item.ItemStack)4 ItemUseContext (net.minecraft.item.ItemUseContext)4 BlockPos (net.minecraft.util.math.BlockPos)4 AgriApi (com.infinityraider.agricraft.api.v1.AgriApi)3 AgriTabs (com.infinityraider.agricraft.content.AgriTabs)3 Names (com.infinityraider.agricraft.reference.Names)3 ItemBase (com.infinityraider.infinitylib.item.ItemBase)3 Nonnull (javax.annotation.Nonnull)3 ActionResultType (net.minecraft.util.ActionResultType)3 World (net.minecraft.world.World)3 AgriCraft (com.infinityraider.agricraft.AgriCraft)2 IAgriCrop (com.infinityraider.agricraft.api.v1.crop.IAgriCrop)2 AgriCropEvent (com.infinityraider.agricraft.api.v1.event.AgriCropEvent)2 IAgriGenome (com.infinityraider.agricraft.api.v1.genetics.IAgriGenome)2 IAgriPlant (com.infinityraider.agricraft.api.v1.plant.IAgriPlant)2 NoPlant (com.infinityraider.agricraft.impl.v1.plant.NoPlant)2 AgriToolTips (com.infinityraider.agricraft.reference.AgriToolTips)2 List (java.util.List)2 Optional (java.util.Optional)2