2019-08-03 03:38:09 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2019-08-03 03:38:09 +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.player;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-09-16 22:28:29 +00:00
|
|
|
import com.github.steveice10.mc.auth.data.GameProfile;
|
2020-04-04 06:27:34 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
|
2021-06-05 02:26:29 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
|
2021-11-19 01:44:03 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
|
2021-11-20 19:25:21 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.BooleanEntityMetadata;
|
2021-11-18 03:02:38 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.ByteEntityMetadata;
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.type.FloatEntityMetadata;
|
2021-10-01 20:24:25 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreboardPosition;
|
2021-10-05 21:06:15 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
|
2020-05-10 19:38:39 +00:00
|
|
|
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
|
2019-10-09 18:39:38 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-11-10 17:05:16 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3i;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.AttributeData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.command.CommandPermission;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
|
2020-11-10 17:05:16 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData;
|
2021-10-01 20:24:25 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2019-08-03 03:38:09 +00:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2020-11-27 23:28:08 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.entity.EntityDefinitions;
|
2021-12-11 21:05:12 +00:00
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.LivingEntity;
|
|
|
|
import org.geysermc.geyser.entity.type.living.animal.tameable.ParrotEntity;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.scoreboard.Objective;
|
|
|
|
import org.geysermc.geyser.scoreboard.Score;
|
|
|
|
import org.geysermc.geyser.scoreboard.Team;
|
|
|
|
import org.geysermc.geyser.scoreboard.UpdateType;
|
2021-12-11 21:05:12 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
|
|
|
import org.geysermc.geyser.translator.text.MessageTranslator;
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2021-10-05 21:06:15 +00:00
|
|
|
import javax.annotation.Nullable;
|
2021-07-08 02:44:53 +00:00
|
|
|
import java.util.Collections;
|
2021-11-20 19:25:21 +00:00
|
|
|
import java.util.Optional;
|
2020-05-10 19:38:39 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2019-09-16 22:28:29 +00:00
|
|
|
@Getter @Setter
|
2019-12-01 00:19:03 +00:00
|
|
|
public class PlayerEntity extends LivingEntity {
|
2021-12-26 03:46:16 +00:00
|
|
|
public static final float SNEAKING_POSE_HEIGHT = 1.5f;
|
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
private GameProfile profile;
|
2019-09-16 22:28:29 +00:00
|
|
|
private String username;
|
2020-08-07 16:33:21 +00:00
|
|
|
private boolean playerList = true; // Player is in the player list
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2021-11-19 01:44:03 +00:00
|
|
|
private Vector3i bedPosition;
|
|
|
|
|
2020-09-02 04:39:06 +00:00
|
|
|
/**
|
|
|
|
* Saves the parrot currently on the player's left shoulder; otherwise null
|
|
|
|
*/
|
|
|
|
private ParrotEntity leftParrot;
|
|
|
|
/**
|
|
|
|
* Saves the parrot currently on the player's right shoulder; otherwise null
|
|
|
|
*/
|
|
|
|
private ParrotEntity rightParrot;
|
2020-05-10 19:38:39 +00:00
|
|
|
|
2021-12-21 00:25:11 +00:00
|
|
|
public PlayerEntity(GeyserSession session, int entityId, long geyserId, GameProfile gameProfile, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
|
2021-11-18 03:02:38 +00:00
|
|
|
super(session, entityId, geyserId, gameProfile.getId(), EntityDefinitions.PLAYER, position, motion, yaw, pitch, headYaw);
|
2019-08-03 03:38:09 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
profile = gameProfile;
|
2019-09-16 22:28:29 +00:00
|
|
|
username = gameProfile.getName();
|
2021-11-18 03:02:38 +00:00
|
|
|
}
|
2021-05-10 04:48:01 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
@Override
|
|
|
|
protected void initializeMetadata() {
|
|
|
|
super.initializeMetadata();
|
2021-05-10 04:48:01 +00:00
|
|
|
// For the OptionalPack, set all bits as invisible by default as this matches Java Edition behavior
|
2021-11-18 03:02:38 +00:00
|
|
|
dirtyMetadata.put(EntityData.MARK_VARIANT, 0xff);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void spawnEntity() {
|
2021-10-01 20:24:25 +00:00
|
|
|
// Check to see if the player should have a belowname counterpart added
|
|
|
|
Objective objective = session.getWorldCache().getScoreboard().getObjectiveSlots().get(ScoreboardPosition.BELOW_NAME);
|
|
|
|
if (objective != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
setBelowNameText(objective);
|
2021-10-01 20:24:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 18:54:06 +00:00
|
|
|
// The name can't be updated later (the entity metadata for it is ignored), so we need to check for this now
|
2021-11-18 03:02:38 +00:00
|
|
|
updateDisplayName(null, false);
|
2021-10-10 18:54:06 +00:00
|
|
|
|
2019-08-03 03:38:09 +00:00
|
|
|
AddPlayerPacket addPlayerPacket = new AddPlayerPacket();
|
2019-08-03 06:51:05 +00:00
|
|
|
addPlayerPacket.setUuid(uuid);
|
2019-09-16 22:28:29 +00:00
|
|
|
addPlayerPacket.setUsername(username);
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
addPlayerPacket.setUniqueEntityId(geyserId);
|
2021-11-18 03:02:38 +00:00
|
|
|
addPlayerPacket.setPosition(position.sub(0, definition.offset(), 0));
|
2019-10-02 20:45:29 +00:00
|
|
|
addPlayerPacket.setRotation(getBedrockRotation());
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setMotion(motion);
|
2019-08-03 03:38:09 +00:00
|
|
|
addPlayerPacket.setHand(hand);
|
2020-02-06 00:55:34 +00:00
|
|
|
addPlayerPacket.getAdventureSettings().setCommandPermission(CommandPermission.NORMAL);
|
2020-08-08 22:41:12 +00:00
|
|
|
addPlayerPacket.getAdventureSettings().setPlayerPermission(PlayerPermission.MEMBER);
|
2019-09-25 21:52:28 +00:00
|
|
|
addPlayerPacket.setDeviceId("");
|
|
|
|
addPlayerPacket.setPlatformChatId("");
|
2021-11-18 03:02:38 +00:00
|
|
|
addPlayerPacket.getMetadata().putFlags(flags);
|
2021-11-19 01:44:03 +00:00
|
|
|
dirtyMetadata.apply(addPlayerPacket.getMetadata());
|
2021-11-18 03:02:38 +00:00
|
|
|
|
|
|
|
setFlagsDirty(false);
|
2019-08-03 06:51:05 +00:00
|
|
|
|
|
|
|
valid = true;
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(addPlayerPacket);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
public void sendPlayer() {
|
2020-09-16 04:18:18 +00:00
|
|
|
if (session.getEntityCache().getPlayerEntity(uuid) == null)
|
2020-04-29 16:04:45 +00:00
|
|
|
return;
|
2019-10-09 18:39:38 +00:00
|
|
|
|
2021-11-13 16:03:55 +00:00
|
|
|
if (session.getEntityCache().getEntityByGeyserId(geyserId) == null) {
|
2019-10-09 18:39:38 +00:00
|
|
|
session.getEntityCache().spawnEntity(this);
|
|
|
|
} else {
|
2021-11-18 03:02:38 +00:00
|
|
|
spawnEntity();
|
2019-10-09 18:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 23:39:34 +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-02-15 23:39:34 +00:00
|
|
|
setPosition(position);
|
2021-11-18 03:02:38 +00:00
|
|
|
setYaw(yaw);
|
|
|
|
setPitch(pitch);
|
|
|
|
setHeadYaw(headYaw);
|
2020-02-15 23:39:34 +00:00
|
|
|
|
2020-06-16 23:58:06 +00:00
|
|
|
setOnGround(isOnGround);
|
2020-05-23 23:26:20 +00:00
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
|
|
|
|
movePlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
movePlayerPacket.setPosition(this.position);
|
|
|
|
movePlayerPacket.setRotation(getBedrockRotation());
|
|
|
|
movePlayerPacket.setOnGround(isOnGround);
|
2020-04-29 16:06:25 +00:00
|
|
|
movePlayerPacket.setMode(teleported ? MovePlayerPacket.Mode.TELEPORT : MovePlayerPacket.Mode.NORMAL);
|
|
|
|
|
|
|
|
if (teleported) {
|
|
|
|
movePlayerPacket.setTeleportationCause(MovePlayerPacket.TeleportationCause.UNKNOWN);
|
|
|
|
}
|
2020-02-15 23:39:34 +00:00
|
|
|
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(movePlayerPacket);
|
2020-05-10 19:38:39 +00:00
|
|
|
if (leftParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
leftParrot.moveAbsolute(position, yaw, pitch, headYaw, true, teleported);
|
2020-05-10 19:38:39 +00:00
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
rightParrot.moveAbsolute(position, yaw, pitch, headYaw, true, teleported);
|
2020-05-10 19:38:39 +00:00
|
|
|
}
|
2020-02-15 23:39:34 +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) {
|
|
|
|
setYaw(yaw);
|
|
|
|
setPitch(pitch);
|
|
|
|
setHeadYaw(headYaw);
|
2020-02-15 23:39:34 +00:00
|
|
|
this.position = Vector3f.from(position.getX() + relX, position.getY() + relY, position.getZ() + relZ);
|
|
|
|
|
2020-06-16 23:58:06 +00:00
|
|
|
setOnGround(isOnGround);
|
2020-05-23 23:26:20 +00:00
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
|
|
|
|
movePlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
movePlayerPacket.setPosition(position);
|
|
|
|
movePlayerPacket.setRotation(getBedrockRotation());
|
|
|
|
movePlayerPacket.setOnGround(isOnGround);
|
|
|
|
movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL);
|
2020-11-10 17:05:16 +00:00
|
|
|
// If the player is moved while sleeping, we have to adjust their y, so it appears
|
|
|
|
// correctly on Bedrock. This fixes GSit's lay.
|
2021-11-19 01:44:03 +00:00
|
|
|
if (getFlag(EntityFlag.SLEEPING)) {
|
2020-11-10 17:05:16 +00:00
|
|
|
if (bedPosition != null && (bedPosition.getY() == 0 || bedPosition.distanceSquared(position.toInt()) > 4)) {
|
|
|
|
// Force the player movement by using a teleport
|
2021-11-18 03:02:38 +00:00
|
|
|
movePlayerPacket.setPosition(Vector3f.from(position.getX(), position.getY() - definition.offset() + 0.2f, position.getZ()));
|
2020-11-10 17:05:16 +00:00
|
|
|
movePlayerPacket.setMode(MovePlayerPacket.Mode.TELEPORT);
|
|
|
|
movePlayerPacket.setTeleportationCause(MovePlayerPacket.TeleportationCause.UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 15:51:43 +00:00
|
|
|
session.sendUpstreamPacket(movePlayerPacket);
|
2020-05-10 19:38:39 +00:00
|
|
|
if (leftParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
leftParrot.moveRelative(relX, relY, relZ, yaw, pitch, headYaw, true);
|
2020-05-10 19:38:39 +00:00
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
rightParrot.moveRelative(relX, relY, relZ, yaw, pitch, headYaw, true);
|
2020-05-10 19:38:39 +00:00
|
|
|
}
|
2020-02-15 23:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:58:06 +00:00
|
|
|
@Override
|
2021-11-18 03:02:38 +00:00
|
|
|
public void updateHeadLookRotation(float headYaw) {
|
|
|
|
moveRelative(0, 0, 0, yaw, pitch, headYaw, onGround);
|
2020-06-16 23:58:06 +00:00
|
|
|
MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
|
|
|
|
movePlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
movePlayerPacket.setPosition(position);
|
|
|
|
movePlayerPacket.setRotation(getBedrockRotation());
|
2020-06-23 00:11:09 +00:00
|
|
|
movePlayerPacket.setMode(MovePlayerPacket.Mode.HEAD_ROTATION);
|
2020-06-16 23:58:06 +00:00
|
|
|
session.sendUpstreamPacket(movePlayerPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
if (leftParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
leftParrot.moveRelative(moveX, moveY, moveZ, yaw, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
rightParrot.moveRelative(moveX, moveY, moveZ, yaw, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
}
|
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) {
|
|
|
|
super.updateRotation(yaw, pitch, isOnGround);
|
2020-06-16 23:58:06 +00:00
|
|
|
// Both packets need to be sent or else player head rotation isn't correctly updated
|
|
|
|
MovePlayerPacket movePlayerPacket = new MovePlayerPacket();
|
|
|
|
movePlayerPacket.setRuntimeEntityId(geyserId);
|
|
|
|
movePlayerPacket.setPosition(position);
|
|
|
|
movePlayerPacket.setRotation(getBedrockRotation());
|
|
|
|
movePlayerPacket.setOnGround(isOnGround);
|
2020-06-23 00:11:09 +00:00
|
|
|
movePlayerPacket.setMode(MovePlayerPacket.Mode.HEAD_ROTATION);
|
2020-06-16 23:58:06 +00:00
|
|
|
session.sendUpstreamPacket(movePlayerPacket);
|
2020-09-02 04:39:06 +00:00
|
|
|
if (leftParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
leftParrot.updateRotation(yaw, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
2021-11-18 03:02:38 +00:00
|
|
|
rightParrot.updateRotation(yaw, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
}
|
2020-06-16 23:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
@Override
|
|
|
|
public void setPosition(Vector3f position) {
|
2021-11-18 03:02:38 +00:00
|
|
|
super.setPosition(position.add(0, definition.offset(), 0));
|
2020-02-15 23:39:34 +00:00
|
|
|
}
|
2020-04-04 06:27:34 +00:00
|
|
|
|
2021-11-19 01:44:03 +00:00
|
|
|
@Override
|
2021-11-20 19:25:21 +00:00
|
|
|
public Vector3i setBedPosition(EntityMetadata<Optional<Position>, ?> entityMetadata) {
|
2021-11-19 01:44:03 +00:00
|
|
|
return bedPosition = super.setBedPosition(entityMetadata);
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setAbsorptionHearts(FloatEntityMetadata entityMetadata) {
|
2020-05-14 16:30:33 +00:00
|
|
|
// Extra hearts - is not metadata but an attribute on Bedrock
|
2021-11-18 03:02:38 +00:00
|
|
|
UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
|
|
|
|
attributesPacket.setRuntimeEntityId(geyserId);
|
|
|
|
// Setting to a higher maximum since plugins/datapacks can probably extend the Bedrock soft limit
|
|
|
|
attributesPacket.setAttributes(Collections.singletonList(
|
2021-11-20 19:25:21 +00:00
|
|
|
new AttributeData("minecraft:absorption", 0.0f, 1024f, entityMetadata.getPrimitiveValue(), 0.0f)));
|
2021-11-18 03:02:38 +00:00
|
|
|
session.sendUpstreamPacket(attributesPacket);
|
|
|
|
}
|
2020-05-14 16:30:33 +00:00
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setSkinVisibility(ByteEntityMetadata entityMetadata) {
|
2021-11-18 03:02:38 +00:00
|
|
|
// OptionalPack usage for toggling skin bits
|
|
|
|
// In Java Edition, a bit being set means that part should be enabled
|
|
|
|
// However, to ensure that the pack still works on other servers, we invert the bit so all values by default
|
|
|
|
// are true (0).
|
2021-11-20 19:25:21 +00:00
|
|
|
dirtyMetadata.put(EntityData.MARK_VARIANT, ~entityMetadata.getPrimitiveValue() & 0xff);
|
2021-11-18 03:02:38 +00:00
|
|
|
}
|
2021-05-10 04:48:01 +00:00
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setLeftParrot(EntityMetadata<CompoundTag, ?> entityMetadata) {
|
2021-11-18 03:02:38 +00:00
|
|
|
setParrot(entityMetadata.getValue(), true);
|
|
|
|
}
|
|
|
|
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setRightParrot(EntityMetadata<CompoundTag, ?> entityMetadata) {
|
2021-11-18 03:02:38 +00:00
|
|
|
setParrot(entityMetadata.getValue(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the parrot occupying the shoulder. Bedrock Edition requires a full entity whereas Java Edition just
|
|
|
|
* spawns it from the NBT data provided
|
|
|
|
*/
|
|
|
|
private void setParrot(CompoundTag tag, boolean isLeft) {
|
|
|
|
if (tag != null && !tag.isEmpty()) {
|
|
|
|
if ((isLeft && leftParrot != null) || (!isLeft && rightParrot != null)) {
|
|
|
|
// No need to update a parrot's data when it already exists
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The parrot is a separate entity in Bedrock, but part of the player entity in Java //TODO is a UUID provided in NBT?
|
|
|
|
ParrotEntity parrot = new ParrotEntity(session, 0, session.getEntityCache().getNextEntityId().incrementAndGet(),
|
|
|
|
null, EntityDefinitions.PARROT, position, motion, yaw, pitch, headYaw);
|
|
|
|
parrot.spawnEntity();
|
|
|
|
parrot.getDirtyMetadata().put(EntityData.VARIANT, tag.get("Variant").getValue());
|
|
|
|
// Different position whether the parrot is left or right
|
|
|
|
float offset = isLeft ? 0.4f : -0.4f;
|
|
|
|
parrot.getDirtyMetadata().put(EntityData.RIDER_SEAT_POSITION, Vector3f.from(offset, -0.22, -0.1));
|
|
|
|
parrot.getDirtyMetadata().put(EntityData.RIDER_ROTATION_LOCKED, 1);
|
|
|
|
parrot.updateBedrockMetadata();
|
|
|
|
SetEntityLinkPacket linkPacket = new SetEntityLinkPacket();
|
|
|
|
EntityLinkData.Type type = isLeft ? EntityLinkData.Type.RIDER : EntityLinkData.Type.PASSENGER;
|
|
|
|
linkPacket.setEntityLink(new EntityLinkData(geyserId, parrot.getGeyserId(), type, false, false));
|
|
|
|
// Delay, or else spawned-in players won't get the link
|
|
|
|
// TODO: Find a better solution.
|
|
|
|
session.scheduleInEventLoop(() -> session.sendUpstreamPacket(linkPacket), 500, TimeUnit.MILLISECONDS);
|
|
|
|
if (isLeft) {
|
|
|
|
leftParrot = parrot;
|
|
|
|
} else {
|
|
|
|
rightParrot = parrot;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Entity parrot = isLeft ? leftParrot : rightParrot;
|
|
|
|
if (parrot != null) {
|
|
|
|
parrot.despawnEntity();
|
2021-07-21 17:44:11 +00:00
|
|
|
if (isLeft) {
|
2021-11-18 03:02:38 +00:00
|
|
|
leftParrot = null;
|
2020-05-10 19:38:39 +00:00
|
|
|
} else {
|
2021-11-18 03:02:38 +00:00
|
|
|
rightParrot = null;
|
2020-05-10 19:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setDisplayName(EntityMetadata<Optional<Component>, ?> entityMetadata) {
|
2021-10-10 18:54:06 +00:00
|
|
|
// Doesn't do anything for players
|
2021-10-05 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo this will become common entity logic once UUID support is implemented for them
|
|
|
|
/**
|
|
|
|
* @param useGivenTeam even if there is no team, update the username in the entity metadata anyway, and don't look for a team
|
|
|
|
*/
|
2021-11-18 03:02:38 +00:00
|
|
|
public void updateDisplayName(@Nullable Team team, boolean useGivenTeam) {
|
2021-10-05 21:06:15 +00:00
|
|
|
if (team == null && !useGivenTeam) {
|
|
|
|
// Only search for the team if we are not supposed to use the given team
|
|
|
|
// If the given team is null, this is intentional that we are being removed from the team
|
|
|
|
team = session.getWorldCache().getScoreboard().getTeamFor(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean needsUpdate;
|
2021-10-10 18:54:06 +00:00
|
|
|
String newDisplayName = this.username;
|
2021-10-05 21:06:15 +00:00
|
|
|
if (team != null) {
|
|
|
|
if (team.isVisibleFor(session.getPlayerEntity().getUsername())) {
|
|
|
|
TeamColor color = team.getColor();
|
2021-12-13 18:24:58 +00:00
|
|
|
String chatColor = MessageTranslator.toChatColor(color);
|
2021-10-05 21:06:15 +00:00
|
|
|
// We have to emulate what modern Java text already does for us and add the color to each section
|
|
|
|
String prefix = team.getCurrentData().getPrefix();
|
|
|
|
String suffix = team.getCurrentData().getSuffix();
|
2021-10-10 18:54:06 +00:00
|
|
|
newDisplayName = chatColor + prefix + chatColor + this.username + chatColor + suffix;
|
2021-10-05 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
// The name is not visible to the session player; clear name
|
|
|
|
newDisplayName = "";
|
|
|
|
}
|
2021-11-19 01:44:03 +00:00
|
|
|
needsUpdate = useGivenTeam && !newDisplayName.equals(nametag);
|
|
|
|
nametag = newDisplayName;
|
2021-11-18 03:02:38 +00:00
|
|
|
dirtyMetadata.put(EntityData.NAMETAG, newDisplayName);
|
2021-10-05 21:06:15 +00:00
|
|
|
} else if (useGivenTeam) {
|
|
|
|
// The name has reset, if it was previously something else
|
2021-11-19 01:44:03 +00:00
|
|
|
needsUpdate = !newDisplayName.equals(nametag);
|
2021-11-18 03:02:38 +00:00
|
|
|
dirtyMetadata.put(EntityData.NAMETAG, this.username);
|
2021-10-05 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
needsUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsUpdate) {
|
|
|
|
// Update the metadata as it won't be updated later
|
|
|
|
SetEntityDataPacket packet = new SetEntityDataPacket();
|
|
|
|
packet.getMetadata().put(EntityData.NAMETAG, newDisplayName);
|
|
|
|
packet.setRuntimeEntityId(geyserId);
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
2020-04-04 06:27:34 +00:00
|
|
|
}
|
2020-07-01 16:28:03 +00:00
|
|
|
|
2021-10-10 18:54:06 +00:00
|
|
|
@Override
|
2021-11-20 19:25:21 +00:00
|
|
|
public void setDisplayNameVisible(BooleanEntityMetadata entityMetadata) {
|
2021-10-10 18:54:06 +00:00
|
|
|
// Doesn't do anything for players
|
|
|
|
}
|
|
|
|
|
2021-06-05 02:26:29 +00:00
|
|
|
@Override
|
|
|
|
protected void setDimensions(Pose pose) {
|
|
|
|
float height;
|
2022-01-20 00:30:45 +00:00
|
|
|
float width;
|
2021-06-05 02:26:29 +00:00
|
|
|
switch (pose) {
|
2022-01-20 00:30:45 +00:00
|
|
|
case SNEAKING -> {
|
|
|
|
height = SNEAKING_POSE_HEIGHT;
|
|
|
|
width = definition.width();
|
|
|
|
}
|
|
|
|
case FALL_FLYING, SPIN_ATTACK, SWIMMING -> {
|
|
|
|
height = 0.6f;
|
|
|
|
width = definition.width();
|
|
|
|
}
|
|
|
|
case DYING -> {
|
|
|
|
height = 0.2f;
|
|
|
|
width = 0.2f;
|
|
|
|
}
|
2021-09-10 18:10:56 +00:00
|
|
|
default -> {
|
2021-06-05 02:26:29 +00:00
|
|
|
super.setDimensions(pose);
|
|
|
|
return;
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
2021-06-05 02:26:29 +00:00
|
|
|
}
|
2022-01-20 00:30:45 +00:00
|
|
|
setBoundingBoxWidth(width);
|
2021-11-19 01:44:03 +00:00
|
|
|
setBoundingBoxHeight(height);
|
2021-06-05 02:26:29 +00:00
|
|
|
}
|
2021-10-01 20:24:25 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
public void setBelowNameText(Objective objective) {
|
2021-10-01 20:24:25 +00:00
|
|
|
if (objective != null && objective.getUpdateType() != UpdateType.REMOVE) {
|
|
|
|
int amount;
|
|
|
|
Score score = objective.getScores().get(username);
|
|
|
|
if (score != null) {
|
|
|
|
amount = score.getCurrentData().getScore();
|
|
|
|
} else {
|
|
|
|
amount = 0;
|
|
|
|
}
|
|
|
|
String displayString = amount + " " + objective.getDisplayName();
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
// Already spawned - we still need to run the rest of this code because the spawn packet will be
|
|
|
|
// providing the information
|
|
|
|
SetEntityDataPacket packet = new SetEntityDataPacket();
|
|
|
|
packet.setRuntimeEntityId(geyserId);
|
|
|
|
packet.getMetadata().put(EntityData.SCORE_TAG, displayString);
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
2021-11-19 01:44:03 +00:00
|
|
|
} else if (valid) {
|
|
|
|
SetEntityDataPacket packet = new SetEntityDataPacket();
|
|
|
|
packet.setRuntimeEntityId(geyserId);
|
|
|
|
packet.getMetadata().put(EntityData.SCORE_TAG, "");
|
|
|
|
session.sendUpstreamPacket(packet);
|
2021-10-01 20:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|