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 66d50fd3a..65ebe98f2 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/Entity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/Entity.java @@ -33,11 +33,14 @@ import com.nukkitx.protocol.bedrock.packet.AddEntityPacket; import com.nukkitx.protocol.bedrock.packet.RemoveEntityPacket; import lombok.Getter; import lombok.Setter; +import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; @Getter @Setter @@ -50,18 +53,19 @@ public class Entity { protected Vector3f position; protected Vector3f motion; + + // 1 - pitch, 2 - yaw, 3 - roll (head yaw) protected Vector3f rotation; protected int scale = 1; - protected float yaw; - protected float pitch; - protected boolean shouldMove; + protected boolean movePending; protected EntityType entityType; protected boolean valid; - protected Map attributes = new HashMap(); + protected Set passengers = new HashSet(); + protected Map attributes = new HashMap(); public Entity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { this.entityId = entityId; @@ -72,20 +76,24 @@ public class Entity { this.rotation = rotation; this.valid = false; - this.shouldMove = false; + this.movePending = false; } public void spawnEntity(GeyserSession session) { AddEntityPacket addEntityPacket = new AddEntityPacket(); + addEntityPacket.setIdentifier("minecraft:" + entityType.name().toLowerCase()); addEntityPacket.setRuntimeEntityId(geyserId); - addEntityPacket.setUniqueEntityId(entityId); + addEntityPacket.setUniqueEntityId(geyserId); addEntityPacket.setPosition(position); addEntityPacket.setMotion(motion); addEntityPacket.setRotation(rotation); addEntityPacket.setEntityType(entityType.getType()); + addEntityPacket.getMetadata().putAll(getMetadata()); - session.getUpstream().sendPacket(addEntityPacket); valid = true; + session.getUpstream().sendPacket(addEntityPacket); + + GeyserLogger.DEFAULT.debug("Spawned entity " + entityType + " at location " + position + " with id " + geyserId + " (java id " + entityId + ")"); } public void despawnEntity(GeyserSession session) { @@ -93,35 +101,38 @@ public class Entity { return; RemoveEntityPacket removeEntityPacket = new RemoveEntityPacket(); - removeEntityPacket.setUniqueEntityId(entityId); + removeEntityPacket.setUniqueEntityId(geyserId); session.getUpstream().sendPacket(removeEntityPacket); } public void moveRelative(double relX, double relY, double relZ, float pitch, float yaw) { - if (relX == 0 && relY != 0 && relZ != 0 && yaw != 0 && pitch != 0) + moveRelative(relX, relY, relZ, new Vector3f(pitch, yaw, 0)); + } + + public void moveRelative(double relX, double relY, double relZ, Vector3f rotation) { + if (relX == 0 && relY != 0 && relZ != 0 && position.getX() == 0 && position.getY() == 0) return; - if (pitch != 0) - this.pitch = pitch; - - if (yaw != 0) - this.yaw = yaw; - + this.rotation = rotation; this.position = new Vector3f(position.getX() + relX, position.getX() + relY, position.getX() + relZ); - this.shouldMove = true; + this.movePending = true; } public void moveAbsolute(Vector3f position, float pitch, float yaw) { - if (position.getX() == 0 && position.getX() != 0 && position.getX() != 0 && yaw != 0 && pitch != 0) + moveAbsolute(position, new Vector3f(pitch, yaw, 0)); + } + + public void moveAbsolute(Vector3f position, Vector3f rotation) { + if (position.getX() == 0 && position.getX() != 0 && position.getX() != 0 && rotation.getX() == 0 && rotation.getY() == 0) return; this.position = position; - this.pitch = pitch; - this.yaw = yaw; - this.shouldMove = true; + this.rotation = rotation; + this.movePending = true; } - protected EntityDataDictionary getMetadata() { + + public EntityDataDictionary getMetadata() { EntityDataDictionary dictionary = new EntityDataDictionary(); dictionary.put(EntityData.NAMETAG, ""); dictionary.put(EntityData.ENTITY_AGE, 0); diff --git a/connector/src/main/java/org/geysermc/connector/entity/ExpOrbEntity.java b/connector/src/main/java/org/geysermc/connector/entity/ExpOrbEntity.java new file mode 100644 index 000000000..036829e8a --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/entity/ExpOrbEntity.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 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.entity; + +import com.flowpowered.math.vector.Vector3f; +import com.nukkitx.protocol.bedrock.packet.SpawnExperienceOrbPacket; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; + +public class ExpOrbEntity extends Entity { + + private int amount; + + public ExpOrbEntity(int amount, long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { + super(entityId, geyserId, entityType, position, motion, rotation); + + this.amount = amount; + } + + @Override + public void spawnEntity(GeyserSession session) { + SpawnExperienceOrbPacket spawnExpOrbPacket = new SpawnExperienceOrbPacket(); + spawnExpOrbPacket.setPosition(position); + spawnExpOrbPacket.setAmount(amount); + + valid = true; + session.getUpstream().sendPacket(spawnExpOrbPacket); + } +} 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 8797d20c3..eee33c177 100644 --- a/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java +++ b/connector/src/main/java/org/geysermc/connector/entity/PlayerEntity.java @@ -37,12 +37,14 @@ import lombok.Setter; import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; +import java.util.Random; +import java.util.UUID; + @Getter @Setter public class PlayerEntity extends Entity { - // This is the session linked to the player entity, can be null - private GeyserSession session; + private UUID uuid; private ItemData hand; @@ -51,10 +53,10 @@ public class PlayerEntity extends Entity { private ItemData leggings; private ItemData boots; - public PlayerEntity(GeyserSession session, long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { + public PlayerEntity(UUID uuid, long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) { super(entityId, geyserId, entityType, position, motion, rotation); - this.session = session; + this.uuid = uuid; } // TODO: Break this into an EquippableEntity class @@ -76,9 +78,9 @@ public class PlayerEntity extends Entity { public void spawnEntity(GeyserSession session) { AddPlayerPacket addPlayerPacket = new AddPlayerPacket(); addPlayerPacket.setUniqueEntityId(geyserId); - addPlayerPacket.setUniqueEntityId(entityId); - addPlayerPacket.setUuid(this.session.getAuthenticationData().getUUID()); - addPlayerPacket.setUsername(this.session.getAuthenticationData().getName()); + addPlayerPacket.setUniqueEntityId(geyserId); + addPlayerPacket.setUuid(uuid); + addPlayerPacket.setUsername("Player" + new Random().nextInt(1000) + 1); // TODO: Cache player list values and set it here addPlayerPacket.setPlatformChatId(""); addPlayerPacket.setPosition(position); addPlayerPacket.setMotion(motion); @@ -91,6 +93,9 @@ public class PlayerEntity extends Entity { addPlayerPacket.setPlayerPermission(0); addPlayerPacket.setCustomFlags(0); addPlayerPacket.setDeviceId("WIN10"); // TODO: Find this value + addPlayerPacket.getMetadata().putAll(getMetadata()); + + valid = true; session.getUpstream().sendPacket(addPlayerPacket); } } 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 fbc11cb97..2d0b03bf9 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 @@ -35,6 +35,7 @@ import com.github.steveice10.packetlib.event.session.ConnectedEvent; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.PacketReceivedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.tcp.TcpSessionFactory; import com.nukkitx.network.util.DisconnectReason; import com.nukkitx.protocol.PlayerSession; @@ -45,6 +46,7 @@ import com.nukkitx.protocol.bedrock.packet.PlayStatusPacket; import com.nukkitx.protocol.bedrock.packet.StartGamePacket; import com.nukkitx.protocol.bedrock.packet.TextPacket; import lombok.Getter; +import lombok.Setter; import org.geysermc.api.Player; import org.geysermc.api.RemoteServer; import org.geysermc.api.session.AuthData; @@ -52,6 +54,7 @@ import org.geysermc.api.window.FormWindow; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.entity.PlayerEntity; import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.cache.DataCache; import org.geysermc.connector.network.session.cache.EntityCache; import org.geysermc.connector.network.session.cache.InventoryCache; import org.geysermc.connector.network.session.cache.ScoreboardCache; @@ -59,53 +62,48 @@ import org.geysermc.connector.network.session.cache.WindowCache; import org.geysermc.connector.network.translators.Registry; import org.geysermc.connector.utils.Toolbox; +import java.util.UUID; + +@Getter public class GeyserSession implements PlayerSession, Player { private GeyserConnector connector; - - @Getter private RemoteServer remoteServer; - - @Getter private BedrockServerSession upstream; - @Getter private Client downstream; - @Getter private AuthData authenticationData; - @Getter private PlayerEntity playerEntity; - - @Getter private EntityCache entityCache; - - @Getter private InventoryCache inventoryCache; - - @Getter private WindowCache windowCache; - - @Getter private ScoreboardCache scoreboardCache; - @Getter + private DataCache javaPacketCache; + private boolean loggedIn; + @Setter + private boolean spawned; + private boolean closed; public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) { this.connector = connector; this.upstream = bedrockServerSession; - this.playerEntity = new PlayerEntity(this, 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); + this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); this.entityCache = new EntityCache(this); this.inventoryCache = new InventoryCache(this); this.windowCache = new WindowCache(this); this.scoreboardCache = new ScoreboardCache(this); + this.javaPacketCache = new DataCache(); + + this.spawned = false; this.loggedIn = false; } @@ -145,6 +143,7 @@ public class GeyserSession implements PlayerSession, Player { public void connected(ConnectedEvent event) { loggedIn = true; connector.getLogger().info(authenticationData.getName() + " (logged in as: " + protocol.getProfile().getName() + ")" + " has connected to remote java server on address " + remoteServer.getAddress()); + playerEntity.setUuid(protocol.getProfile().getId()); } @Override @@ -237,13 +236,13 @@ public class GeyserSession implements PlayerSession, Player { private void startGame() { StartGamePacket startGamePacket = new StartGamePacket(); - startGamePacket.setUniqueEntityId(1); // TODO: Cache this value - startGamePacket.setRuntimeEntityId(1); // TODO: Cache this value + startGamePacket.setUniqueEntityId(1); + startGamePacket.setRuntimeEntityId(1); startGamePacket.setPlayerGamemode(0); startGamePacket.setPlayerPosition(new Vector3f(0, 0, 0)); startGamePacket.setRotation(new Vector2f(1, 1)); - startGamePacket.setSeed(1111); + startGamePacket.setSeed(0); startGamePacket.setDimensionId(0); startGamePacket.setGeneratorId(0); startGamePacket.setLevelGamemode(0); @@ -274,7 +273,7 @@ public class GeyserSession implements PlayerSession, Player { startGamePacket.setFromWorldTemplate(false); startGamePacket.setWorldTemplateOptionLocked(false); - startGamePacket.setLevelId("oerjhii"); + startGamePacket.setLevelId("world"); startGamePacket.setWorldName("world"); startGamePacket.setPremiumWorldTemplateId("00000000-0000-0000-0000-000000000000"); startGamePacket.setCurrentTick(0); diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/DataCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/DataCache.java new file mode 100644 index 000000000..58d33a736 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/DataCache.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2019 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.session.cache; + +import lombok.Getter; + +import java.util.HashMap; +import java.util.Map; + +public class DataCache { + + @Getter + private Map cachedValues = new HashMap(); +} diff --git a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java index e15d8c526..e2bf53c33 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/cache/EntityCache.java @@ -25,22 +25,18 @@ package org.geysermc.connector.network.session.cache; -import com.flowpowered.math.vector.Vector3f; -import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnMobPacket; import lombok.Getter; -import org.geysermc.api.Geyser; -import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.entity.Entity; -import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; -import org.geysermc.connector.utils.EntityUtils; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; -// Each session has its own EntityCache in the occasion that an entity packet is sent specifically -// for that player (e.g. seeing vanished players from /vanish) +/** + * Each session has its own EntityCache in the occasion that an entity packet is sent specifically + * for that player (e.g. seeing vanished players from /vanish) + */ public class EntityCache { private GeyserSession session; @@ -48,25 +44,35 @@ public class EntityCache { @Getter private Map entities = new HashMap(); + private Map entityIdTranslations = new HashMap(); + + @Getter private AtomicLong nextEntityId = new AtomicLong(2L); public EntityCache(GeyserSession session) { this.session = session; } - public Entity spawnEntity(ServerSpawnMobPacket packet) { - EntityType type = EntityUtils.toBedrockEntity(packet.getType()); - if (type == null) { - GeyserLogger.DEFAULT.warning("Mob " + packet.getType() + " is not supported yet!"); - return null; - } + public void spawnEntity(Entity entity) { + entity.moveAbsolute(entity.getPosition(), entity.getRotation().getX(), entity.getRotation().getY()); + entityIdTranslations.put(entity.getEntityId(), entity.getGeyserId()); + entities.put(entity.getGeyserId(), entity); + entity.spawnEntity(session); + } - Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); - Vector3f motion = new Vector3f(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()); - Vector3f rotation = new Vector3f(packet.getPitch(), packet.getYaw(), packet.getHeadYaw()); + public void removeEntity(Entity entity) { + if (entity == null) + return; - Entity entity = new Entity(packet.getEntityId(), nextEntityId.incrementAndGet(), type, position, motion, rotation); - entity.moveAbsolute(position, packet.getPitch(), packet.getYaw()); - return entities.put(entity.getGeyserId(), entity); + entityIdTranslations.remove(entity.getGeyserId()); + entity.despawnEntity(session); + } + + public Entity getEntityByGeyserId(long geyserId) { + return entities.get(geyserId); + } + + public Entity getEntityByJavaId(long javaId) { + return entities.get(entityIdTranslations.get(javaId)); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java index 4357f9f65..3ac7ee3a3 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/TranslatorsInit.java @@ -30,11 +30,21 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerTitlePacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityDestroyPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityHeadLookPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityMetadataPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPositionPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPositionRotationPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPropertiesPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityRotationPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityTeleportPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityVelocityPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerPositionRotationPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnExpOrbPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnGlobalEntityPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnMobPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnObjectPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPaintingPacket; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPlayerPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerOpenWindowPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerSetSlotPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.window.ServerWindowItemsPacket; @@ -57,11 +67,21 @@ import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.network.translators.java.JavaChatTranslator; import org.geysermc.connector.network.translators.java.JavaJoinGameTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityDestroyTranslator; +import org.geysermc.connector.network.translators.java.entity.JavaEntityHeadLookTranslator; +import org.geysermc.connector.network.translators.java.entity.JavaEntityMetadataTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityPositionRotationTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityPositionTranslator; +import org.geysermc.connector.network.translators.java.entity.JavaEntityPropertiesTranslator; +import org.geysermc.connector.network.translators.java.entity.JavaEntityRotationTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityTeleportTranslator; import org.geysermc.connector.network.translators.java.entity.JavaEntityVelocityTranslator; +import org.geysermc.connector.network.translators.java.entity.player.JavaPlayerPositionRotationTranslator; import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnExpOrbTranslator; +import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnGlobalEntityTranslator; +import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnMobTranslator; +import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnObjectTranslator; +import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnPaintingTranslator; +import org.geysermc.connector.network.translators.java.entity.spawn.JavaSpawnPlayerTranslator; import org.geysermc.connector.network.translators.java.world.JavaNotifyClientTranslator; import org.geysermc.connector.network.translators.java.window.JavaOpenWindowTranslator; import org.geysermc.connector.network.translators.java.window.JavaSetSlotTranslator; @@ -104,13 +124,27 @@ public class TranslatorsInit { Registry.registerJava(ServerChatPacket.class, new JavaChatTranslator()); Registry.registerJava(ServerTitlePacket.class, new JavaTitleTranslator()); Registry.registerJava(ServerUpdateTimePacket.class, new JavaUpdateTimeTranslator()); + Registry.registerJava(ServerEntityPositionPacket.class, new JavaEntityPositionTranslator()); Registry.registerJava(ServerEntityPositionRotationPacket.class, new JavaEntityPositionRotationTranslator()); Registry.registerJava(ServerEntityTeleportPacket.class, new JavaEntityTeleportTranslator()); Registry.registerJava(ServerEntityVelocityPacket.class, new JavaEntityVelocityTranslator()); + Registry.registerJava(ServerEntityPropertiesPacket.class, new JavaEntityPropertiesTranslator()); + Registry.registerJava(ServerEntityRotationPacket.class, new JavaEntityRotationTranslator()); + Registry.registerJava(ServerEntityHeadLookPacket.class, new JavaEntityHeadLookTranslator()); + Registry.registerJava(ServerEntityMetadataPacket.class, new JavaEntityMetadataTranslator()); + + Registry.registerJava(ServerSpawnExpOrbPacket.class, new JavaSpawnExpOrbTranslator()); + Registry.registerJava(ServerSpawnGlobalEntityPacket.class, new JavaSpawnGlobalEntityTranslator()); + Registry.registerJava(ServerSpawnMobPacket.class, new JavaSpawnMobTranslator()); + Registry.registerJava(ServerSpawnObjectPacket.class, new JavaSpawnObjectTranslator()); + Registry.registerJava(ServerSpawnPaintingPacket.class, new JavaSpawnPaintingTranslator()); + Registry.registerJava(ServerSpawnPlayerPacket.class, new JavaSpawnPlayerTranslator()); + + Registry.registerJava(ServerPlayerPositionRotationPacket.class, new JavaPlayerPositionRotationTranslator()); + Registry.registerJava(ServerNotifyClientPacket.class, new JavaNotifyClientTranslator()); Registry.registerJava(ServerEntityDestroyPacket.class, new JavaEntityDestroyTranslator()); - Registry.registerJava(ServerSpawnExpOrbPacket.class, new JavaSpawnExpOrbTranslator()); Registry.registerJava(ServerWindowItemsPacket.class, new JavaWindowItemsTranslator()); Registry.registerJava(ServerOpenWindowPacket.class, new JavaOpenWindowTranslator()); Registry.registerJava(ServerSetSlotPacket.class, new JavaSetSlotTranslator()); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java index a43fd4a43..ec03f219c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaJoinGameTranslator.java @@ -38,7 +38,7 @@ public class JavaJoinGameTranslator extends PacketTranslator { + + @Override + public void translate(ServerEntityHeadLookPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId()); + if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) { + entity = session.getPlayerEntity(); + } + if (entity == null) + return; + + entity.setRotation(new Vector3f(entity.getRotation().getX(), entity.getRotation().getY(), packet.getHeadYaw())); + + MoveEntityAbsolutePacket moveEntityAbsolutePacket = new MoveEntityAbsolutePacket(); + moveEntityAbsolutePacket.setRuntimeEntityId(entity.getGeyserId()); + moveEntityAbsolutePacket.setRotation(entity.getRotation()); + moveEntityAbsolutePacket.setPosition(entity.getPosition()); + moveEntityAbsolutePacket.setOnGround(true); + + session.getUpstream().sendPacket(moveEntityAbsolutePacket); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityMetadataTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityMetadataTranslator.java new file mode 100644 index 000000000..c0451eb7e --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityMetadataTranslator.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2019 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.entity; + +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityMetadataPacket; +import com.nukkitx.protocol.bedrock.packet.SetEntityDataPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaEntityMetadataTranslator extends PacketTranslator { + + @Override + public void translate(ServerEntityMetadataPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId()); + if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) { + entity = session.getPlayerEntity(); + } + if (entity == null) + return; + + if (entity.isValid()) { + // TODO: Make this actually useful lol + SetEntityDataPacket entityDataPacket = new SetEntityDataPacket(); + entityDataPacket.setRuntimeEntityId(entity.getGeyserId()); + entityDataPacket.getMetadata().putAll(entity.getMetadata()); + + session.getUpstream().sendPacket(entityDataPacket); + } else { + entity.spawnEntity(session); + } + } +} 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 713a24323..e97c40dab 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 @@ -25,9 +25,9 @@ package org.geysermc.connector.network.translators.java.entity; -import com.flowpowered.math.vector.Vector3f; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityPositionRotationPacket; import com.nukkitx.protocol.bedrock.packet.MoveEntityAbsolutePacket; +import org.geysermc.connector.entity.Entity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; @@ -35,13 +35,25 @@ public class JavaEntityPositionRotationTranslator extends PacketTranslator { + + @Override + public void translate(ServerEntityPropertiesPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId()); + if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) { + entity = session.getPlayerEntity(); + } + if (entity == null) + return; + + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityRotationTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityRotationTranslator.java new file mode 100644 index 000000000..77ed8855e --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityRotationTranslator.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2019 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.entity; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityRotationPacket; +import com.nukkitx.protocol.bedrock.packet.MoveEntityAbsolutePacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaEntityRotationTranslator extends PacketTranslator { + + @Override + public void translate(ServerEntityRotationPacket packet, GeyserSession session) { + Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId()); + if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) { + entity = session.getPlayerEntity(); + } + if (entity == null) + return; + + entity.moveRelative(packet.getMovementX(), packet.getMovementY(), packet.getMovementZ(), packet.getPitch(), packet.getYaw()); + + Vector3f rotation = new Vector3f(entity.getRotation().getX() / (360d / 256d), entity.getRotation().getY() / (360d / 256d), + entity.getRotation().getZ() / (360d / 256d)); + if (entity.isMovePending()) { + MoveEntityAbsolutePacket moveEntityAbsolutePacket = new MoveEntityAbsolutePacket(); + moveEntityAbsolutePacket.setRuntimeEntityId(entity.getGeyserId()); + moveEntityAbsolutePacket.setPosition(entity.getPosition()); + moveEntityAbsolutePacket.setRotation(rotation); + entity.setMovePending(false); + + session.getUpstream().sendPacket(moveEntityAbsolutePacket); + } + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityTeleportTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityTeleportTranslator.java index c5ec98fc0..eb7262d67 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityTeleportTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/JavaEntityTeleportTranslator.java @@ -28,6 +28,7 @@ package org.geysermc.connector.network.translators.java.entity; import com.flowpowered.math.vector.Vector3f; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.ServerEntityTeleportPacket; import com.nukkitx.protocol.bedrock.packet.MoveEntityAbsolutePacket; +import org.geysermc.connector.entity.Entity; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; @@ -35,13 +36,25 @@ public class JavaEntityTeleportTranslator extends PacketTranslator { + + @Override + public void translate(ServerPlayerPositionRotationPacket packet, GeyserSession session) { + Entity entity = session.getPlayerEntity(); + if (entity == null) + return; + + if (!session.isLoggedIn()) + return; + + if (session.isSpawned()) + return; + + ServerJoinGamePacket javaPacket = (ServerJoinGamePacket) session.getJavaPacketCache().getCachedValues().remove("java_join_packet"); + + StartGamePacket startGamePacket = new StartGamePacket(); + startGamePacket.setUniqueEntityId(entity.getEntityId()); + startGamePacket.setRuntimeEntityId(entity.getEntityId()); + startGamePacket.setPlayerGamemode(0); + startGamePacket.setPlayerPosition(new Vector3f(packet.getX(), packet.getY(), packet.getZ())); + startGamePacket.setRotation(new Vector2f(entity.getRotation().getX(), entity.getRotation().getY())); + + startGamePacket.setSeed(0); + startGamePacket.setDimensionId(entity.getDimension()); + startGamePacket.setGeneratorId(0); + startGamePacket.setLevelGamemode(javaPacket.getGameMode().ordinal()); + startGamePacket.setDifficulty(1); + startGamePacket.setDefaultSpawn(new Vector3i(packet.getX(), packet.getY(), packet.getZ())); + startGamePacket.setAcheivementsDisabled(true); + startGamePacket.setTime(0); + startGamePacket.setEduLevel(false); + startGamePacket.setEduFeaturesEnabled(false); + startGamePacket.setRainLevel(0); + startGamePacket.setLightningLevel(0); + startGamePacket.setMultiplayerGame(true); + startGamePacket.setBroadcastingToLan(true); + startGamePacket.getGamerules().add(new GameRule<>("showcoordinates", true)); + startGamePacket.setPlatformBroadcastMode(GamePublishSetting.PUBLIC); + startGamePacket.setXblBroadcastMode(GamePublishSetting.PUBLIC); + startGamePacket.setCommandsEnabled(true); + startGamePacket.setTexturePacksRequired(false); + startGamePacket.setBonusChestEnabled(false); + startGamePacket.setStartingWithMap(false); + startGamePacket.setTrustingPlayers(true); + startGamePacket.setDefaultPlayerPermission(1); + startGamePacket.setServerChunkTickRange(4); + startGamePacket.setBehaviorPackLocked(false); + startGamePacket.setResourcePackLocked(false); + startGamePacket.setFromLockedWorldTemplate(false); + startGamePacket.setUsingMsaGamertagsOnly(false); + startGamePacket.setFromWorldTemplate(false); + startGamePacket.setWorldTemplateOptionLocked(false); + + startGamePacket.setLevelId("world"); + startGamePacket.setWorldName("world"); + startGamePacket.setPremiumWorldTemplateId("00000000-0000-0000-0000-000000000000"); + startGamePacket.setCurrentTick(0); + startGamePacket.setEnchantmentSeed(0); + startGamePacket.setMultiplayerCorrelationId(""); + startGamePacket.setCachedPalette(Toolbox.CACHED_PALLETE); + startGamePacket.setItemEntries(Toolbox.ITEMS); + session.getUpstream().sendPacket(startGamePacket); + + entity.moveAbsolute(new Vector3f(packet.getX(), packet.getY(), packet.getZ()), packet.getPitch(), packet.getYaw()); + + SetEntityDataPacket entityDataPacket = new SetEntityDataPacket(); + entityDataPacket.setRuntimeEntityId(entity.getGeyserId()); + entityDataPacket.getMetadata().putAll(entity.getMetadata()); + + session.getUpstream().sendPacket(entityDataPacket); + + MovePlayerPacket movePlayerPacket = new MovePlayerPacket(); + movePlayerPacket.setRuntimeEntityId(entity.getGeyserId()); + movePlayerPacket.setPosition(new Vector3f(packet.getX(), packet.getY(), packet.getZ())); + movePlayerPacket.setRotation(new Vector3f(packet.getPitch(), packet.getYaw(), 0)); + movePlayerPacket.setMode(MovePlayerPacket.Mode.NORMAL); + movePlayerPacket.setOnGround(true); + entity.setMovePending(false); + + session.getUpstream().sendPacket(movePlayerPacket); + session.setSpawned(true); + + System.out.println("resent! " + packet.getX() + " " + packet.getY() + " " + packet.getZ()); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnExpOrbTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnExpOrbTranslator.java index c7f754dce..324909572 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnExpOrbTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnExpOrbTranslator.java @@ -1,19 +1,51 @@ +/* + * Copyright (c) 2019 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.entity.spawn; import com.flowpowered.math.vector.Vector3f; import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnExpOrbPacket; import com.nukkitx.protocol.bedrock.packet.SpawnExperienceOrbPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.ExpOrbEntity; +import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.translators.PacketTranslator; +import org.geysermc.connector.utils.EntityUtils; public class JavaSpawnExpOrbTranslator extends PacketTranslator { @Override public void translate(ServerSpawnExpOrbPacket packet, GeyserSession session) { - SpawnExperienceOrbPacket spawnExperienceOrbPacket = new SpawnExperienceOrbPacket(); - spawnExperienceOrbPacket.setPosition(new Vector3f(packet.getX(), packet.getY(), packet.getZ())); - spawnExperienceOrbPacket.setAmount(packet.getExp()); + Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); + Entity entity = new ExpOrbEntity(packet.getExp(), packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + EntityType.EXPERIENCE_ORB, position, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); - session.getUpstream().sendPacket(spawnExperienceOrbPacket); + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnGlobalEntityTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnGlobalEntityTranslator.java new file mode 100644 index 000000000..cb6b9f126 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnGlobalEntityTranslator.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 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.entity.spawn; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnGlobalEntityPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaSpawnGlobalEntityTranslator extends PacketTranslator { + + @Override + public void translate(ServerSpawnGlobalEntityPacket packet, GeyserSession session) { + Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); + + // Currently GlobalEntityType only has a lightning bolt + Entity entity = new Entity(packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + EntityType.LIGHTNING_BOLT, position, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); + + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnMobTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnMobTranslator.java new file mode 100644 index 000000000..93ff22a37 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnMobTranslator.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 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.entity.spawn; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnMobPacket; +import org.geysermc.connector.console.GeyserLogger; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; +import org.geysermc.connector.utils.EntityUtils; + +public class JavaSpawnMobTranslator extends PacketTranslator { + + @Override + public void translate(ServerSpawnMobPacket packet, GeyserSession session) { + Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); + Vector3f motion = new Vector3f(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()); + Vector3f rotation = new Vector3f(packet.getPitch(), packet.getYaw(), packet.getHeadYaw()); + + EntityType type = EntityUtils.toBedrockEntity(packet.getType()); + if (type == null) { + GeyserLogger.DEFAULT.warning("Entity type " + packet.getType() + " was null."); + return; + } + + Entity entity = new Entity(packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + type, position, motion, rotation); + + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnObjectTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnObjectTranslator.java new file mode 100644 index 000000000..a484d4a39 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnObjectTranslator.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2019 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.entity.spawn; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.data.game.entity.type.object.ObjectType; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnObjectPacket; +import org.geysermc.connector.console.GeyserLogger; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; +import org.geysermc.connector.utils.EntityUtils; + +public class JavaSpawnObjectTranslator extends PacketTranslator { + + @Override + public void translate(ServerSpawnObjectPacket packet, GeyserSession session) { + if (packet.getType() == ObjectType.ITEM_FRAME) + return; + + Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); + Vector3f motion = new Vector3f(packet.getMotionX(), packet.getMotionY(), packet.getMotionZ()); + Vector3f rotation = new Vector3f(packet.getPitch(), packet.getYaw(), 0); + + EntityType type = EntityUtils.toBedrockEntity(packet.getType()); + if (type == null) { + GeyserLogger.DEFAULT.warning("Entity type " + packet.getType() + " was null."); + return; + } + + Entity entity = new Entity(packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + type, position, motion, rotation); + + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); + } +} 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 new file mode 100644 index 000000000..d98e6c655 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPaintingTranslator.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 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.entity.spawn; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPaintingPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaSpawnPaintingTranslator extends PacketTranslator { + + @Override + public void translate(ServerSpawnPaintingPacket packet, GeyserSession session) { + Vector3f position = new Vector3f(packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ()); + + Entity entity = new Entity(packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + EntityType.PAINTING, position, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0)); + + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPlayerTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPlayerTranslator.java new file mode 100644 index 000000000..628ded6b9 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/entity/spawn/JavaSpawnPlayerTranslator.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2019 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.entity.spawn; + +import com.flowpowered.math.vector.Vector3f; +import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPlayerPacket; +import org.geysermc.connector.entity.Entity; +import org.geysermc.connector.entity.PlayerEntity; +import org.geysermc.connector.entity.type.EntityType; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.network.translators.PacketTranslator; + +public class JavaSpawnPlayerTranslator extends PacketTranslator { + + @Override + public void translate(ServerSpawnPlayerPacket packet, GeyserSession session) { + Vector3f position = new Vector3f(packet.getX(), packet.getY(), packet.getZ()); + Vector3f rotation = new Vector3f(packet.getPitch(), packet.getYaw(), 0); + Entity entity = new PlayerEntity(packet.getUUID(), packet.getEntityId(), session.getEntityCache().getNextEntityId().incrementAndGet(), + EntityType.EXPERIENCE_ORB, position, new Vector3f(0, 0, 0), rotation); + + if (entity == null) + return; + + session.getEntityCache().spawnEntity(entity); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/utils/EntityUtils.java b/connector/src/main/java/org/geysermc/connector/utils/EntityUtils.java index 50b616ea5..15c86d9b8 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/EntityUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/EntityUtils.java @@ -1,6 +1,7 @@ package org.geysermc.connector.utils; import com.github.steveice10.mc.protocol.data.game.entity.type.MobType; +import com.github.steveice10.mc.protocol.data.game.entity.type.object.ObjectType; import org.geysermc.connector.entity.type.EntityType; public class EntityUtils { @@ -20,4 +21,12 @@ public class EntityUtils { return null; } } + + public static EntityType toBedrockEntity(ObjectType type) { + try { + return EntityType.valueOf(type.name()); + } catch (IllegalArgumentException ex) { + return null; + } + } } diff --git a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java index 4386a4f4b..f8464c104 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java +++ b/connector/src/main/java/org/geysermc/connector/utils/Toolbox.java @@ -1,23 +1,17 @@ package org.geysermc.connector.utils; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import com.nukkitx.network.VarInts; import com.nukkitx.protocol.bedrock.packet.StartGamePacket; import com.nukkitx.protocol.bedrock.v361.BedrockUtils; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import org.apache.logging.log4j.core.util.Patterns; import org.geysermc.connector.network.translators.item.BedrockItem; -import org.geysermc.connector.network.translators.item.DyeColor; import org.geysermc.connector.network.translators.item.JavaItem; -import java.io.File; import java.io.InputStream; import java.util.*; -import java.util.regex.Pattern; public class Toolbox {