2019-12-21 22:19:04 +00:00
|
|
|
/*
|
2020-04-15 19:39:21 +00:00
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
2019-12-21 22:19:04 +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:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-12-21 22:18:34 +00:00
|
|
|
package org.geysermc.platform.sponge;
|
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
2020-04-27 20:45:14 +00:00
|
|
|
import ninja.leaping.configurate.ConfigurationNode;
|
2019-12-21 22:18:34 +00:00
|
|
|
import ninja.leaping.configurate.loader.ConfigurationLoader;
|
|
|
|
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
|
2020-06-27 22:27:00 +00:00
|
|
|
import org.geysermc.connector.common.PlatformType;
|
2020-06-10 22:58:29 +00:00
|
|
|
import org.geysermc.connector.configuration.GeyserConfiguration;
|
2019-12-21 22:18:34 +00:00
|
|
|
import org.geysermc.connector.GeyserConnector;
|
2020-04-29 20:14:25 +00:00
|
|
|
import org.geysermc.connector.bootstrap.GeyserBootstrap;
|
2020-04-19 23:08:14 +00:00
|
|
|
import org.geysermc.connector.command.CommandManager;
|
2020-06-27 15:36:48 +00:00
|
|
|
import org.geysermc.connector.dump.BootstrapDumpInfo;
|
2020-05-23 21:50:04 +00:00
|
|
|
import org.geysermc.connector.ping.GeyserLegacyPingPassthrough;
|
|
|
|
import org.geysermc.connector.ping.IGeyserPingPassthrough;
|
2019-12-21 22:18:34 +00:00
|
|
|
import org.geysermc.connector.utils.FileUtils;
|
2020-07-05 23:35:51 +00:00
|
|
|
import org.geysermc.connector.utils.LanguageUtils;
|
2020-03-17 16:43:09 +00:00
|
|
|
import org.geysermc.platform.sponge.command.GeyserSpongeCommandExecutor;
|
2020-04-19 23:08:14 +00:00
|
|
|
import org.geysermc.platform.sponge.command.GeyserSpongeCommandManager;
|
2019-12-21 22:18:34 +00:00
|
|
|
import org.slf4j.Logger;
|
2020-03-17 16:43:09 +00:00
|
|
|
import org.spongepowered.api.Sponge;
|
2019-12-21 22:18:34 +00:00
|
|
|
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;
|
2020-04-27 20:45:14 +00:00
|
|
|
import java.net.InetSocketAddress;
|
2020-06-20 17:54:40 +00:00
|
|
|
import java.nio.file.Path;
|
2019-12-21 22:18:34 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Plugin(id = "geyser", name = GeyserConnector.NAME + "-Sponge", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC")
|
2020-04-29 20:01:53 +00:00
|
|
|
public class GeyserSpongePlugin implements GeyserBootstrap {
|
2019-12-21 22:18:34 +00:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
private Logger logger;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
@ConfigDir(sharedRoot = false)
|
|
|
|
private File configDir;
|
|
|
|
|
2020-04-19 23:08:14 +00:00
|
|
|
private GeyserSpongeCommandManager geyserCommandManager;
|
2019-12-21 22:18:34 +00:00
|
|
|
private GeyserSpongeConfiguration geyserConfig;
|
|
|
|
private GeyserSpongeLogger geyserLogger;
|
2020-05-23 21:50:04 +00:00
|
|
|
private IGeyserPingPassthrough geyserSpongePingPassthrough;
|
2019-12-21 22:18:34 +00:00
|
|
|
|
2020-03-17 16:43:09 +00:00
|
|
|
private GeyserConnector connector;
|
|
|
|
|
2019-12-21 22:18:34 +00:00
|
|
|
@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) {
|
2020-07-05 23:35:51 +00:00
|
|
|
logger.warn(LanguageUtils.getLocaleStringLog("geyser.config.failed"));
|
2019-12-21 22:18:34 +00:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigurationLoader loader = YAMLConfigurationLoader.builder().setPath(configFile.toPath()).build();
|
2020-04-27 20:45:14 +00:00
|
|
|
ConfigurationNode config;
|
2019-12-21 22:18:34 +00:00
|
|
|
try {
|
2020-04-27 20:45:14 +00:00
|
|
|
config = loader.load();
|
|
|
|
this.geyserConfig = new GeyserSpongeConfiguration(configDir, config);
|
2019-12-21 22:18:34 +00:00
|
|
|
} catch (IOException ex) {
|
2020-07-05 23:35:51 +00:00
|
|
|
logger.warn(LanguageUtils.getLocaleStringLog("geyser.config.failed"));
|
2019-12-21 22:18:34 +00:00
|
|
|
ex.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:45:14 +00:00
|
|
|
ConfigurationNode serverIP = config.getNode("remote").getNode("address");
|
|
|
|
ConfigurationNode serverPort = config.getNode("remote").getNode("port");
|
|
|
|
|
|
|
|
if (Sponge.getServer().getBoundAddress().isPresent()) {
|
|
|
|
InetSocketAddress javaAddr = Sponge.getServer().getBoundAddress().get();
|
|
|
|
|
|
|
|
// Don't change the ip if its listening on all interfaces
|
|
|
|
// By default this should be 127.0.0.1 but may need to be changed in some circumstances
|
2020-05-09 03:58:29 +00:00
|
|
|
if (!javaAddr.getHostString().equals("0.0.0.0") && !javaAddr.getHostString().equals("")) {
|
2020-04-27 20:45:14 +00:00
|
|
|
serverIP.setValue("127.0.0.1");
|
|
|
|
}
|
|
|
|
|
|
|
|
serverPort.setValue(javaAddr.getPort());
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:09:40 +00:00
|
|
|
ConfigurationNode bedrockPort = config.getNode("bedrock").getNode("port");
|
|
|
|
if (geyserConfig.getBedrock().isCloneRemotePort()){
|
|
|
|
bedrockPort.setValue(serverPort.getValue());
|
|
|
|
}
|
|
|
|
|
2019-12-21 22:18:34 +00:00
|
|
|
this.geyserLogger = new GeyserSpongeLogger(logger, geyserConfig.isDebugMode());
|
2020-05-21 03:43:22 +00:00
|
|
|
GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger);
|
2020-03-17 16:43:09 +00:00
|
|
|
this.connector = GeyserConnector.start(PlatformType.SPONGE, this);
|
2019-12-21 22:18:34 +00:00
|
|
|
|
2020-05-23 21:50:04 +00:00
|
|
|
if (geyserConfig.isLegacyPingPassthrough()) {
|
|
|
|
this.geyserSpongePingPassthrough = GeyserLegacyPingPassthrough.init(connector);
|
|
|
|
} else {
|
|
|
|
this.geyserSpongePingPassthrough = new GeyserSpongePingPassthrough();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.geyserCommandManager = new GeyserSpongeCommandManager(Sponge.getCommandManager(), connector);
|
2020-03-17 16:43:09 +00:00
|
|
|
Sponge.getCommandManager().register(this, new GeyserSpongeCommandExecutor(connector), "geyser");
|
2019-12-21 22:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2020-03-17 16:43:09 +00:00
|
|
|
connector.shutdown();
|
2019-12-21 22:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserSpongeConfiguration getGeyserConfig() {
|
|
|
|
return geyserConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserSpongeLogger getGeyserLogger() {
|
|
|
|
return geyserLogger;
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:08:14 +00:00
|
|
|
@Override
|
|
|
|
public CommandManager getGeyserCommandManager() {
|
|
|
|
return this.geyserCommandManager;
|
|
|
|
}
|
|
|
|
|
2020-05-23 21:50:04 +00:00
|
|
|
@Override
|
|
|
|
public IGeyserPingPassthrough getGeyserPingPassthrough() {
|
|
|
|
return geyserSpongePingPassthrough;
|
|
|
|
}
|
|
|
|
|
2020-06-20 17:54:40 +00:00
|
|
|
@Override
|
|
|
|
public Path getConfigFolder() {
|
|
|
|
return configDir.toPath();
|
|
|
|
}
|
|
|
|
|
2019-12-21 22:18:34 +00:00
|
|
|
@Listener
|
|
|
|
public void onServerStart(GameStartedServerEvent event) {
|
|
|
|
onEnable();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Listener
|
|
|
|
public void onServerStop(GameStoppedEvent event) {
|
|
|
|
onDisable();
|
|
|
|
}
|
2020-06-27 15:36:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BootstrapDumpInfo getDumpInfo() {
|
|
|
|
return new GeyserSpongeDumpInfo();
|
|
|
|
}
|
2019-12-21 22:18:34 +00:00
|
|
|
}
|