From ab540b19512956dc42e34df13350703e2cca8004 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Sat, 21 Aug 2021 09:54:52 -0400 Subject: [PATCH] Ensure more entity tasks are run on the player session --- .../connector/entity/PaintingEntity.java | 39 ++++++++----------- .../entity/player/BedrockEmoteTranslator.java | 20 +++++++--- .../spawn/JavaSpawnPaintingTranslator.java | 20 +++------- 3 files changed, 36 insertions(+), 43 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/entity/PaintingEntity.java b/connector/src/main/java/org/geysermc/connector/entity/PaintingEntity.java index dd119f1a9..3e2ff7ead 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/PaintingEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/PaintingEntity.java @@ -27,24 +27,19 @@ package org.geysermc.connector.entity; import com.nukkitx.math.vector.Vector3f; import com.nukkitx.protocol.bedrock.packet.AddPaintingPacket; - -import lombok.Getter; -import lombok.Setter; -import lombok.experimental.Accessors; - import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.utils.PaintingType; -@Getter @Setter -@Accessors(chain = true) public class PaintingEntity extends Entity { private static final double OFFSET = -0.46875; - private PaintingType paintingName; - private int direction; + private final PaintingType paintingName; + private final int direction; - public PaintingEntity(long entityId, long geyserId, Vector3f position) { + public PaintingEntity(long entityId, long geyserId, Vector3f position, PaintingType paintingName, int direction) { super(entityId, geyserId, EntityType.PAINTING, position, Vector3f.ZERO, Vector3f.ZERO); + this.paintingName = paintingName; + this.direction = direction; } @Override @@ -53,7 +48,7 @@ public class PaintingEntity extends Entity { addPaintingPacket.setUniqueEntityId(geyserId); addPaintingPacket.setRuntimeEntityId(geyserId); addPaintingPacket.setMotive(paintingName.getBedrockName()); - addPaintingPacket.setPosition(fixOffset(true)); + addPaintingPacket.setPosition(fixOffset()); addPaintingPacket.setDirection(direction); session.sendUpstreamPacket(addPaintingPacket); @@ -67,19 +62,17 @@ public class PaintingEntity extends Entity { // Do nothing, as head look messes up paintings } - public Vector3f fixOffset(boolean toBedrock) { - if (toBedrock) { - Vector3f position = super.position; - position = position.add(0.5, 0.5, 0.5); - double widthOffset = paintingName.getWidth() > 1 ? 0.5 : 0; - double heightOffset = paintingName.getHeight() > 1 && paintingName.getHeight() != 3 ? 0.5 : 0; + private Vector3f fixOffset() { + Vector3f position = super.position; + position = position.add(0.5, 0.5, 0.5); + double widthOffset = paintingName.getWidth() > 1 ? 0.5 : 0; + double heightOffset = paintingName.getHeight() > 1 && paintingName.getHeight() != 3 ? 0.5 : 0; - switch (direction) { - case 0: return position.add(widthOffset, heightOffset, OFFSET); - case 1: return position.add(-OFFSET, heightOffset, widthOffset); - case 2: return position.add(-widthOffset, heightOffset, -OFFSET); - case 3: return position.add(OFFSET, heightOffset, -widthOffset); - } + switch (direction) { + case 0: return position.add(widthOffset, heightOffset, OFFSET); + case 1: return position.add(-OFFSET, heightOffset, widthOffset); + case 2: return position.add(-widthOffset, heightOffset, -OFFSET); + case 3: return position.add(OFFSET, heightOffset, -widthOffset); } return position; } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockEmoteTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockEmoteTranslator.java index fb7c7a147..890dc0c8c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockEmoteTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/entity/player/BedrockEmoteTranslator.java @@ -56,13 +56,21 @@ public class BedrockEmoteTranslator extends PacketTranslator { for (GeyserSession otherSession : session.getConnector().getPlayers()) { if (otherSession != session) { if (otherSession.isClosed()) continue; - Entity otherEntity = otherSession.getEntityCache().getEntityByJavaId(javaId); - if (otherEntity == null) continue; - EmotePacket otherEmotePacket = new EmotePacket(); - otherEmotePacket.setEmoteId(packet.getEmoteId()); - otherEmotePacket.setRuntimeEntityId(otherEntity.getGeyserId()); - otherSession.sendUpstreamPacket(otherEmotePacket); + if (otherSession.getEventLoop().inEventLoop()) { + playEmote(otherSession, javaId, packet.getEmoteId()); + } else { + session.executeInEventLoop(() -> playEmote(otherSession, javaId, packet.getEmoteId())); + } } } } + + private void playEmote(GeyserSession otherSession, long javaId, String emoteId) { + Entity otherEntity = otherSession.getEntityCache().getEntityByJavaId(javaId); // Must be ran on same thread + if (otherEntity == null) return; + EmotePacket otherEmotePacket = new EmotePacket(); + otherEmotePacket.setEmoteId(emoteId); + otherEmotePacket.setRuntimeEntityId(otherEntity.getGeyserId()); + otherSession.sendUpstreamPacket(otherEmotePacket); + } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPaintingTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPaintingTranslator.java index 1abe9937d..fea6fd3d4 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPaintingTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPaintingTranslator.java @@ -25,16 +25,14 @@ package org.geysermc.connector.network.translators.java.entity.spawn; -import org.geysermc.connector.GeyserConnector; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPaintingPacket; +import com.nukkitx.math.vector.Vector3f; import org.geysermc.connector.entity.PaintingEntity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.utils.PaintingType; -import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPaintingPacket; -import com.nukkitx.math.vector.Vector3f; - @Translator(packet = ServerSpawnPaintingPacket.class) public class JavaSpawnPaintingTranslator extends PacketTranslator { @@ -42,16 +40,10 @@ public class JavaSpawnPaintingTranslator extends PacketTranslator { // #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()); + PaintingEntity entity = new PaintingEntity(packet.getEntityId(), + session.getEntityCache().getNextEntityId().incrementAndGet(), + position, PaintingType.getByPaintingType(packet.getPaintingType()), packet.getDirection().ordinal()); - session.getEntityCache().spawnEntity(entity); - }); + session.getEntityCache().spawnEntity(entity); } }