Geyser/bootstrap/fabric/src/main/java/org/geysermc/platform/fabric/GeyserFabricMod.java

216 lines
8.2 KiB
Java
Raw Normal View History

2020-10-06 16:15:07 +00:00
/*
* Copyright (c) 2020 GeyserMC. http://geysermc.org
*
* 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
*/
package org.geysermc.platform.fabric;
2020-10-06 22:43:02 +00:00
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
2020-10-06 16:15:07 +00:00
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
2020-10-06 16:15:07 +00:00
import net.fabricmc.loader.api.FabricLoader;
2020-10-06 22:43:02 +00:00
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import org.apache.logging.log4j.LogManager;
2020-10-06 16:15:07 +00:00
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.GeyserLogger;
import org.geysermc.connector.bootstrap.GeyserBootstrap;
import org.geysermc.connector.command.CommandManager;
2020-10-06 22:43:02 +00:00
import org.geysermc.connector.command.GeyserCommand;
2020-10-06 16:15:07 +00:00
import org.geysermc.connector.common.PlatformType;
import org.geysermc.connector.configuration.GeyserConfiguration;
import org.geysermc.connector.dump.BootstrapDumpInfo;
import org.geysermc.connector.ping.GeyserLegacyPingPassthrough;
import org.geysermc.connector.ping.IGeyserPingPassthrough;
import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.LanguageUtils;
2020-10-06 22:43:02 +00:00
import org.geysermc.platform.fabric.command.GeyserFabricCommandExecutor;
2020-10-06 16:15:07 +00:00
import org.geysermc.platform.fabric.command.GeyserFabricCommandManager;
import java.io.File;
2020-10-08 02:42:17 +00:00
import java.io.FileOutputStream;
2020-10-06 16:15:07 +00:00
import java.io.IOException;
2020-10-08 02:42:17 +00:00
import java.io.InputStream;
2020-10-06 16:15:07 +00:00
import java.nio.file.Path;
2020-10-08 02:42:17 +00:00
import java.util.*;
import java.util.function.Function;
2020-10-06 16:15:07 +00:00
@Environment(EnvType.SERVER)
public class GeyserFabricMod implements DedicatedServerModInitializer, GeyserBootstrap {
private GeyserConnector connector;
private Path dataFolder;
2020-10-08 02:42:17 +00:00
private List<String> playerCommands;
2020-10-06 22:43:02 +00:00
private MinecraftServer server;
2020-10-06 16:15:07 +00:00
private GeyserFabricCommandManager geyserCommandManager;
private GeyserFabricConfiguration geyserConfig;
private GeyserFabricLogger geyserLogger;
private IGeyserPingPassthrough geyserPingPassthrough;
@Override
public void onInitializeServer() {
this.onEnable();
}
@Override
public void onEnable() {
dataFolder = FabricLoader.getInstance().getConfigDir().resolve("Geyser-Fabric");
if (!dataFolder.toFile().exists()) {
//noinspection ResultOfMethodCallIgnored
dataFolder.toFile().mkdir();
}
try {
File configFile = FileUtils.fileOrCopiedFromResource(dataFolder.resolve("config.yml").toFile(), "config.yml",
(x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserFabricConfiguration.class);
2020-10-08 02:42:17 +00:00
File permissionsFile = fileOrCopiedFromResource(dataFolder.resolve("permissions.yml").toFile(), "permissions.yml");
this.playerCommands = Arrays.asList(FileUtils.loadConfig(permissionsFile, GeyserFabricPermissions.class).getCommands());
2020-10-06 16:15:07 +00:00
} catch (IOException ex) {
LogManager.getLogger("geyser-fabric").error(LanguageUtils.getLocaleStringLog("geyser.config.failed"), ex);
2020-10-06 16:15:07 +00:00
ex.printStackTrace();
}
this.geyserLogger = new GeyserFabricLogger(geyserConfig.isDebugMode());
GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger);
2020-10-12 03:15:22 +00:00
if (server == null) {
// Server has yet to start
// Set as an event so we can get the proper IP and port if needed
ServerLifecycleEvents.SERVER_STARTED.register((server) -> {
this.server = server;
startGeyser();
});
// Register onDisable so players are properly kicked
ServerLifecycleEvents.SERVER_STOPPING.register((server) -> onDisable());
} else {
// Server has started and this is a reload
startGeyser();
}
}
2020-10-06 16:15:07 +00:00
2020-10-12 03:15:22 +00:00
/**
* Initialize core Geyser.
* A function, as it needs to be called in different places depending on if Geyser is being reloaded or not.
*/
public void startGeyser() {
if (this.geyserConfig.getRemote().getAddress().equalsIgnoreCase("auto")) {
this.geyserConfig.setAutoconfiguredRemote(true);
String ip = server.getServerIp();
int port = server.getServerPort();
if (ip != null && !ip.isEmpty() && !ip.equals("0.0.0.0")) {
this.geyserConfig.getRemote().setAddress(ip);
2020-10-06 18:11:27 +00:00
}
2020-10-12 03:15:22 +00:00
this.geyserConfig.getRemote().setPort(port);
}
2020-10-06 16:15:07 +00:00
2020-10-12 03:15:22 +00:00
if (geyserConfig.getBedrock().isCloneRemotePort()) {
geyserConfig.getBedrock().setPort(geyserConfig.getRemote().getPort());
}
2020-10-06 16:15:07 +00:00
2020-10-12 03:15:22 +00:00
this.connector = GeyserConnector.start(PlatformType.FABRIC, this);
2020-10-12 03:15:22 +00:00
this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(connector);
2020-10-06 22:43:02 +00:00
2020-10-12 03:15:22 +00:00
this.geyserCommandManager = new GeyserFabricCommandManager(connector);
2020-10-06 16:15:07 +00:00
2020-10-12 03:15:22 +00:00
// Start command building
// Set just "geyser" as the help command
LiteralArgumentBuilder<ServerCommandSource> builder = net.minecraft.server.command.CommandManager.literal("geyser")
.executes(new GeyserFabricCommandExecutor(connector, "help", !playerCommands.contains("help")));
for (Map.Entry<String, GeyserCommand> command : connector.getCommandManager().getCommands().entrySet()) {
// Register all subcommands as valid
builder.then(net.minecraft.server.command.CommandManager.literal(
command.getKey()).executes(new GeyserFabricCommandExecutor(connector, command.getKey(),
!playerCommands.contains(command.getKey()))));
}
server.getCommandManager().getDispatcher().register(builder);
2020-10-06 16:15:07 +00:00
}
@Override
public void onDisable() {
if (connector != null) {
connector.shutdown();
2020-10-12 03:15:22 +00:00
connector = null;
2020-10-06 16:15:07 +00:00
}
}
@Override
public GeyserConfiguration getGeyserConfig() {
return geyserConfig;
}
@Override
public GeyserLogger getGeyserLogger() {
return geyserLogger;
}
@Override
public CommandManager getGeyserCommandManager() {
return geyserCommandManager;
}
@Override
public IGeyserPingPassthrough getGeyserPingPassthrough() {
return geyserPingPassthrough;
}
@Override
public Path getConfigFolder() {
return dataFolder;
}
@Override
public BootstrapDumpInfo getDumpInfo() {
2020-10-06 22:43:02 +00:00
return new GeyserFabricDumpInfo(server);
2020-10-06 16:15:07 +00:00
}
2020-10-08 02:42:17 +00:00
private File fileOrCopiedFromResource(File file, String name) throws IOException {
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
InputStream input = GeyserFabricMod.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
byte[] bytes = new byte[input.available()];
//noinspection ResultOfMethodCallIgnored
input.read(bytes);
for(char c : new String(bytes).toCharArray()) {
fos.write(c);
}
fos.flush();
input.close();
fos.close();
}
return file;
}
2020-10-06 16:15:07 +00:00
}