Geyser/connector/src/main/java/org/geysermc/connector/GeyserConnector.java

142 lines
4.8 KiB
Java
Raw Normal View History

2019-07-08 17:55:14 +00:00
/*
* GNU LESSER GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* You can view the LICENCE file for details.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
2019-07-08 23:35:32 +00:00
import com.nukkitx.protocol.bedrock.BedrockServer;
import com.nukkitx.protocol.bedrock.v354.Bedrock_v354;
2019-07-08 19:19:41 +00:00
import lombok.Getter;
2019-07-08 17:55:14 +00:00
import org.geysermc.api.ChatColor;
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;
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-07-08 23:35:32 +00:00
import org.geysermc.connector.network.listener.ConnectorServerEventListener;
2019-07-08 23:22:50 +00:00
import org.geysermc.connector.plugin.GeyserPluginLoader;
import org.geysermc.connector.plugin.GeyserPluginManager;
2019-07-08 17:55:14 +00:00
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
2019-07-08 23:35:32 +00:00
import java.net.InetSocketAddress;
2019-07-08 17:55:14 +00:00
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
2019-07-08 23:22:50 +00:00
public class GeyserConnector implements Connector {
2019-07-08 17:55:14 +00:00
public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v354.V354_CODEC;
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-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
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;
public static void main(String[] args) {
instance = new GeyserConnector();
}
private GeyserConnector() {
instance = this;
this.generalThreadPool = Executors.newScheduledThreadPool(32); //TODO: Make configurable value
this.logger = new GeyserLogger(this);
ConsoleCommandReader consoleReader = new ConsoleCommandReader(this);
consoleReader.startConsole();
logger.info(ChatColor.AQUA + "******************************************");
logger.info("");
logger.info("Loading " + NAME + " vesion " + VERSION);
logger.info("");
logger.info("******************************************");
try {
File configFile = new File("config.yml");
if (!configFile.exists()) {
FileOutputStream fos = new FileOutputStream(configFile);
InputStream is = GeyserConnector.class.getResourceAsStream("/config.yml");
int data;
while ((data = is.read()) != -1)
fos.write(data);
is.close();
fos.close();
}
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
config = objectMapper.readValue(configFile, GeyserConfiguration.class);
} catch (IOException ex) {
logger.severe("Failed to create config.yml! Make sure it's up to date and writable!");
shutdown();
}
2019-07-08 19:47:37 +00:00
2019-07-08 17:55:14 +00:00
commandMap = new GeyserCommandMap(this);
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
BedrockServer bedrockServer = new BedrockServer(new InetSocketAddress(config.getBedrock().getAddress(), config.getBedrock().getPort()));
bedrockServer.setHandler(new ConnectorServerEventListener(this));
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);
}
}