Search in sources :

Example 1 with PacketPortalFX

use of mekanism.common.network.to_client.PacketPortalFX in project Mekanism by mekanism.

the class TileEntityTeleporter method sendTeleportParticles.

/**
 * @apiNote Only call this from the server
 */
public void sendTeleportParticles() {
    BlockPos teleporterTargetPos = getTeleporterTargetPos();
    Direction offsetDirection;
    if (frameDirection == null || frameDirection.getAxis().isVertical()) {
        // Down is up as well because we have the bottom pos be the bottom of the two spots
        offsetDirection = Direction.UP;
    } else {
        offsetDirection = frameDirection;
    }
    Mekanism.packetHandler.sendToAllTracking(new PacketPortalFX(teleporterTargetPos, offsetDirection), level, teleporterTargetPos);
}
Also used : PacketPortalFX(mekanism.common.network.to_client.PacketPortalFX) BlockPos(net.minecraft.util.math.BlockPos) Direction(net.minecraft.util.Direction)

Example 2 with PacketPortalFX

use of mekanism.common.network.to_client.PacketPortalFX in project Mekanism by mekanism.

the class PacketPortableTeleporterTeleport method handle.

@Override
public void handle(NetworkEvent.Context context) {
    ServerPlayerEntity player = context.getSender();
    if (player == null) {
        return;
    }
    ItemStack stack = player.getItemInHand(currentHand);
    if (!stack.isEmpty() && stack.getItem() instanceof ItemPortableTeleporter) {
        // Note: We make use of the player's own UUID, given they shouldn't be allowed to teleport to a private frequency of another player
        TeleporterFrequency found = FrequencyType.TELEPORTER.getFrequency(identity, player.getUUID());
        if (found == null) {
            return;
        }
        Coord4D coords = found.getClosestCoords(new Coord4D(player));
        if (coords != null) {
            World teleWorld = ServerLifecycleHooks.getCurrentServer().getLevel(coords.dimension);
            TileEntityTeleporter teleporter = WorldUtils.getTileEntity(TileEntityTeleporter.class, teleWorld, coords.getPos());
            if (teleporter != null) {
                if (!player.isCreative()) {
                    FloatingLong energyCost = TileEntityTeleporter.calculateEnergyCost(player, coords);
                    IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
                    if (energyContainer == null || energyContainer.extract(energyCost, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyCost)) {
                        return;
                    }
                    energyContainer.extract(energyCost, Action.EXECUTE, AutomationType.MANUAL);
                }
                // TODO: Figure out what this try catch is meant to be catching as I don't see much of a reason for it to exist
                try {
                    teleporter.didTeleport.add(player.getUUID());
                    teleporter.teleDelay = 5;
                    player.connection.aboveGroundTickCount = 0;
                    player.closeContainer();
                    Mekanism.packetHandler.sendToAllTracking(new PacketPortalFX(player.blockPosition()), player.level, coords.getPos());
                    if (player.isPassenger()) {
                        player.stopRiding();
                    }
                    double oldX = player.getX();
                    double oldY = player.getY();
                    double oldZ = player.getZ();
                    World oldWorld = player.level;
                    BlockPos teleporterTargetPos = teleporter.getTeleporterTargetPos();
                    TileEntityTeleporter.teleportEntityTo(player, teleWorld, teleporterTargetPos);
                    TileEntityTeleporter.alignPlayer(player, teleporterTargetPos, teleporter);
                    if (player.level != oldWorld || player.distanceToSqr(oldX, oldY, oldZ) >= 25) {
                        // If the player teleported over 5 blocks, play the sound at both the destination and the source
                        oldWorld.playSound(null, oldX, oldY, oldZ, SoundEvents.ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
                    }
                    player.level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
                    teleporter.sendTeleportParticles();
                } catch (Exception ignored) {
                }
            }
        }
    }
}
Also used : PacketPortalFX(mekanism.common.network.to_client.PacketPortalFX) TeleporterFrequency(mekanism.common.content.teleporter.TeleporterFrequency) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) World(net.minecraft.world.World) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) ItemPortableTeleporter(mekanism.common.item.ItemPortableTeleporter) Coord4D(mekanism.api.Coord4D) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) TileEntityTeleporter(mekanism.common.tile.TileEntityTeleporter)

Example 3 with PacketPortalFX

use of mekanism.common.network.to_client.PacketPortalFX in project Mekanism by mekanism.

the class ItemMekaTool method use.

@Nonnull
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, @Nonnull Hand hand) {
    ItemStack stack = player.getItemInHand(hand);
    if (!world.isClientSide()) {
        IModule<ModuleTeleportationUnit> module = getModule(stack, MekanismModules.TELEPORTATION_UNIT);
        if (module != null && module.isEnabled()) {
            BlockRayTraceResult result = MekanismUtils.rayTrace(player, MekanismConfig.gear.mekaToolMaxTeleportReach.get());
            // If we don't require a block target or are not a miss, allow teleporting
            if (!module.getCustomInstance().requiresBlockTarget() || result.getType() != RayTraceResult.Type.MISS) {
                BlockPos pos = result.getBlockPos();
                // make sure we fit
                if (isValidDestinationBlock(world, pos.above()) && isValidDestinationBlock(world, pos.above(2))) {
                    double distance = player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ());
                    if (distance < 5) {
                        return new ActionResult<>(ActionResultType.PASS, stack);
                    }
                    IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
                    FloatingLong energyNeeded = MekanismConfig.gear.mekaToolEnergyUsageTeleport.get().multiply(distance / 10D);
                    if (energyContainer == null || energyContainer.getEnergy().smallerThan(energyNeeded)) {
                        return new ActionResult<>(ActionResultType.FAIL, stack);
                    }
                    energyContainer.extract(energyNeeded, Action.EXECUTE, AutomationType.MANUAL);
                    if (player.isPassenger()) {
                        player.stopRiding();
                    }
                    player.teleportTo(pos.getX() + 0.5, pos.getY() + 1.5, pos.getZ() + 0.5);
                    player.fallDistance = 0.0F;
                    Mekanism.packetHandler.sendToAllTracking(new PacketPortalFX(pos.above()), world, pos);
                    world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDERMAN_TELEPORT, SoundCategory.PLAYERS, 1.0F, 1.0F);
                    return new ActionResult<>(ActionResultType.SUCCESS, stack);
                }
            }
        }
    }
    return new ActionResult<>(ActionResultType.PASS, stack);
}
Also used : FloatingLong(mekanism.api.math.FloatingLong) PacketPortalFX(mekanism.common.network.to_client.PacketPortalFX) IEnergyContainer(mekanism.api.energy.IEnergyContainer) ActionResult(net.minecraft.util.ActionResult) ModuleTeleportationUnit(mekanism.common.content.gear.mekatool.ModuleTeleportationUnit) BlockPos(net.minecraft.util.math.BlockPos) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

PacketPortalFX (mekanism.common.network.to_client.PacketPortalFX)3 BlockPos (net.minecraft.util.math.BlockPos)3 IEnergyContainer (mekanism.api.energy.IEnergyContainer)2 FloatingLong (mekanism.api.math.FloatingLong)2 ItemStack (net.minecraft.item.ItemStack)2 Nonnull (javax.annotation.Nonnull)1 Coord4D (mekanism.api.Coord4D)1 ModuleTeleportationUnit (mekanism.common.content.gear.mekatool.ModuleTeleportationUnit)1 TeleporterFrequency (mekanism.common.content.teleporter.TeleporterFrequency)1 ItemPortableTeleporter (mekanism.common.item.ItemPortableTeleporter)1 TileEntityTeleporter (mekanism.common.tile.TileEntityTeleporter)1 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 ActionResult (net.minecraft.util.ActionResult)1 Direction (net.minecraft.util.Direction)1 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)1 World (net.minecraft.world.World)1