2020-10-06 16:15:07 +00:00
|
|
|
/*
|
2022-10-17 21:08:52 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-10-06 16:15:07 +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
|
|
|
|
*/
|
|
|
|
|
2022-10-17 21:08:52 +00:00
|
|
|
package org.geysermc.geyser.platform.fabric;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
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.EnvType;
|
2020-10-25 16:29:19 +00:00
|
|
|
import net.fabricmc.api.ModInitializer;
|
2020-10-06 17:41:50 +00:00
|
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
2022-10-24 17:21:02 +00:00
|
|
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
2020-10-06 16:15:07 +00:00
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
2021-04-02 00:07:32 +00:00
|
|
|
import net.fabricmc.loader.api.ModContainer;
|
2022-10-18 16:06:18 +00:00
|
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
|
|
import net.minecraft.commands.Commands;
|
2020-10-06 22:43:02 +00:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
2020-10-06 17:41:50 +00:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
2020-11-14 23:53:59 +00:00
|
|
|
import org.geysermc.common.PlatformType;
|
2022-10-18 16:06:18 +00:00
|
|
|
import org.geysermc.geyser.GeyserBootstrap;
|
2021-12-02 16:08:25 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
|
|
|
import org.geysermc.geyser.GeyserLogger;
|
2022-09-22 23:49:35 +00:00
|
|
|
import org.geysermc.geyser.api.command.Command;
|
|
|
|
import org.geysermc.geyser.api.network.AuthType;
|
2021-12-02 16:08:25 +00:00
|
|
|
import org.geysermc.geyser.command.GeyserCommand;
|
2022-09-22 23:49:35 +00:00
|
|
|
import org.geysermc.geyser.command.GeyserCommandManager;
|
2021-12-02 16:08:25 +00:00
|
|
|
import org.geysermc.geyser.configuration.GeyserConfiguration;
|
2022-10-18 16:06:18 +00:00
|
|
|
import org.geysermc.geyser.dump.BootstrapDumpInfo;
|
2021-12-02 16:08:25 +00:00
|
|
|
import org.geysermc.geyser.level.WorldManager;
|
2022-10-18 16:06:18 +00:00
|
|
|
import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
|
2021-12-02 16:08:25 +00:00
|
|
|
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
|
2022-10-17 21:08:52 +00:00
|
|
|
import org.geysermc.geyser.platform.fabric.command.GeyserFabricCommandExecutor;
|
|
|
|
import org.geysermc.geyser.platform.fabric.world.GeyserFabricWorldManager;
|
2022-10-18 16:06:18 +00:00
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
|
|
|
import org.geysermc.geyser.util.FileUtils;
|
2021-12-03 16:08:05 +00:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
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;
|
2021-04-02 00:07:32 +00:00
|
|
|
import java.util.*;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
2020-10-25 16:29:19 +00:00
|
|
|
public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
|
|
|
|
private static GeyserFabricMod instance;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
2020-10-25 20:22:50 +00:00
|
|
|
private boolean reloading;
|
|
|
|
|
2022-10-24 17:21:02 +00:00
|
|
|
private GeyserImpl geyser;
|
2021-12-03 16:08:05 +00:00
|
|
|
private ModContainer mod;
|
2020-10-06 16:15:07 +00:00
|
|
|
private Path dataFolder;
|
2020-10-06 22:43:02 +00:00
|
|
|
private MinecraftServer server;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
2022-10-18 20:05:55 +00:00
|
|
|
private GeyserCommandManager geyserCommandManager;
|
2020-10-06 16:15:07 +00:00
|
|
|
private GeyserFabricConfiguration geyserConfig;
|
|
|
|
private GeyserFabricLogger geyserLogger;
|
|
|
|
private IGeyserPingPassthrough geyserPingPassthrough;
|
2021-03-14 17:33:53 +00:00
|
|
|
private WorldManager geyserWorldManager;
|
2020-10-06 16:15:07 +00:00
|
|
|
|
|
|
|
@Override
|
2020-10-25 16:29:19 +00:00
|
|
|
public void onInitialize() {
|
|
|
|
instance = this;
|
2021-12-03 16:08:05 +00:00
|
|
|
mod = FabricLoader.getInstance().getModContainer("geyser-fabric").orElseThrow();
|
2020-10-25 16:29:19 +00:00
|
|
|
|
2020-10-06 16:15:07 +00:00
|
|
|
this.onEnable();
|
2020-10-25 16:29:19 +00:00
|
|
|
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
|
|
|
|
// Set as an event so we can get the proper IP and port if needed
|
|
|
|
ServerLifecycleEvents.SERVER_STARTED.register(this::startGeyser);
|
|
|
|
}
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
dataFolder = FabricLoader.getInstance().getConfigDir().resolve("Geyser-Fabric");
|
|
|
|
if (!dataFolder.toFile().exists()) {
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
dataFolder.toFile().mkdir();
|
|
|
|
}
|
2022-05-16 17:26:53 +00:00
|
|
|
|
|
|
|
// Init dataFolder first as local language overrides call getConfigFolder()
|
|
|
|
GeyserLocale.init(this);
|
|
|
|
|
2020-10-06 16:15:07 +00:00
|
|
|
try {
|
|
|
|
File configFile = FileUtils.fileOrCopiedFromResource(dataFolder.resolve("config.yml").toFile(), "config.yml",
|
2021-12-03 16:08:05 +00:00
|
|
|
(x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()), this);
|
2020-10-06 16:15:07 +00:00
|
|
|
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserFabricConfiguration.class);
|
|
|
|
} catch (IOException ex) {
|
2021-12-02 16:08:25 +00:00
|
|
|
LogManager.getLogger("geyser-fabric").error(GeyserLocale.getLocaleStringLog("geyser.config.failed"), ex);
|
2020-10-06 16:15:07 +00:00
|
|
|
ex.printStackTrace();
|
2021-06-07 13:58:17 +00:00
|
|
|
return;
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.geyserLogger = new GeyserFabricLogger(geyserConfig.isDebugMode());
|
|
|
|
|
|
|
|
GeyserConfiguration.checkGeyserConfiguration(geyserConfig, geyserLogger);
|
|
|
|
|
2022-10-24 17:21:02 +00:00
|
|
|
this.geyser = GeyserImpl.load(PlatformType.FABRIC, this);
|
|
|
|
|
2020-10-12 03:15:22 +00:00
|
|
|
if (server == null) {
|
|
|
|
// Server has yet to start
|
|
|
|
// Register onDisable so players are properly kicked
|
|
|
|
ServerLifecycleEvents.SERVER_STOPPING.register((server) -> onDisable());
|
2022-10-24 17:21:02 +00:00
|
|
|
|
|
|
|
ServerPlayConnectionEvents.JOIN.register((handler, $, $$) -> GeyserFabricUpdateListener.onPlayReady(handler));
|
2020-10-12 03:15:22 +00:00
|
|
|
} else {
|
|
|
|
// Server has started and this is a reload
|
2020-10-25 16:29:19 +00:00
|
|
|
startGeyser(this.server);
|
2020-10-25 20:22:50 +00:00
|
|
|
reloading = false;
|
2020-10-12 03:15:22 +00:00
|
|
|
}
|
|
|
|
}
|
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.
|
2020-10-25 16:29:19 +00:00
|
|
|
*
|
|
|
|
* @param server The minecraft server.
|
2020-10-12 03:15:22 +00:00
|
|
|
*/
|
2020-10-25 16:29:19 +00:00
|
|
|
public void startGeyser(MinecraftServer server) {
|
|
|
|
this.server = server;
|
|
|
|
|
2022-09-22 23:49:35 +00:00
|
|
|
if (this.geyserConfig.getRemote().address().equalsIgnoreCase("auto")) {
|
2020-10-12 03:15:22 +00:00
|
|
|
this.geyserConfig.setAutoconfiguredRemote(true);
|
2022-10-18 16:06:18 +00:00
|
|
|
String ip = server.getLocalIp();
|
2020-10-25 16:29:19 +00:00
|
|
|
int port = ((GeyserServerPortGetter) server).geyser$getServerPort();
|
2020-10-12 03:15:22 +00:00
|
|
|
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()) {
|
2022-09-22 23:49:35 +00:00
|
|
|
geyserConfig.getBedrock().setPort(geyserConfig.getRemote().port());
|
2020-10-12 03:15:22 +00:00
|
|
|
}
|
2020-10-06 16:15:07 +00:00
|
|
|
|
2021-04-02 00:07:32 +00:00
|
|
|
Optional<ModContainer> floodgate = FabricLoader.getInstance().getModContainer("floodgate");
|
|
|
|
boolean floodgatePresent = floodgate.isPresent();
|
2022-09-22 23:49:35 +00:00
|
|
|
if (geyserConfig.getRemote().authType() == AuthType.FLOODGATE && !floodgatePresent) {
|
2021-12-02 16:08:25 +00:00
|
|
|
geyserLogger.severe(GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.not_installed") + " " + GeyserLocale.getLocaleStringLog("geyser.bootstrap.floodgate.disabling"));
|
2021-04-02 00:07:32 +00:00
|
|
|
return;
|
|
|
|
} else if (geyserConfig.isAutoconfiguredRemote() && floodgatePresent) {
|
|
|
|
// Floodgate installed means that the user wants Floodgate authentication
|
|
|
|
geyserLogger.debug("Auto-setting to Floodgate authentication.");
|
2021-07-28 23:53:08 +00:00
|
|
|
geyserConfig.getRemote().setAuthType(AuthType.FLOODGATE);
|
2021-04-02 00:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
geyserConfig.loadFloodgate(this, floodgate.orElse(null));
|
|
|
|
|
2022-10-24 17:21:02 +00:00
|
|
|
GeyserImpl.start();
|
2020-10-06 17:41:50 +00:00
|
|
|
|
2022-10-24 17:21:02 +00:00
|
|
|
this.geyserPingPassthrough = GeyserLegacyPingPassthrough.init(geyser);
|
2020-10-06 22:43:02 +00:00
|
|
|
|
2022-10-24 17:21:02 +00:00
|
|
|
this.geyserCommandManager = new GeyserCommandManager(geyser);
|
2022-09-23 01:25:46 +00:00
|
|
|
this.geyserCommandManager.init();
|
2020-10-06 16:15:07 +00:00
|
|
|
|
2021-03-14 17:33:53 +00:00
|
|
|
this.geyserWorldManager = new GeyserFabricWorldManager(server);
|
|
|
|
|
2020-10-12 03:15:22 +00:00
|
|
|
// Start command building
|
|
|
|
// Set just "geyser" as the help command
|
2022-10-24 17:21:02 +00:00
|
|
|
GeyserFabricCommandExecutor helpExecutor = new GeyserFabricCommandExecutor(geyser,
|
|
|
|
(GeyserCommand) geyser.commandManager().getCommands().get("help"));
|
2022-10-18 16:06:18 +00:00
|
|
|
LiteralArgumentBuilder<CommandSourceStack> builder = Commands.literal("geyser").executes(helpExecutor);
|
2021-10-31 23:46:51 +00:00
|
|
|
|
|
|
|
// Register all subcommands as valid
|
2022-10-24 17:21:02 +00:00
|
|
|
for (Map.Entry<String, Command> command : geyser.commandManager().getCommands().entrySet()) {
|
|
|
|
GeyserFabricCommandExecutor executor = new GeyserFabricCommandExecutor(geyser, (GeyserCommand) command.getValue());
|
|
|
|
builder.then(Commands.literal(command.getKey())
|
|
|
|
.executes(executor)
|
|
|
|
// Could also test for Bedrock but depending on when this is called it may backfire
|
|
|
|
.requires(executor::testPermission));
|
2020-10-12 03:15:22 +00:00
|
|
|
}
|
2022-10-18 16:06:18 +00:00
|
|
|
server.getCommands().getDispatcher().register(builder);
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2022-10-24 17:21:02 +00:00
|
|
|
if (geyser != null) {
|
|
|
|
geyser.shutdown();
|
|
|
|
geyser = null;
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|
2020-10-25 20:22:50 +00:00
|
|
|
if (!reloading) {
|
|
|
|
this.server = null;
|
|
|
|
}
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserConfiguration getGeyserConfig() {
|
|
|
|
return geyserConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GeyserLogger getGeyserLogger() {
|
|
|
|
return geyserLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-09-22 23:49:35 +00:00
|
|
|
public GeyserCommandManager getGeyserCommandManager() {
|
2020-10-06 16:15:07 +00:00
|
|
|
return geyserCommandManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IGeyserPingPassthrough getGeyserPingPassthrough() {
|
|
|
|
return geyserPingPassthrough;
|
|
|
|
}
|
|
|
|
|
2021-03-14 17:33:53 +00:00
|
|
|
@Override
|
|
|
|
public WorldManager getWorldManager() {
|
|
|
|
return geyserWorldManager;
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:15:07 +00:00
|
|
|
@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
|
|
|
|
2021-02-04 15:17:06 +00:00
|
|
|
@Override
|
|
|
|
public String getMinecraftServerVersion() {
|
2022-10-18 16:06:18 +00:00
|
|
|
return this.server.getServerVersion();
|
2021-02-04 15:17:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 16:08:05 +00:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public InputStream getResourceOrNull(String resource) {
|
|
|
|
// We need to handle this differently, because Fabric shares the classloader across multiple mods
|
2022-11-01 13:59:44 +00:00
|
|
|
Path path = this.mod.findPath(resource).orElse(null);
|
|
|
|
if (path == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-12-03 16:08:05 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
return path.getFileSystem()
|
|
|
|
.provider()
|
|
|
|
.newInputStream(path);
|
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 17:29:15 +00:00
|
|
|
public void setReloading(boolean reloading) {
|
|
|
|
this.reloading = reloading;
|
|
|
|
}
|
|
|
|
|
2020-10-25 16:29:19 +00:00
|
|
|
public static GeyserFabricMod getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
2020-10-06 16:15:07 +00:00
|
|
|
}
|