Search in sources :

Example 6 with ActionResultType

use of net.minecraft.util.ActionResultType 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)

Aggregations

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