Update for Geyser usage

This fixes https://github.com/GeyserMC/Floodgate-Fabric/issues/7 on the Geyser-Fabric end. Floodgate-Fabric still needs to be fixed.
This commit is contained in:
Camotoy 2021-12-03 11:08:05 -05:00
parent 257e04fa6f
commit dd632ef4b1

View file

@ -51,6 +51,7 @@ import org.geysermc.geyser.util.FileUtils;
import org.geysermc.platform.fabric.command.GeyserFabricCommandExecutor; import org.geysermc.platform.fabric.command.GeyserFabricCommandExecutor;
import org.geysermc.platform.fabric.command.GeyserFabricCommandManager; import org.geysermc.platform.fabric.command.GeyserFabricCommandManager;
import org.geysermc.platform.fabric.world.GeyserFabricWorldManager; import org.geysermc.platform.fabric.world.GeyserFabricWorldManager;
import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -66,6 +67,7 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
private boolean reloading; private boolean reloading;
private GeyserImpl connector; private GeyserImpl connector;
private ModContainer mod;
private Path dataFolder; private Path dataFolder;
private MinecraftServer server; private MinecraftServer server;
@ -84,6 +86,7 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
@Override @Override
public void onInitialize() { public void onInitialize() {
instance = this; instance = this;
mod = FabricLoader.getInstance().getModContainer("geyser-fabric").orElseThrow();
this.onEnable(); this.onEnable();
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) { if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
@ -94,6 +97,8 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
@Override @Override
public void onEnable() { public void onEnable() {
GeyserLocale.init(this);
dataFolder = FabricLoader.getInstance().getConfigDir().resolve("Geyser-Fabric"); dataFolder = FabricLoader.getInstance().getConfigDir().resolve("Geyser-Fabric");
if (!dataFolder.toFile().exists()) { if (!dataFolder.toFile().exists()) {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
@ -101,7 +106,7 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
} }
try { try {
File configFile = FileUtils.fileOrCopiedFromResource(dataFolder.resolve("config.yml").toFile(), "config.yml", File configFile = FileUtils.fileOrCopiedFromResource(dataFolder.resolve("config.yml").toFile(), "config.yml",
(x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()), this);
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserFabricConfiguration.class); this.geyserConfig = FileUtils.loadConfig(configFile, GeyserFabricConfiguration.class);
File permissionsFile = fileOrCopiedFromResource(dataFolder.resolve("permissions.yml").toFile(), "permissions.yml"); File permissionsFile = fileOrCopiedFromResource(dataFolder.resolve("permissions.yml").toFile(), "permissions.yml");
this.playerCommands = Arrays.asList(FileUtils.loadConfig(permissionsFile, GeyserFabricPermissions.class).getCommands()); this.playerCommands = Arrays.asList(FileUtils.loadConfig(permissionsFile, GeyserFabricPermissions.class).getCommands());
@ -238,6 +243,21 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
return this.server.getVersion(); return this.server.getVersion();
} }
@Nullable
@Override
public InputStream getResourceOrNull(String resource) {
// We need to handle this differently, because Fabric shares the classloader across multiple mods
Path path = this.mod.getPath(resource);
try {
return path.getFileSystem()
.provider()
.newInputStream(path);
} catch (IOException e) {
return null;
}
}
public void setReloading(boolean reloading) { public void setReloading(boolean reloading) {
this.reloading = reloading; this.reloading = reloading;
} }
@ -247,7 +267,7 @@ public class GeyserFabricMod implements ModInitializer, GeyserBootstrap {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
file.createNewFile(); file.createNewFile();
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
InputStream input = GeyserFabricMod.class.getResourceAsStream("/" + name); // resources need leading "/" prefix InputStream input = getResource(name);
byte[] bytes = new byte[input.available()]; byte[] bytes = new byte[input.available()];