2019-07-08 17:55:14 +00:00
|
|
|
/*
|
2019-07-11 21:30:35 +00:00
|
|
|
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
2019-07-08 17:55:14 +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 17:55:14 +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 17:55:14 +00:00
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.geysermc.connector;
|
|
|
|
|
2019-07-08 19:13:31 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
|
2019-07-08 23:35:32 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.BedrockServer;
|
2019-07-11 20:53:44 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.v361.Bedrock_v361;
|
2019-07-08 19:19:41 +00:00
|
|
|
import lombok.Getter;
|
2019-08-06 01:59:54 +00:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2019-07-13 19:00:51 +00:00
|
|
|
import org.fusesource.jansi.AnsiConsole;
|
2019-07-08 23:22:50 +00:00
|
|
|
import org.geysermc.api.Connector;
|
|
|
|
import org.geysermc.api.Geyser;
|
|
|
|
import org.geysermc.api.command.CommandMap;
|
|
|
|
import org.geysermc.api.logger.Logger;
|
|
|
|
import org.geysermc.api.plugin.Plugin;
|
2019-07-08 17:55:14 +00:00
|
|
|
import org.geysermc.connector.command.GeyserCommandMap;
|
2019-07-08 19:13:31 +00:00
|
|
|
import org.geysermc.connector.configuration.GeyserConfiguration;
|
2019-07-08 17:55:14 +00:00
|
|
|
import org.geysermc.connector.console.ConsoleCommandReader;
|
|
|
|
import org.geysermc.connector.console.GeyserLogger;
|
2019-08-06 01:59:54 +00:00
|
|
|
import org.geysermc.connector.metrics.Metrics;
|
2019-07-10 06:34:10 +00:00
|
|
|
import org.geysermc.connector.network.ConnectorServerEventHandler;
|
2019-07-10 17:52:29 +00:00
|
|
|
import org.geysermc.connector.network.remote.RemoteJavaServer;
|
2019-07-11 22:39:28 +00:00
|
|
|
import org.geysermc.connector.network.translators.TranslatorsInit;
|
2019-07-08 23:22:50 +00:00
|
|
|
import org.geysermc.connector.plugin.GeyserPluginLoader;
|
|
|
|
import org.geysermc.connector.plugin.GeyserPluginManager;
|
2019-07-21 23:52:30 +00:00
|
|
|
import org.geysermc.connector.thread.PingPassthroughThread;
|
2019-08-02 04:16:17 +00:00
|
|
|
import org.geysermc.connector.utils.FileUtils;
|
2019-07-11 22:39:28 +00:00
|
|
|
import org.geysermc.connector.utils.Toolbox;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-08-02 04:16:17 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2019-07-08 23:35:32 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2019-08-06 01:59:54 +00:00
|
|
|
import java.util.Random;
|
|
|
|
import java.util.UUID;
|
2019-07-08 17:55:14 +00:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
2019-07-21 23:52:30 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-07-08 23:22:50 +00:00
|
|
|
public class GeyserConnector implements Connector {
|
2019-07-08 17:55:14 +00:00
|
|
|
|
2019-07-11 20:53:44 +00:00
|
|
|
public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v361.V361_CODEC;
|
2019-07-08 19:13:31 +00:00
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
private static final String NAME = "Geyser";
|
|
|
|
private static final String VERSION = "1.0-SNAPSHOT";
|
|
|
|
|
|
|
|
private static GeyserConnector instance;
|
|
|
|
|
2019-07-10 17:52:29 +00:00
|
|
|
@Getter
|
|
|
|
private RemoteJavaServer remoteServer;
|
|
|
|
|
2019-07-08 19:19:41 +00:00
|
|
|
@Getter
|
2019-07-08 23:22:50 +00:00
|
|
|
private Logger logger;
|
2019-07-08 19:19:41 +00:00
|
|
|
|
|
|
|
@Getter
|
2019-07-08 23:22:50 +00:00
|
|
|
private CommandMap commandMap;
|
2019-07-08 19:19:41 +00:00
|
|
|
|
|
|
|
@Getter
|
2019-07-08 19:13:31 +00:00
|
|
|
private GeyserConfiguration config;
|
|
|
|
|
2019-07-08 23:22:50 +00:00
|
|
|
@Getter
|
|
|
|
private GeyserPluginManager pluginManager;
|
|
|
|
|
2019-07-08 19:19:41 +00:00
|
|
|
@Getter
|
|
|
|
private boolean shuttingDown = false;
|
|
|
|
|
|
|
|
@Getter
|
2019-07-08 17:55:14 +00:00
|
|
|
private final ScheduledExecutorService generalThreadPool;
|
|
|
|
|
2019-07-21 23:52:30 +00:00
|
|
|
@Getter
|
|
|
|
private PingPassthroughThread passthroughThread;
|
|
|
|
|
2019-08-06 01:59:54 +00:00
|
|
|
@Getter
|
|
|
|
private Metrics METRICS;
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
public static void main(String[] args) {
|
|
|
|
instance = new GeyserConnector();
|
|
|
|
}
|
|
|
|
|
|
|
|
private GeyserConnector() {
|
2019-07-31 00:10:27 +00:00
|
|
|
//Metric
|
2019-07-13 19:00:51 +00:00
|
|
|
if(!(System.console() == null) && System.getProperty("os.name", "Windows 10").toLowerCase().contains("windows")) {
|
|
|
|
AnsiConsole.systemInstall();
|
|
|
|
}
|
2019-07-08 17:55:14 +00:00
|
|
|
instance = this;
|
|
|
|
|
|
|
|
this.generalThreadPool = Executors.newScheduledThreadPool(32); //TODO: Make configurable value
|
2019-07-13 19:00:51 +00:00
|
|
|
this.logger = GeyserLogger.DEFAULT;
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
ConsoleCommandReader consoleReader = new ConsoleCommandReader(this);
|
|
|
|
consoleReader.startConsole();
|
|
|
|
|
2019-07-13 19:00:51 +00:00
|
|
|
logger.info("******************************************");
|
2019-07-08 17:55:14 +00:00
|
|
|
logger.info("");
|
|
|
|
logger.info("Loading " + NAME + " vesion " + VERSION);
|
|
|
|
logger.info("");
|
|
|
|
logger.info("******************************************");
|
|
|
|
|
2019-07-08 19:13:31 +00:00
|
|
|
try {
|
2019-08-06 01:59:54 +00:00
|
|
|
File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("UUIDTESTUUIDTEST", UUID.randomUUID().toString()));
|
2019-08-02 04:16:17 +00:00
|
|
|
|
|
|
|
config = FileUtils.loadConfig(configFile, GeyserConfiguration.class);
|
2019-07-08 19:13:31 +00:00
|
|
|
} catch (IOException ex) {
|
2019-08-02 20:15:32 +00:00
|
|
|
logger.severe("Failed to read/create config.yml! Make sure it's up to date and/or readable+writable!");
|
2019-07-08 19:13:31 +00:00
|
|
|
shutdown();
|
|
|
|
}
|
2019-07-08 19:47:37 +00:00
|
|
|
|
2019-08-06 01:59:54 +00:00
|
|
|
METRICS = new Metrics("GeyserMC", instance.getConfig().getUUID(), true, java.util.logging.Logger.getLogger(""));
|
|
|
|
|
|
|
|
addMetrics(METRICS);
|
|
|
|
|
2019-07-21 21:18:20 +00:00
|
|
|
logger.setDebug(config.isDebugMode());
|
|
|
|
|
2019-07-11 22:39:28 +00:00
|
|
|
Toolbox.CACHED_PALLETE.array();
|
|
|
|
|
|
|
|
TranslatorsInit.start();
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
commandMap = new GeyserCommandMap(this);
|
2019-07-10 17:52:29 +00:00
|
|
|
remoteServer = new RemoteJavaServer(config.getRemote().getAddress(), config.getRemote().getPort());
|
|
|
|
|
2019-07-08 23:22:50 +00:00
|
|
|
Geyser.setConnector(this);
|
|
|
|
|
|
|
|
pluginManager = new GeyserPluginManager(new GeyserPluginLoader(this));
|
|
|
|
pluginManager.getLoader().loadPlugins();
|
2019-07-08 23:35:32 +00:00
|
|
|
|
2019-07-21 23:52:30 +00:00
|
|
|
passthroughThread = new PingPassthroughThread(this);
|
|
|
|
if (config.isPingPassthrough())
|
|
|
|
generalThreadPool.scheduleAtFixedRate(passthroughThread, 1, 1, TimeUnit.SECONDS);
|
|
|
|
|
2019-07-08 23:35:32 +00:00
|
|
|
BedrockServer bedrockServer = new BedrockServer(new InetSocketAddress(config.getBedrock().getAddress(), config.getBedrock().getPort()));
|
2019-07-10 06:34:10 +00:00
|
|
|
bedrockServer.setHandler(new ConnectorServerEventHandler(this));
|
2019-07-08 23:35:32 +00:00
|
|
|
bedrockServer.bind().whenComplete((avoid, throwable) -> {
|
|
|
|
if (throwable == null) {
|
|
|
|
logger.info("Started RakNet on " + config.getBedrock().getAddress() + ":" + config.getBedrock().getPort());
|
|
|
|
} else {
|
|
|
|
logger.severe("Failed to start RakNet on " + config.getBedrock().getAddress() + ":" + config.getBedrock().getPort());
|
|
|
|
throwable.printStackTrace();
|
|
|
|
}
|
|
|
|
}).join();
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void shutdown() {
|
|
|
|
logger.info("Shutting down connector.");
|
2019-07-08 23:22:50 +00:00
|
|
|
for (Plugin plugin : pluginManager.getPlugins()) {
|
|
|
|
pluginManager.disablePlugin(plugin);
|
|
|
|
pluginManager.unloadPlugin(plugin);
|
|
|
|
}
|
|
|
|
|
2019-07-08 17:55:14 +00:00
|
|
|
shuttingDown = true;
|
|
|
|
|
|
|
|
generalThreadPool.shutdown();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2019-08-06 01:59:54 +00:00
|
|
|
|
|
|
|
private static void addMetrics(Metrics m) {
|
|
|
|
m.addCustomChart(new Metrics.SingleLineChart("servers", () -> 3 + new Random().nextInt(4)));
|
|
|
|
|
|
|
|
m.addCustomChart(new Metrics.SingleLineChart("players", () -> 5 + new Random().nextInt(7)));
|
|
|
|
}
|
2019-07-08 17:55:14 +00:00
|
|
|
}
|