2019-07-08 23:35:32 +00:00
|
|
|
/*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
2019-07-08 23:35:32 +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-08 23:35:32 +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-08 23:35:32 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
package org.geysermc.connector.network;
|
2019-07-08 23:35:32 +00:00
|
|
|
|
2019-07-21 23:52:30 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
|
2019-07-08 23:35:32 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPong;
|
|
|
|
import com.nukkitx.protocol.bedrock.BedrockServerEventHandler;
|
|
|
|
import com.nukkitx.protocol.bedrock.BedrockServerSession;
|
2019-09-13 13:37:31 +00:00
|
|
|
import org.geysermc.api.Player;
|
2019-07-23 23:16:25 +00:00
|
|
|
import org.geysermc.api.events.PingEvent;
|
2019-07-08 23:35:32 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
|
|
|
import org.geysermc.connector.configuration.GeyserConfiguration;
|
2019-07-17 01:05:10 +00:00
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-21 23:52:30 +00:00
|
|
|
import org.geysermc.connector.utils.MessageUtils;
|
2019-07-08 23:35:32 +00:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
public class ConnectorServerEventHandler implements BedrockServerEventHandler {
|
2019-07-08 23:35:32 +00:00
|
|
|
|
|
|
|
private GeyserConnector connector;
|
|
|
|
|
2019-07-10 06:34:10 +00:00
|
|
|
public ConnectorServerEventHandler(GeyserConnector connector) {
|
2019-07-08 23:35:32 +00:00
|
|
|
this.connector = connector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onConnectionRequest(InetSocketAddress inetSocketAddress) {
|
2019-07-19 00:11:58 +00:00
|
|
|
GeyserLogger.DEFAULT.info(inetSocketAddress + " tried to connect!");
|
2019-07-08 23:35:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
|
2019-07-22 14:20:49 +00:00
|
|
|
PingEvent pong = new PingEvent(inetSocketAddress);
|
2019-07-21 21:18:20 +00:00
|
|
|
GeyserLogger.DEFAULT.debug(inetSocketAddress + " has pinged you!");
|
2019-07-08 23:35:32 +00:00
|
|
|
GeyserConfiguration config = connector.getConfig();
|
|
|
|
pong.setEdition("MCPE");
|
|
|
|
pong.setGameType("Default");
|
|
|
|
pong.setNintendoLimited(false);
|
|
|
|
pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion());
|
2019-07-13 19:00:51 +00:00
|
|
|
pong.setVersion("1.12.0");
|
2019-07-08 23:35:32 +00:00
|
|
|
|
2019-07-23 16:53:39 +00:00
|
|
|
connector.getPluginManager().runEvent(pong);
|
2019-07-21 23:52:30 +00:00
|
|
|
if (connector.getConfig().isPingPassthrough()) {
|
|
|
|
ServerStatusInfo serverInfo = connector.getPassthroughThread().getInfo();
|
|
|
|
|
|
|
|
if (serverInfo != null) {
|
|
|
|
pong.setMotd(MessageUtils.getBedrockMessage(serverInfo.getDescription()));
|
|
|
|
pong.setSubMotd(config.getBedrock().getMotd2());
|
|
|
|
pong.setPlayerCount(serverInfo.getPlayerInfo().getOnlinePlayers());
|
|
|
|
pong.setMaximumPlayerCount(serverInfo.getPlayerInfo().getMaxPlayers());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pong.setPlayerCount(1);
|
|
|
|
pong.setMaximumPlayerCount(config.getMaxPlayers());
|
|
|
|
pong.setMotd(config.getBedrock().getMotd1());
|
|
|
|
pong.setSubMotd(config.getBedrock().getMotd2());
|
|
|
|
}
|
2019-07-22 14:20:49 +00:00
|
|
|
|
|
|
|
BedrockPong c = new BedrockPong();
|
|
|
|
|
|
|
|
c.setEdition(pong.getEdition());
|
|
|
|
c.setGameType(pong.getGameType());
|
|
|
|
c.setNintendoLimited(pong.isNintendoLimited());
|
|
|
|
c.setProtocolVersion(pong.getProtocolVersion());
|
|
|
|
c.setVersion(pong.getVersion());
|
|
|
|
|
|
|
|
c.setMotd(pong.getMotd());
|
|
|
|
c.setSubMotd(pong.getSubMotd());
|
|
|
|
c.setPlayerCount(pong.getPlayerCount());
|
|
|
|
c.setMaximumPlayerCount(pong.getMaximumPlayerCount());
|
2019-08-03 06:56:54 +00:00
|
|
|
c.setIpv4Port(config.getBedrock().getPort());
|
2019-07-22 14:20:49 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
|
2019-07-08 23:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSessionCreation(BedrockServerSession bedrockServerSession) {
|
2019-07-10 06:34:10 +00:00
|
|
|
bedrockServerSession.setLogging(true);
|
|
|
|
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(connector, new GeyserSession(connector, bedrockServerSession)));
|
2019-09-13 13:37:31 +00:00
|
|
|
bedrockServerSession.addDisconnectHandler(disconnectReason -> {
|
|
|
|
GeyserLogger.DEFAULT.info("Bedrock user with ip: " + bedrockServerSession.getAddress().getAddress() + " has disconnected for reason " + disconnectReason);
|
|
|
|
|
|
|
|
Player player = connector.getPlayers().get(bedrockServerSession.getAddress());
|
|
|
|
if (player != null) {
|
|
|
|
player.disconnect(disconnectReason.name());
|
2019-09-13 14:45:55 +00:00
|
|
|
connector.removePlayer(player);
|
2019-09-13 13:37:31 +00:00
|
|
|
}
|
|
|
|
});
|
2019-07-24 22:53:15 +00:00
|
|
|
bedrockServerSession.setPacketCodec(GeyserConnector.BEDROCK_PACKET_CODEC);
|
2019-07-08 23:35:32 +00:00
|
|
|
}
|
|
|
|
}
|