Provide a platform independent method of retrieving the datafolder (#769)

* Provide a platform independent method of retrieving the datafolder

* LocaleUtils now uses datafolder

* Make use of Path instead of File

Changes:
* Rename getDataFolder() to getConfigFile() and update to return a Path in each bootstrap

* Rename filePath to tmpFilePath

* Update Velocity configFile to configFile Path
This commit is contained in:
bundabrg 2020-06-21 01:54:40 +08:00 committed by GitHub
parent 100d7b7759
commit e66f57f9f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 12 deletions

View file

@ -44,6 +44,7 @@ import us.myles.ViaVersion.api.Via;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -154,6 +155,11 @@ public class GeyserBukkitPlugin extends JavaPlugin implements GeyserBootstrap {
return this.geyserWorldManager; return this.geyserWorldManager;
} }
@Override
public Path getConfigFolder() {
return getDataFolder().toPath();
}
public boolean isCompatible(String version, String whichVersion) { public boolean isCompatible(String version, String whichVersion) {
int[] currentVersion = parseVersion(version); int[] currentVersion = parseVersion(version);
int[] otherVersion = parseVersion(whichVersion); int[] otherVersion = parseVersion(whichVersion);
@ -187,4 +193,5 @@ public class GeyserBukkitPlugin extends JavaPlugin implements GeyserBootstrap {
} }
return temp; return temp;
} }
} }

View file

@ -44,6 +44,7 @@ import org.geysermc.platform.bungeecord.command.GeyserBungeeCommandManager;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -134,4 +135,9 @@ public class GeyserBungeePlugin extends Plugin implements GeyserBootstrap {
public IGeyserPingPassthrough getGeyserPingPassthrough() { public IGeyserPingPassthrough getGeyserPingPassthrough() {
return geyserBungeePingPassthrough; return geyserBungeePingPassthrough;
} }
@Override
public Path getConfigFolder() {
return getDataFolder().toPath();
}
} }

View file

@ -50,6 +50,7 @@ import org.spongepowered.api.plugin.Plugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
@Plugin(id = "geyser", name = GeyserConnector.NAME + "-Sponge", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC") @Plugin(id = "geyser", name = GeyserConnector.NAME + "-Sponge", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC")
@ -147,6 +148,11 @@ public class GeyserSpongePlugin implements GeyserBootstrap {
return geyserSpongePingPassthrough; return geyserSpongePingPassthrough;
} }
@Override
public Path getConfigFolder() {
return configDir.toPath();
}
@Listener @Listener
public void onServerStart(GameStartedServerEvent event) { public void onServerStart(GameStartedServerEvent event) {
onEnable(); onEnable();

View file

@ -37,6 +37,8 @@ import org.geysermc.platform.standalone.command.GeyserCommandManager;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID; import java.util.UUID;
public class GeyserStandaloneBootstrap implements GeyserBootstrap { public class GeyserStandaloneBootstrap implements GeyserBootstrap {
@ -100,4 +102,10 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
public IGeyserPingPassthrough getGeyserPingPassthrough() { public IGeyserPingPassthrough getGeyserPingPassthrough() {
return geyserPingPassthrough; return geyserPingPassthrough;
} }
@Override
public Path getConfigFolder() {
// Return the current working directory
return Paths.get(System.getProperty("user.dir"));
}
} }

View file

@ -34,6 +34,7 @@ import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import lombok.Getter;
import org.geysermc.common.PlatformType; import org.geysermc.common.PlatformType;
import org.geysermc.connector.configuration.GeyserConfiguration; import org.geysermc.connector.configuration.GeyserConfiguration;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
@ -48,6 +49,8 @@ import org.slf4j.Logger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID; import java.util.UUID;
@Plugin(id = "geyser", name = GeyserConnector.NAME + "-Velocity", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC") @Plugin(id = "geyser", name = GeyserConnector.NAME + "-Velocity", version = GeyserConnector.VERSION, url = "https://geysermc.org", authors = "GeyserMC")
@ -69,14 +72,16 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
private GeyserConnector connector; private GeyserConnector connector;
@Getter
private final Path configFolder = Paths.get("plugins/" + GeyserConnector.NAME + "-Velocity/");
@Override @Override
public void onEnable() { public void onEnable() {
File configDir = new File("plugins/" + GeyserConnector.NAME + "-Velocity/");
try { try {
if (!configDir.exists()) if (!configFolder.toFile().exists())
configDir.mkdir(); //noinspection ResultOfMethodCallIgnored
File configFile = FileUtils.fileOrCopiedFromResource(new File(configDir, "config.yml"), "config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); configFolder.toFile().mkdirs();
File configFile = FileUtils.fileOrCopiedFromResource(configFolder.resolve("config.yml").toFile(), "config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserVelocityConfiguration.class); this.geyserConfig = FileUtils.loadConfig(configFile, GeyserVelocityConfiguration.class);
} catch (IOException ex) { } catch (IOException ex) {
logger.warn("Failed to read/create config.yml! Make sure it's up to date and/or readable+writable!", ex); logger.warn("Failed to read/create config.yml! Make sure it's up to date and/or readable+writable!", ex);
@ -101,7 +106,7 @@ public class GeyserVelocityPlugin implements GeyserBootstrap {
return; return;
} }
geyserConfig.loadFloodgate(this, proxyServer, configDir); geyserConfig.loadFloodgate(this, proxyServer, configFolder.toFile());
this.connector = GeyserConnector.start(PlatformType.VELOCITY, this); this.connector = GeyserConnector.start(PlatformType.VELOCITY, this);

View file

@ -33,6 +33,8 @@ import org.geysermc.connector.command.CommandManager;
import org.geysermc.connector.network.translators.world.CachedChunkManager; import org.geysermc.connector.network.translators.world.CachedChunkManager;
import org.geysermc.connector.network.translators.world.WorldManager; import org.geysermc.connector.network.translators.world.WorldManager;
import java.nio.file.Path;
public interface GeyserBootstrap { public interface GeyserBootstrap {
CachedChunkManager DEFAULT_CHUNK_MANAGER = new CachedChunkManager(); CachedChunkManager DEFAULT_CHUNK_MANAGER = new CachedChunkManager();
@ -83,4 +85,11 @@ public interface GeyserBootstrap {
default WorldManager getWorldManager() { default WorldManager getWorldManager() {
return DEFAULT_CHUNK_MANAGER; return DEFAULT_CHUNK_MANAGER;
} }
/**
* Return the data folder where files get stored
*
* @return Path location of data folder
*/
Path getConfigFolder();
} }

View file

@ -33,6 +33,7 @@ import org.geysermc.connector.GeyserConnector;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.*; import java.util.*;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
@ -49,7 +50,8 @@ public class LocaleUtils {
static { static {
// Create the locales folder // Create the locales folder
File localesFolder = new File("locales/"); File localesFolder = GeyserConnector.getInstance().getBootstrap().getConfigFolder().resolve("locales").toFile();
//noinspection ResultOfMethodCallIgnored
localesFolder.mkdir(); localesFolder.mkdir();
// Download the latest asset list and cache it // Download the latest asset list and cache it
@ -132,7 +134,7 @@ public class LocaleUtils {
* @param locale Locale to download * @param locale Locale to download
*/ */
private static void downloadLocale(String locale) { private static void downloadLocale(String locale) {
File localeFile = new File("locales/" + locale + ".json"); File localeFile = Paths.get(GeyserConnector.getInstance().getBootstrap().getConfigFolder().toString(),"locales",locale + ".json").toFile();
// Check if we have already downloaded the locale file // Check if we have already downloaded the locale file
if (localeFile.exists()) { if (localeFile.exists()) {
@ -149,7 +151,7 @@ public class LocaleUtils {
// Get the hash and download the locale // Get the hash and download the locale
String hash = ASSET_MAP.get("minecraft/lang/" + locale + ".json").getHash(); String hash = ASSET_MAP.get("minecraft/lang/" + locale + ".json").getHash();
WebUtils.downloadFile("http://resources.download.minecraft.net/" + hash.substring(0, 2) + "/" + hash, "locales/" + locale + ".json"); WebUtils.downloadFile("http://resources.download.minecraft.net/" + hash.substring(0, 2) + "/" + hash, localeFile.toString());
} }
/** /**
@ -205,10 +207,11 @@ public class LocaleUtils {
GeyserConnector.getInstance().getLogger().debug("Download URL: " + smallestURL); GeyserConnector.getInstance().getLogger().debug("Download URL: " + smallestURL);
// Download the smallest JAR (client or server) // Download the smallest JAR (client or server)
WebUtils.downloadFile(smallestURL, "tmp_locale.jar"); Path tmpFilePath = GeyserConnector.getInstance().getBootstrap().getConfigFolder().resolve("tmp_locale.jar");
WebUtils.downloadFile(smallestURL, tmpFilePath.toString());
// Load in the JAR as a zip and extract the file // Load in the JAR as a zip and extract the file
ZipFile localeJar = new ZipFile("tmp_locale.jar"); ZipFile localeJar = new ZipFile(tmpFilePath.toString());
InputStream inputStream = localeJar.getInputStream(localeJar.getEntry("assets/minecraft/lang/en_us.json")); InputStream inputStream = localeJar.getInputStream(localeJar.getEntry("assets/minecraft/lang/en_us.json"));
FileOutputStream outputStream = new FileOutputStream(localeFile); FileOutputStream outputStream = new FileOutputStream(localeFile);
@ -227,7 +230,7 @@ public class LocaleUtils {
localeJar.close(); localeJar.close();
// Delete the nolonger needed client/server jar // Delete the nolonger needed client/server jar
Files.delete(Paths.get("tmp_locale.jar")); Files.delete(tmpFilePath);
} catch (Exception e) { } catch (Exception e) {
throw new AssertionError("Unable to download and extract en_us locale!", e); throw new AssertionError("Unable to download and extract en_us locale!", e);
} }