From 6192237cc986a9fcbae3350efe92936836b14ef5 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+DoctorMacc@users.noreply.github.com> Date: Sun, 10 May 2020 15:38:39 -0400 Subject: [PATCH] Add parrots on player shoulders (#530) * Add parrots on player shoulders Parrots on player shoulders are a separate entity in Bedrock, but part of the player metadata in Java. This commit creates a parrot entity from the NBT data given by the player's entity data. * Remove unused import * Nullify parrot after despawning * Remove debug code --- .../connector/entity/PlayerEntity.java | 62 ++++++++++++++++++- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java b/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java index 82e91f553..a5d2d9a89 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java @@ -28,13 +28,13 @@ package org.geysermc.connector.entity; import com.github.steveice10.mc.auth.data.GameProfile; import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata; import com.github.steveice10.mc.protocol.data.message.TextMessage; +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.data.CommandPermission; import com.nukkitx.protocol.bedrock.data.EntityData; +import com.nukkitx.protocol.bedrock.data.EntityLink; import com.nukkitx.protocol.bedrock.data.PlayerPermission; -import com.nukkitx.protocol.bedrock.packet.AddPlayerPacket; -import com.nukkitx.protocol.bedrock.packet.MovePlayerPacket; -import com.nukkitx.protocol.bedrock.packet.PlayerListPacket; +import com.nukkitx.protocol.bedrock.packet.*; import lombok.Getter; import lombok.Setter; @@ -48,6 +48,7 @@ import org.geysermc.connector.network.session.cache.EntityEffectCache; import org.geysermc.connector.utils.SkinUtils; import java.util.UUID; +import java.util.concurrent.TimeUnit; @Getter @Setter public class PlayerEntity extends LivingEntity { @@ -58,6 +59,9 @@ public class PlayerEntity extends LivingEntity { private boolean playerList = true; private final EntityEffectCache effectCache; + private Entity leftParrot; + private Entity rightParrot; + public PlayerEntity(GameProfile gameProfile, long entityId, long geyserId, Vector3f position, Vector3f motion, Vector3f rotation) { super(entityId, geyserId, EntityType.PLAYER, position, motion, rotation); @@ -146,6 +150,12 @@ public class PlayerEntity extends LivingEntity { } session.sendUpstreamPacket(movePlayerPacket); + if (leftParrot != null) { + leftParrot.moveAbsolute(session, position, rotation, true, teleported); + } + if (rightParrot != null) { + rightParrot.moveAbsolute(session, position, rotation, true, teleported); + } } @Override @@ -160,6 +170,12 @@ public class PlayerEntity extends LivingEntity { 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); + } } @Override @@ -188,5 +204,45 @@ public class PlayerEntity extends LivingEntity { metadata.put(EntityData.NAMETAG, team.getPrefix() + MessageUtils.toChatColor(team.getColor()) + username + team.getSuffix()); } } + + // Parrot occupying shoulder + if (entityMetadata.getId() == 18 || entityMetadata.getId() == 19) { + CompoundTag tag = (CompoundTag) entityMetadata.getValue(); + if (tag != null && !tag.isEmpty()) { + // The parrot is a separate entity in Bedrock, but part of the player entity in Java + Entity parrot = new Entity(0, session.getEntityCache().getNextEntityId().incrementAndGet(), + 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 + float offset = (entityMetadata.getId() == 18) ? 0.4f : -0.4f; + 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(); + EntityLink.Type type = (entityMetadata.getId() == 18) ? EntityLink.Type.RIDER : EntityLink.Type.PASSENGER; + linkPacket.setEntityLink(new EntityLink(geyserId, parrot.getGeyserId(), type, false)); + // Delay, or else spawned-in players won't get the link + // TODO: Find a better solution. This problem also exists with item frames + session.getConnector().getGeneralThreadPool().schedule(() -> session.sendUpstreamPacket(linkPacket), 500, TimeUnit.MILLISECONDS); + if (entityMetadata.getId() == 18) { + leftParrot = parrot; + } else { + rightParrot = parrot; + } + } else { + Entity parrot = (entityMetadata.getId() == 18 ? leftParrot : rightParrot); + if (parrot != null) { + SetEntityLinkPacket linkPacket = new SetEntityLinkPacket(); + linkPacket.setEntityLink(new EntityLink(parrot.getGeyserId(), geyserId, EntityLink.Type.REMOVE, false)); + parrot.despawnEntity(session); + if (entityMetadata.getId() == 18) { + leftParrot = null; + } else { + rightParrot = null; + } + } + } + } } }