Search in sources :

Example 1 with ActionResultType

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

use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.

the class ItemTrowel method tryNewPlant.

protected ActionResultType tryNewPlant(World world, BlockPos pos, ItemStack stack, @Nullable PlayerEntity player) {
    if (AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
        BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, pos);
        if (newState != null && world.setBlockState(pos, newState, 11)) {
            boolean success = AgriApi.getCrop(world, pos).map(crop -> {
                if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Pre(crop, stack, player))) {
                    return false;
                }
                return this.getGenome(stack).map(genome -> this.getGrowthStage(stack).map(stage -> {
                    boolean result = crop.plantGenome(genome, player) && this.setGrowthStage(crop, stage);
                    if (result) {
                        this.removePlant(stack);
                        MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Post(crop, stack, player));
                    }
                    return result;
                }).orElse(false)).orElse(false);
            }).orElse(false);
            if (success) {
                return ActionResultType.SUCCESS;
            } else {
                world.setBlockState(pos, Blocks.AIR.getDefaultState());
            }
        }
    }
    return ActionResultType.FAIL;
}
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) BlockState(net.minecraft.block.BlockState) AgriCropEvent(com.infinityraider.agricraft.api.v1.event.AgriCropEvent)

Example 3 with ActionResultType

use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.

the class BlockCropBase method onBlockActivated.

@Override
@Deprecated
@SuppressWarnings("deprecation")
public final ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
    // fetch crop
    Optional<IAgriCrop> optional = this.getCrop(world, pos);
    if (!optional.isPresent()) {
        return ActionResultType.FAIL;
    }
    IAgriCrop crop = optional.get();
    // run pre logic
    if (crop.hasPlant()) {
        IAgriPlant plant = crop.getPlant();
        if (plant.isPlant()) {
            Optional<ActionResultType> result = plant.onRightClickPre(crop, player.getHeldItem(hand), player);
            if (result.isPresent()) {
                return result.get();
            }
        }
    }
    // run default logic
    ActionResultType result = this.onCropRightClicked(world, pos, crop, player, hand, hit);
    // run post logic
    if (crop.hasPlant()) {
        IAgriPlant plant = crop.getPlant();
        if (plant.isPlant()) {
            Optional<ActionResultType> override = plant.onRightClickPost(crop, player.getHeldItem(hand), player);
            if (override.isPresent()) {
                return override.get();
            }
        }
    }
    return result;
}
Also used : IAgriPlant(com.infinityraider.agricraft.api.v1.plant.IAgriPlant) IAgriCrop(com.infinityraider.agricraft.api.v1.crop.IAgriCrop) ActionResultType(net.minecraft.util.ActionResultType)

Example 4 with ActionResultType

use of net.minecraft.util.ActionResultType in project Overloaded by CJ-MC-Mods.

the class PlayerInteractionUtil method placeBlock.

@Nonnull
public static BlockPlaceResult placeBlock(@Nonnull ItemStack searchStack, @Nonnull ServerPlayerEntity player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull Direction facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
    // Can we place a block at this Pos
    BlockItem itemBlock = ((BlockItem) searchStack.getItem());
    // if (worldIn.loadedAndEntityCanStandOn(newPosition, player)) {
    // return BlockPlaceResult.FAIL_DENY;
    // }
    BlockSnapshot blockSnapshot = BlockSnapshot.create(worldIn.dimension(), worldIn, newPosition);
    BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(facing.getOpposite()));
    BlockEvent.EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, player);
    MinecraftForge.EVENT_BUS.post(event);
    if (event.isCanceled()) {
        return BlockPlaceResult.FAIL_DENY;
    }
    long distance = Math.round(Math.sqrt(player.blockPosition().distSqr(newPosition)));
    long cost = OverloadedConfig.INSTANCE.multiToolConfig.placeBaseCost + OverloadedConfig.INSTANCE.multiToolConfig.costPerMeterAway * distance;
    if (!player.abilities.instabuild && (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost))
        return BlockPlaceResult.FAIL_ENERGY;
    LazyOptional<IItemHandler> opInventory = player.getCapability(ITEM_HANDLER_CAPABILITY, Direction.UP);
    if (!opInventory.isPresent()) {
        Overloaded.logger.warn("Player has no ItemHandler Capability? NBT: " + player.serializeNBT());
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    IItemHandler inventory = opInventory.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    int foundStackSlot = findItemStackSlot(searchStack, inventory);
    if (foundStackSlot == -1) {
        return BlockPlaceResult.FAIL_PREREQUISITE;
    }
    ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.abilities.instabuild);
    BlockItemUseContext context = new BlockItemUseContextPublic(worldIn, player, Hand.MAIN_HAND, foundStack, new BlockRayTraceResult(new Vector3d(hitX + newPosition.getX(), hitY + newPosition.getY(), hitZ + newPosition.getZ()), facing, newPosition, false));
    ActionResultType result = ForgeHooks.onPlaceItemIntoWorld(context);
    switch(result) {
        case CONSUME:
        case SUCCESS:
            SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
            worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            if (!player.abilities.instabuild) {
                energy.extractEnergy((int) cost, false);
            }
            return BlockPlaceResult.SUCCESS;
        case PASS:
        case FAIL:
        default:
            inventory.insertItem(foundStackSlot, foundStack, player.abilities.instabuild);
            return BlockPlaceResult.FAIL_DENY;
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ActionResultType(net.minecraft.util.ActionResultType) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BlockItem(net.minecraft.item.BlockItem) BlockItemUseContext(net.minecraft.item.BlockItemUseContext) Vector3d(net.minecraft.util.math.vector.Vector3d) ItemStack(net.minecraft.item.ItemStack) BlockEvent(net.minecraftforge.event.world.BlockEvent) Nonnull(javax.annotation.Nonnull)

Example 5 with ActionResultType

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

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