2019-07-10 06:34:10 +00:00
|
|
|
/*
|
2020-01-09 03:05:42 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +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:
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
2019-07-11 21:30:35 +00:00
|
|
|
* 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.
|
2019-07-10 06:34:10 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector.network.session;
|
|
|
|
|
2019-09-16 22:28:29 +00:00
|
|
|
import com.github.steveice10.mc.auth.data.GameProfile;
|
2020-04-05 09:42:02 +00:00
|
|
|
import com.github.steveice10.mc.auth.exception.request.InvalidCredentialsException;
|
2019-07-24 06:29:54 +00:00
|
|
|
import com.github.steveice10.mc.auth.exception.request.RequestException;
|
2019-07-10 06:34:10 +00:00
|
|
|
import com.github.steveice10.mc.protocol.MinecraftProtocol;
|
2020-05-05 15:51:43 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.SubProtocol;
|
2019-10-27 09:56:47 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
2020-05-19 17:41:44 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.window.VillagerTrade;
|
2020-06-28 22:38:27 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
|
2019-11-30 12:34:45 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.handshake.client.HandshakePacket;
|
2020-05-06 21:50:01 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.client.world.ClientTeleportConfirmPacket;
|
|
|
|
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerRespawnPacket;
|
2020-05-05 17:53:25 +00:00
|
|
|
import com.github.steveice10.mc.protocol.packet.login.server.LoginSuccessPacket;
|
2019-07-10 06:34:10 +00:00
|
|
|
import com.github.steveice10.packetlib.Client;
|
2019-11-30 12:34:45 +00:00
|
|
|
import com.github.steveice10.packetlib.event.session.*;
|
2019-08-06 02:09:45 +00:00
|
|
|
import com.github.steveice10.packetlib.packet.Packet;
|
2019-07-10 06:34:10 +00:00
|
|
|
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
|
2020-02-11 22:42:02 +00:00
|
|
|
import com.nukkitx.math.GenericMath;
|
2020-05-04 06:06:08 +00:00
|
|
|
import com.nukkitx.math.vector.*;
|
2020-05-05 15:51:43 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacket;
|
2019-07-10 06:34:10 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockServerSession;
|
2020-06-08 12:13:25 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.*;
|
2019-12-04 18:13:49 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.*;
|
2020-06-15 18:24:52 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
2020-05-02 20:44:05 +00:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2LongMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
|
2019-07-10 06:34:10 +00:00
|
|
|
import lombok.Getter;
|
2019-08-06 02:09:45 +00:00
|
|
|
import lombok.Setter;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.common.window.FormWindow;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.command.CommandSender;
|
2020-06-28 22:38:27 +00:00
|
|
|
import org.geysermc.connector.common.AuthType;
|
2020-05-23 21:39:17 +00:00
|
|
|
import org.geysermc.connector.entity.Entity;
|
2019-08-06 02:09:45 +00:00
|
|
|
import org.geysermc.connector.entity.PlayerEntity;
|
|
|
|
import org.geysermc.connector.inventory.PlayerInventory;
|
2019-12-21 17:35:48 +00:00
|
|
|
import org.geysermc.connector.network.remote.RemoteServer;
|
|
|
|
import org.geysermc.connector.network.session.auth.AuthData;
|
2019-11-30 12:34:45 +00:00
|
|
|
import org.geysermc.connector.network.session.auth.BedrockClientData;
|
2019-09-13 10:49:18 +00:00
|
|
|
import org.geysermc.connector.network.session.cache.*;
|
2020-05-25 01:07:05 +00:00
|
|
|
import org.geysermc.connector.network.translators.BiomeTranslator;
|
|
|
|
import org.geysermc.connector.network.translators.EntityIdentifierRegistry;
|
|
|
|
import org.geysermc.connector.network.translators.PacketTranslatorRegistry;
|
|
|
|
import org.geysermc.connector.network.translators.item.ItemRegistry;
|
2020-04-29 20:01:53 +00:00
|
|
|
import org.geysermc.connector.network.translators.world.block.BlockTranslator;
|
2020-06-21 02:24:45 +00:00
|
|
|
import org.geysermc.connector.utils.*;
|
2019-11-30 12:34:45 +00:00
|
|
|
import org.geysermc.floodgate.util.BedrockData;
|
|
|
|
import org.geysermc.floodgate.util.EncryptionUtil;
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2019-11-30 12:34:45 +00:00
|
|
|
import java.io.IOException;
|
2019-09-13 13:37:31 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2019-11-30 12:34:45 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.PublicKey;
|
|
|
|
import java.security.spec.InvalidKeySpecException;
|
2020-06-08 12:13:25 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-08-06 02:09:45 +00:00
|
|
|
import java.util.UUID;
|
2020-02-06 04:21:09 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2019-08-06 02:09:45 +00:00
|
|
|
|
|
|
|
@Getter
|
2019-12-21 17:35:48 +00:00
|
|
|
public class GeyserSession implements CommandSender {
|
2019-09-30 16:44:25 +00:00
|
|
|
|
2019-09-13 13:37:31 +00:00
|
|
|
private final GeyserConnector connector;
|
2019-10-02 20:45:29 +00:00
|
|
|
private final UpstreamSession upstream;
|
2019-07-23 23:16:25 +00:00
|
|
|
private RemoteServer remoteServer;
|
2019-07-10 06:34:10 +00:00
|
|
|
private Client downstream;
|
2020-04-29 16:06:25 +00:00
|
|
|
@Setter
|
|
|
|
private AuthData authData;
|
|
|
|
@Setter
|
|
|
|
private BedrockClientData clientData;
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2019-08-06 02:09:45 +00:00
|
|
|
private PlayerEntity playerEntity;
|
|
|
|
private PlayerInventory inventory;
|
2019-07-29 22:20:48 +00:00
|
|
|
|
2019-09-15 23:34:14 +00:00
|
|
|
private ChunkCache chunkCache;
|
2019-08-06 02:09:45 +00:00
|
|
|
private EntityCache entityCache;
|
|
|
|
private InventoryCache inventoryCache;
|
2019-07-30 02:57:43 +00:00
|
|
|
private ScoreboardCache scoreboardCache;
|
2019-09-15 23:34:14 +00:00
|
|
|
private WindowCache windowCache;
|
2020-04-29 16:06:25 +00:00
|
|
|
@Setter
|
|
|
|
private TeleportCache teleportCache;
|
2019-07-30 02:57:43 +00:00
|
|
|
|
2020-06-15 18:24:52 +00:00
|
|
|
@Getter
|
|
|
|
private final Long2ObjectMap<ClientboundMapItemDataPacket> storedMaps = new Long2ObjectOpenHashMap<>();
|
|
|
|
|
2020-05-02 20:44:05 +00:00
|
|
|
/**
|
|
|
|
* A map of Vector3i positions to Java entity IDs.
|
|
|
|
* Used for translating Bedrock block actions to Java entity actions.
|
|
|
|
*/
|
|
|
|
private final Object2LongMap<Vector3i> itemFrameCache = new Object2LongOpenHashMap<>();
|
|
|
|
|
2019-08-06 02:09:45 +00:00
|
|
|
private DataCache<Packet> javaPacketCache;
|
|
|
|
|
2020-03-06 01:26:36 +00:00
|
|
|
@Setter
|
|
|
|
private Vector2i lastChunkPosition = null;
|
2019-09-21 07:42:44 +00:00
|
|
|
private int renderDistance;
|
2019-09-13 10:49:18 +00:00
|
|
|
|
2019-07-24 06:29:54 +00:00
|
|
|
private boolean loggedIn;
|
2019-09-21 07:42:44 +00:00
|
|
|
private boolean loggingIn;
|
2019-07-24 06:29:54 +00:00
|
|
|
|
2019-08-06 02:09:45 +00:00
|
|
|
@Setter
|
|
|
|
private boolean spawned;
|
2019-07-10 06:34:10 +00:00
|
|
|
private boolean closed;
|
|
|
|
|
2019-09-30 16:44:25 +00:00
|
|
|
@Setter
|
2019-10-27 09:56:47 +00:00
|
|
|
private GameMode gameMode = GameMode.SURVIVAL;
|
2019-09-30 16:44:25 +00:00
|
|
|
|
2020-02-06 04:21:09 +00:00
|
|
|
private final AtomicInteger pendingDimSwitches = new AtomicInteger(0);
|
2020-04-30 05:21:02 +00:00
|
|
|
|
|
|
|
@Setter
|
|
|
|
private boolean sneaking;
|
|
|
|
|
2019-12-27 11:29:46 +00:00
|
|
|
@Setter
|
2020-02-16 18:40:54 +00:00
|
|
|
private boolean sprinting;
|
|
|
|
|
|
|
|
@Setter
|
|
|
|
private boolean jumping;
|
|
|
|
|
2020-04-23 04:40:49 +00:00
|
|
|
@Setter
|
2020-06-19 01:44:50 +00:00
|
|
|
private int breakingBlock;
|
2020-04-23 04:40:49 +00:00
|
|
|
|
|
|
|
@Setter
|
|
|
|
private Vector3i lastBlockPlacePosition;
|
|
|
|
|
2020-04-23 06:01:33 +00:00
|
|
|
@Setter
|
|
|
|
private String lastBlockPlacedId;
|
|
|
|
|
2020-04-30 05:21:02 +00:00
|
|
|
@Setter
|
|
|
|
private boolean interacting;
|
|
|
|
|
|
|
|
@Setter
|
|
|
|
private Vector3i lastInteractionPosition;
|
|
|
|
|
2019-12-27 11:29:46 +00:00
|
|
|
@Setter
|
2019-12-28 13:35:21 +00:00
|
|
|
private boolean switchingDimension = false;
|
|
|
|
private boolean manyDimPackets = false;
|
2019-12-27 11:29:46 +00:00
|
|
|
private ServerRespawnPacket lastDimPacket = null;
|
|
|
|
|
2020-05-23 21:39:17 +00:00
|
|
|
@Setter
|
|
|
|
private Entity ridingVehicleEntity;
|
|
|
|
|
2019-10-20 21:25:41 +00:00
|
|
|
@Setter
|
2019-11-28 03:55:58 +00:00
|
|
|
private int craftSlot = 0;
|
2019-10-20 21:25:41 +00:00
|
|
|
|
2020-06-02 16:48:26 +00:00
|
|
|
@Setter
|
|
|
|
private long lastWindowCloseTime = 0;
|
|
|
|
|
2020-05-19 17:41:44 +00:00
|
|
|
@Setter
|
|
|
|
private VillagerTrade[] villagerTrades;
|
2020-06-06 04:04:05 +00:00
|
|
|
@Setter
|
|
|
|
private long lastInteractedVillagerEid;
|
2020-05-19 17:41:44 +00:00
|
|
|
|
2020-06-17 00:03:28 +00:00
|
|
|
/**
|
|
|
|
* The current attack speed of the player. Used for sending proper cooldown timings.
|
|
|
|
*/
|
|
|
|
@Setter
|
|
|
|
private double attackSpeed;
|
|
|
|
/**
|
|
|
|
* The time of the last hit. Used to gauge how long the cooldown is taking.
|
|
|
|
* This is a session variable in order to prevent more scheduled threads than necessary.
|
|
|
|
*/
|
|
|
|
@Setter
|
|
|
|
private long lastHitTime;
|
|
|
|
|
2020-05-05 15:51:43 +00:00
|
|
|
private MinecraftProtocol protocol;
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession) {
|
|
|
|
this.connector = connector;
|
2019-10-02 20:45:29 +00:00
|
|
|
this.upstream = new UpstreamSession(bedrockServerSession);
|
2019-07-23 23:16:25 +00:00
|
|
|
|
2019-09-15 23:34:14 +00:00
|
|
|
this.chunkCache = new ChunkCache(this);
|
2019-08-06 02:09:45 +00:00
|
|
|
this.entityCache = new EntityCache(this);
|
2019-07-29 22:20:48 +00:00
|
|
|
this.inventoryCache = new InventoryCache(this);
|
2019-07-30 02:57:43 +00:00
|
|
|
this.scoreboardCache = new ScoreboardCache(this);
|
2019-09-15 23:34:14 +00:00
|
|
|
this.windowCache = new WindowCache(this);
|
2019-07-30 02:57:43 +00:00
|
|
|
|
2019-09-25 21:52:28 +00:00
|
|
|
this.playerEntity = new PlayerEntity(new GameProfile(UUID.randomUUID(), "unknown"), 1, 1, Vector3f.ZERO, Vector3f.ZERO, Vector3f.ZERO);
|
2019-08-06 02:09:45 +00:00
|
|
|
this.inventory = new PlayerInventory();
|
|
|
|
|
2019-09-21 07:42:44 +00:00
|
|
|
this.javaPacketCache = new DataCache<>();
|
2019-08-06 02:09:45 +00:00
|
|
|
|
|
|
|
this.spawned = false;
|
2019-07-24 06:29:54 +00:00
|
|
|
this.loggedIn = false;
|
2019-08-06 02:09:45 +00:00
|
|
|
|
|
|
|
this.inventoryCache.getInventories().put(0, inventory);
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 23:16:25 +00:00
|
|
|
public void connect(RemoteServer remoteServer) {
|
2019-07-24 06:29:54 +00:00
|
|
|
startGame();
|
|
|
|
this.remoteServer = remoteServer;
|
2019-11-06 00:55:59 +00:00
|
|
|
|
2019-12-29 03:17:00 +00:00
|
|
|
ChunkUtils.sendEmptyChunks(this, playerEntity.getPosition().toInt(), 0, false);
|
2019-11-06 00:55:59 +00:00
|
|
|
|
2020-03-06 02:53:58 +00:00
|
|
|
BiomeDefinitionListPacket biomeDefinitionListPacket = new BiomeDefinitionListPacket();
|
2020-07-05 20:58:43 +00:00
|
|
|
biomeDefinitionListPacket.setDefinitions(BiomeTranslator.BIOMES);
|
2020-03-06 02:53:58 +00:00
|
|
|
upstream.sendPacket(biomeDefinitionListPacket);
|
2019-11-16 04:21:26 +00:00
|
|
|
|
2019-11-14 02:26:45 +00:00
|
|
|
AvailableEntityIdentifiersPacket entityPacket = new AvailableEntityIdentifiersPacket();
|
2020-07-05 20:58:43 +00:00
|
|
|
entityPacket.setIdentifiers(EntityIdentifierRegistry.ENTITY_IDENTIFIERS);
|
2019-11-14 02:26:45 +00:00
|
|
|
upstream.sendPacket(entityPacket);
|
2019-11-10 22:53:01 +00:00
|
|
|
|
2020-06-27 15:35:02 +00:00
|
|
|
CreativeContentPacket creativePacket = new CreativeContentPacket();
|
|
|
|
for (int i = 0; i < ItemRegistry.CREATIVE_ITEMS.length; i++) {
|
2020-06-28 04:47:10 +00:00
|
|
|
creativePacket.getEntries().put(i + 1, ItemRegistry.CREATIVE_ITEMS[i]);
|
2020-06-27 15:35:02 +00:00
|
|
|
}
|
2019-11-15 23:55:15 +00:00
|
|
|
upstream.sendPacket(creativePacket);
|
|
|
|
|
2019-11-06 00:55:59 +00:00
|
|
|
PlayStatusPacket playStatusPacket = new PlayStatusPacket();
|
|
|
|
playStatusPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
|
|
|
|
upstream.sendPacket(playStatusPacket);
|
2020-06-08 12:13:25 +00:00
|
|
|
|
|
|
|
UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
|
|
|
|
attributesPacket.setRuntimeEntityId(getPlayerEntity().getGeyserId());
|
2020-06-23 00:11:09 +00:00
|
|
|
List<AttributeData> attributes = new ArrayList<>();
|
2020-06-08 12:13:25 +00:00
|
|
|
// Default move speed
|
|
|
|
// Bedrock clients move very fast by default until they get an attribute packet correcting the speed
|
2020-06-23 00:11:09 +00:00
|
|
|
attributes.add(new AttributeData("minecraft:movement", 0.0f, 1024f, 0.1f, 0.1f));
|
2020-06-08 12:13:25 +00:00
|
|
|
attributesPacket.setAttributes(attributes);
|
|
|
|
upstream.sendPacket(attributesPacket);
|
2019-07-24 06:29:54 +00:00
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2020-05-23 21:06:34 +00:00
|
|
|
public void fetchOurSkin(PlayerListPacket.Entry entry) {
|
|
|
|
PlayerSkinPacket playerSkinPacket = new PlayerSkinPacket();
|
|
|
|
playerSkinPacket.setUuid(authData.getUUID());
|
|
|
|
playerSkinPacket.setSkin(entry.getSkin());
|
|
|
|
playerSkinPacket.setOldSkinName("OldName");
|
|
|
|
playerSkinPacket.setNewSkinName("NewName");
|
|
|
|
playerSkinPacket.setTrustedSkin(true);
|
|
|
|
upstream.sendPacket(playerSkinPacket);
|
|
|
|
getConnector().getLogger().debug("Sending skin for " + playerEntity.getUsername() + " " + authData.getUUID());
|
|
|
|
}
|
|
|
|
|
2020-04-11 18:33:06 +00:00
|
|
|
public void login() {
|
|
|
|
if (connector.getAuthType() != AuthType.ONLINE) {
|
2020-07-05 23:35:51 +00:00
|
|
|
if (connector.getAuthType() == AuthType.OFFLINE) {
|
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.auth.login.offline"));
|
|
|
|
} else {
|
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.auth.login.floodgate"));
|
|
|
|
}
|
2020-04-11 18:33:06 +00:00
|
|
|
authenticate(authData.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 06:29:54 +00:00
|
|
|
public void authenticate(String username) {
|
|
|
|
authenticate(username, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void authenticate(String username, String password) {
|
|
|
|
if (loggedIn) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().severe(LanguageUtils.getLocaleStringLog("geyser.auth.already_loggedin", username));
|
2019-07-24 06:29:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2020-04-11 18:53:27 +00:00
|
|
|
loggingIn = true;
|
2019-09-21 07:42:44 +00:00
|
|
|
// new thread so clients don't timeout
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
if (password != null && !password.isEmpty()) {
|
|
|
|
protocol = new MinecraftProtocol(username, password);
|
|
|
|
} else {
|
|
|
|
protocol = new MinecraftProtocol(username);
|
2019-07-24 06:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 12:34:45 +00:00
|
|
|
boolean floodgate = connector.getAuthType() == AuthType.FLOODGATE;
|
|
|
|
final PublicKey publicKey;
|
|
|
|
|
|
|
|
if (floodgate) {
|
|
|
|
PublicKey key = null;
|
|
|
|
try {
|
|
|
|
key = EncryptionUtil.getKeyFromFile(
|
2020-01-26 17:22:21 +00:00
|
|
|
connector.getConfig().getFloodgateKeyFile(),
|
2019-11-30 12:34:45 +00:00
|
|
|
PublicKey.class
|
|
|
|
);
|
|
|
|
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().error(LanguageUtils.getLocaleStringLog("geyser.auth.floodgate.bad_key"), e);
|
2019-11-30 12:34:45 +00:00
|
|
|
}
|
|
|
|
publicKey = key;
|
|
|
|
} else publicKey = null;
|
|
|
|
|
2019-11-30 17:38:09 +00:00
|
|
|
if (publicKey != null) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.auth.floodgate.loaded_key"));
|
2019-11-30 17:38:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 07:42:44 +00:00
|
|
|
downstream = new Client(remoteServer.getAddress(), remoteServer.getPort(), protocol, new TcpSessionFactory());
|
|
|
|
downstream.getSession().addListener(new SessionAdapter() {
|
2019-11-30 12:34:45 +00:00
|
|
|
@Override
|
|
|
|
public void packetSending(PacketSendingEvent event) {
|
|
|
|
//todo move this somewhere else
|
|
|
|
if (event.getPacket() instanceof HandshakePacket && floodgate) {
|
|
|
|
String encrypted = "";
|
|
|
|
try {
|
2019-11-30 14:32:13 +00:00
|
|
|
encrypted = EncryptionUtil.encryptBedrockData(publicKey, new BedrockData(
|
2019-11-30 12:34:45 +00:00
|
|
|
clientData.getGameVersion(),
|
2020-01-04 05:58:58 +00:00
|
|
|
authData.getName(),
|
2020-03-14 19:02:58 +00:00
|
|
|
authData.getXboxUUID(),
|
2019-11-30 12:34:45 +00:00
|
|
|
clientData.getDeviceOS().ordinal(),
|
|
|
|
clientData.getLanguageCode(),
|
2019-12-17 22:27:29 +00:00
|
|
|
clientData.getCurrentInputMode().ordinal(),
|
|
|
|
upstream.getSession().getAddress().getAddress().getHostAddress()
|
2019-11-30 12:34:45 +00:00
|
|
|
));
|
|
|
|
} catch (Exception e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().error(LanguageUtils.getLocaleStringLog("geyser.auth.floodgate.encrypt_fail"), e);
|
2019-11-30 12:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HandshakePacket handshakePacket = event.getPacket();
|
|
|
|
event.setPacket(new HandshakePacket(
|
|
|
|
handshakePacket.getProtocolVersion(),
|
2019-12-17 22:27:29 +00:00
|
|
|
handshakePacket.getHostname() + '\0' + BedrockData.FLOODGATE_IDENTIFIER + '\0' + encrypted,
|
2019-11-30 12:34:45 +00:00
|
|
|
handshakePacket.getPort(),
|
|
|
|
handshakePacket.getIntent()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2019-09-21 07:42:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connected(ConnectedEvent event) {
|
|
|
|
loggingIn = false;
|
|
|
|
loggedIn = true;
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.remote.connect", authData.getName(), protocol.getProfile().getName(), remoteServer.getAddress()));
|
2019-09-21 07:42:44 +00:00
|
|
|
playerEntity.setUuid(protocol.getProfile().getId());
|
|
|
|
playerEntity.setUsername(protocol.getProfile().getName());
|
2020-04-08 23:20:41 +00:00
|
|
|
|
2020-04-09 16:21:51 +00:00
|
|
|
String locale = clientData.getLanguageCode();
|
|
|
|
|
|
|
|
// Let the user know there locale may take some time to download
|
|
|
|
// as it has to be extracted from a JAR
|
|
|
|
if (locale.toLowerCase().equals("en_us") && !LocaleUtils.LOCALE_MAPPINGS.containsKey("en_us")) {
|
2020-07-05 23:35:51 +00:00
|
|
|
// This should probably be left hardcoded as it will only show for en_us clients
|
2020-04-09 16:21:51 +00:00
|
|
|
sendMessage("Downloading your locale (en_us) this may take some time");
|
|
|
|
}
|
2020-04-09 16:06:17 +00:00
|
|
|
|
2020-04-08 23:20:41 +00:00
|
|
|
// Download and load the language for the player
|
2020-04-09 16:21:51 +00:00
|
|
|
LocaleUtils.downloadAndLoadLocale(locale);
|
2019-09-21 07:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disconnected(DisconnectedEvent event) {
|
|
|
|
loggingIn = false;
|
|
|
|
loggedIn = false;
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.network.remote.disconnect", authData.getName(), remoteServer.getAddress(), event.getReason()));
|
2020-04-15 04:27:16 +00:00
|
|
|
if (event.getCause() != null) {
|
|
|
|
event.getCause().printStackTrace();
|
|
|
|
}
|
2020-06-28 22:38:27 +00:00
|
|
|
|
|
|
|
upstream.disconnect(MessageUtils.getBedrockMessage(MessageSerializer.fromString(event.getReason())));
|
2019-09-21 07:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void packetReceived(PacketReceivedEvent event) {
|
2019-10-02 20:45:29 +00:00
|
|
|
if (!closed) {
|
2019-12-27 11:29:46 +00:00
|
|
|
//handle consecutive respawn packets
|
|
|
|
if (event.getPacket().getClass().equals(ServerRespawnPacket.class)) {
|
2019-12-28 13:35:21 +00:00
|
|
|
manyDimPackets = lastDimPacket != null;
|
2019-12-27 11:29:46 +00:00
|
|
|
lastDimPacket = event.getPacket();
|
|
|
|
return;
|
|
|
|
} else if (lastDimPacket != null) {
|
2020-05-25 01:07:05 +00:00
|
|
|
PacketTranslatorRegistry.JAVA_TRANSLATOR.translate(lastDimPacket.getClass(), lastDimPacket, GeyserSession.this);
|
2019-12-27 11:29:46 +00:00
|
|
|
lastDimPacket = null;
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:53:25 +00:00
|
|
|
// Required, or else Floodgate players break with Bukkit chunk caching
|
|
|
|
if (event.getPacket() instanceof LoginSuccessPacket) {
|
|
|
|
GameProfile profile = ((LoginSuccessPacket) event.getPacket()).getProfile();
|
|
|
|
playerEntity.setUsername(profile.getName());
|
|
|
|
playerEntity.setUuid(profile.getId());
|
2020-05-06 21:50:01 +00:00
|
|
|
|
|
|
|
// Check if they are not using a linked account
|
2020-05-12 04:45:16 +00:00
|
|
|
if (connector.getAuthType() == AuthType.OFFLINE || playerEntity.getUuid().getMostSignificantBits() == 0) {
|
2020-05-06 21:50:01 +00:00
|
|
|
SkinUtils.handleBedrockSkin(playerEntity, clientData);
|
|
|
|
}
|
2020-05-05 17:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 01:07:05 +00:00
|
|
|
PacketTranslatorRegistry.JAVA_TRANSLATOR.translate(event.getPacket().getClass(), event.getPacket(), GeyserSession.this);
|
2019-10-02 20:45:29 +00:00
|
|
|
}
|
2019-09-21 07:42:44 +00:00
|
|
|
}
|
2020-05-30 22:31:20 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void packetError(PacketErrorEvent event) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().warning(LanguageUtils.getLocaleStringLog("geyser.network.downstream_error", event.getCause().getMessage()));
|
2020-05-30 22:31:20 +00:00
|
|
|
if (connector.getConfig().isDebugMode())
|
|
|
|
event.getCause().printStackTrace();
|
|
|
|
event.setSuppress(true);
|
|
|
|
}
|
2019-09-21 07:42:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
downstream.getSession().connect();
|
2019-10-16 18:55:05 +00:00
|
|
|
connector.addPlayer(this);
|
2020-04-17 13:34:44 +00:00
|
|
|
} catch (InvalidCredentialsException | IllegalArgumentException e) {
|
2020-07-05 23:35:51 +00:00
|
|
|
connector.getLogger().info(LanguageUtils.getLocaleStringLog("geyser.auth.login.invalid", username));
|
|
|
|
disconnect(LanguageUtils.getPlayerLocaleString("geyser.auth.login.invalid.kick", getClientData().getLanguageCode()));
|
2019-09-21 07:42:44 +00:00
|
|
|
} catch (RequestException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void disconnect(String reason) {
|
|
|
|
if (!closed) {
|
2019-07-24 06:29:54 +00:00
|
|
|
loggedIn = false;
|
2019-08-02 20:54:40 +00:00
|
|
|
if (downstream != null && downstream.getSession() != null) {
|
|
|
|
downstream.getSession().disconnect(reason);
|
|
|
|
}
|
2019-09-13 13:37:31 +00:00
|
|
|
if (upstream != null && !upstream.isClosed()) {
|
2020-04-25 22:53:35 +00:00
|
|
|
connector.getPlayers().remove(this.upstream.getAddress());
|
2019-08-02 20:54:40 +00:00
|
|
|
upstream.disconnect(reason);
|
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
2019-08-06 02:09:45 +00:00
|
|
|
|
2020-04-29 20:01:53 +00:00
|
|
|
this.chunkCache = null;
|
|
|
|
this.entityCache = null;
|
|
|
|
this.scoreboardCache = null;
|
|
|
|
this.inventoryCache = null;
|
|
|
|
this.windowCache = null;
|
2020-04-25 22:53:35 +00:00
|
|
|
|
2019-08-06 02:09:45 +00:00
|
|
|
closed = true;
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void close() {
|
2020-07-05 23:35:51 +00:00
|
|
|
disconnect(LanguageUtils.getPlayerLocaleString("geyser.network.close", getClientData().getLanguageCode()));
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 23:16:25 +00:00
|
|
|
public void setAuthenticationData(AuthData authData) {
|
2019-12-21 17:35:48 +00:00
|
|
|
this.authData = authData;
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 00:52:20 +00:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2019-12-21 17:35:48 +00:00
|
|
|
return authData.getName();
|
2019-07-22 00:52:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void sendMessage(String message) {
|
|
|
|
TextPacket textPacket = new TextPacket();
|
|
|
|
textPacket.setPlatformChatId("");
|
|
|
|
textPacket.setSourceName("");
|
|
|
|
textPacket.setXuid("");
|
|
|
|
textPacket.setType(TextPacket.Type.CHAT);
|
|
|
|
textPacket.setNeedsTranslation(false);
|
|
|
|
textPacket.setMessage(message);
|
|
|
|
|
|
|
|
upstream.sendPacket(textPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-05 02:44:42 +00:00
|
|
|
public boolean isConsole() {
|
|
|
|
return false;
|
2019-07-22 00:52:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 23:16:25 +00:00
|
|
|
public void sendForm(FormWindow window, int id) {
|
|
|
|
windowCache.showWindow(window, id);
|
|
|
|
}
|
2019-07-10 06:34:10 +00:00
|
|
|
|
2020-02-06 04:32:33 +00:00
|
|
|
public void setRenderDistance(int renderDistance) {
|
2020-06-09 12:50:21 +00:00
|
|
|
renderDistance = GenericMath.ceil(++renderDistance * MathUtils.SQRT_OF_TWO); //square to circle
|
2020-02-06 04:32:33 +00:00
|
|
|
if (renderDistance > 32) renderDistance = 32; // <3 u ViaVersion but I don't like crashing clients x)
|
|
|
|
this.renderDistance = renderDistance;
|
|
|
|
|
|
|
|
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
|
|
|
|
chunkRadiusUpdatedPacket.setRadius(renderDistance);
|
|
|
|
upstream.sendPacket(chunkRadiusUpdatedPacket);
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:37:31 +00:00
|
|
|
public InetSocketAddress getSocketAddress() {
|
|
|
|
return this.upstream.getAddress();
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:16:25 +00:00
|
|
|
public void sendForm(FormWindow window) {
|
|
|
|
windowCache.showWindow(window);
|
2019-07-10 06:34:10 +00:00
|
|
|
}
|
2019-07-24 06:29:54 +00:00
|
|
|
|
|
|
|
private void startGame() {
|
|
|
|
StartGamePacket startGamePacket = new StartGamePacket();
|
2019-08-06 02:09:45 +00:00
|
|
|
startGamePacket.setUniqueEntityId(playerEntity.getGeyserId());
|
|
|
|
startGamePacket.setRuntimeEntityId(playerEntity.getGeyserId());
|
2020-06-23 00:11:09 +00:00
|
|
|
startGamePacket.setPlayerGameType(GameType.SURVIVAL);
|
2019-10-09 18:39:38 +00:00
|
|
|
startGamePacket.setPlayerPosition(Vector3f.from(0, 69, 0));
|
|
|
|
startGamePacket.setRotation(Vector2f.from(1, 1));
|
2019-07-24 06:29:54 +00:00
|
|
|
|
2019-11-10 22:53:01 +00:00
|
|
|
startGamePacket.setSeed(-1);
|
2020-06-21 02:24:45 +00:00
|
|
|
startGamePacket.setDimensionId(DimensionUtils.javaToBedrock(playerEntity.getDimension()));
|
2019-08-06 02:09:45 +00:00
|
|
|
startGamePacket.setGeneratorId(1);
|
2020-06-23 00:11:09 +00:00
|
|
|
startGamePacket.setLevelGameType(GameType.SURVIVAL);
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setDifficulty(1);
|
2019-10-09 18:39:38 +00:00
|
|
|
startGamePacket.setDefaultSpawn(Vector3i.ZERO);
|
2019-12-04 18:13:49 +00:00
|
|
|
startGamePacket.setAchievementsDisabled(true);
|
2020-06-23 00:11:09 +00:00
|
|
|
startGamePacket.setCurrentTick(-1);
|
2019-11-10 22:53:01 +00:00
|
|
|
startGamePacket.setEduEditionOffers(0);
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setEduFeaturesEnabled(false);
|
|
|
|
startGamePacket.setRainLevel(0);
|
|
|
|
startGamePacket.setLightningLevel(0);
|
|
|
|
startGamePacket.setMultiplayerGame(true);
|
|
|
|
startGamePacket.setBroadcastingToLan(true);
|
2020-02-14 23:39:26 +00:00
|
|
|
startGamePacket.getGamerules().add(new GameRuleData<>("showcoordinates", true));
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setPlatformBroadcastMode(GamePublishSetting.PUBLIC);
|
|
|
|
startGamePacket.setXblBroadcastMode(GamePublishSetting.PUBLIC);
|
|
|
|
startGamePacket.setCommandsEnabled(true);
|
|
|
|
startGamePacket.setTexturePacksRequired(false);
|
|
|
|
startGamePacket.setBonusChestEnabled(false);
|
|
|
|
startGamePacket.setStartingWithMap(false);
|
|
|
|
startGamePacket.setTrustingPlayers(true);
|
2020-03-21 22:59:16 +00:00
|
|
|
startGamePacket.setDefaultPlayerPermission(PlayerPermission.MEMBER);
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setServerChunkTickRange(4);
|
|
|
|
startGamePacket.setBehaviorPackLocked(false);
|
|
|
|
startGamePacket.setResourcePackLocked(false);
|
|
|
|
startGamePacket.setFromLockedWorldTemplate(false);
|
|
|
|
startGamePacket.setUsingMsaGamertagsOnly(false);
|
|
|
|
startGamePacket.setFromWorldTemplate(false);
|
|
|
|
startGamePacket.setWorldTemplateOptionLocked(false);
|
|
|
|
|
2019-08-06 02:09:45 +00:00
|
|
|
startGamePacket.setLevelId("world");
|
2020-06-23 00:11:09 +00:00
|
|
|
startGamePacket.setLevelName("world");
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setPremiumWorldTemplateId("00000000-0000-0000-0000-000000000000");
|
2019-11-10 22:53:01 +00:00
|
|
|
// startGamePacket.setCurrentTick(0);
|
2019-07-24 06:29:54 +00:00
|
|
|
startGamePacket.setEnchantmentSeed(0);
|
|
|
|
startGamePacket.setMultiplayerCorrelationId("");
|
2019-12-31 00:14:38 +00:00
|
|
|
startGamePacket.setBlockPalette(BlockTranslator.BLOCKS);
|
2020-05-25 01:07:05 +00:00
|
|
|
startGamePacket.setItemEntries(ItemRegistry.ITEMS);
|
2019-12-21 05:05:20 +00:00
|
|
|
startGamePacket.setVanillaVersion("*");
|
2019-11-10 22:53:01 +00:00
|
|
|
// startGamePacket.setMovementServerAuthoritative(true);
|
2019-07-24 06:29:54 +00:00
|
|
|
upstream.sendPacket(startGamePacket);
|
|
|
|
}
|
2020-04-29 16:06:25 +00:00
|
|
|
|
2020-05-04 06:06:08 +00:00
|
|
|
public boolean confirmTeleport(Vector3d position) {
|
2020-04-29 16:06:25 +00:00
|
|
|
if (teleportCache != null) {
|
|
|
|
if (!teleportCache.canConfirm(position)) {
|
|
|
|
GeyserConnector.getInstance().getLogger().debug("Unconfirmed Teleport " + teleportCache.getTeleportConfirmId()
|
|
|
|
+ " Ignore movement " + position + " expected " + teleportCache);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int teleportId = teleportCache.getTeleportConfirmId();
|
|
|
|
teleportCache = null;
|
|
|
|
ClientTeleportConfirmPacket teleportConfirmPacket = new ClientTeleportConfirmPacket(teleportId);
|
2020-05-05 15:51:43 +00:00
|
|
|
sendDownstreamPacket(teleportConfirmPacket);
|
2020-04-29 16:06:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-05 15:51:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a packet to be sent to player.
|
|
|
|
*
|
|
|
|
* @param packet the bedrock packet from the NukkitX protocol lib
|
|
|
|
*/
|
|
|
|
public void sendUpstreamPacket(BedrockPacket packet) {
|
|
|
|
if (upstream != null && !upstream.isClosed()) {
|
|
|
|
upstream.sendPacket(packet);
|
|
|
|
} else {
|
|
|
|
connector.getLogger().debug("Tried to send upstream packet " + packet.getClass().getSimpleName() + " but the session was null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet immediately to the player.
|
|
|
|
*
|
|
|
|
* @param packet the bedrock packet from the NukkitX protocol lib
|
|
|
|
*/
|
|
|
|
public void sendUpstreamPacketImmediately(BedrockPacket packet) {
|
|
|
|
if (upstream != null && !upstream.isClosed()) {
|
|
|
|
upstream.sendPacketImmediately(packet);
|
|
|
|
} else {
|
|
|
|
connector.getLogger().debug("Tried to send upstream packet " + packet.getClass().getSimpleName() + " immediately but the session was null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet to the remote server.
|
|
|
|
*
|
|
|
|
* @param packet the java edition packet from MCProtocolLib
|
|
|
|
*/
|
|
|
|
public void sendDownstreamPacket(Packet packet) {
|
|
|
|
if (downstream != null && downstream.getSession() != null && protocol.getSubProtocol().equals(SubProtocol.GAME)) {
|
|
|
|
downstream.getSession().send(packet);
|
|
|
|
} else {
|
|
|
|
connector.getLogger().debug("Tried to send downstream packet " + packet.getClass().getSimpleName() + " before connected to the server");
|
|
|
|
}
|
|
|
|
}
|
2019-08-24 03:49:48 +00:00
|
|
|
}
|