13
0
geforkt von Mirrors/Velocity

Create temp default config for getting defaults

Dieser Commit ist enthalten in:
Frank van der Heijden 2020-08-19 23:30:34 +02:00
Ursprung 62fac7db32
Commit e3fe538491
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 26DA56488D314D11
3 geänderte Dateien mit 52 neuen und 23 gelöschten Zeilen

Datei anzeigen

@ -3,22 +3,28 @@ package com.velocitypowered.proxy.config;
import com.electronwill.nightconfig.core.CommentedConfig; import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.UnmodifiableConfig; import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.toml.TomlFormat;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.velocitypowered.api.proxy.config.ProxyConfig; import com.velocitypowered.api.proxy.config.ProxyConfig;
import com.velocitypowered.api.util.Favicon; import com.velocitypowered.api.util.Favicon;
import com.velocitypowered.proxy.util.AddressUtil; import com.velocitypowered.proxy.util.AddressUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
@ -392,15 +398,28 @@ public class VelocityConfiguration implements ProxyConfig {
* @throws IOException if we could not read from the {@code path}. * @throws IOException if we could not read from the {@code path}.
*/ */
public static VelocityConfiguration read(Path path) throws IOException { public static VelocityConfiguration read(Path path) throws IOException {
String defaultResource = "default-velocity.toml";
boolean mustResave = false; boolean mustResave = false;
CommentedFileConfig config = CommentedFileConfig.builder(path) CommentedFileConfig config = CommentedFileConfig.builder(path)
.defaultResource("/default-velocity.toml") .defaultResource(defaultResource)
.autosave() .autosave()
.preserveInsertionOrder() .preserveInsertionOrder()
.sync() .sync()
.build(); .build();
config.load(); config.load();
// Create temporary default configuration
File tmpFile = File.createTempFile(defaultResource, null);
tmpFile.deleteOnExit();
// Copy over default file to tmp location
ClassLoader loader = VelocityConfiguration.class.getClassLoader();
try (InputStream in = loader.getResourceAsStream(defaultResource)) {
Files.copy(Objects.requireNonNull(in), tmpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
CommentedFileConfig defaultConfig = CommentedFileConfig.of(tmpFile, TomlFormat.instance());
defaultConfig.load();
// Handle any cases where the config needs to be saved again // Handle any cases where the config needs to be saved again
byte[] forwardingSecret; byte[] forwardingSecret;
String forwardingSecretString = config.get("forwarding-secret"); String forwardingSecretString = config.get("forwarding-secret");
@ -454,7 +473,7 @@ public class VelocityConfiguration implements ProxyConfig {
new Advanced(advancedConfig), new Advanced(advancedConfig),
new Query(queryConfig), new Query(queryConfig),
new Metrics(metricsConfig), new Metrics(metricsConfig),
new Messages(messagesConfig) new Messages(messagesConfig, defaultConfig.get("messages"))
); );
} }
@ -793,25 +812,32 @@ public class VelocityConfiguration implements ProxyConfig {
} }
public static class Messages { public static class Messages {
private String kickPrefix = "&cKicked from %s: ";
private String disconnectPrefix = "&cCan't connect to %s: ";
private String onlineModeOnly = "&cThis server only accepts connections from online-mode clients."
+ "\n\n&7Did you change your username? Sign out of Minecraft, sign back in, and try again.";
private String noAvailableServers = "&cThere are no available servers.";
private String alreadyConnected = "&cYou are already connected to this proxy!";
private String movedToNewServerPrefix = "&cThe server you were on kicked you: ";
private String genericConnectionError = "&cAn internal error occurred in your connection.";
private Messages(CommentedConfig toml) { private final CommentedConfig toml;
if (toml != null) { private final CommentedConfig defaultToml;
this.kickPrefix = toml.getOrElse("kick-prefix", kickPrefix);
this.disconnectPrefix = toml.getOrElse("disconnect-prefix", disconnectPrefix); private final String kickPrefix;
this.onlineModeOnly = toml.getOrElse("online-mode-only", onlineModeOnly); private final String disconnectPrefix;
this.noAvailableServers = toml.getOrElse("no-available-servers", noAvailableServers); private final String onlineModeOnly;
this.alreadyConnected = toml.getOrElse("already-connected", alreadyConnected); private final String noAvailableServers;
this.movedToNewServerPrefix = toml.getOrElse("moved-to-new-server-prefix", movedToNewServerPrefix); private final String alreadyConnected;
this.genericConnectionError = toml.getOrElse("generic-connection-error", genericConnectionError); private final String movedToNewServerPrefix;
private final String genericConnectionError;
private Messages(CommentedConfig toml, CommentedConfig defaultToml) {
this.toml = toml;
this.defaultToml = defaultToml;
this.kickPrefix = getString("kick-prefix");
this.disconnectPrefix = getString("disconnect-prefix");
this.onlineModeOnly = getString("online-mode-only");
this.noAvailableServers = getString("no-available-servers");
this.alreadyConnected = getString("already-connected");
this.movedToNewServerPrefix = getString("moved-to-new-server-prefix");
this.genericConnectionError = getString("generic-connection-error");
} }
private String getString(String path) {
return toml.getOrElse(path, defaultToml.getOrElse(path, ""));
} }
public Component getKickPrefix(String server) { public Component getKickPrefix(String server) {

Datei anzeigen

@ -598,7 +598,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
disconnect(friendlyReason); disconnect(friendlyReason);
} else { } else {
if (res.getMessageComponent() == null) { if (res.getMessageComponent() == null) {
sendMessage(server.getConfiguration().getMessages().getMovedToNewServerPrefix().append(friendlyReason)); sendMessage(server.getConfiguration().getMessages()
.getMovedToNewServerPrefix().append(friendlyReason));
} else { } else {
sendMessage(res.getMessageComponent()); sendMessage(res.getMessageComponent());
} }

Datei anzeigen

@ -274,7 +274,8 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
player.disconnect0(reason.get(), true); player.disconnect0(reason.get(), true);
} else { } else {
if (!server.registerConnection(player)) { if (!server.registerConnection(player)) {
player.disconnect0(server.getConfiguration().getMessages().getAlreadyConnected(), true); player.disconnect0(server.getConfiguration().getMessages()
.getAlreadyConnected(), true);
return; return;
} }
@ -294,7 +295,8 @@ public class LoginSessionHandler implements MinecraftSessionHandler {
.thenRunAsync(() -> { .thenRunAsync(() -> {
Optional<RegisteredServer> toTry = event.getInitialServer(); Optional<RegisteredServer> toTry = event.getInitialServer();
if (!toTry.isPresent()) { if (!toTry.isPresent()) {
player.disconnect0(server.getConfiguration().getMessages().getNoAvailableServers(), true); player.disconnect0(server.getConfiguration().getMessages()
.getNoAvailableServers(), true);
return; return;
} }
player.createConnectionRequest(toTry.get()).fireAndForget(); player.createConnectionRequest(toTry.get()).fireAndForget();