com.nukkitx.math.vector.Vector3f Java Examples

The following examples show how to use com.nukkitx.math.vector.Vector3f. 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: BedrockMovePlayerTranslator.java    From Geyser with MIT License 6 votes vote down vote up
public boolean isValidMove(GeyserSession session, MovePlayerPacket.Mode mode, Vector3f currentPosition, Vector3f newPosition) {
    if (mode != MovePlayerPacket.Mode.NORMAL)
        return true;

    double xRange = newPosition.getX() - currentPosition.getX();
    double yRange = newPosition.getY() - currentPosition.getY();
    double zRange = newPosition.getZ() - currentPosition.getZ();

    if (xRange < 0)
        xRange = -xRange;
    if (yRange < 0)
        yRange = -yRange;
    if (zRange < 0)
        zRange = -zRange;

    if ((xRange + yRange + zRange) > 100) {
        session.getConnector().getLogger().debug(ChatColor.RED + session.getName() + " moved too quickly." +
                " current position: " + currentPosition + ", new position: " + newPosition);

        return false;
    }

    return true;
}
 
Example #2
Source File: PlayerAuthInputSerializer_v388.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, PlayerAuthInputPacket packet) {
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    packet.setPosition(BedrockUtils.readVector3f(buffer));
    packet.setMotion(Vector2f.from(buffer.readFloatLE(), buffer.readFloatLE()));
    float z = buffer.readFloatLE();
    packet.setRotation(Vector3f.from(x, y, z));
    packet.setInputData(VarInts.readUnsignedLong(buffer));
    packet.setInputMode(VarInts.readUnsignedInt(buffer));
    packet.setPlayMode(VarInts.readUnsignedInt(buffer));

    if (packet.getPlayMode() == 4) {
        packet.setVrGazeDirection(BedrockUtils.readVector3f(buffer));
    }
}
 
Example #3
Source File: Entity.java    From Geyser with MIT License 6 votes vote down vote up
public Entity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
    this.entityId = entityId;
    this.geyserId = geyserId;
    this.entityType = entityType;
    this.motion = motion;
    this.rotation = rotation;

    this.valid = false;
    this.dimension = 0;

    setPosition(position);

    metadata.put(EntityData.SCALE, 1f);
    metadata.put(EntityData.COLOR, 0);
    metadata.put(EntityData.MAX_AIR, (short) 300);
    metadata.put(EntityData.AIR, (short) 0);
    metadata.put(EntityData.LEAD_HOLDER_EID, -1L);
    metadata.put(EntityData.BOUNDING_BOX_HEIGHT, entityType.getHeight());
    metadata.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth());
    EntityFlags flags = new EntityFlags();
    flags.setFlag(EntityFlag.HAS_GRAVITY, true);
    flags.setFlag(EntityFlag.HAS_COLLISION, true);
    flags.setFlag(EntityFlag.CAN_SHOW_NAME, true);
    flags.setFlag(EntityFlag.CAN_CLIMB, true);
    metadata.putFlags(flags);
}
 
Example #4
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 #5
Source File: JavaEntityVelocityTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translate(ServerEntityVelocityPacket packet, GeyserSession session) {
    Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
    if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) {
        entity = session.getPlayerEntity();
    }
    if (entity == null) return;

    entity.setMotion(Vector3f.from(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()));

    SetEntityMotionPacket entityMotionPacket = new SetEntityMotionPacket();
    entityMotionPacket.setRuntimeEntityId(entity.getGeyserId());
    entityMotionPacket.setMotion(entity.getMotion());

    session.sendUpstreamPacket(entityMotionPacket);
}
 
Example #6
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 #7
Source File: FlintAndSteelInteractionHandler.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
    LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
    levelSoundEventPacket.setPosition(position);
    levelSoundEventPacket.setBabySound(false);
    levelSoundEventPacket.setRelativeVolumeDisabled(false);
    levelSoundEventPacket.setIdentifier(":");
    levelSoundEventPacket.setSound(SoundEvent.IGNITE);
    levelSoundEventPacket.setExtraData(-1);
    session.sendUpstreamPacket(levelSoundEventPacket);
}
 
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: MoveEntityDeltaSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    byte flags = buffer.readByte();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example #10
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3f(ByteBuf buffer, Vector3f vector3f) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3f, "vector3f");
    buffer.writeFloatLE(vector3f.getX());
    buffer.writeFloatLE(vector3f.getY());
    buffer.writeFloatLE(vector3f.getZ());
}
 
Example #11
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 #12
Source File: FishingHookEntity.java    From Geyser with MIT License 5 votes vote down vote up
public FishingHookEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation, ProjectileData data) {
    super(entityId, geyserId, entityType, position, motion, rotation);

    for (GeyserSession session : GeyserConnector.getInstance().getPlayers().values()) {
        Entity entity = session.getEntityCache().getEntityByJavaId(data.getOwnerId());
        if (entity == null && session.getPlayerEntity().getEntityId() == data.getOwnerId()) {
            entity = session.getPlayerEntity();
        }

        if (entity != null) {
            this.metadata.put(EntityData.OWNER_EID, entity.getGeyserId());
            return;
        }
    }
}
 
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: MoveEntityDeltaSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    short flags = buffer.readShortLE();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example #15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3f(ByteBuf buffer, Vector3f vector3f) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3f, "vector3f");
    buffer.writeFloatLE(vector3f.getX());
    buffer.writeFloatLE(vector3f.getY());
    buffer.writeFloatLE(vector3f.getZ());
}
 
Example #16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3f(ByteBuf buffer, Vector3f vector3f) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3f, "vector3f");
    buffer.writeFloatLE(vector3f.getX());
    buffer.writeFloatLE(vector3f.getY());
    buffer.writeFloatLE(vector3f.getZ());
}
 
Example #17
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 #18
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3f(ByteBuf buffer, Vector3f vector3f) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3f, "vector3f");
    buffer.writeFloatLE(vector3f.getX());
    buffer.writeFloatLE(vector3f.getY());
    buffer.writeFloatLE(vector3f.getZ());
}
 
Example #19
Source File: AbstractFishEntity.java    From Geyser with MIT License 5 votes vote down vote up
public AbstractFishEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
    super(entityId, geyserId, entityType, position, motion, rotation);

    metadata.getFlags().setFlag(EntityFlag.CAN_SWIM, true);
    metadata.getFlags().setFlag(EntityFlag.BREATHING, true);
    metadata.getFlags().setFlag(EntityFlag.CAN_CLIMB, false);
    metadata.getFlags().setFlag(EntityFlag.HAS_GRAVITY, false);
}
 
Example #20
Source File: JavaSpawnWeatherEntityTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translate(ServerSpawnWeatherEntityPacket packet, GeyserSession session) {
    Vector3f position = Vector3f.from(packet.getX(), packet.getY(), packet.getZ());

    // Currently WeatherEntityType only has a lightning bolt
    Entity entity = new Entity(
            packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(),
            EntityType.LIGHTNING_BOLT, position, Vector3f.ZERO, Vector3f.ZERO
    );

    session.getEntityCache().spawnEntity(entity);
}
 
Example #21
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 #22
Source File: PlayerEntity.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void updateHeadLookRotation(GeyserSession session, float headYaw) {
    moveRelative(session, 0, 0, 0, Vector3f.from(rotation.getX(), rotation.getY(), headYaw), onGround);
    MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
    movePlayerPacket.setRuntimeEntityId(geyserId);
    movePlayerPacket.setPosition(position);
    movePlayerPacket.setRotation(getBedrockRotation());
    movePlayerPacket.setMode(MovePlayerPacket.Mode.ROTATION);
    session.sendUpstreamPacket(movePlayerPacket);
}
 
Example #23
Source File: BedrockMovePlayerTranslator.java    From Geyser with MIT License 5 votes vote down vote up
public void recalculatePosition(GeyserSession session, Entity entity, Vector3f currentPosition) {
    // Gravity might need to be reset...
    SetEntityDataPacket entityDataPacket = new SetEntityDataPacket();
    entityDataPacket.setRuntimeEntityId(entity.getGeyserId());
    entityDataPacket.getMetadata().putAll(entity.getMetadata());
    session.sendUpstreamPacket(entityDataPacket);

    MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
    movePlayerPacket.setRuntimeEntityId(entity.getGeyserId());
    movePlayerPacket.setPosition(entity.getPosition());
    movePlayerPacket.setRotation(entity.getBedrockRotation());
    movePlayerPacket.setMode(MovePlayerPacket.Mode.RESET);
    session.sendUpstreamPacket(movePlayerPacket);
}
 
Example #24
Source File: MoveEntityDeltaSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    byte flags = buffer.readByte();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example #25
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 #26
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 #27
Source File: MoveEntityDeltaSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    byte flags = 0;
    Vector3i movementDelta = packet.getMovementDelta();
    Vector3f rotationDelta = packet.getRotationDelta();
    flags |= movementDelta.getX() != 0 ? HAS_X : 0;
    flags |= movementDelta.getY() != 0 ? HAS_Y : 0;
    flags |= movementDelta.getZ() != 0 ? HAS_Z : 0;
    flags |= rotationDelta.getX() != 0 ? HAS_PITCH : 0;
    flags |= rotationDelta.getY() != 0 ? HAS_YAW : 0;
    flags |= rotationDelta.getZ() != 0 ? HAS_ROLL : 0;
    buffer.writeByte(flags);
    if ((flags & HAS_X) != 0) {
        VarInts.writeInt(buffer, movementDelta.getX());
    }
    if ((flags & HAS_Y) != 0) {
        VarInts.writeInt(buffer, movementDelta.getY());
    }
    if ((flags & HAS_Z) != 0) {
        VarInts.writeInt(buffer, movementDelta.getZ());
    }
    if ((flags & HAS_PITCH) != 0) {
        BedrockUtils.writeByteAngle(buffer, rotationDelta.getX());
    }
    if ((flags & HAS_YAW) != 0) {
        BedrockUtils.writeByteAngle(buffer, rotationDelta.getY());
    }
    if ((flags & HAS_ROLL) != 0) {
        BedrockUtils.writeByteAngle(buffer, rotationDelta.getZ());
    }
}
 
Example #28
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 #29
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 #30
Source File: HoeInteractionHandler.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
    LevelSoundEventPacket levelSoundEventPacket = new LevelSoundEventPacket();
    levelSoundEventPacket.setPosition(position);
    levelSoundEventPacket.setBabySound(false);
    levelSoundEventPacket.setRelativeVolumeDisabled(false);
    levelSoundEventPacket.setIdentifier(":");
    levelSoundEventPacket.setSound(SoundEvent.ITEM_USE_ON);
    levelSoundEventPacket.setExtraData(BlockTranslator.getBedrockBlockId(BlockTranslator.getJavaBlockState(identifier)));
    session.sendUpstreamPacket(levelSoundEventPacket);
}