It's a start!

This commit is contained in:
RednedEpic 2019-07-08 12:55:14 -05:00
commit 6c881cd9f0
21 changed files with 1855 additions and 0 deletions

82
connector/pom.xml Normal file
View file

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.geysermc</groupId>
<artifactId>geyser-parent</artifactId>
<version>${revision}</version>
</parent>
<artifactId>connector</artifactId>
<properties>
<outputName>Geyser</outputName>
</properties>
<dependencies>
<dependency>
<groupId>org.geysermc</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.geysermc</groupId>
<artifactId>protocol</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>net.minecrell</groupId>
<artifactId>terminalconsoleappender</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<finalName>${outputName}-noshade</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Main-Class>org.geysermc.connector.GeyserConnector</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<finalName>${outputName}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${outputName}</finalName>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,82 @@
/*
* 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 org.geysermc.api.ChatColor;
import org.geysermc.connector.command.GeyserCommandMap;
import org.geysermc.connector.console.ConsoleCommandReader;
import org.geysermc.connector.console.GeyserLogger;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class GeyserConnector {
private static final String NAME = "Geyser";
private static final String VERSION = "1.0-SNAPSHOT";
private static GeyserConnector instance;
private boolean shuttingDown = false;
private GeyserLogger logger;
private GeyserCommandMap commandMap;
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("******************************************");
commandMap = new GeyserCommandMap(this);
}
public ScheduledExecutorService getGeneralThreadPool() {
return generalThreadPool;
}
public GeyserCommandMap getCommandMap() {
return commandMap;
}
public GeyserLogger getLogger() {
return logger;
}
public boolean isShuttingDown() {
return shuttingDown;
}
public void shutdown() {
logger.info("Shutting down connector.");
shuttingDown = true;
generalThreadPool.shutdown();
System.exit(0);
}
}

View file

@ -0,0 +1,73 @@
/*
* 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.command;
import org.geysermc.api.command.Command;
import org.geysermc.api.command.CommandSender;
import java.util.List;
public abstract class GeyserCommand implements Command {
private String name;
private String description;
private GeyserCommandMap commandMap;
private List<String> aliases;
public GeyserCommand(String name) {
this(name, "A geyser command.");
}
public GeyserCommand(String name, String description) {
this.name = name;
this.description = description;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public List<String> getAliases() {
return aliases;
}
@Override
public void setAliases(List<String> aliases) {
this.aliases = aliases;
}
@Override
public abstract void execute(CommandSender sender, String[] args);
public GeyserCommandMap getCommandMap() {
return commandMap;
}
}

View file

@ -0,0 +1,82 @@
/*
* 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.command;
import org.geysermc.api.command.Command;
import org.geysermc.api.command.CommandSender;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.defaults.HelpCommand;
import org.geysermc.connector.command.defaults.StopCommand;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class GeyserCommandMap {
private final Map<String, Command> commandMap = Collections.synchronizedMap(new HashMap<String, Command>());
private GeyserConnector connector;
public GeyserCommandMap(GeyserConnector connector) {
this.connector = connector;
registerDefaults();
}
public void registerDefaults() {
registerCommand(new HelpCommand(connector, "help", "Shows help for all registered commands."));
registerCommand(new StopCommand(connector, "stop", "Shut down Geyser."));
}
public void registerCommand(Command command) {
commandMap.put(command.getName(), command);
connector.getLogger().debug("Registered command " + command.getName());
for (String alias : command.getAliases())
commandMap.put(alias, command);
}
public void runCommand(CommandSender sender, String command) {
String trim = command.trim();
String label = null;
String[] args = null;
if (!trim.contains(" ")) {
label = trim.toLowerCase();
args = new String[0];
} else {
label = trim.substring(0, trim.indexOf(" ")).toLowerCase();
String argLine = trim.substring(trim.indexOf(" " + 1));
args = argLine.contains(" ") ? argLine.split(" ") : new String[] { argLine };
}
if (label == null) {
connector.getLogger().warning("Invalid Command! Try /help for a list of commands.");
return;
}
Command cmd = commandMap.get(label);
if (cmd == null) {
connector.getLogger().warning("Invalid Command! Try /help for a list of commands.");
return;
}
cmd.execute(sender, args);
}
public Map<String, Command> getCommands() {
return commandMap;
}
}

View file

@ -0,0 +1,38 @@
/*
* 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.command;
import org.geysermc.api.command.ConsoleCommandSender;
import org.geysermc.connector.console.GeyserLogger;
public class GeyserConsoleCommandSender implements ConsoleCommandSender {
@Override
public String getName() {
return "CONSOLE";
}
@Override
public void sendMessage(String message) {
System.out.println(GeyserLogger.printConsole(message, true));
}
@Override
public void sendMessage(String[] messages) {
for (String message : messages) {
sendMessage(message);
}
}
}

View file

@ -0,0 +1,52 @@
/*
* 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.command.defaults;
import org.geysermc.api.ChatColor;
import org.geysermc.api.command.Command;
import org.geysermc.api.command.CommandSender;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.GeyserCommand;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class HelpCommand extends GeyserCommand {
public GeyserConnector connector;
public HelpCommand(GeyserConnector connector, String name, String description) {
super(name, description);
this.connector = connector;
this.setAliases(Arrays.asList("?"));
}
@Override
public void execute(CommandSender sender, String[] args) {
sender.sendMessage("---- Showing Help For: Geyser (Page 1/1) ----");
Map<String, Command> cmds = connector.getCommandMap().getCommands();
List<String> commands = new ArrayList<String>(cmds.keySet());
Collections.sort(commands);
for (String cmd : commands) {
sender.sendMessage(ChatColor.YELLOW + "/" + cmd + ChatColor.WHITE + ": " + cmds.get(cmd).getDescription());
}
}
}

View file

@ -0,0 +1,38 @@
/*
* 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.command.defaults;
import org.geysermc.api.command.CommandSender;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.GeyserCommand;
import java.util.Arrays;
public class StopCommand extends GeyserCommand {
public GeyserConnector connector;
public StopCommand(GeyserConnector connector, String name, String description) {
super(name, description);
this.connector = connector;
this.setAliases(Arrays.asList("shutdown"));
}
@Override
public void execute(CommandSender sender, String[] args) {
connector.shutdown();
}
}

View file

@ -0,0 +1,90 @@
/*
* 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.console;
import org.geysermc.api.command.ConsoleCommandSender;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.GeyserConsoleCommandSender;
import net.minecrell.terminalconsole.TerminalConsoleAppender;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConsoleCommandReader {
private GeyserConnector connector;
private Terminal terminal;
private Thread thread;
public ConsoleCommandReader(GeyserConnector connector) {
this.connector = connector;
this.terminal = TerminalConsoleAppender.getTerminal();
}
public void startConsole() {
thread = new Thread() {
@Override
public void run() {
if (terminal != null) {
LineReader lineReader = LineReaderBuilder.builder()
.appName("Geyser")
.terminal(terminal)
.build();
TerminalConsoleAppender.setReader(lineReader);
try {
String line;
while (true) {
try {
line = lineReader.readLine("> ");
} catch (EndOfFileException ignored) {
continue;
}
if (line == null)
break;
}
} catch (UserInterruptException e /* do nothing */) {
} finally {
TerminalConsoleAppender.setReader(null);
}
} else {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String line;
while ((line = reader.readLine()) != null) {
ConsoleCommandSender sender = new GeyserConsoleCommandSender();
connector.getCommandMap().runCommand(sender, line);
}
} catch (IOException ex) {
Logger.getLogger("Geyser").log(Level.SEVERE, null, ex);
}
}
}
};
thread.setName("ConsoleCommandThread");
connector.getGeneralThreadPool().execute(thread);
}
}

View file

@ -0,0 +1,124 @@
/*
* 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.console;
import org.geysermc.api.ChatColor;
import org.geysermc.connector.GeyserConnector;
import io.sentry.Sentry;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class GeyserLogger {
private Logger logger;
private boolean colored = true;
private boolean debug = true;
public GeyserLogger(GeyserConnector connector) {
this.logger = Logger.getLogger(connector.getClass().getName());
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
System.setOut(new PrintStream(new LoggingOutputStream(this.logger, Level.INFO), true));
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.INFO);
consoleHandler.setFormatter(new SimpleFormatter() {
private static final String format = "[%1$tT][%2$-5s] %3$s %n";
@Override
public synchronized String format(LogRecord lr) {
return String.format(format,
new Date(lr.getMillis()),
lr.getLevel().getLocalizedName(),
lr.getMessage()
);
}
});
logger.addHandler(consoleHandler);
try {
File logDir = new File("logs");
logDir.mkdir();
File logFile = new File(logDir, "latest.log");
int maxLogFileSize = 20;//Mo
if (logFile.exists() && (logFile.length()) > maxLogFileSize * 1024L * 1024L)
logger.warning("Your log file is larger than " + maxLogFileSize + "Mo, you should backup and clean it !");
FileHandler fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new SimpleFormatter() {
private static final String format = "[%1$tF %1$tT][%2$-5s] %3$s %n";
@Override
public synchronized String format(LogRecord lr) {
return String.format(format,
new Date(lr.getMillis()),
lr.getLevel().getLocalizedName(),
lr.getMessage()
);
}
});
logger.addHandler(fileHandler);
} catch (IOException | SecurityException ex) {
Logger.getLogger(GeyserLogger.class.getName()).log(Level.SEVERE, null, ex);
}
if (System.getenv().containsKey("DP_SENTRY_CLIENT_KEY")) {
Handler sentryHandler = new io.sentry.jul.SentryHandler();
sentryHandler.setLevel(Level.SEVERE);
logger.addHandler(sentryHandler);
Sentry.init(System.getenv().get("DP_SENTRY_CLIENT_KEY"));
}
}
public void info(String message) {
logger.info(printConsole(message, colored));
}
public void severe(String message) {
logger.severe(printConsole(message, colored));
}
public void warning(String message) {
logger.warning(printConsole(message, colored));
}
public void debug(String message) {
if (debug)
info(message);
}
public void stop() {
for (Handler handler : logger.getHandlers())
handler.close();
}
public static String printConsole(String message, boolean colors) {
return colors ? ChatColor.toANSI(message + ChatColor.RESET) : ChatColor.stripColors(message + ChatColor.RESET);
}
}

View file

@ -0,0 +1,56 @@
/*
* 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.console;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingOutputStream extends ByteArrayOutputStream {
private Logger logger;
private Level level;
public LoggingOutputStream(Logger logger, Level level) {
this.logger = logger;
this.level = level;
}
@Override
public synchronized void write(int b) {
super.write(b);
try {
String contents = toString(Charsets.UTF_8.name());
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))
logger.logp(level, "", "", contents);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(LoggingOutputStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public synchronized void flush() throws IOException {
String contents = toString(Charsets.UTF_8.name());
super.reset();
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))
logger.logp(level, "", "", contents);
}
}