2019-08-03 03:38:09 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 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
|
|
|
|
*/
|
|
|
|
|
2020-11-20 19:56:39 +00:00
|
|
|
package org.geysermc.connector.entity.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-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-10-05 21:06:15 +00:00
|
|
|
import org.geysermc.connector.common.ChatColor;
|
2020-11-20 19:56:39 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
|
|
|
import org.geysermc.connector.entity.LivingEntity;
|
2020-09-02 04:39:06 +00:00
|
|
|
import org.geysermc.connector.entity.living.animal.tameable.ParrotEntity;
|
2019-08-03 03:38:09 +00:00
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2020-11-16 23:57:57 +00:00
|
|
|
import org.geysermc.connector.network.translators.chat.MessageTranslator;
|
2021-10-01 20:24:25 +00:00
|
|
|
import org.geysermc.connector.scoreboard.Objective;
|
|
|
|
import org.geysermc.connector.scoreboard.Score;
|
2021-07-08 02:44:53 +00:00
|
|
|
import org.geysermc.connector.scoreboard.Team;
|
2021-10-01 20:24:25 +00:00
|
|
|
import org.geysermc.connector.scoreboard.UpdateType;
|
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;
|
|
|
|
import java.util.UUID;
|
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 {
|
2019-09-25 21:52:28 +00:00
|
|
|
private GameProfile profile;
|
2019-08-03 06:51:05 +00:00
|
|
|
private UUID uuid;
|
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
|
|
|
|
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
|
|
|
|
2019-09-21 07:42:44 +00:00
|
|
|
public PlayerEntity(GameProfile gameProfile, long entityId, long geyserId, Vector3f position, Vector3f motion, Vector3f rotation) {
|
|
|
|
super(entityId, geyserId, EntityType.PLAYER, position, motion, rotation);
|
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
|
|
|
uuid = gameProfile.getId();
|
|
|
|
username = gameProfile.getName();
|
2021-05-10 04:48:01 +00:00
|
|
|
|
|
|
|
// For the OptionalPack, set all bits as invisible by default as this matches Java Edition behavior
|
|
|
|
metadata.put(EntityData.MARK_VARIANT, 0xff);
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void spawnEntity(GeyserSession session) {
|
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) {
|
|
|
|
setBelowNameText(session, objective);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
updateDisplayName(session, null, false);
|
|
|
|
|
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-07-06 22:09:01 +00:00
|
|
|
addPlayerPacket.setPosition(position.sub(0, EntityType.PLAYER.getOffset(), 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("");
|
2020-02-14 02:04:22 +00:00
|
|
|
addPlayerPacket.getMetadata().putAll(metadata);
|
2019-08-03 06:51:05 +00:00
|
|
|
|
2020-05-23 21:39:17 +00:00
|
|
|
long linkedEntityId = session.getEntityCache().getCachedPlayerEntityLink(entityId);
|
|
|
|
if (linkedEntityId != -1) {
|
2021-01-28 16:33:26 +00:00
|
|
|
Entity linkedEntity = session.getEntityCache().getEntityByJavaId(linkedEntityId);
|
|
|
|
if (linkedEntity != null) {
|
|
|
|
addPlayerPacket.getEntityLinks().add(new EntityLinkData(linkedEntity.getGeyserId(), geyserId, EntityLinkData.Type.RIDER, false, false));
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
public void sendPlayer(GeyserSession session) {
|
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 {
|
|
|
|
spawnEntity(session);
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 23:39:34 +00:00
|
|
|
|
|
|
|
@Override
|
2020-04-29 16:06:25 +00:00
|
|
|
public void moveAbsolute(GeyserSession session, Vector3f position, Vector3f rotation, boolean isOnGround, boolean teleported) {
|
2020-02-15 23:39:34 +00:00
|
|
|
setPosition(position);
|
|
|
|
setRotation(rotation);
|
|
|
|
|
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) {
|
|
|
|
leftParrot.moveAbsolute(session, position, rotation, true, teleported);
|
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
|
|
|
rightParrot.moveAbsolute(session, position, rotation, true, teleported);
|
|
|
|
}
|
2020-02-15 23:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
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.
|
|
|
|
if (metadata.getFlags().getFlag(EntityFlag.SLEEPING)) {
|
|
|
|
Vector3i bedPosition = metadata.getPos(EntityData.BED_POSITION);
|
|
|
|
if (bedPosition != null && (bedPosition.getY() == 0 || bedPosition.distanceSquared(position.toInt()) > 4)) {
|
|
|
|
// Force the player movement by using a teleport
|
|
|
|
movePlayerPacket.setPosition(Vector3f.from(position.getX(), position.getY() - entityType.getOffset() + 0.2f, position.getZ()));
|
|
|
|
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) {
|
|
|
|
leftParrot.moveRelative(session, relX, relY, relZ, rotation, true);
|
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
|
|
|
rightParrot.moveRelative(session, relX, relY, relZ, rotation, true);
|
|
|
|
}
|
2020-02-15 23:39:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:58:06 +00:00
|
|
|
@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());
|
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
|
|
|
|
public void updatePositionAndRotation(GeyserSession session, double moveX, double moveY, double moveZ, float yaw, float pitch, boolean isOnGround) {
|
|
|
|
moveRelative(session, moveX, moveY, moveZ, yaw, pitch, isOnGround);
|
2020-09-02 04:39:06 +00:00
|
|
|
if (leftParrot != null) {
|
|
|
|
leftParrot.moveRelative(session, moveX, moveY, moveZ, yaw, pitch, isOnGround);
|
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
|
|
|
rightParrot.moveRelative(session, moveX, moveY, moveZ, yaw, pitch, isOnGround);
|
|
|
|
}
|
2020-06-16 23:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateRotation(GeyserSession session, float yaw, float pitch, boolean isOnGround) {
|
|
|
|
super.updateRotation(session, yaw, pitch, isOnGround);
|
|
|
|
// 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) {
|
|
|
|
leftParrot.updateRotation(session, yaw, pitch, isOnGround);
|
|
|
|
}
|
|
|
|
if (rightParrot != null) {
|
|
|
|
rightParrot.updateRotation(session, yaw, pitch, isOnGround);
|
|
|
|
}
|
2020-06-16 23:58:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:39:34 +00:00
|
|
|
@Override
|
|
|
|
public void setPosition(Vector3f position) {
|
2021-07-18 14:58:08 +00:00
|
|
|
super.setPosition(position.add(0, entityType.getOffset(), 0));
|
2020-02-15 23:39:34 +00:00
|
|
|
}
|
2020-04-04 06:27:34 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
|
|
|
super.updateBedrockMetadata(entityMetadata, session);
|
|
|
|
|
2020-05-14 16:30:33 +00:00
|
|
|
// Extra hearts - is not metadata but an attribute on Bedrock
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 15) {
|
2020-05-14 16:30:33 +00:00
|
|
|
UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
|
|
|
|
attributesPacket.setRuntimeEntityId(geyserId);
|
|
|
|
// Setting to a higher maximum since plugins/datapacks can probably extend the Bedrock soft limit
|
2021-07-06 22:09:01 +00:00
|
|
|
attributesPacket.setAttributes(Collections.singletonList(
|
|
|
|
new AttributeData("minecraft:absorption", 0.0f, 1024f, (float) entityMetadata.getValue(), 0.0f)));
|
2020-05-14 16:30:33 +00:00
|
|
|
session.sendUpstreamPacket(attributesPacket);
|
|
|
|
}
|
|
|
|
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 17) {
|
2021-05-10 04:48:01 +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).
|
|
|
|
metadata.put(EntityData.MARK_VARIANT, ~((byte) entityMetadata.getValue()) & 0xff);
|
|
|
|
}
|
|
|
|
|
2020-05-10 19:38:39 +00:00
|
|
|
// Parrot occupying shoulder
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 19 || entityMetadata.getId() == 20) {
|
2020-05-10 19:38:39 +00:00
|
|
|
CompoundTag tag = (CompoundTag) entityMetadata.getValue();
|
2021-07-21 17:44:11 +00:00
|
|
|
boolean isLeft = entityMetadata.getId() == 19;
|
2020-05-10 19:38:39 +00:00
|
|
|
if (tag != null && !tag.isEmpty()) {
|
2021-07-21 17:44:11 +00:00
|
|
|
if ((isLeft && leftParrot != null) || (!isLeft && rightParrot != null)) {
|
2020-09-02 04:39:06 +00:00
|
|
|
// No need to update a parrot's data when it already exists
|
|
|
|
return;
|
|
|
|
}
|
2020-05-10 19:38:39 +00:00
|
|
|
// The parrot is a separate entity in Bedrock, but part of the player entity in Java
|
2020-09-02 04:39:06 +00:00
|
|
|
ParrotEntity parrot = new ParrotEntity(0, session.getEntityCache().getNextEntityId().incrementAndGet(),
|
2020-05-10 19:38:39 +00:00
|
|
|
EntityType.PARROT, position, motion, rotation);
|
|
|
|
parrot.spawnEntity(session);
|
|
|
|
parrot.getMetadata().put(EntityData.VARIANT, tag.get("Variant").getValue());
|
|
|
|
// Different position whether the parrot is left or right
|
2021-07-21 17:44:11 +00:00
|
|
|
float offset = isLeft ? 0.4f : -0.4f;
|
2020-05-10 19:38:39 +00:00
|
|
|
parrot.getMetadata().put(EntityData.RIDER_SEAT_POSITION, Vector3f.from(offset, -0.22, -0.1));
|
|
|
|
parrot.getMetadata().put(EntityData.RIDER_ROTATION_LOCKED, 1);
|
|
|
|
parrot.updateBedrockMetadata(session);
|
|
|
|
SetEntityLinkPacket linkPacket = new SetEntityLinkPacket();
|
2021-07-21 17:44:11 +00:00
|
|
|
EntityLinkData.Type type = isLeft ? EntityLinkData.Type.RIDER : EntityLinkData.Type.PASSENGER;
|
|
|
|
linkPacket.setEntityLink(new EntityLinkData(geyserId, parrot.getGeyserId(), type, false, false));
|
2020-05-10 19:38:39 +00:00
|
|
|
// Delay, or else spawned-in players won't get the link
|
|
|
|
// TODO: Find a better solution. This problem also exists with item frames
|
2021-11-13 16:03:55 +00:00
|
|
|
session.scheduleInEventLoop(() -> session.sendUpstreamPacket(linkPacket), 500, TimeUnit.MILLISECONDS);
|
2021-07-21 17:44:11 +00:00
|
|
|
if (isLeft) {
|
2020-05-10 19:38:39 +00:00
|
|
|
leftParrot = parrot;
|
|
|
|
} else {
|
|
|
|
rightParrot = parrot;
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-21 17:44:11 +00:00
|
|
|
Entity parrot = isLeft ? leftParrot : rightParrot;
|
2020-05-10 19:38:39 +00:00
|
|
|
if (parrot != null) {
|
|
|
|
parrot.despawnEntity(session);
|
2021-07-21 17:44:11 +00:00
|
|
|
if (isLeft) {
|
2020-05-10 19:38:39 +00:00
|
|
|
leftParrot = null;
|
|
|
|
} else {
|
|
|
|
rightParrot = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-10 18:54:06 +00:00
|
|
|
protected void setDisplayName(GeyserSession session, Component name) {
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public void updateDisplayName(GeyserSession session, @Nullable Team team, boolean useGivenTeam) {
|
|
|
|
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();
|
|
|
|
String chatColor;
|
|
|
|
if (color == TeamColor.NONE) {
|
|
|
|
chatColor = ChatColor.RESET;
|
|
|
|
} else {
|
|
|
|
chatColor = MessageTranslator.toChatColor(color);
|
|
|
|
}
|
|
|
|
// 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 = "";
|
|
|
|
}
|
|
|
|
needsUpdate = useGivenTeam && !newDisplayName.equals(metadata.getString(EntityData.NAMETAG, null));
|
|
|
|
metadata.put(EntityData.NAMETAG, newDisplayName);
|
|
|
|
} else if (useGivenTeam) {
|
|
|
|
// The name has reset, if it was previously something else
|
|
|
|
needsUpdate = !newDisplayName.equals(metadata.getString(EntityData.NAMETAG));
|
2021-10-10 18:54:06 +00:00
|
|
|
metadata.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
|
|
|
|
protected void setDisplayNameVisible(EntityMetadata entityMetadata) {
|
|
|
|
// Doesn't do anything for players
|
|
|
|
}
|
|
|
|
|
2021-06-05 02:26:29 +00:00
|
|
|
@Override
|
|
|
|
protected void setDimensions(Pose pose) {
|
|
|
|
float height;
|
|
|
|
switch (pose) {
|
2021-09-10 18:10:56 +00:00
|
|
|
case SNEAKING -> height = 1.5f;
|
|
|
|
case FALL_FLYING, SPIN_ATTACK, SWIMMING -> height = 0.6f;
|
|
|
|
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
|
|
|
}
|
|
|
|
metadata.put(EntityData.BOUNDING_BOX_WIDTH, entityType.getWidth());
|
|
|
|
metadata.put(EntityData.BOUNDING_BOX_HEIGHT, height);
|
|
|
|
}
|
2021-10-01 20:24:25 +00:00
|
|
|
|
|
|
|
public void setBelowNameText(GeyserSession session, Objective objective) {
|
|
|
|
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();
|
|
|
|
|
|
|
|
metadata.put(EntityData.SCORE_TAG, displayString);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-01 20:38:04 +00:00
|
|
|
// Always remove the score tag first, then check for valid.
|
|
|
|
// That way the score tag is removed if the player was spawned, then despawned, and is being respawned
|
|
|
|
if (metadata.remove(EntityData.SCORE_TAG) != null && valid) {
|
2021-10-01 20:24:25 +00:00
|
|
|
SetEntityDataPacket packet = new SetEntityDataPacket();
|
|
|
|
packet.setRuntimeEntityId(geyserId);
|
|
|
|
packet.getMetadata().put(EntityData.SCORE_TAG, "");
|
|
|
|
session.sendUpstreamPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 03:38:09 +00:00
|
|
|
}
|