Search in sources :

Example 1 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project wildmod by Osmiooo.

the class FrogSpawnItem method use.

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
    BlockHitResult blockHitResult = FrogSpawnItem.raycast(world, user, RaycastContext.FluidHandling.SOURCE_ONLY);
    BlockHitResult blockHitResult2 = blockHitResult.withBlockPos(blockHitResult.getBlockPos().up());
    ActionResult actionResult = super.useOnBlock(new ItemUsageContext(user, hand, blockHitResult2));
    return new TypedActionResult<ItemStack>(actionResult, user.getStackInHand(hand));
}
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)

Example 2 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project tweakermore by Fallen-Breath.

the class PlacementTweaksMixin method tweakmAutoPickSchematicBlock.

@Inject(method = "tryPlaceBlock", at = @At("HEAD"), remap = false)
private static void tweakmAutoPickSchematicBlock(ClientPlayerInteractionManager controller, ClientPlayerEntity player, ClientWorld world, BlockPos posIn, Direction sideIn, Direction sideRotatedIn, float playerYaw, Vec3d hitVec, Hand hand, PositionUtils.HitPart hitPart, boolean isFirstClick, CallbackInfoReturnable<ActionResult> cir) {
    MinecraftClient mc = MinecraftClient.getInstance();
    if (mc.player != null) {
        if (DataManager.getToolMode() != ToolMode.REBUILD && !Configs.Generic.EASY_PLACE_MODE.getBooleanValue()) {
            if (TweakerMoreConfigs.TWEAKM_AUTO_PICK_SCHEMATIC_BLOCK.getBooleanValue() && EntityUtils.shouldPickBlock(mc.player)) {
                BlockHitResult hitResult = new BlockHitResult(hitVec, sideIn, posIn, false);
                ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hitResult));
                doSchematicWorldPickBlock(mc, ctx.getBlockPos(), hand);
            }
        }
    }
}
Also used : MinecraftClient(net.minecraft.client.MinecraftClient) ItemUsageContext(net.minecraft.item.ItemUsageContext) ItemPlacementContext(net.minecraft.item.ItemPlacementContext) BlockHitResult(net.minecraft.util.hit.BlockHitResult) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with ItemUsageContext

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

the class AxeItemMixin 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();
    BlockState blockState = world.getBlockState(blockPos);
    Map<Block, Block> AETHER_STRIPPED_BLOCKS = new HashMap<>();
    AETHER_STRIPPED_BLOCKS.put(AetherBlocks.CRYSTAL_LOG, AetherBlocks.STRIPPED_CRYSTAL_LOG);
    AETHER_STRIPPED_BLOCKS.put(AetherBlocks.GOLDEN_OAK_LOG, AetherBlocks.STRIPPED_GOLDEN_OAK_LOG);
    AETHER_STRIPPED_BLOCKS.put(AetherBlocks.SKYROOT_LOG, AetherBlocks.STRIPPED_SKYROOT_LOG);
    AETHER_STRIPPED_BLOCKS.put(AetherBlocks.WISTERIA_LOG, AetherBlocks.STRIPPED_WISTERIA_LOG);
    Block block = AETHER_STRIPPED_BLOCKS.get(blockState.getBlock());
    if (block != null) {
        PlayerEntity playerEntity = context.getPlayer();
        world.playSound(playerEntity, blockPos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
        if (!world.isClient) {
            world.setBlockState(blockPos, block.getDefaultState().with(PillarBlock.AXIS, blockState.get(PillarBlock.AXIS)), 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) PillarBlock(net.minecraft.block.PillarBlock) ActionResult(net.minecraft.util.ActionResult) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) 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) AxeItem(net.minecraft.item.AxeItem) At(org.spongepowered.asm.mixin.injection.At) BlockState(net.minecraft.block.BlockState) HashMap(java.util.HashMap) PillarBlock(net.minecraft.block.PillarBlock) 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 4 with ItemUsageContext

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

the class ShovelItemMixin 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();
    BlockState blockState = world.getBlockState(blockPos);
    Map<Block, BlockState> AETHER_PATH_STATES = new HashMap<>();
    AETHER_PATH_STATES.put(AetherBlocks.AETHER_GRASS_BLOCK, AetherBlocks.AETHER_DIRT_PATH.getDefaultState());
    AETHER_PATH_STATES.put(AetherBlocks.AETHER_DIRT, AetherBlocks.AETHER_DIRT_PATH.getDefaultState());
    if (context.getSide() == Direction.DOWN) {
        cir.setReturnValue(ActionResult.PASS);
    } else {
        PlayerEntity playerEntity = context.getPlayer();
        BlockState blockState2 = AETHER_PATH_STATES.get(blockState.getBlock());
        BlockState blockState3 = null;
        if (blockState2 != null && world.getBlockState(blockPos.up()).isAir()) {
            world.playSound(playerEntity, blockPos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
            blockState3 = blockState2;
        } else if (blockState.getBlock() instanceof CampfireBlock && blockState.get(CampfireBlock.LIT)) {
            if (!world.isClient())
                world.syncWorldEvent(null, 1009, blockPos, 0);
            CampfireBlock.extinguish(world, blockPos, blockState);
            blockState3 = blockState.with(CampfireBlock.LIT, false);
        }
        if (blockState3 != null) {
            if (!world.isClient) {
                world.setBlockState(blockPos, blockState3, 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) ShovelItem(net.minecraft.item.ShovelItem) MiningToolItem(net.minecraft.item.MiningToolItem) Map(java.util.Map) BlockState(net.minecraft.block.BlockState) SoundCategory(net.minecraft.sound.SoundCategory) CampfireBlock(net.minecraft.block.CampfireBlock) At(org.spongepowered.asm.mixin.injection.At) BlockState(net.minecraft.block.BlockState) HashMap(java.util.HashMap) CampfireBlock(net.minecraft.block.CampfireBlock) Block(net.minecraft.block.Block) CampfireBlock(net.minecraft.block.CampfireBlock) 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 5 with ItemUsageContext

use of net.minecraft.item.ItemUsageContext in project FastAsyncWorldEdit by IntellectualSites.

the class FabricWorld method useItem.

@Override
public boolean useItem(BlockVector3 position, BaseItem item, Direction face) {
    ItemStack stack = FabricAdapter.adapt(new BaseItemStack(item.getType(), item.getNbtData(), 1));
    ServerWorld world = (ServerWorld) getWorld();
    final WorldEditFakePlayer fakePlayer;
    try {
        fakePlayer = fakePlayers.get(world);
    } catch (ExecutionException ignored) {
        return false;
    }
    fakePlayer.setStackInHand(Hand.MAIN_HAND, stack);
    fakePlayer.setPositionAndAngles(position.getBlockX(), position.getBlockY(), position.getBlockZ(), (float) face.toVector().toYaw(), (float) face.toVector().toPitch());
    final BlockPos blockPos = FabricAdapter.toBlockPos(position);
    final BlockHitResult rayTraceResult = new BlockHitResult(FabricAdapter.toVec3(position), FabricAdapter.adapt(face), blockPos, false);
    ItemUsageContext itemUseContext = new ItemUsageContext(fakePlayer, Hand.MAIN_HAND, rayTraceResult);
    ActionResult used = stack.useOnBlock(itemUseContext);
    if (used != ActionResult.SUCCESS) {
        // try activating the block
        if (getWorld().getBlockState(blockPos).activate(world, fakePlayer, Hand.MAIN_HAND, rayTraceResult)) {
            used = ActionResult.SUCCESS;
        } else {
            used = stack.getItem().use(world, fakePlayer, Hand.MAIN_HAND).getResult();
        }
    }
    return used == ActionResult.SUCCESS;
}
Also used : ServerWorld(net.minecraft.server.world.ServerWorld) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ActionResult(net.minecraft.util.ActionResult) ItemUsageContext(net.minecraft.item.ItemUsageContext) BlockPos(net.minecraft.util.math.BlockPos) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.item.ItemStack) ExecutionException(java.util.concurrent.ExecutionException) BlockHitResult(net.minecraft.util.hit.BlockHitResult)

Aggregations

ItemUsageContext (net.minecraft.item.ItemUsageContext)7 ActionResult (net.minecraft.util.ActionResult)6 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 MiningToolItem (net.minecraft.item.MiningToolItem)3 ToolMaterial (net.minecraft.item.ToolMaterial)3 SoundCategory (net.minecraft.sound.SoundCategory)3 SoundEvents (net.minecraft.sound.SoundEvents)3 BlockHitResult (net.minecraft.util.hit.BlockHitResult)3 World (net.minecraft.world.World)3 Mixin (org.spongepowered.asm.mixin.Mixin)3 At (org.spongepowered.asm.mixin.injection.At)3 CallbackInfoReturnable (org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable)3