mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2025-01-10 17:58:05 +01:00
Safely handle bad configuration of 'block-protocols' and 'block-versions' (#2983)
This commit is contained in:
parent
548f80bb7c
commit
34f0dbf642
@ -31,6 +31,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.IntPredicate;
|
import java.util.function.IntPredicate;
|
||||||
|
|
||||||
@ -160,10 +161,12 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BlockedProtocolVersions loadBlockedProtocolVersions() {
|
private BlockedProtocolVersions loadBlockedProtocolVersions() {
|
||||||
IntSet blockedProtocols = new IntOpenHashSet(getIntegerList("block-protocols"));
|
List<Integer> blockProtocols = getListSafe("block-protocols", Integer.class, "Invalid blocked version protocol found in config: '%s'");
|
||||||
|
List<String> blockVersions = getListSafe("block-versions", String.class, "Invalid blocked version found in config: '%s'");
|
||||||
|
IntSet blockedProtocols = new IntOpenHashSet(blockProtocols);
|
||||||
int lowerBound = -1;
|
int lowerBound = -1;
|
||||||
int upperBound = -1;
|
int upperBound = -1;
|
||||||
for (String s : getStringList("block-versions")) {
|
for (String s : blockVersions) {
|
||||||
if (s.isEmpty()) {
|
if (s.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
package com.viaversion.viaversion.util;
|
package com.viaversion.viaversion.util;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
import com.viaversion.viaversion.api.Via;
|
||||||
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
|
import com.viaversion.viaversion.api.configuration.ConfigurationProvider;
|
||||||
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||||
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import com.viaversion.viaversion.libs.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
@ -26,11 +27,7 @@ import org.yaml.snakeyaml.DumperOptions;
|
|||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
import org.yaml.snakeyaml.representer.Representer;
|
import org.yaml.snakeyaml.representer.Representer;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -214,6 +211,23 @@ public abstract class Config implements ConfigurationProvider {
|
|||||||
return o != null ? (List<String>) o : new ArrayList<>();
|
return o != null ? (List<String>) o : new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> List<T> getListSafe(String key, Class<T> type, String invalidValueMessage) {
|
||||||
|
Object o = this.config.get(key);
|
||||||
|
if (o instanceof List) {
|
||||||
|
List<?> list = (List<?>) o;
|
||||||
|
List<T> filteredValues = new ArrayList<>();
|
||||||
|
for (Object o1 : list) {
|
||||||
|
if (type.isInstance(o1)) {
|
||||||
|
filteredValues.add(type.cast(o1));
|
||||||
|
} else if (invalidValueMessage != null) {
|
||||||
|
Via.getPlatform().getLogger().warning(String.format(invalidValueMessage, o1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredValues;
|
||||||
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
public @Nullable JsonElement getSerializedComponent(String key) {
|
public @Nullable JsonElement getSerializedComponent(String key) {
|
||||||
final Object o = this.config.get(key);
|
final Object o = this.config.get(key);
|
||||||
if (o != null && !((String) o).isEmpty()) {
|
if (o != null && !((String) o).isEmpty()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user