Search in sources :

Example 1 with SMoveVehiclePacket

use of net.minecraft.network.play.server.SMoveVehiclePacket in project Arclight by IzzelAliz.

the class ServerPlayNetHandlerMixin method processVehicleMove.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void processVehicleMove(final CMoveVehiclePacket packetplayinvehiclemove) {
    PacketThreadUtil.checkThreadAndEnqueue(packetplayinvehiclemove, (ServerPlayNetHandler) (Object) this, this.player.getServerWorld());
    if (isMoveVehiclePacketInvalid(packetplayinvehiclemove)) {
        this.disconnect(new TranslationTextComponent("multiplayer.disconnect.invalid_vehicle_movement"));
    } else {
        Entity entity = this.player.getLowestRidingEntity();
        if (entity != this.player && entity.getControllingPassenger() == this.player && entity == this.lowestRiddenEnt) {
            ServerWorld worldserver = this.player.getServerWorld();
            double d0 = entity.posX;
            double d2 = entity.posY;
            double d3 = entity.posZ;
            double d4 = packetplayinvehiclemove.getX();
            double d5 = packetplayinvehiclemove.getY();
            double d6 = packetplayinvehiclemove.getZ();
            float f = packetplayinvehiclemove.getYaw();
            float f2 = packetplayinvehiclemove.getPitch();
            double d7 = d4 - this.lowestRiddenX;
            double d8 = d5 - this.lowestRiddenY;
            double d9 = d6 - this.lowestRiddenZ;
            double d10 = entity.getMotion().lengthSquared();
            double d11 = d7 * d7 + d8 * d8 + d9 * d9;
            this.allowedPlayerTicks += (int) (System.currentTimeMillis() / 50L - this.lastTick);
            this.allowedPlayerTicks = Math.max(this.allowedPlayerTicks, 1);
            this.lastTick = (int) (System.currentTimeMillis() / 50L);
            ++this.movePacketCounter;
            int i = this.movePacketCounter - this.lastMovePacketCounter;
            if (i > Math.max(this.allowedPlayerTicks, 5)) {
                LOGGER.debug(this.player.getScoreboardName() + " is sending move packets too frequently (" + i + " packets since last tick)");
                i = 1;
            }
            if (d11 > 0.0) {
                --this.allowedPlayerTicks;
            } else {
                this.allowedPlayerTicks = 20;
            }
            double speed;
            if (this.player.abilities.isFlying) {
                speed = this.player.abilities.flySpeed * 20.0f;
            } else {
                speed = this.player.abilities.walkSpeed * 10.0f;
            }
            speed *= 2.0;
            if (d11 - d10 > Math.max(100.0, Math.pow(10.0f * i * speed, 2.0)) && !this.func_217264_d()) {
                LOGGER.warn("{} (vehicle of {}) moved too quickly! {},{},{}", entity.getName().getString(), this.player.getName().getString(), d7, d8, d9);
                this.netManager.sendPacket(new SMoveVehiclePacket(entity));
                return;
            }
            boolean flag = this.bridge$worldNoCollision(worldserver, entity, entity.getBoundingBox().shrink(0.0625));
            d7 = d4 - this.lowestRiddenX1;
            d8 = d5 - this.lowestRiddenY1 - 1.0E-6;
            d9 = d6 - this.lowestRiddenZ1;
            entity.move(MoverType.PLAYER, new Vec3d(d7, d8, d9));
            double d12 = d8;
            d7 = d4 - entity.posX;
            d8 = d5 - entity.posY;
            if (d8 > -0.5 || d8 < 0.5) {
                d8 = 0.0;
            }
            d9 = d6 - entity.posZ;
            d11 = d7 * d7 + d8 * d8 + d9 * d9;
            boolean flag2 = false;
            if (d11 > 0.0625) {
                flag2 = true;
                LOGGER.warn("{} moved wrongly!", entity.getName().getString());
            }
            entity.setPositionAndRotation(d4, d5, d6, f, f2);
            this.player.setPositionAndRotation(d4, d5, d6, this.player.rotationYaw, this.player.rotationPitch);
            boolean flag3 = this.bridge$worldNoCollision(worldserver, entity, entity.getBoundingBox().shrink(0.0625));
            if (flag && (flag2 || !flag3)) {
                entity.setPositionAndRotation(d0, d2, d3, f, f2);
                this.player.setPositionAndRotation(d0, d2, d3, this.player.rotationYaw, this.player.rotationPitch);
                this.netManager.sendPacket(new SMoveVehiclePacket(entity));
                return;
            }
            Player player = this.getPlayer();
            Location from = new Location(player.getWorld(), this.lastPosX, this.lastPosY, this.lastPosZ, this.lastYaw, this.lastPitch);
            Location to = player.getLocation().clone();
            to.setX(packetplayinvehiclemove.getX());
            to.setY(packetplayinvehiclemove.getY());
            to.setZ(packetplayinvehiclemove.getZ());
            to.setYaw(packetplayinvehiclemove.getYaw());
            to.setPitch(packetplayinvehiclemove.getPitch());
            double delta = Math.pow(this.lastPosX - to.getX(), 2.0) + Math.pow(this.lastPosY - to.getY(), 2.0) + Math.pow(this.lastPosZ - to.getZ(), 2.0);
            float deltaAngle = Math.abs(this.lastYaw - to.getYaw()) + Math.abs(this.lastPitch - to.getPitch());
            if ((delta > 0.00390625 || deltaAngle > 10.0f) && !((ServerPlayerEntityBridge) this.player).bridge$isMovementBlocked()) {
                this.lastPosX = to.getX();
                this.lastPosY = to.getY();
                this.lastPosZ = to.getZ();
                this.lastYaw = to.getYaw();
                this.lastPitch = to.getPitch();
                if (from.getX() != Double.MAX_VALUE) {
                    Location oldTo = to.clone();
                    PlayerMoveEvent event = new PlayerMoveEvent(player, from, to);
                    this.server.getPluginManager().callEvent(event);
                    if (event.isCancelled()) {
                        this.bridge$teleport(from);
                        return;
                    }
                    if (!oldTo.equals(event.getTo()) && !event.isCancelled()) {
                        ((ServerPlayerEntityBridge) this.player).bridge$getBukkitEntity().teleport(event.getTo(), PlayerTeleportEvent.TeleportCause.PLUGIN);
                        return;
                    }
                    if (!from.equals(this.getPlayer().getLocation()) && this.justTeleported) {
                        this.justTeleported = false;
                        return;
                    }
                }
            }
            this.player.getServerWorld().getChunkProvider().updatePlayerPosition(this.player);
            this.player.addMovementStat(this.player.posX - d0, this.player.posY - d2, this.player.posZ - d3);
            this.vehicleFloating = (d12 >= -0.03125 && !this.minecraftServer.isFlightAllowed() && !worldserver.checkBlockCollision(entity.getBoundingBox().grow(0.0625).expand(0.0, -0.55, 0.0)));
            this.lowestRiddenX1 = entity.posX;
            this.lowestRiddenY1 = entity.posY;
            this.lowestRiddenZ1 = entity.posZ;
        }
    }
}
Also used : SignTileEntity(net.minecraft.tileentity.SignTileEntity) LivingEntity(net.minecraft.entity.LivingEntity) TileEntity(net.minecraft.tileentity.TileEntity) AbstractArrowEntity(net.minecraft.entity.projectile.AbstractArrowEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) AbstractFishEntity(net.minecraft.entity.passive.fish.AbstractFishEntity) ExperienceOrbEntity(net.minecraft.entity.item.ExperienceOrbEntity) MobEntity(net.minecraft.entity.MobEntity) Entity(net.minecraft.entity.Entity) ItemEntity(net.minecraft.entity.item.ItemEntity) Player(org.bukkit.entity.Player) CraftPlayer(org.bukkit.craftbukkit.v.entity.CraftPlayer) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) SMoveVehiclePacket(net.minecraft.network.play.server.SMoveVehiclePacket) Vec3d(net.minecraft.util.math.Vec3d) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) ServerWorld(net.minecraft.world.server.ServerWorld) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) Location(org.bukkit.Location) ResourceLocation(net.minecraft.util.ResourceLocation) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with SMoveVehiclePacket

use of net.minecraft.network.play.server.SMoveVehiclePacket in project Mekanism by mekanism.

the class TileEntityTeleporter method teleportEntityTo.

@Nullable
public static Entity teleportEntityTo(Entity entity, World targetWorld, BlockPos target) {
    if (entity.getCommandSenderWorld().dimension() == targetWorld.dimension()) {
        entity.teleportTo(target.getX() + 0.5, target.getY(), target.getZ() + 0.5);
        if (!entity.getPassengers().isEmpty()) {
            // Force re-apply any passengers so that players don't get "stuck" outside what they may be riding
            ((ServerChunkProvider) entity.getCommandSenderWorld().getChunkSource()).broadcast(entity, new SSetPassengersPacket(entity));
            Entity controller = entity.getControllingPassenger();
            if (controller != entity && controller instanceof ServerPlayerEntity && !(controller instanceof FakePlayer)) {
                ServerPlayerEntity player = (ServerPlayerEntity) controller;
                if (player.connection != null) {
                    // Force sync the fact that the vehicle moved to the client that is controlling it
                    // so that it makes sure to use the correct positions when sending move packets
                    // back to the server instead of running into moved wrongly issues
                    player.connection.send(new SMoveVehiclePacket(entity));
                }
            }
        }
        return entity;
    }
    Vector3d destination = new Vector3d(target.getX() + 0.5, target.getY(), target.getZ() + 0.5);
    // Note: We grab the passengers here instead of in placeEntity as changeDimension starts by removing any passengers
    List<Entity> passengers = entity.getPassengers();
    return entity.changeDimension((ServerWorld) targetWorld, new ITeleporter() {

        @Override
        public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
            Entity repositionedEntity = repositionEntity.apply(false);
            if (repositionedEntity != null) {
                // Teleport all passengers to the other dimension and then make them start riding the entity again
                for (Entity passenger : passengers) {
                    teleportPassenger(destWorld, destination, repositionedEntity, passenger);
                }
            }
            return repositionedEntity;
        }

        @Override
        public PortalInfo getPortalInfo(Entity entity, ServerWorld destWorld, Function<ServerWorld, PortalInfo> defaultPortalInfo) {
            return new PortalInfo(destination, entity.getDeltaMovement(), entity.yRot, entity.xRot);
        }

        @Override
        public boolean playTeleportSound(ServerPlayerEntity player, ServerWorld sourceWorld, ServerWorld destWorld) {
            return false;
        }
    });
}
Also used : ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) Entity(net.minecraft.entity.Entity) PartEntity(net.minecraftforge.entity.PartEntity) SSetPassengersPacket(net.minecraft.network.play.server.SSetPassengersPacket) PortalInfo(net.minecraft.block.PortalInfo) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SMoveVehiclePacket(net.minecraft.network.play.server.SMoveVehiclePacket) FakePlayer(net.minecraftforge.common.util.FakePlayer) ServerWorld(net.minecraft.world.server.ServerWorld) Vector3d(net.minecraft.util.math.vector.Vector3d) ITeleporter(net.minecraftforge.common.util.ITeleporter) ServerChunkProvider(net.minecraft.world.server.ServerChunkProvider) Nullable(javax.annotation.Nullable)

Aggregations

Entity (net.minecraft.entity.Entity)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 SMoveVehiclePacket (net.minecraft.network.play.server.SMoveVehiclePacket)2 ServerWorld (net.minecraft.world.server.ServerWorld)2 ServerPlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge)1 Nullable (javax.annotation.Nullable)1 PortalInfo (net.minecraft.block.PortalInfo)1 LivingEntity (net.minecraft.entity.LivingEntity)1 MobEntity (net.minecraft.entity.MobEntity)1 ExperienceOrbEntity (net.minecraft.entity.item.ExperienceOrbEntity)1 ItemEntity (net.minecraft.entity.item.ItemEntity)1 AbstractFishEntity (net.minecraft.entity.passive.fish.AbstractFishEntity)1 AbstractArrowEntity (net.minecraft.entity.projectile.AbstractArrowEntity)1 SSetPassengersPacket (net.minecraft.network.play.server.SSetPassengersPacket)1 SignTileEntity (net.minecraft.tileentity.SignTileEntity)1 TileEntity (net.minecraft.tileentity.TileEntity)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 Vec3d (net.minecraft.util.math.Vec3d)1 Vector3d (net.minecraft.util.math.vector.Vector3d)1 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)1