From 4bbb057f62089fe23496a6936fee09fc073cb32b Mon Sep 17 00:00:00 2001 From: Luke <32024335+lukeeey@users.noreply.github.com> Date: Sat, 26 Sep 2020 02:37:13 +0100 Subject: [PATCH] Get the basics working --- .../org/geysermc/connector/entity/Entity.java | 7 +-- .../network/session/GeyserSession.java | 58 +++++++++++++++++++ ...BedrockInventoryTransactionTranslator.java | 11 ++++ .../player/BedrockInteractTranslator.java | 5 ++ .../java/JavaSwitchCameraTranslator.java | 54 +++++++++++++++++ .../JavaEntityPositionRotationTranslator.java | 4 ++ .../entity/JavaEntityPositionTranslator.java | 4 ++ .../spawn/JavaSpawnEntityTranslator.java | 1 + .../JavaSpawnLivingEntityTranslator.java | 1 + .../spawn/JavaSpawnPaintingTranslator.java | 1 + .../spawn/JavaSpawnPlayerTranslator.java | 1 + 11 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/java/JavaSwitchCameraTranslator.java diff --git a/connector/src/main/java/org/geysermc/connector/entity/Entity.java b/connector/src/main/java/org/geysermc/connector/entity/Entity.java index 5e825e89..c42c56ea 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -56,10 +56,7 @@ import org.geysermc.connector.utils.AttributeUtils; import org.geysermc.connector.utils.ChunkUtils; import org.geysermc.connector.utils.MessageUtils; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Getter @Setter @@ -67,6 +64,8 @@ public class Entity { protected long entityId; protected long geyserId; + protected UUID javaUuid; + protected String dimension; protected Vector3f position; diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 0a28b11f..bb2037e4 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -47,10 +47,13 @@ import com.nukkitx.protocol.bedrock.BedrockPacket; import com.nukkitx.protocol.bedrock.BedrockServerSession; import com.nukkitx.protocol.bedrock.data.*; import com.nukkitx.protocol.bedrock.data.command.CommandPermission; +import com.nukkitx.protocol.bedrock.data.entity.EntityData; +import com.nukkitx.protocol.bedrock.data.entity.EntityLinkData; import com.nukkitx.protocol.bedrock.packet.*; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMaps; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.objects.Object2LongMap; import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; import lombok.Getter; @@ -62,6 +65,7 @@ import org.geysermc.connector.command.CommandSender; import org.geysermc.connector.common.AuthType; import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.PlayerEntity; +import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.inventory.PlayerInventory; import org.geysermc.connector.network.remote.RemoteServer; import org.geysermc.connector.network.session.auth.AuthData; @@ -261,6 +265,11 @@ public class GeyserSession implements CommandSender { @Setter private String lastSignMessage; + private Entity spectatingEntity = null; + private Vector3f originalPosition = null; + private Vector3f originalRotation = null; + private boolean originalOnGround = false; + private MinecraftProtocol protocol; public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) { @@ -751,4 +760,53 @@ public class GeyserSession implements CommandSender { adventureSettingsPacket.getSettings().addAll(flags); sendUpstreamPacket(adventureSettingsPacket); } + + public void stopSpectatingEntity() { + playerEntity.updatePositionAndRotation(this, originalPosition.getX(), originalPosition.getY(), originalPosition.getZ(), + originalRotation.getZ(), originalRotation.getY(), originalOnGround); + + SetEntityLinkPacket linkPacket = new SetEntityLinkPacket(); + linkPacket.setEntityLink(new EntityLinkData(spectatingEntity.getGeyserId(), playerEntity.getGeyserId(), EntityLinkData.Type.REMOVE, false)); + this.sendUpstreamPacket(linkPacket); + + spectatingEntity.getPassengers().remove(playerEntity.getEntityId()); + + spectatingEntity = null; + + this.sendMessage("Stopping spectating entity"); + } + + public void setSpectatingEntity(Entity entity) { + this.sendMessage("Starting spectating entity"); + + originalPosition = playerEntity.getPosition(); + originalRotation = playerEntity.getRotation(); + originalOnGround = playerEntity.isOnGround(); + + SetEntityLinkPacket linkPacket = new SetEntityLinkPacket(); + linkPacket.setEntityLink(new EntityLinkData(entity.getGeyserId(), playerEntity.getGeyserId(), + EntityLinkData.Type.RIDER, false)); + + this.sendUpstreamPacket(linkPacket); + + entity.getPassengers().add(playerEntity.getEntityId()); + + spectatingEntity = entity; + } + + public void sendSpectateLocation() { + Vector3f pos = spectatingEntity.getPosition(); + Vector3f rot = spectatingEntity.getRotation(); + + playerEntity.updatePositionAndRotation(this, pos.getX(), pos.getY(), pos.getZ(), rot.getZ(), rot.getY(), spectatingEntity.isOnGround()); + + SetTitlePacket setTitlePacket = new SetTitlePacket(); + setTitlePacket.setFadeOutTime(0); + setTitlePacket.setFadeInTime(0); + setTitlePacket.setStayTime(30); + setTitlePacket.setText("Sending position: " + pos.getX() + " " + pos.getY() + " " + pos.getZ()); + setTitlePacket.setType(SetTitlePacket.Type.ACTIONBAR); + + this.sendUpstreamPacket(setTitlePacket); + } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java index 8e891d92..6535226f 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockInventoryTransactionTranslator.java @@ -36,6 +36,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlaye import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerInteractEntityPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerPlaceBlockPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerUseItemPacket; +import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientSpectatePacket; import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3i; import com.nukkitx.protocol.bedrock.data.LevelEventType; @@ -45,6 +46,7 @@ import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket; import com.nukkitx.protocol.bedrock.packet.InventorySlotPacket; import com.nukkitx.protocol.bedrock.packet.InventoryTransactionPacket; import com.nukkitx.protocol.bedrock.packet.LevelEventPacket; +import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.entity.CommandBlockMinecartEntity; import org.geysermc.connector.entity.Entity; import org.geysermc.connector.entity.ItemFrameEntity; @@ -242,6 +244,15 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator ClientPlayerStatePacket sneakPacket = new ClientPlayerStatePacket((int) entity.getEntityId(), PlayerState.START_SNEAKING); session.sendDownstreamPacket(sneakPacket); session.setRidingVehicleEntity(null); + + if (session.getGameMode() == GameMode.SPECTATOR && session.getSpectatingEntity() != null) { + //session.stopSpectatingEntity(); + } break; case MOUSEOVER: // Handle the buttons for mobile - "Mount", etc; and the suggestions for console - "ZL: Mount", etc diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaSwitchCameraTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaSwitchCameraTranslator.java new file mode 100644 index 00000000..19b58b26 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaSwitchCameraTranslator.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2019-2020 GeyserMC. http://geysermc.org + * + * 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 + */ + +package org.geysermc.connector.network.translators.java; + +import com.github.steveice10.mc.protocol.packet.ingame.server.ServerSwitchCameraPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; +import org.geysermc.connector.network.translators.Translator; + +@Translator(packet = ServerSwitchCameraPacket.class) +public class JavaSwitchCameraTranslator extends PacketTranslator { + + @Override + public void translate(ServerSwitchCameraPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getCameraEntityId()); + if (session.getPlayerEntity().getEntityId() == packet.getCameraEntityId()) { + entity = session.getPlayerEntity(); + } + + if (entity == null) + return; + + if (session.getSpectatingEntity() != null && session.getPlayerEntity().getGeyserId() == entity.getGeyserId()) { + session.stopSpectatingEntity(); + return; + } + + session.setSpectatingEntity(entity); + } +} \ No newline at end of file diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityPositionRotationTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityPositionRotationTranslator.java index c4bb799e..aaf7c3a1 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityPositionRotationTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityPositionRotationTranslator.java @@ -43,5 +43,9 @@ public class JavaEntityPositionRotationTranslator extends PacketTranslator