Search in sources :

Example 6 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project Paradise-Lost by devs-immortal.

the class HoeItemMixin method useOnBlock.

@Inject(at = @At("HEAD"), method = "useOnBlock", cancellable = true)
public void useOnBlock(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir) {
    World world = context.getWorld();
    BlockPos blockPos = context.getBlockPos();
    Map<Block, BlockState> AETHER_TILLED_BLOCKS = new HashMap<>();
    AETHER_TILLED_BLOCKS.put(AetherBlocks.AETHER_GRASS_BLOCK, AetherBlocks.AETHER_FARMLAND.getDefaultState());
    AETHER_TILLED_BLOCKS.put(AetherBlocks.AETHER_DIRT, AetherBlocks.AETHER_FARMLAND.getDefaultState());
    if (context.getSide() != Direction.DOWN && world.getBlockState(blockPos.up()).isAir()) {
        BlockState blockState = AETHER_TILLED_BLOCKS.get(world.getBlockState(blockPos).getBlock());
        if (blockState != null) {
            PlayerEntity playerEntity = context.getPlayer();
            world.playSound(playerEntity, blockPos, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
            if (!world.isClient) {
                world.setBlockState(blockPos, blockState, 11);
                if (playerEntity != null)
                    context.getStack().damage(1, playerEntity, (p) -> p.sendToolBreakStatus(context.getHand()));
            }
            cir.setReturnValue(ActionResult.success(world.isClient));
        }
    }
}
Also used : AetherBlocks(com.aether.blocks.AetherBlocks) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ToolMaterial(net.minecraft.item.ToolMaterial) World(net.minecraft.world.World) Inject(org.spongepowered.asm.mixin.injection.Inject) Set(java.util.Set) BlockPos(net.minecraft.util.math.BlockPos) HashMap(java.util.HashMap) ActionResult(net.minecraft.util.ActionResult) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) Direction(net.minecraft.util.math.Direction) ItemUsageContext(net.minecraft.item.ItemUsageContext) Block(net.minecraft.block.Block) SoundEvents(net.minecraft.sound.SoundEvents) Mixin(org.spongepowered.asm.mixin.Mixin) MiningToolItem(net.minecraft.item.MiningToolItem) Map(java.util.Map) BlockState(net.minecraft.block.BlockState) SoundCategory(net.minecraft.sound.SoundCategory) HoeItem(net.minecraft.item.HoeItem) At(org.spongepowered.asm.mixin.injection.At) BlockState(net.minecraft.block.BlockState) HashMap(java.util.HashMap) Block(net.minecraft.block.Block) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 7 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project friends-and-foes by Faboslav.

the class CopperButtonBlock method onUse.

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
    ItemStack itemStack = player.getStackInHand(hand);
    Item itemInHand = itemStack.getItem();
    if (itemInHand instanceof AxeItem) {
        ItemUsageContext itemUsageContext = new ItemUsageContext(player, hand, hit);
        ActionResult itemInHandUsageResult = itemInHand.useOnBlock(itemUsageContext);
        if (itemInHandUsageResult.isAccepted()) {
            return itemInHandUsageResult;
        }
    }
    return super.onUse(state, world, pos, player, hand, hit);
}
Also used : Item(net.minecraft.item.Item) AxeItem(net.minecraft.item.AxeItem) ActionResult(net.minecraft.util.ActionResult) ItemUsageContext(net.minecraft.item.ItemUsageContext) ItemStack(net.minecraft.item.ItemStack) AxeItem(net.minecraft.item.AxeItem)

Example 8 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project JexClient by DustinRepo.

the class BlockPlacer method placeBlockWithoutInteractingBlock.

private static void placeBlockWithoutInteractingBlock(MinecraftClient minecraftClient, BlockHitResult hitResult) {
    ClientPlayerEntity player = minecraftClient.player;
    ItemStack itemStack = player.getStackInHand(Hand.MAIN_HAND);
    minecraftClient.getNetworkHandler().sendPacket(new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, hitResult));
    if (!itemStack.isEmpty() && !player.getItemCooldownManager().isCoolingDown(itemStack.getItem())) {
        ItemUsageContext itemUsageContext = new ItemUsageContext(player, Hand.MAIN_HAND, hitResult);
        itemStack.useOnBlock(itemUsageContext);
    }
}
Also used : ItemUsageContext(net.minecraft.item.ItemUsageContext) ClientPlayerEntity(net.minecraft.client.network.ClientPlayerEntity) ItemStack(net.minecraft.item.ItemStack) PlayerInteractBlockC2SPacket(net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket)

Example 9 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project Immersive-Weathering by AstralOrdana.

the class LeafPileBlockItem method use.

@Override
public TypedActionResult<ItemStack> use(World p_43441_, PlayerEntity p_43442_, Hand p_43443_) {
    BlockHitResult blockhitresult = raycast(p_43441_, p_43442_, RaycastContext.FluidHandling.SOURCE_ONLY);
    BlockHitResult blockhitresult1 = blockhitresult.withBlockPos(blockhitresult.getBlockPos().up());
    ActionResult interactionresult = super.useOnBlock(new ItemUsageContext(p_43442_, p_43443_, blockhitresult1));
    return new TypedActionResult<>(interactionresult, p_43442_.getStackInHand(p_43443_));
}
Also used : TypedActionResult(net.minecraft.util.TypedActionResult) ActionResult(net.minecraft.util.ActionResult) ItemUsageContext(net.minecraft.item.ItemUsageContext) BlockHitResult(net.minecraft.util.hit.BlockHitResult) TypedActionResult(net.minecraft.util.TypedActionResult)

Aggregations

ItemUsageContext (net.minecraft.item.ItemUsageContext)9 ActionResult (net.minecraft.util.ActionResult)7 BlockHitResult (net.minecraft.util.hit.BlockHitResult)4 BlockPos (net.minecraft.util.math.BlockPos)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 AetherBlocks (com.aether.blocks.AetherBlocks)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 Block (net.minecraft.block.Block)3 BlockState (net.minecraft.block.BlockState)3 PlayerEntity (net.minecraft.entity.player.PlayerEntity)3 ItemStack (net.minecraft.item.ItemStack)3 MiningToolItem (net.minecraft.item.MiningToolItem)3 ToolMaterial (net.minecraft.item.ToolMaterial)3 SoundCategory (net.minecraft.sound.SoundCategory)3 SoundEvents (net.minecraft.sound.SoundEvents)3 World (net.minecraft.world.World)3 Mixin (org.spongepowered.asm.mixin.Mixin)3 At (org.spongepowered.asm.mixin.injection.At)3