Search in sources :

Example 1 with LeftClickBlock

use of net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock in project Create by Creators-of-Create.

the class DeployerHandler method activateInner.

private static void activateInner(DeployerFakePlayer player, Vec3 vec, BlockPos clickedPos, Vec3 extensionVector, Mode mode) {
    Vec3 rayOrigin = vec.add(extensionVector.scale(3 / 2f + 1 / 64f));
    Vec3 rayTarget = vec.add(extensionVector.scale(5 / 2f - 1 / 64f));
    player.setPos(rayOrigin.x, rayOrigin.y, rayOrigin.z);
    BlockPos pos = new BlockPos(vec);
    ItemStack stack = player.getMainHandItem();
    Item item = stack.getItem();
    // Check for entities
    final ServerLevel world = player.getLevel();
    List<Entity> entities = world.getEntitiesOfClass(Entity.class, new AABB(clickedPos)).stream().filter(e -> !(e instanceof AbstractContraptionEntity)).collect(Collectors.toList());
    InteractionHand hand = InteractionHand.MAIN_HAND;
    if (!entities.isEmpty()) {
        Entity entity = entities.get(world.random.nextInt(entities.size()));
        List<ItemEntity> capturedDrops = new ArrayList<>();
        boolean success = false;
        entity.captureDrops(capturedDrops);
        // Use on entity
        if (mode == Mode.USE) {
            InteractionResult cancelResult = ForgeHooks.onInteractEntity(player, entity, hand);
            if (cancelResult == InteractionResult.FAIL) {
                entity.captureDrops(null);
                return;
            }
            if (cancelResult == null) {
                if (entity.interact(player, hand).consumesAction()) {
                    if (entity instanceof AbstractVillager) {
                        AbstractVillager villager = ((AbstractVillager) entity);
                        if (villager.getTradingPlayer() instanceof DeployerFakePlayer)
                            villager.setTradingPlayer(null);
                    }
                    success = true;
                } else if (entity instanceof LivingEntity && stack.interactLivingEntity(player, (LivingEntity) entity, hand).consumesAction())
                    success = true;
            }
            if (!success && stack.isEdible() && entity instanceof Player) {
                Player playerEntity = (Player) entity;
                if (playerEntity.canEat(item.getFoodProperties().canAlwaysEat())) {
                    playerEntity.eat(world, stack);
                    player.spawnedItemEffects = stack.copy();
                    success = true;
                }
            }
        }
        // Punch entity
        if (mode == Mode.PUNCH) {
            player.resetAttackStrengthTicker();
            player.attack(entity);
            success = true;
        }
        entity.captureDrops(null);
        capturedDrops.forEach(e -> player.getInventory().placeItemBackInInventory(e.getItem()));
        if (success)
            return;
    }
    // Shoot ray
    ClipContext rayTraceContext = new ClipContext(rayOrigin, rayTarget, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player);
    BlockHitResult result = world.clip(rayTraceContext);
    if (result.getBlockPos() != clickedPos)
        result = new BlockHitResult(result.getLocation(), result.getDirection(), clickedPos, result.isInside());
    BlockState clickedState = world.getBlockState(clickedPos);
    Direction face = result.getDirection();
    if (face == null)
        face = Direction.getNearest(extensionVector.x, extensionVector.y, extensionVector.z).getOpposite();
    // Left click
    if (mode == Mode.PUNCH) {
        if (!world.mayInteract(player, clickedPos))
            return;
        if (clickedState.getShape(world, clickedPos).isEmpty()) {
            player.blockBreakingProgress = null;
            return;
        }
        LeftClickBlock event = ForgeHooks.onLeftClickBlock(player, clickedPos, face);
        if (event.isCanceled())
            return;
        if (// FIXME: is there an equivalent in world,
        BlockHelper.extinguishFire(world, player, clickedPos, face))
            // as there was in 1.15?
            return;
        if (event.getUseBlock() != DENY)
            clickedState.attack(world, clickedPos, player);
        if (stack.isEmpty())
            return;
        float progress = clickedState.getDestroyProgress(player, world, clickedPos) * 16;
        float before = 0;
        Pair<BlockPos, Float> blockBreakingProgress = player.blockBreakingProgress;
        if (blockBreakingProgress != null)
            before = blockBreakingProgress.getValue();
        progress += before;
        world.playSound(null, clickedPos, clickedState.getSoundType().getHitSound(), SoundSource.NEUTRAL, .25f, 1);
        if (progress >= 1) {
            tryHarvestBlock(player, player.gameMode, clickedPos);
            world.destroyBlockProgress(player.getId(), clickedPos, -1);
            player.blockBreakingProgress = null;
            return;
        }
        if (progress <= 0) {
            player.blockBreakingProgress = null;
            return;
        }
        if ((int) (before * 10) != (int) (progress * 10))
            world.destroyBlockProgress(player.getId(), clickedPos, (int) (progress * 10));
        player.blockBreakingProgress = Pair.of(clickedPos, progress);
        return;
    }
    // Right click
    UseOnContext itemusecontext = new UseOnContext(player, hand, result);
    Event.Result useBlock = DEFAULT;
    Event.Result useItem = DEFAULT;
    if (!clickedState.getShape(world, clickedPos).isEmpty()) {
        RightClickBlock event = ForgeHooks.onRightClickBlock(player, hand, clickedPos, result);
        useBlock = event.getUseBlock();
        useItem = event.getUseItem();
    }
    // Item has custom active use
    if (useItem != DENY) {
        InteractionResult actionresult = stack.onItemUseFirst(itemusecontext);
        if (actionresult != InteractionResult.PASS)
            return;
    }
    boolean holdingSomething = !player.getMainHandItem().isEmpty();
    boolean flag1 = !(player.isShiftKeyDown() && holdingSomething) || (stack.doesSneakBypassUse(world, clickedPos, player));
    // Use on block
    if (useBlock != DENY && flag1 && safeOnUse(clickedState, world, clickedPos, player, hand, result).consumesAction())
        return;
    if (stack.isEmpty())
        return;
    if (useItem == DENY)
        return;
    if (item instanceof BlockItem && !(item instanceof CartAssemblerBlockItem) && !clickedState.canBeReplaced(new BlockPlaceContext(itemusecontext)))
        return;
    // Reposition fire placement for convenience
    if (item == Items.FLINT_AND_STEEL) {
        Direction newFace = result.getDirection();
        BlockPos newPos = result.getBlockPos();
        if (!BaseFireBlock.canBePlacedAt(world, clickedPos, newFace))
            newFace = Direction.UP;
        if (clickedState.getMaterial() == Material.AIR)
            newPos = newPos.relative(face.getOpposite());
        result = new BlockHitResult(result.getLocation(), newFace, newPos, result.isInside());
        itemusecontext = new UseOnContext(player, hand, result);
    }
    // 'Inert' item use behaviour & block placement
    InteractionResult onItemUse = stack.useOn(itemusecontext);
    if (onItemUse.consumesAction())
        return;
    if (item == Items.ENDER_PEARL)
        return;
    // buckets create their own ray, We use a fake wall to contain the active area
    Level itemUseWorld = world;
    if (item instanceof BucketItem || item instanceof SandPaperItem)
        itemUseWorld = new ItemUseWorld(world, face, pos);
    InteractionResultHolder<ItemStack> onItemRightClick = item.use(itemUseWorld, player, hand);
    ItemStack resultStack = onItemRightClick.getObject();
    if (resultStack != stack || resultStack.getCount() != stack.getCount() || resultStack.getUseDuration() > 0 || resultStack.getDamageValue() != stack.getDamageValue()) {
        player.setItemInHand(hand, onItemRightClick.getObject());
    }
    CompoundTag tag = stack.getTag();
    if (tag != null && stack.getItem() instanceof SandPaperItem && tag.contains("Polishing")) {
        player.spawnedItemEffects = ItemStack.of(tag.getCompound("Polishing"));
        AllSoundEvents.SANDING_SHORT.playOnServer(world, pos, .25f, 1f);
    }
    if (!player.getUseItem().isEmpty())
        player.setItemInHand(hand, stack.finishUsingItem(world, player));
    player.stopUsingItem();
}
Also used : RightClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock) LivingEntity(net.minecraft.world.entity.LivingEntity) Items(net.minecraft.world.item.Items) AABB(net.minecraft.world.phys.AABB) Direction(net.minecraft.core.Direction) InteractionResultHolder(net.minecraft.world.InteractionResultHolder) Item(net.minecraft.world.item.Item) AbstractVillager(net.minecraft.world.entity.npc.AbstractVillager) BeehiveBlock(net.minecraft.world.level.block.BeehiveBlock) Mode(com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode) Pair(org.apache.commons.lang3.tuple.Pair) Event(net.minecraftforge.eventbus.api.Event) AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) TransportedItemStackHandlerBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.belt.TransportedItemStackHandlerBehaviour) BlockHitResult(net.minecraft.world.phys.BlockHitResult) InteractionResult(net.minecraft.world.InteractionResult) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Collectors(java.util.stream.Collectors) Player(net.minecraft.world.entity.player.Player) Blocks(net.minecraft.world.level.block.Blocks) DoubleBlockHalf(net.minecraft.world.level.block.state.properties.DoubleBlockHalf) BlockHelper(com.simibubi.create.foundation.utility.BlockHelper) List(java.util.List) DoublePlantBlock(net.minecraft.world.level.block.DoublePlantBlock) CompoundTag(net.minecraft.nbt.CompoundTag) BlockPos(net.minecraft.core.BlockPos) ItemEntity(net.minecraft.world.entity.item.ItemEntity) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) BucketItem(net.minecraft.world.item.BucketItem) ItemStack(net.minecraft.world.item.ItemStack) GameType(net.minecraft.world.level.GameType) Level(net.minecraft.world.level.Level) SoundSource(net.minecraft.sounds.SoundSource) ForgeHooks(net.minecraftforge.common.ForgeHooks) DENY(net.minecraftforge.eventbus.api.Event.Result.DENY) WrappedWorld(com.simibubi.create.foundation.utility.worldWrappers.WrappedWorld) BlockState(net.minecraft.world.level.block.state.BlockState) Multimap(com.google.common.collect.Multimap) DEFAULT(net.minecraftforge.eventbus.api.Event.Result.DEFAULT) ServerLevel(net.minecraft.server.level.ServerLevel) ArrayList(java.util.ArrayList) LeftClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock) ServerPlayer(net.minecraft.server.level.ServerPlayer) ClipContext(net.minecraft.world.level.ClipContext) AttributeModifier(net.minecraft.world.entity.ai.attributes.AttributeModifier) SoundEvents(net.minecraft.sounds.SoundEvents) Fluid(net.minecraft.world.level.material.Fluid) UseOnContext(net.minecraft.world.item.context.UseOnContext) Fluids(net.minecraft.world.level.material.Fluids) CartAssemblerBlockItem(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlockItem) Nullable(javax.annotation.Nullable) SandPaperItem(com.simibubi.create.content.curiosities.tools.SandPaperItem) BaseFireBlock(net.minecraft.world.level.block.BaseFireBlock) TileEntityBehaviour(com.simibubi.create.foundation.tileEntity.TileEntityBehaviour) BlockItem(net.minecraft.world.item.BlockItem) Material(net.minecraft.world.level.material.Material) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) ServerPlayerGameMode(net.minecraft.server.level.ServerPlayerGameMode) Entity(net.minecraft.world.entity.Entity) Vec3(net.minecraft.world.phys.Vec3) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Block(net.minecraft.world.level.block.Block) InteractionHand(net.minecraft.world.InteractionHand) AllSoundEvents(com.simibubi.create.AllSoundEvents) ServerLevel(net.minecraft.server.level.ServerLevel) LivingEntity(net.minecraft.world.entity.LivingEntity) AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Entity(net.minecraft.world.entity.Entity) ClipContext(net.minecraft.world.level.ClipContext) InteractionHand(net.minecraft.world.InteractionHand) ArrayList(java.util.ArrayList) SandPaperItem(com.simibubi.create.content.curiosities.tools.SandPaperItem) Direction(net.minecraft.core.Direction) CartAssemblerBlockItem(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlockItem) BlockItem(net.minecraft.world.item.BlockItem) InteractionResult(net.minecraft.world.InteractionResult) LivingEntity(net.minecraft.world.entity.LivingEntity) Item(net.minecraft.world.item.Item) BucketItem(net.minecraft.world.item.BucketItem) CartAssemblerBlockItem(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlockItem) SandPaperItem(com.simibubi.create.content.curiosities.tools.SandPaperItem) BlockItem(net.minecraft.world.item.BlockItem) Vec3(net.minecraft.world.phys.Vec3) BucketItem(net.minecraft.world.item.BucketItem) BlockPos(net.minecraft.core.BlockPos) AbstractVillager(net.minecraft.world.entity.npc.AbstractVillager) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CompoundTag(net.minecraft.nbt.CompoundTag) ItemEntity(net.minecraft.world.entity.item.ItemEntity) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) UseOnContext(net.minecraft.world.item.context.UseOnContext) RightClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock) AbstractContraptionEntity(com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity) CartAssemblerBlockItem(com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlockItem) BlockState(net.minecraft.world.level.block.state.BlockState) LeftClickBlock(net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock) Event(net.minecraftforge.eventbus.api.Event) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) ItemStack(net.minecraft.world.item.ItemStack) AABB(net.minecraft.world.phys.AABB)

Aggregations

Multimap (com.google.common.collect.Multimap)1 AllSoundEvents (com.simibubi.create.AllSoundEvents)1 Mode (com.simibubi.create.content.contraptions.components.deployer.DeployerTileEntity.Mode)1 AbstractContraptionEntity (com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity)1 CartAssemblerBlockItem (com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlockItem)1 SandPaperItem (com.simibubi.create.content.curiosities.tools.SandPaperItem)1 TileEntityBehaviour (com.simibubi.create.foundation.tileEntity.TileEntityBehaviour)1 TransportedItemStackHandlerBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.belt.TransportedItemStackHandlerBehaviour)1 BlockHelper (com.simibubi.create.foundation.utility.BlockHelper)1 WrappedWorld (com.simibubi.create.foundation.utility.worldWrappers.WrappedWorld)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 ServerPlayerGameMode (net.minecraft.server.level.ServerPlayerGameMode)1