forked from SteamWar/SteamWar
Improve YMLWrapper
This commit is contained in:
@@ -25,6 +25,7 @@ import java.io.File;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -55,13 +56,23 @@ public abstract class YMLWrapper<M, ST, W> {
|
|||||||
|
|
||||||
public abstract String getDefaultGameName();
|
public abstract String getDefaultGameName();
|
||||||
|
|
||||||
public abstract String getString(String path, String defaultValue);
|
public abstract <T> T get(String path, T defaultValue, Function<Object, T> mapper);
|
||||||
|
|
||||||
public abstract int getInt(String path, int defaultValue);
|
public String getString(String path, String defaultValue) {
|
||||||
|
return get(path, defaultValue, Objects::toString);
|
||||||
|
}
|
||||||
|
|
||||||
public abstract double getDouble(String path, double defaultValue);
|
public int getInt(String path, int defaultValue) {
|
||||||
|
return get(path, defaultValue, Integer.class::cast);
|
||||||
|
}
|
||||||
|
|
||||||
public abstract boolean getBoolean(String path, boolean defaultValue);
|
public double getDouble(String path, double defaultValue) {
|
||||||
|
return get(path, defaultValue, Double.class::cast);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getBoolean(String path, boolean defaultValue) {
|
||||||
|
return get(path, defaultValue, Boolean.class::cast);
|
||||||
|
}
|
||||||
|
|
||||||
public final ST getSchematicType(String path, String defaultValue) {
|
public final ST getSchematicType(String path, String defaultValue) {
|
||||||
String schematicType = getString(path, defaultValue);
|
String schematicType = getString(path, defaultValue);
|
||||||
|
|||||||
-1
@@ -36,7 +36,6 @@ public class CheckSchemTypeManager {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
File folder = new File(SchematicSystem.getInstance().getDataFolder().getParentFile(), "FightSystem");
|
File folder = new File(SchematicSystem.getInstance().getDataFolder().getParentFile(), "FightSystem");
|
||||||
|
|
||||||
if(folder.exists()) {
|
if(folder.exists()) {
|
||||||
for(File configFile : folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))) {
|
for(File configFile : folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))) {
|
||||||
GameModeConfig<Material, SchematicType, String> gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofTyped(configFile));
|
GameModeConfig<Material, SchematicType, String> gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofTyped(configFile));
|
||||||
|
|||||||
@@ -101,27 +101,13 @@ public class YMLWrapperImpl<M, ST, W> extends YMLWrapper<M, ST, W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String path, String defaultValue) {
|
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
|
||||||
if (config == null) return defaultValue;
|
if (config == null) return defaultValue;
|
||||||
return config.getString(pathPrefix + path, defaultValue);
|
try {
|
||||||
}
|
return mapper.apply(config.get(path, defaultValue));
|
||||||
|
} catch (ClassCastException e) {
|
||||||
@Override
|
return defaultValue;
|
||||||
public int getInt(String path, int defaultValue) {
|
}
|
||||||
if (config == null) return defaultValue;
|
|
||||||
return config.getInt(pathPrefix + path, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getDouble(String path, double defaultValue) {
|
|
||||||
if (config == null) return defaultValue;
|
|
||||||
return config.getDouble(pathPrefix + path, defaultValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getBoolean(String path, boolean defaultValue) {
|
|
||||||
if (config == null) return defaultValue;
|
|
||||||
return config.getBoolean(pathPrefix + path, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -88,41 +88,12 @@ public class YMLWrapperImpl<ST> extends YMLWrapper<String, ST, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getString(String path, String defaultValue) {
|
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
|
||||||
Object value = this.document.get(path);
|
Object value = this.document.get(path);
|
||||||
if (value instanceof String) {
|
if (value == null) return defaultValue;
|
||||||
return (String) value;
|
try {
|
||||||
} else {
|
return mapper.apply(value);
|
||||||
return defaultValue;
|
} catch (ClassCastException e) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getInt(String path, int defaultValue) {
|
|
||||||
Object value = this.document.get(path);
|
|
||||||
if (value instanceof Integer) {
|
|
||||||
return (Integer) value;
|
|
||||||
} else {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getDouble(String path, double defaultValue) {
|
|
||||||
Object value = this.document.get(path);
|
|
||||||
if (value instanceof Double) {
|
|
||||||
return (Double) value;
|
|
||||||
} else {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean getBoolean(String path, boolean defaultValue) {
|
|
||||||
Object value = this.document.get(path);
|
|
||||||
if (value instanceof Boolean) {
|
|
||||||
return (Boolean) value;
|
|
||||||
} else {
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user