Search in sources :

Example 71 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Electrona-Project by Max094Reikeb.

the class CosmicGemFunction method use.

/**
 * Use the power of the Gem
 *
 * @param world        The world of the user
 * @param playerEntity The user of the Gem
 * @param stack        The Gem as ItemStack
 * @return Returns true if the Gem was used with success
 */
public static boolean use(Level world, Player playerEntity, ItemStack stack) {
    if (GemPower.INVISIBILITY.equalsTo(getPower(stack))) {
        playerEntity.addEffect(new MobEffectInstance(MobEffects.INVISIBILITY, 600, 0, false, false, false));
        return true;
    } else if (GemPower.STRENGTH.equalsTo(getPower(stack))) {
        playerEntity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_BOOST, 600, 2, false, false, false));
        return true;
    } else if (GemPower.TELEPORTATION.equalsTo(getPower(stack))) {
        HitResult rayTraceResult = ElectronaUtils.lookAt(playerEntity, 100D, 1F, false);
        Vec3 location = rayTraceResult.getLocation();
        int stepX = 0;
        int stepY = 1;
        int stepZ = 0;
        if ((rayTraceResult instanceof BlockHitResult) && (!(world.getBlockState(new BlockPos(location).above()).getMaterial() == Material.AIR))) {
            Direction rayTraceDirection = ((BlockHitResult) rayTraceResult).getDirection();
            stepX = rayTraceDirection.getStepX();
            stepY = rayTraceDirection.getStepY();
            stepZ = rayTraceDirection.getStepZ();
        }
        double tx = location.x() + stepX;
        double ty = location.y() + stepY;
        double tz = location.z() + stepZ;
        BlockPos teleportPos = new BlockPos(tx, ty, tz);
        playerEntity.fallDistance = 0;
        TeleporterFunction.teleport(world, playerEntity.blockPosition(), teleportPos, playerEntity);
        return true;
    } else if (GemPower.YO_YO.equalsTo(getPower(stack))) {
        if (playerEntity.isShiftKeyDown()) {
            stack.getOrCreateTag().putDouble("powerYoYoX", playerEntity.getX());
            stack.getOrCreateTag().putDouble("powerYoYoY", playerEntity.getY());
            stack.getOrCreateTag().putDouble("powerYoYoZ", playerEntity.getZ());
            stack.getOrCreateTag().putBoolean("powerYoYo", true);
            if (!world.isClientSide) {
                playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.yoyo_location_saved"), true);
            }
            return true;
        } else {
            if (stack.getOrCreateTag().getBoolean("powerYoYo")) {
                double posX = stack.getOrCreateTag().getDouble("powerYoYoX");
                double posY = stack.getOrCreateTag().getDouble("powerYoYoY");
                double posZ = stack.getOrCreateTag().getDouble("powerYoYoZ");
                BlockPos teleportPos = new BlockPos(posX, posY, posZ);
                playerEntity.fallDistance = 0;
                TeleporterFunction.teleport(world, playerEntity.blockPosition(), teleportPos, playerEntity);
                return true;
            } else {
                if (!world.isClientSide) {
                    playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.yoyo_not_setup"), true);
                }
                return false;
            }
        }
    } else if (GemPower.DIMENSION_TRAVEL.equalsTo(getPower(stack))) {
        if (stack.getOrCreateTag().getBoolean("dimensionTravel")) {
            BlockPos playerPos = playerEntity.blockPosition();
            String dimension = "";
            if (stack.getOrCreateTag().getString("dimension").equals("nether")) {
                dimension = "minecraft:the_nether";
            } else if (stack.getOrCreateTag().getString("dimension").equals("end")) {
                dimension = "minecraft:the_end";
            }
            if (world instanceof ServerLevel) {
                ResourceKey<Level> key = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(dimension));
                if ((!dimension.equals("")) && (((ServerLevel) world).getServer().getLevel(key) != null)) {
                    ResourceKey<Level> newKey = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation("minecraft:overworld"));
                    ServerLevel _newWorld = ((ServerLevel) world).getServer().getLevel(key);
                    ServerLevel _defaultWorld = ((ServerLevel) world).getServer().getLevel(newKey);
                    if (world == _defaultWorld) {
                        if (!playerEntity.level.isClientSide && playerEntity instanceof ServerPlayer) {
                            if (_newWorld != null) {
                                playerEntity.fallDistance = 0;
                                {
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.WIN_GAME, 0));
                                    ((ServerPlayer) playerEntity).teleportTo(_newWorld, _newWorld.getSharedSpawnPos().getX(), _newWorld.getSharedSpawnPos().getY() + 1, _newWorld.getSharedSpawnPos().getZ(), playerEntity.yRot, playerEntity.xRot);
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundPlayerAbilitiesPacket(playerEntity.abilities));
                                    for (MobEffectInstance effectinstance : playerEntity.getActiveEffects()) {
                                        ((ServerPlayer) playerEntity).connection.send(new ClientboundUpdateMobEffectPacket(playerEntity.getId(), effectinstance));
                                    }
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
                                }
                                if (stack.getOrCreateTag().getString("dimension").equals("nether")) {
                                    BlockPos teleportPos = new BlockPos(playerPos.getX() / 8, 50, playerPos.getZ() / 8);
                                    TeleporterFunction.teleport(_newWorld, playerPos, teleportPos, playerEntity);
                                    Block teleportBlock = _newWorld.getBlockState(teleportPos).getBlock();
                                    Block aboveTpBlock = _newWorld.getBlockState(teleportPos.above()).getBlock();
                                    Block belowTpBlock = _newWorld.getBlockState(teleportPos.below()).getBlock();
                                    if (teleportBlock != Blocks.AIR) {
                                        _newWorld.setBlockAndUpdate(teleportPos, Blocks.AIR.defaultBlockState());
                                    }
                                    if (aboveTpBlock != Blocks.AIR) {
                                        _newWorld.setBlockAndUpdate(teleportPos.above(), Blocks.AIR.defaultBlockState());
                                    }
                                    if (belowTpBlock == Blocks.AIR || belowTpBlock == Blocks.LAVA || belowTpBlock == Blocks.MAGMA_BLOCK) {
                                        _newWorld.setBlockAndUpdate(teleportPos.below(), Blocks.NETHERRACK.defaultBlockState());
                                    }
                                }
                                if (stack.getOrCreateTag().getString("dimension").equals("end")) {
                                    BlockPos endPos = _newWorld.getSharedSpawnPos().below();
                                    _newWorld.setBlockAndUpdate(endPos, Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.east(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.north(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.north().west(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.north().east(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.south(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.south().west(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.south().east(), Blocks.OBSIDIAN.defaultBlockState());
                                    _newWorld.setBlockAndUpdate(endPos.west(), Blocks.OBSIDIAN.defaultBlockState());
                                }
                                return true;
                            }
                        }
                    } else if (world == _newWorld) {
                        if (!playerEntity.level.isClientSide && playerEntity instanceof ServerPlayer) {
                            if (_defaultWorld != null) {
                                playerEntity.fallDistance = 0;
                                {
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.WIN_GAME, 0));
                                    ((ServerPlayer) playerEntity).teleportTo(_defaultWorld, _defaultWorld.getSharedSpawnPos().getX(), _defaultWorld.getSharedSpawnPos().getY() + 1, _defaultWorld.getSharedSpawnPos().getZ(), playerEntity.yRot, playerEntity.xRot);
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundPlayerAbilitiesPacket(playerEntity.abilities));
                                    for (MobEffectInstance effectinstance : playerEntity.getActiveEffects()) {
                                        ((ServerPlayer) playerEntity).connection.send(new ClientboundUpdateMobEffectPacket(playerEntity.getId(), effectinstance));
                                    }
                                    ((ServerPlayer) playerEntity).connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
                                }
                                if (stack.getOrCreateTag().getString("dimension").equals("nether")) {
                                    BlockPos teleportPos = new BlockPos(playerPos.getX() * 8, 50, playerPos.getZ() * 8);
                                    TeleporterFunction.teleport(_defaultWorld, playerPos, teleportPos, playerEntity);
                                    Block teleportBlock = _defaultWorld.getBlockState(teleportPos).getBlock();
                                    Block aboveTpBlock = _defaultWorld.getBlockState(teleportPos.above()).getBlock();
                                    Block belowTpBlock = _defaultWorld.getBlockState(teleportPos.below()).getBlock();
                                    if (teleportBlock != Blocks.AIR) {
                                        _defaultWorld.setBlockAndUpdate(teleportPos, Blocks.AIR.defaultBlockState());
                                    }
                                    if (aboveTpBlock != Blocks.AIR) {
                                        _defaultWorld.setBlockAndUpdate(teleportPos.above(), Blocks.AIR.defaultBlockState());
                                    }
                                    if (belowTpBlock == Blocks.AIR || belowTpBlock == Blocks.LAVA) {
                                        _defaultWorld.setBlockAndUpdate(teleportPos.below(), Blocks.STONE.defaultBlockState());
                                    }
                                    return true;
                                }
                                return true;
                            }
                        }
                    }
                } else {
                    if (!world.isClientSide) {
                        playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.no_dimension_info"), true);
                    }
                    return false;
                }
            }
        } else {
            if (!world.isClientSide) {
                playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.dimension_travel_not_setup"), true);
            }
            return false;
        }
    } else if (GemPower.KNOCKBACK.equalsTo(getPower(stack)) && playerEntity.isShiftKeyDown()) {
        int x = playerEntity.blockPosition().getX();
        int y = playerEntity.blockPosition().getY();
        int z = playerEntity.blockPosition().getZ();
        boolean flag = false;
        List<LivingEntity> livingEntities = world.getEntitiesOfClass(LivingEntity.class, new AABB(x - 5, y - 5, z - 5, x + 5, y + 5, z + 5), EntitySelector.LIVING_ENTITY_STILL_ALIVE).stream().sorted(new Object() {

            Comparator<Entity> compareDistOf(double x, double y, double z) {
                return Comparator.comparing(_entcnd -> _entcnd.distanceToSqr(x, y, z));
            }
        }.compareDistOf(x, y, z)).collect(Collectors.toList());
        for (LivingEntity entityiterator : livingEntities) {
            if (entityiterator != playerEntity) {
                flag = true;
                entityiterator.knockback(5F * 0.5F, Mth.sin(playerEntity.yRot * ((float) Math.PI / 180F)), -Mth.cos(playerEntity.yRot * ((float) Math.PI / 180F)));
                playerEntity.setDeltaMovement(playerEntity.getDeltaMovement().multiply(0.6D, 1.0D, 0.6D));
            }
        }
        return flag;
    } else if (GemPower.FLYING.equalsTo(getPower(stack))) {
        if (playerEntity.abilities.flying) {
            playerEntity.abilities.flying = false;
            playerEntity.onUpdateAbilities();
            if (!world.isClientSide) {
                playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.flight_disabled"), true);
            }
        } else {
            playerEntity.abilities.flying = true;
            playerEntity.onUpdateAbilities();
            if (!world.isClientSide) {
                playerEntity.displayClientMessage(new TranslatableComponent("message.electrona.flight_enabled"), true);
            }
        }
        return true;
    }
    return false;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) LivingEntity(net.minecraft.world.entity.LivingEntity) Entity(net.minecraft.world.entity.Entity) ClientboundLevelEventPacket(net.minecraft.network.protocol.game.ClientboundLevelEventPacket) ClientboundUpdateMobEffectPacket(net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket) Direction(net.minecraft.core.Direction) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) LivingEntity(net.minecraft.world.entity.LivingEntity) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Vec3(net.minecraft.world.phys.Vec3) ResourceLocation(net.minecraft.resources.ResourceLocation) BlockPos(net.minecraft.core.BlockPos) List(java.util.List) BlockHitResult(net.minecraft.world.phys.BlockHitResult) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ClientboundGameEventPacket(net.minecraft.network.protocol.game.ClientboundGameEventPacket) ClientboundPlayerAbilitiesPacket(net.minecraft.network.protocol.game.ClientboundPlayerAbilitiesPacket) ServerPlayer(net.minecraft.server.level.ServerPlayer) Block(net.minecraft.world.level.block.Block) ServerLevel(net.minecraft.server.level.ServerLevel) Level(net.minecraft.world.level.Level) AABB(net.minecraft.world.phys.AABB)

Example 72 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project Electrona-Project by Max094Reikeb.

the class EmptyCell method use.

@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
    ItemStack itemstack = playerIn.getItemInHand(handIn);
    BlockHitResult raytraceresult = getPlayerPOVHitResult(worldIn, playerIn, ClipContext.Fluid.SOURCE_ONLY);
    InteractionResultHolder<ItemStack> ret = ForgeEventFactory.onBucketUse(playerIn, worldIn, itemstack, raytraceresult);
    if (ret != null)
        return ret;
    if (raytraceresult.getType() == HitResult.Type.MISS)
        return InteractionResultHolder.pass(itemstack);
    if (raytraceresult.getType() != HitResult.Type.BLOCK)
        return InteractionResultHolder.pass(itemstack);
    BlockPos blockpos = raytraceresult.getBlockPos();
    Direction direction = raytraceresult.getDirection();
    BlockPos blockpos1 = blockpos.relative(direction);
    if (worldIn.mayInteract(playerIn, blockpos) && playerIn.mayUseItemAt(blockpos1, direction, itemstack)) {
        BlockState blockstate1 = worldIn.getBlockState(blockpos);
        if (blockstate1.getBlock() instanceof BucketPickup) {
            BucketPickup bucketPickup = (BucketPickup) blockstate1.getBlock();
            ItemStack itemStack1 = bucketPickup.pickupBlock(worldIn, blockpos, blockstate1);
            if (!itemStack1.isEmpty()) {
                ItemStack itemStack2 = ItemUtils.createFilledResult(itemstack, playerIn, itemStack1);
                /*
                Fluid fluid = ((BucketPickup) blockstate1.getBlock()).takeLiquid(worldIn, blockpos, blockstate1);
                if ((fluid != Fluids.WATER) && (fluid != Fluids.LAVA)) return InteractionResultHolder.fail(itemstack);
                worldIn.setBlock(blockpos, Blocks.AIR.defaultBlockState(), 3);
                if (playerIn.isCreative()) {
                    if (!((fluid == Fluids.WATER && playerIn.inventory.contains(new ItemStack(ItemInit.WATER_CELL.get(), 1)))
                            || (fluid == Fluids.LAVA && playerIn.inventory.contains(new ItemStack(ItemInit.LAVA_CELL.get(), 1))))) {
                        playerIn.inventory.add(new ItemStack((fluid == Fluids.WATER ? ItemInit.WATER_CELL.get() : ItemInit.LAVA_CELL.get()), 1));
                    }
                } else {
                    playerIn.setItemInHand(handIn, new ItemStack((fluid == Fluids.WATER ? ItemInit.WATER_CELL.get() : ItemInit.LAVA_CELL.get()), 1));
                }
                return InteractionResultHolder.success(itemstack);
                */
                return InteractionResultHolder.sidedSuccess(itemStack2, worldIn.isClientSide);
            }
        }
    }
    return InteractionResultHolder.fail(itemstack);
}
Also used : BucketPickup(net.minecraft.world.level.block.BucketPickup) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult) Direction(net.minecraft.core.Direction)

Example 73 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project BuildingGadgets by Direwolf20-MC.

the class AbstractGadget method onAnchor.

public final void onAnchor(ItemStack stack, Player player) {
    if (getAnchor(stack) == null) {
        BlockHitResult lookingAt = VectorHelper.getLookingAt(player, stack);
        if ((player.level.isEmptyBlock(lookingAt.getBlockPos())))
            return;
        onAnchorSet(stack, player, lookingAt);
        player.displayClientMessage(MessageTranslation.ANCHOR_SET.componentTranslation().setStyle(Styles.AQUA), true);
    } else {
        onAnchorRemoved(stack, player);
        player.displayClientMessage(MessageTranslation.ANCHOR_REMOVED.componentTranslation().setStyle(Styles.AQUA), true);
    }
}
Also used : BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Example 74 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project BuildingGadgets by Direwolf20-MC.

the class GadgetCopyPaste method use.

@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
    ItemStack stack = player.getItemInHand(hand);
    player.startUsingItem(hand);
    BlockHitResult posLookingAt = VectorHelper.getLookingAt(player, stack);
    BlockEntity tileEntity = world.getBlockEntity(posLookingAt.getBlockPos());
    boolean lookingAtInventory = tileEntity != null && tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).isPresent();
    if (!world.isClientSide()) {
        if (player.isShiftKeyDown() && lookingAtInventory) {
            return InteractionResultHolder.pass(stack);
        }
        if (getToolMode(stack) == ToolMode.COPY) {
            if (world.getBlockState(posLookingAt.getBlockPos()) != Blocks.AIR.defaultBlockState())
                setRegionAndCopy(stack, world, player, posLookingAt.getBlockPos());
        } else if (getToolMode(stack) == ToolMode.PASTE && !player.isShiftKeyDown())
            getActivePos(player, stack).ifPresent(pos -> build(stack, world, player, pos));
    } else {
        if (player.isShiftKeyDown() && Screen.hasControlDown() && lookingAtInventory) {
            PacketHandler.sendToServer(new PacketBindTool());
            return InteractionResultHolder.pass(stack);
        }
        if (getToolMode(stack) == ToolMode.COPY) {
            if (player.isShiftKeyDown() && world.getBlockState(posLookingAt.getBlockPos()) == Blocks.AIR.defaultBlockState())
                GuiMod.COPY.openScreen(player);
        } else if (player.isShiftKeyDown()) {
            GuiMod.PASTE.openScreen(player);
        } else {
            BaseRenderer.updateInventoryCache();
        }
    }
    return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
}
Also used : InteractionResultHolder(net.minecraft.world.InteractionResultHolder) PacketBindTool(com.direwolf20.buildinggadgets.common.network.packets.PacketBindTool) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 75 with BlockHitResult

use of net.minecraft.world.phys.BlockHitResult in project BuildingGadgets by Direwolf20-MC.

the class GadgetCopyPaste method getActivePos.

public static Optional<BlockPos> getActivePos(Player playerEntity, ItemStack stack) {
    BlockPos pos = ((AbstractGadget) stack.getItem()).getAnchor(stack);
    if (pos == null) {
        BlockHitResult res = VectorHelper.getLookingAt(playerEntity, stack);
        if (res == null || res.getType() == Type.MISS)
            return Optional.empty();
        pos = res.getBlockPos().relative(res.getDirection());
    }
    return Optional.of(pos).map(p -> p.offset(getRelativeVector(stack)));
}
Also used : BlockPos(net.minecraft.core.BlockPos) BlockHitResult(net.minecraft.world.phys.BlockHitResult)

Aggregations

BlockHitResult (net.minecraft.world.phys.BlockHitResult)181 BlockPos (net.minecraft.core.BlockPos)117 Vec3 (net.minecraft.world.phys.Vec3)79 BlockState (net.minecraft.world.level.block.state.BlockState)66 ItemStack (net.minecraft.world.item.ItemStack)63 HitResult (net.minecraft.world.phys.HitResult)51 Direction (net.minecraft.core.Direction)46 Level (net.minecraft.world.level.Level)41 ClipContext (net.minecraft.world.level.ClipContext)33 Player (net.minecraft.world.entity.player.Player)32 ServerLevel (net.minecraft.server.level.ServerLevel)23 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)20 Entity (net.minecraft.world.entity.Entity)19 InteractionHand (net.minecraft.world.InteractionHand)18 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)18 ServerPlayer (net.minecraft.server.level.ServerPlayer)17 LivingEntity (net.minecraft.world.entity.LivingEntity)17 AABB (net.minecraft.world.phys.AABB)17 EntityHitResult (net.minecraft.world.phys.EntityHitResult)17 Minecraft (net.minecraft.client.Minecraft)16