diff --git a/bootstrap/bukkit/src/main/java/org/geysermc/platform/bukkit/GeyserBukkitLogger.java b/bootstrap/bukkit/src/main/java/org/geysermc/platform/bukkit/GeyserBukkitLogger.java index 95214972..05fd2c6c 100644 --- a/bootstrap/bukkit/src/main/java/org/geysermc/platform/bukkit/GeyserBukkitLogger.java +++ b/bootstrap/bukkit/src/main/java/org/geysermc/platform/bukkit/GeyserBukkitLogger.java @@ -25,21 +25,19 @@ package org.geysermc.platform.bukkit; +import lombok.AllArgsConstructor; + import org.geysermc.common.logger.IGeyserLogger; import java.util.logging.Level; import java.util.logging.Logger; +@AllArgsConstructor public class GeyserBukkitLogger implements IGeyserLogger { private Logger logger; private boolean debugMode; - public GeyserBukkitLogger(Logger logger, boolean debugMode) { - this.logger = logger; - this.debugMode = debugMode; - } - @Override public void severe(String message) { logger.severe(message); diff --git a/bootstrap/pom.xml b/bootstrap/pom.xml index 7cc7e2ba..b826f61a 100644 --- a/bootstrap/pom.xml +++ b/bootstrap/pom.xml @@ -33,7 +33,7 @@ bukkit bungeecord - + sponge standalone diff --git a/bootstrap/sponge/pom.xml b/bootstrap/sponge/pom.xml new file mode 100644 index 00000000..0e822273 --- /dev/null +++ b/bootstrap/sponge/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + + org.geysermc + bootstrap-parent + 1.0-SNAPSHOT + ../ + + bootstrap-sponge + + + org.geysermc + connector + 1.0-SNAPSHOT + compile + + + org.spongepowered + spongeapi + 7.1.0 + provided + + + + ${outputName}-Sponge + ../../target + + + src/main/resources/ + true + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + com.fasterxml.jackson + org.geysermc.platform.sponge.shaded.jackson + + + io.netty + org.geysermc.platform.sponge.shaded.netty + + + true + + + + + + + *:* + + META-INF/* + + + + + + com.google.code.gson:* + org.yaml:* + com.nukkitx:fastutil-lite:* + org.slf4j:* + org.ow2.asm:* + + + + + + + \ No newline at end of file diff --git a/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeConfiguration.java b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeConfiguration.java new file mode 100644 index 00000000..8ae4c31d --- /dev/null +++ b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeConfiguration.java @@ -0,0 +1,161 @@ +package org.geysermc.platform.sponge; + +import lombok.AllArgsConstructor; + +import ninja.leaping.configurate.ConfigurationNode; + +import org.geysermc.common.IGeyserConfiguration; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class GeyserSpongeConfiguration implements IGeyserConfiguration { + + private ConfigurationNode node; + + private SpongeBedrockConfiguration bedrockConfig; + private SpongeRemoteConfiguration remoteConfig; + private SpongeMetricsInfo metricsInfo; + + private Map userAuthInfo = new HashMap<>(); + + public GeyserSpongeConfiguration(ConfigurationNode node) { + this.node = node; + + this.bedrockConfig = new SpongeBedrockConfiguration(node.getNode("bedrock")); + this.remoteConfig = new SpongeRemoteConfiguration(node.getNode("remote")); + this.metricsInfo = new SpongeMetricsInfo(); + + if (node.getNode("userAuths").getValue() == null) + return; + + for (String key : (List) node.getNode("userAuths").getValue()) { + userAuthInfo.put(key, new SpongeUserAuthenticationInfo(key)); + } + } + + @Override + public SpongeBedrockConfiguration getBedrock() { + return bedrockConfig; + } + + @Override + public SpongeRemoteConfiguration getRemote() { + return remoteConfig; + } + + @Override + public Map getUserAuths() { + return userAuthInfo; + } + + @Override + public boolean isPingPassthrough() { + return node.getNode("ping-passthrough").getBoolean(false); + } + + @Override + public int getMaxPlayers() { + return node.getNode("max-players").getInt(100); + } + + @Override + public boolean isDebugMode() { + return node.getNode("debug-mode").getBoolean(false); + } + + @Override + public int getGeneralThreadPool() { + return node.getNode("genereal-thread-pool").getInt(32); + } + + @Override + public boolean isAllowThirdPartyCapes() { + return node.getNode("allow-third-party-capes").getBoolean(true); + } + + @Override + public SpongeMetricsInfo getMetrics() { + return metricsInfo; + } + + @AllArgsConstructor + public class SpongeBedrockConfiguration implements IBedrockConfiguration { + + private ConfigurationNode node; + + @Override + public String getAddress() { + return node.getNode("address").getString("0.0.0.0"); + } + + @Override + public int getPort() { + return node.getNode("port").getInt(19132); + } + + @Override + public String getMotd1() { + return node.getNode("motd1").getString("GeyserMC"); + } + + @Override + public String getMotd2() { + return node.getNode("motd2").getString("GeyserMC"); + } + } + + @AllArgsConstructor + public class SpongeRemoteConfiguration implements IRemoteConfiguration { + + private ConfigurationNode node; + + @Override + public String getAddress() { + return node.getNode("address").getString("127.0.0.1"); + } + + @Override + public int getPort() { + return node.getNode("port").getInt(25565); + } + + @Override + public String getAuthType() { + return node.getNode("auth-type").getString("online"); + } + } + + public class SpongeUserAuthenticationInfo implements IUserAuthenticationInfo { + + private String key; + + public SpongeUserAuthenticationInfo(String key) { + this.key = key; + } + + @Override + public String getEmail() { + return node.getNode("userAuths").getNode(key).getNode("email").getString(); + } + + @Override + public String getPassword() { + return node.getNode("userAuths").getNode(key).getNode("password").getString(); + } + } + + public class SpongeMetricsInfo implements IMetricsInfo { + + @Override + public boolean isEnabled() { + return node.getNode("metrics").getNode("enabled").getBoolean(true); + } + + @Override + public String getUniqueId() { + return node.getNode("metrics").getNode("uuid").getString("generateduuid"); + } + } +} diff --git a/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeLogger.java b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeLogger.java new file mode 100644 index 00000000..b183d458 --- /dev/null +++ b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongeLogger.java @@ -0,0 +1,54 @@ +package org.geysermc.platform.sponge; + +import lombok.AllArgsConstructor; + +import org.geysermc.common.logger.IGeyserLogger; +import org.slf4j.Logger; + +@AllArgsConstructor +public class GeyserSpongeLogger implements IGeyserLogger { + + private Logger logger; + private boolean debugMode; + + @Override + public void severe(String message) { + logger.error(message); + } + + @Override + public void severe(String message, Throwable error) { + logger.error(message, error); + } + + @Override + public void error(String message) { + logger.error(message); + } + + @Override + public void error(String message, Throwable error) { + logger.error(message, error); + } + + @Override + public void warning(String message) { + logger.warn(message); + } + + @Override + public void info(String message) { + logger.info(message); + } + + @Override + public void debug(String message) { + if (debugMode) + info(message); + } + + @Override + public void setDebug(boolean debugMode) { + this.debugMode = debugMode; + } +} diff --git a/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongePlugin.java b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongePlugin.java new file mode 100644 index 00000000..76a89cfb --- /dev/null +++ b/bootstrap/sponge/src/main/java/org/geysermc/platform/sponge/GeyserSpongePlugin.java @@ -0,0 +1,87 @@ +package org.geysermc.platform.sponge; + +import com.google.inject.Inject; + +import ninja.leaping.configurate.loader.ConfigurationLoader; +import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; + +import org.geysermc.common.PlatformType; +import org.geysermc.common.bootstrap.IGeyserBootstrap; +import org.geysermc.connector.GeyserConnector; +import org.geysermc.connector.utils.FileUtils; +import org.slf4j.Logger; +import org.spongepowered.api.config.ConfigDir; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.game.state.GameStartedServerEvent; +import org.spongepowered.api.event.game.state.GameStoppedEvent; +import org.spongepowered.api.plugin.Plugin; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +@Plugin(id = "geyser", name = GeyserConnector.NAME + "-Sponge", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC") +public class GeyserSpongePlugin implements IGeyserBootstrap { + + @Inject + private Logger logger; + + @Inject + @ConfigDir(sharedRoot = false) + private File configDir; + + private GeyserSpongeConfiguration geyserConfig; + private GeyserSpongeLogger geyserLogger; + + @Override + public void onEnable() { + if (!configDir.exists()) + configDir.mkdirs(); + + File configFile = null; + try { + configFile = FileUtils.fileOrCopiedFromResource(new File(configDir, "config.yml"), "config.yml", (file) -> file.replaceAll("generateduuid", UUID.randomUUID().toString())); + } catch (IOException ex) { + logger.warn("Failed to copy config.yml from jar path!"); + ex.printStackTrace(); + } + + ConfigurationLoader loader = YAMLConfigurationLoader.builder().setPath(configFile.toPath()).build(); + try { + this.geyserConfig = new GeyserSpongeConfiguration(loader.load()); + } catch (IOException ex) { + logger.warn("Failed to load config.yml!"); + ex.printStackTrace(); + return; + } + + this.geyserLogger = new GeyserSpongeLogger(logger, geyserConfig.isDebugMode()); + + GeyserConnector.start(PlatformType.SPONGE, this); + } + + @Override + public void onDisable() { + GeyserConnector.stop(); + } + + @Override + public GeyserSpongeConfiguration getGeyserConfig() { + return geyserConfig; + } + + @Override + public GeyserSpongeLogger getGeyserLogger() { + return geyserLogger; + } + + @Listener + public void onServerStart(GameStartedServerEvent event) { + onEnable(); + } + + @Listener + public void onServerStop(GameStoppedEvent event) { + onDisable(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java index e28e3662..ff3793f3 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java @@ -2,19 +2,24 @@ package org.geysermc.connector.utils; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; + import org.geysermc.connector.GeyserConnector; import java.io.*; import java.util.function.Function; public class FileUtils { + public static T loadConfig(File src, Class valueType) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); return objectMapper.readValue(src, valueType); } public static File fileOrCopiedFromResource(String name, Function s) throws IOException { - File file = new File(name); + return fileOrCopiedFromResource(new File(name), name, s); + } + + public static File fileOrCopiedFromResource(File file, String name, Function s) throws IOException { if (!file.exists()) { FileOutputStream fos = new FileOutputStream(file); InputStream input = GeyserConnector.class.getResourceAsStream("/" + name); // resources need leading "/" prefix @@ -34,6 +39,4 @@ public class FileUtils { return file; } - - }