Fix welcome message loading

This commit is contained in:
Camotoy 2021-12-03 12:42:31 -05:00
parent 79ce0a146d
commit 5e46415ac9
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 68 additions and 30 deletions

View file

@ -33,10 +33,7 @@ import lombok.Getter;
import org.geysermc.connect.proxy.GeyserProxyBootstrap; import org.geysermc.connect.proxy.GeyserProxyBootstrap;
import org.geysermc.connect.storage.AbstractStorageManager; import org.geysermc.connect.storage.AbstractStorageManager;
import org.geysermc.connect.storage.DisabledStorageManager; import org.geysermc.connect.storage.DisabledStorageManager;
import org.geysermc.connect.utils.Logger; import org.geysermc.connect.utils.*;
import org.geysermc.connect.utils.Player;
import org.geysermc.connect.utils.Server;
import org.geysermc.connect.utils.ServerCategory;
import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.network.MinecraftProtocol; import org.geysermc.geyser.network.MinecraftProtocol;
import org.geysermc.geyser.util.FileUtils; import org.geysermc.geyser.util.FileUtils;
@ -89,7 +86,7 @@ public class MasterServer {
logger = new Logger(); logger = new Logger();
try { try {
File configFile = fileOrCopiedFromResource(new File("config.yml"), "config.yml", (x) -> x); File configFile = GeyserConnectFileUtils.fileOrCopiedFromResource(new File("config.yml"), "config.yml", (x) -> x);
this.geyserConnectConfig = FileUtils.loadConfig(configFile, GeyserConnectConfig.class); this.geyserConnectConfig = FileUtils.loadConfig(configFile, GeyserConnectConfig.class);
} catch (IOException ex) { } catch (IOException ex) {
logger.severe("Failed to read/create config.yml! Make sure it's up to date and/or readable+writable!", ex); logger.severe("Failed to read/create config.yml! Make sure it's up to date and/or readable+writable!", ex);
@ -123,7 +120,7 @@ public class MasterServer {
// Create the base welcome.txt file // Create the base welcome.txt file
try { try {
fileOrCopiedFromResource(new File(getGeyserConnectConfig().getWelcomeFile()), "welcome.txt", (x) -> x); GeyserConnectFileUtils.fileOrCopiedFromResource(new File(getGeyserConnectConfig().getWelcomeFile()), "welcome.txt", (x) -> x);
} catch (IOException ignored) { } } catch (IOException ignored) { }
start(geyserConnectConfig.getPort()); start(geyserConnectConfig.getPort());
@ -211,27 +208,4 @@ public class MasterServer {
public List<Server> getServers(ServerCategory serverCategory) { public List<Server> getServers(ServerCategory serverCategory) {
return getGeyserConnectConfig().getServers().stream().filter(server -> server.getCategory() == serverCategory).collect(Collectors.toList()); return getGeyserConnectConfig().getServers().stream().filter(server -> server.getCategory() == serverCategory).collect(Collectors.toList());
} }
private File fileOrCopiedFromResource(File file, String name, Function<String, String> format) throws IOException {
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
try (FileOutputStream fos = new FileOutputStream(file)) {
try (InputStream input = MasterServer.class.getClassLoader().getResourceAsStream(name)) {
byte[] bytes = new byte[input.available()];
//noinspection ResultOfMethodCallIgnored
input.read(bytes);
for(char c : format.apply(new String(bytes)).toCharArray()) {
fos.write(c);
}
fos.flush();
}
}
}
return file;
}
} }

View file

@ -45,6 +45,7 @@ import com.nukkitx.protocol.bedrock.util.EncryptionUtils;
import com.nukkitx.protocol.bedrock.v471.Bedrock_v471; import com.nukkitx.protocol.bedrock.v471.Bedrock_v471;
import org.geysermc.connect.ui.FormID; import org.geysermc.connect.ui.FormID;
import org.geysermc.connect.ui.UIHandler; import org.geysermc.connect.ui.UIHandler;
import org.geysermc.connect.utils.GeyserConnectFileUtils;
import org.geysermc.connect.utils.Player; import org.geysermc.connect.utils.Player;
import org.geysermc.connect.utils.Server; import org.geysermc.connect.utils.Server;
import org.geysermc.cumulus.Form; import org.geysermc.cumulus.Form;
@ -281,7 +282,7 @@ public class PacketHandler implements BedrockPacketHandler {
String message = ""; String message = "";
try { try {
File messageFile = FileUtils.fileOrCopiedFromResource(new File(MasterServer.getInstance().getGeyserConnectConfig().getWelcomeFile()), "welcome.txt", (x) -> x); File messageFile = GeyserConnectFileUtils.fileOrCopiedFromResource(new File(MasterServer.getInstance().getGeyserConnectConfig().getWelcomeFile()), "welcome.txt", (x) -> x);
message = new String(FileUtils.readAllBytes(messageFile)); message = new String(FileUtils.readAllBytes(messageFile));
} catch (IOException ignored) { } } catch (IOException ignored) { }

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2019-2021 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/GeyserConnect
*/
package org.geysermc.connect.utils;
import org.geysermc.connect.MasterServer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function;
public final class GeyserConnectFileUtils {
public static File fileOrCopiedFromResource(File file, String name, Function<String, String> format) throws IOException {
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
try (FileOutputStream fos = new FileOutputStream(file)) {
try (InputStream input = MasterServer.class.getClassLoader().getResourceAsStream(name)) {
byte[] bytes = new byte[input.available()];
//noinspection ResultOfMethodCallIgnored
input.read(bytes);
for(char c : format.apply(new String(bytes)).toCharArray()) {
fos.write(c);
}
fos.flush();
}
}
}
return file;
}
private GeyserConnectFileUtils() {
}
}