Java Code Examples for com.nukkitx.math.vector.Vector3f#from()

The following examples show how to use com.nukkitx.math.vector.Vector3f#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerEntity.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) {
    setRotation(rotation);
    this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ);

    setOnGround(isOnGround);

    MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
    movePlayerPacket.setRuntimeEntityId(geyserId);
    movePlayerPacket.setPosition(position);
    movePlayerPacket.setRotation(getBedrockRotation());
    movePlayerPacket.setOnGround(isOnGround);
    movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL);
    session.sendUpstreamPacket(movePlayerPacket);
    if (leftParrot != null) {
        leftParrot.moveRelative(session, relX, relY, relZ, rotation, true);
    }
    if (rightParrot != null) {
        rightParrot.moveRelative(session, relX, relY, relZ, rotation, true);
    }
}
 
Example 2
Source File: JavaSpawnPlayerTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translate(ServerSpawnPlayerPacket packet, GeyserSession session) {
    Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ());
    Vector3f rotation = Vector3f.from(packet.getYaw(), packet.getPitch(), packet.getYaw());

    PlayerEntity entity = session.getEntityCache().getPlayerEntity(packet.getUuid());
    if (entity == null) {
        GeyserConnector.getInstance().getLogger().error("Haven't received PlayerListEntry packet before spawning player! We ignore the player " + packet.getUuid());
        return;
    }

    entity.setEntityId(packet.getEntityId());
    entity.setPosition(position);
    entity.setRotation(rotation);
    session.getEntityCache().cacheEntity(entity);

    // async skin loading
    if (session.getUpstream().isInitialized()) {
        SkinUtils.requestAndHandleSkinAndCape(entity, session, skinAndCape -> entity.sendPlayer(session));
    }
}
 
Example 3
Source File: JavaSpawnLivingEntityTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translate(ServerSpawnLivingEntityPacket packet, GeyserSession session) {
    Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ());
    Vector3f motion = Vector3f.from(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ());
    Vector3f rotation = Vector3f.from(packet.getYaw(), packet.getPitch(), packet.getHeadYaw());

    EntityType type = EntityUtils.toBedrockEntity(packet.getType());
    if (type == null) {
        session.getConnector().getLogger().warning("Entity type " + packet.getType() + " was null.");
        return;
    }

    Class<? extends Entity> entityClass = type.getEntityClass();
    try {
        Constructor<? extends Entity> entityConstructor = entityClass.getConstructor(long.class, long.class, EntityType.class,
                Vector3f.class, Vector3f.class, Vector3f.class);

        Entity entity = entityConstructor.newInstance(packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(),
                type, position, motion, rotation
        );
        session.getEntityCache().spawnEntity(entity);
    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
        ex.printStackTrace();
    }
}
 
Example 4
Source File: JavaSpawnPaintingTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translate(ServerSpawnPaintingPacket packet, GeyserSession session) {
    Vector3f position = Vector3f.from(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ());

    GeyserConnector.getInstance().getGeneralThreadPool().execute(() -> { // #slowdownbrother, just don't execute it directly
        PaintingEntity entity = new PaintingEntity(
                packet.getEntityId(),
                session.getEntityCache().getNextEntityId().incrementAndGet(),
                position
        )
                .setPaintingName(PaintingType.getByPaintingType(packet.getPaintingType()))
                .setDirection(packet.getDirection().ordinal());

        session.getEntityCache().spawnEntity(entity);
    });
}
 
Example 5
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 6
Source File: Entity.java    From Geyser with MIT License 5 votes vote down vote up
public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) {
    setRotation(rotation);
    setOnGround(isOnGround);
    this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ);

    MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket();
    moveEntityPacket.setRuntimeEntityId(geyserId);
    moveEntityPacket.setPosition(position);
    moveEntityPacket.setRotation(getBedrockRotation());
    moveEntityPacket.setOnGround(isOnGround);
    moveEntityPacket.setTeleported(false);

    session.sendUpstreamPacket(moveEntityPacket);
}
 
Example 7
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 8
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 9
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 10
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 11
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 12
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 13
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 14
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 18
Source File: BedrockMovePlayerTranslator.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public void translate(MovePlayerPacket packet, GeyserSession session) {
    PlayerEntity entity = session.getPlayerEntity();
    if (entity == null || !session.isSpawned() || session.getPendingDimSwitches().get() > 0) return;

    if (!session.getUpstream().isInitialized()) {
        MoveEntityAbsolutePacket moveEntityBack = new MoveEntityAbsolutePacket();
        moveEntityBack.setRuntimeEntityId(entity.getGeyserId());
        moveEntityBack.setPosition(entity.getPosition());
        moveEntityBack.setRotation(entity.getBedrockRotation());
        moveEntityBack.setTeleported(true);
        moveEntityBack.setOnGround(true);
        session.sendUpstreamPacketImmediately(moveEntityBack);
        return;
    }

    // We need to parse the float as a string since casting a float to a double causes us to
    // lose precision and thus, causes players to get stuck when walking near walls
    double javaY = packet.getPosition().getY() - EntityType.PLAYER.getOffset();
    if (packet.isOnGround()) javaY = Math.ceil(javaY * 2) / 2;

    Vector3d position = Vector3d.from(Double.parseDouble(Float.toString(packet.getPosition().getX())), javaY,
            Double.parseDouble(Float.toString(packet.getPosition().getZ())));

    if(!session.confirmTeleport(position)){
        return;
    }

    if (!isValidMove(session, packet.getMode(), entity.getPosition(), packet.getPosition())) {
        session.getConnector().getLogger().debug("Recalculating position...");
        recalculatePosition(session, entity, entity.getPosition());
        return;
    }

    ClientPlayerPositionRotationPacket playerPositionRotationPacket = new ClientPlayerPositionRotationPacket(
            packet.isOnGround(), position.getX(), position.getY(), position.getZ(), packet.getRotation().getY(), packet.getRotation().getX()
    );

    // head yaw, pitch, head yaw
    Vector3f rotation = Vector3f.from(packet.getRotation().getY(), packet.getRotation().getX(), packet.getRotation().getY());
    entity.setPosition(packet.getPosition().sub(0, EntityType.PLAYER.getOffset(), 0));
    entity.setRotation(rotation);
    entity.setOnGround(packet.isOnGround());

    /*
    boolean colliding = false;
    Position position = new Position((int) packet.getPosition().getX(),
            (int) Math.ceil(javaY * 2) / 2, (int) packet.getPosition().getZ());

    BlockEntry block = session.getChunkCache().getBlockAt(position);
    if (!block.getJavaIdentifier().contains("air"))
        colliding = true;

    if (!colliding)
     */
    session.sendDownstreamPacket(playerPositionRotationPacket);
}
 
Example 19
Source File: BoatEntity.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public void moveRelative(GeyserSession session, double relX, double relY, double relZ, Vector3f rotation, boolean isOnGround) {
    super.moveRelative(session, relX, relY, relZ, Vector3f.from(rotation.getX(), 0, rotation.getX()), isOnGround);
}
 
Example 20
Source File: BoatEntity.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public void moveAbsolute(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround, boolean teleported) {
    // We don't include the rotation (y) as it causes the boat to appear sideways
    super.moveAbsolute(session, position.add(0d, this.entityType.getOffset(), 0d), Vector3f.from(rotation.getX() + 90, 0, rotation.getX() + 90), isOnGround, teleported);
}