2020-05-23 21:39:17 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-05-23 21:39:17 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.entity.type;
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.BooleanEntityMetadata;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.IntEntityMetadata;
|
2020-05-23 21:39:17 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
|
2021-02-25 18:12:25 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.AnimatePacket;
|
2021-05-31 02:07:17 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.MoveEntityAbsolutePacket;
|
2021-11-18 03:02:38 +00:00
|
|
|
import lombok.Getter;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.EntityDefinition;
|
|
|
|
import org.geysermc.geyser.entity.EntityDefinitions;
|
2021-11-21 18:36:42 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSessionImpl;
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import java.util.UUID;
|
2020-05-23 21:39:17 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public class BoatEntity extends Entity {
|
|
|
|
|
2020-12-15 18:09:40 +00:00
|
|
|
/**
|
|
|
|
* Required when IS_BUOYANT is sent in order for boats to work in the water. <br>
|
|
|
|
*
|
|
|
|
* Taken from BDS 1.16.200, with the modification of <code>simulate_waves</code> since Java doesn't bob the boat up and down
|
|
|
|
* like Bedrock.
|
|
|
|
*/
|
|
|
|
private static final String BUOYANCY_DATA = "{\"apply_gravity\":true,\"base_buoyancy\":1.0,\"big_wave_probability\":0.02999999932944775," +
|
|
|
|
"\"big_wave_speed\":10.0,\"drag_down_on_buoyancy_removed\":0.0,\"liquid_blocks\":[\"minecraft:water\"," +
|
2021-02-08 02:18:20 +00:00
|
|
|
"\"minecraft:flowing_water\"],\"simulate_waves\":false}";
|
2020-12-15 18:09:40 +00:00
|
|
|
|
2020-05-23 21:39:17 +00:00
|
|
|
private boolean isPaddlingLeft;
|
|
|
|
private float paddleTimeLeft;
|
|
|
|
private boolean isPaddlingRight;
|
|
|
|
private float paddleTimeRight;
|
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
/**
|
|
|
|
* Saved for using the "pick" functionality on a boat.
|
|
|
|
*/
|
|
|
|
@Getter
|
|
|
|
private int variant;
|
|
|
|
|
2020-05-23 21:39:17 +00:00
|
|
|
// Looks too fast and too choppy with 0.1f, which is how I believe the Microsoftian client handles it
|
|
|
|
private final float ROWING_SPEED = 0.05f;
|
|
|
|
|
2021-11-21 18:36:42 +00:00
|
|
|
public BoatEntity(GeyserSessionImpl session, long entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
2021-11-18 03:02:38 +00:00
|
|
|
// Initial rotation is incorrect
|
|
|
|
super(session, entityId, geyserId, uuid, definition, position.add(0d, definition.offset(), 0d), motion, yaw + 90, 0, yaw + 90);
|
2020-12-15 18:09:40 +00:00
|
|
|
|
|
|
|
// Required to be able to move on land 1.16.200+ or apply gravity not in the water 1.16.100+
|
2021-11-18 03:02:38 +00:00
|
|
|
dirtyMetadata.put(EntityData.IS_BUOYANT, (byte) 1);
|
|
|
|
dirtyMetadata.put(EntityData.BUOYANCY_DATA, BUOYANCY_DATA);
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void moveAbsolute(Vector3f position, float yaw, float pitch, float headYaw, boolean isOnGround, boolean teleported) {
|
2020-05-23 21:39:17 +00:00
|
|
|
// We don't include the rotation (y) as it causes the boat to appear sideways
|
2021-11-18 03:02:38 +00:00
|
|
|
setPosition(position.add(0d, this.definition.offset(), 0d));
|
|
|
|
this.yaw = yaw + 90;
|
|
|
|
this.headYaw = yaw + 90;
|
2021-05-31 02:07:17 +00:00
|
|
|
setOnGround(isOnGround);
|
|
|
|
|
|
|
|
MoveEntityAbsolutePacket moveEntityPacket = new MoveEntityAbsolutePacket();
|
|
|
|
moveEntityPacket.setRuntimeEntityId(geyserId);
|
2021-11-13 03:44:15 +00:00
|
|
|
// Minimal glitching when ClientboundMoveVehiclePacket is sent
|
2021-11-18 03:02:38 +00:00
|
|
|
moveEntityPacket.setPosition(session.getRidingVehicleEntity() == this ? position.up(EntityDefinitions.PLAYER.offset() - this.definition.offset()) : this.position);
|
2021-05-31 02:07:17 +00:00
|
|
|
moveEntityPacket.setRotation(getBedrockRotation());
|
|
|
|
moveEntityPacket.setOnGround(isOnGround);
|
|
|
|
moveEntityPacket.setTeleported(teleported);
|
|
|
|
|
|
|
|
session.sendUpstreamPacket(moveEntityPacket);
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 01:27:38 +00:00
|
|
|
/**
|
|
|
|
* Move the boat without making the adjustments needed to translate from Java
|
|
|
|
*/
|
2021-11-18 03:02:38 +00:00
|
|
|
public void moveAbsoluteWithoutAdjustments(Vector3f position, float yaw, boolean isOnGround, boolean teleported) {
|
|
|
|
super.moveAbsolute(position, yaw, 0, yaw, isOnGround, teleported);
|
2021-09-10 01:27:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:39:17 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void moveRelative(double relX, double relY, double relZ, float yaw, float pitch, float headYaw, boolean isOnGround) {
|
|
|
|
super.moveRelative(relX, relY, relZ, yaw, 0, yaw, isOnGround);
|
2020-06-16 23:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void updatePositionAndRotation(double moveX, double moveY, double moveZ, float yaw, float pitch, boolean isOnGround) {
|
|
|
|
moveRelative(moveX, moveY, moveZ, yaw + 90, pitch, isOnGround);
|
2020-06-16 23:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void updateRotation(float yaw, float pitch, boolean isOnGround) {
|
|
|
|
moveRelative(0, 0, 0, yaw + 90, 0, 0, isOnGround);
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setVariant(IntEntityMetadata entityMetadata) {
|
|
|
|
variant = entityMetadata.getPrimitiveValue();
|
2021-11-18 03:02:38 +00:00
|
|
|
dirtyMetadata.put(EntityData.VARIANT, variant);
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setPaddlingLeft(BooleanEntityMetadata entityMetadata) {
|
|
|
|
isPaddlingLeft = entityMetadata.getPrimitiveValue();
|
2021-11-18 03:02:38 +00:00
|
|
|
if (isPaddlingLeft) {
|
|
|
|
// Java sends simply "true" and "false" (is_paddling_left), Bedrock keeps sending packets as you're rowing
|
|
|
|
// This is an asynchronous method that emulates Bedrock rowing until "false" is sent.
|
|
|
|
paddleTimeLeft = 0f;
|
|
|
|
if (!this.passengers.isEmpty()) {
|
|
|
|
// Get the entity by the first stored passenger and convey motion in this manner
|
|
|
|
Entity entity = session.getEntityCache().getEntityByJavaId(this.passengers.iterator().nextLong());
|
|
|
|
if (entity != null) {
|
|
|
|
updateLeftPaddle(session, entity);
|
2021-02-25 18:12:25 +00:00
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
} else {
|
|
|
|
// Indicate that the row position should be reset
|
|
|
|
dirtyMetadata.put(EntityData.ROW_TIME_LEFT, 0.0f);
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setPaddlingRight(BooleanEntityMetadata entityMetadata) {
|
|
|
|
isPaddlingRight = entityMetadata.getPrimitiveValue();
|
2021-11-18 03:02:38 +00:00
|
|
|
if (isPaddlingRight) {
|
|
|
|
paddleTimeRight = 0f;
|
|
|
|
if (!this.passengers.isEmpty()) {
|
|
|
|
Entity entity = session.getEntityCache().getEntityByJavaId(this.passengers.iterator().nextLong());
|
|
|
|
if (entity != null) {
|
|
|
|
updateRightPaddle(session, entity);
|
2021-02-25 18:12:25 +00:00
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
2021-11-18 03:02:38 +00:00
|
|
|
} else {
|
|
|
|
dirtyMetadata.put(EntityData.ROW_TIME_RIGHT, 0.0f);
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
2021-02-25 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 18:36:42 +00:00
|
|
|
private void updateLeftPaddle(GeyserSessionImpl session, Entity rower) {
|
2020-05-23 21:39:17 +00:00
|
|
|
if (isPaddlingLeft) {
|
|
|
|
paddleTimeLeft += ROWING_SPEED;
|
2021-02-25 18:12:25 +00:00
|
|
|
sendAnimationPacket(session, rower, AnimatePacket.Action.ROW_LEFT, paddleTimeLeft);
|
|
|
|
|
2021-11-13 16:03:55 +00:00
|
|
|
session.scheduleInEventLoop(() ->
|
2021-02-25 18:12:25 +00:00
|
|
|
updateLeftPaddle(session, rower),
|
2020-05-23 21:39:17 +00:00
|
|
|
100,
|
|
|
|
TimeUnit.MILLISECONDS
|
|
|
|
);
|
2021-02-25 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-11-21 18:36:42 +00:00
|
|
|
private void updateRightPaddle(GeyserSessionImpl session, Entity rower) {
|
2020-05-23 21:39:17 +00:00
|
|
|
if (isPaddlingRight) {
|
|
|
|
paddleTimeRight += ROWING_SPEED;
|
2021-02-25 18:12:25 +00:00
|
|
|
sendAnimationPacket(session, rower, AnimatePacket.Action.ROW_RIGHT, paddleTimeRight);
|
|
|
|
|
2021-11-13 16:03:55 +00:00
|
|
|
session.scheduleInEventLoop(() ->
|
2021-02-25 18:12:25 +00:00
|
|
|
updateRightPaddle(session, rower),
|
2020-05-23 21:39:17 +00:00
|
|
|
100,
|
|
|
|
TimeUnit.MILLISECONDS
|
|
|
|
);
|
2021-02-25 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:36:42 +00:00
|
|
|
private void sendAnimationPacket(GeyserSessionImpl session, Entity rower, AnimatePacket.Action action, float rowTime) {
|
2021-02-25 18:12:25 +00:00
|
|
|
AnimatePacket packet = new AnimatePacket();
|
|
|
|
packet.setRuntimeEntityId(rower.getGeyserId());
|
|
|
|
packet.setAction(action);
|
|
|
|
packet.setRowingTime(rowTime);
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|