Search in sources :

Example 1 with WallItemEntity

use of net.tropicraft.core.common.entity.placeable.WallItemEntity in project Tropicraft by Tropicraft.

the class AshenMaskItem method useOn.

/**
 * Called when this item is used when targetting a Block
 */
@Override
public InteractionResult useOn(UseOnContext context) {
    BlockPos pos = context.getClickedPos();
    Direction direction = context.getClickedFace();
    BlockPos offsetPos = pos.relative(direction);
    Player player = context.getPlayer();
    ItemStack itemStack = context.getItemInHand();
    if (player != null && !this.canPlace(player, direction, itemStack, offsetPos)) {
        return InteractionResult.FAIL;
    } else {
        Level world = context.getLevel();
        WallItemEntity wallItem = new WallItemEntity(world, offsetPos, direction);
        wallItem.setItem(itemStack);
        if (wallItem.survives()) {
            if (!world.isClientSide) {
                wallItem.playPlacementSound();
                world.addFreshEntity(wallItem);
            }
            itemStack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : Player(net.minecraft.world.entity.player.Player) WallItemEntity(net.tropicraft.core.common.entity.placeable.WallItemEntity) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Example 2 with WallItemEntity

use of net.tropicraft.core.common.entity.placeable.WallItemEntity in project Tropicraft by Tropicraft.

the class ShellItem method useOn.

@Override
public InteractionResult useOn(final UseOnContext context) {
    final Direction facing = context.getClickedFace();
    final ItemStack stack = context.getPlayer().getItemInHand(context.getHand());
    final BlockPos pos = context.getClickedPos().relative(facing);
    // Must set the world coordinates here, or onValidSurface will be false.
    final Level world = context.getLevel();
    final WallItemEntity hangingEntity = new WallItemEntity(world, pos, facing);
    hangingEntity.setItem(stack);
    if (!context.getPlayer().mayUseItemAt(pos, facing, stack)) {
        return InteractionResult.FAIL;
    } else {
        if (hangingEntity.survives()) {
            if (!world.isClientSide) {
                world.addFreshEntity(hangingEntity);
            }
            stack.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
}
Also used : WallItemEntity(net.tropicraft.core.common.entity.placeable.WallItemEntity) BlockPos(net.minecraft.core.BlockPos) Level(net.minecraft.world.level.Level) ItemStack(net.minecraft.world.item.ItemStack) Direction(net.minecraft.core.Direction)

Aggregations

BlockPos (net.minecraft.core.BlockPos)2 Direction (net.minecraft.core.Direction)2 ItemStack (net.minecraft.world.item.ItemStack)2 Level (net.minecraft.world.level.Level)2 WallItemEntity (net.tropicraft.core.common.entity.placeable.WallItemEntity)2 Player (net.minecraft.world.entity.player.Player)1