Context changes

This commit is contained in:
Draycia committed 2020-12-07 23:15:10 -08:00
1 parent b086e4354e
commit d8f8f6532a
2 files changed
+72 -60

No files matched your search

@@ -4,15 +4,16 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
import java.util.Arrays;
import java.util.List;
@ConfigSerializable
public class Context {
@Setting private final @NonNull String key;
@Setting private final @NonNull Object value;
@Setting private final @NonNull String value;
public Context(final @NonNull String key, final @NonNull Object value) {
public Context(final @NonNull String key, final @NonNull String value) {
this.key = key;
this.value = value;
}
@@ -21,40 +22,76 @@ public class Context {
return this.key;
}
public @NonNull Object value() {
public @NonNull String value() {
return this.value;
}
public boolean isFloat() {
return this.value instanceof Float;
try {
Float.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isDouble() {
return this.value() instanceof Double;
try {
Double.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isByte() {
return this.value() instanceof Byte;
try {
Byte.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isInteger() {
return this.value() instanceof Integer;
try {
Integer.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isLong() {
return this.value() instanceof Long;
try {
Long.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isString() {
return this.value() instanceof String;
return true;
}
public boolean isBoolean() {
return this.value() instanceof Boolean;
try {
Boolean.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public boolean isList() {
return this.value() instanceof List;
return true;
}
public boolean isListChecked(final Class<?> type) {
@@ -76,23 +113,29 @@ public class Context {
}
public boolean isNumber() {
return this.value() instanceof Number;
try {
Double.valueOf(this.value);
} catch (final NumberFormatException ignored) {
return false;
}
return true;
}
public String asString() {
return (String) this.value();
return this.value();
}
public Boolean asBoolean() {
return (Boolean) this.value();
return Boolean.valueOf(this.value());
}
public <T> List<T> asList() {
return (List<T>) this.value();
public List<String> asList() {
return Arrays.asList(this.value.split(","));
}
public Number asNumber() {
return (Number) this.value();
return Double.valueOf(this.value());
}
}
@@ -148,6 +148,7 @@ public final class ChannelOptions {
settings.defaultFormatName = "default";
settings.formats = Collections.singletonMap("default", "<color><<displayname><reset><color>> <message>");
settings.aliases = Lists.newArrayList("global", "g");
settings.contexts = Collections.singletonMap("distance", new Context("distance", "100"));
return settings;
}
@@ -156,7 +157,7 @@ public final class ChannelOptions {
return this.key;
}
public @Nullable String color() {
public @NonNull String color() {
if (this.color == null) {
return this.defaultOptions().color();
}
@@ -174,19 +175,13 @@ public final class ChannelOptions {
}
if (localContext == null) {
final Map<String, Context> defaultContexts = this.defaultOptions().contexts();
if (defaultContexts != null) {
return defaultContexts.get(key);
} else {
return null;
}
return this.defaultOptions().contexts().get(key);
}
return localContext;
}
public @Nullable Map<String, Context> contexts() {
public @NonNull Map<String, Context> contexts() {
if (this.contexts == null) {
return this.defaultOptions().contexts();
}
@@ -194,18 +189,12 @@ public final class ChannelOptions {
return this.contexts;
}
public @Nullable Map<String, Context> contextsAndDefault() {
public @NonNull Map<String, Context> contextsAndDefault() {
if (this.contexts == null) {
return this.defaultOptions().contexts();
}
final Map<String, Context> defaultContexts = this.defaultOptions().contexts();
if (defaultContexts == null) {
return this.contexts;
}
final Map<String, Context> contexts = new HashMap<>(defaultContexts);
final Map<String, Context> contexts = new HashMap<>(this.defaultOptions().contexts());
if (this.contexts != null) {
contexts.putAll(this.contexts);
@@ -215,30 +204,16 @@ public final class ChannelOptions {
}
public @Nullable String format(final @NonNull String key) {
final String localFormat;
final Map<String, String> localFormats = this.formats();
if (localFormats != null) {
localFormat = localFormats.get(key);
} else {
localFormat = null;
}
final String localFormat = this.formats().get(key);
if (localFormat == null) {
final Map<String, String> defaultFormats = this.defaultOptions().formats();
if (defaultFormats != null) {
return defaultFormats.get(key);
} else {
return null;
}
return this.defaultOptions().formats().get(key);
}
return localFormat;
}
public @Nullable Map<@NonNull String, @NonNull String> formats() {
public @NonNull Map<@NonNull String, @NonNull String> formats() {
if (this.formats == null) {
return this.defaultOptions().formats();
}
@@ -246,18 +221,12 @@ public final class ChannelOptions {
return this.formats;
}
public @Nullable Map<@NonNull String, @NonNull String> formatsAndDefault() {
public @NonNull Map<@NonNull String, @NonNull String> formatsAndDefault() {
if (this.formats == null) {
return this.defaultOptions().formats();
}
final Map<String, String> defaultFormats = this.defaultOptions().formats();
if (defaultFormats == null) {
return this.formats;
}
final Map<String, String> formats = new HashMap<>(defaultFormats);
final Map<String, String> formats = new HashMap<>(this.defaultOptions().formats());
if (this.formats != null) {
formats.putAll(this.formats);
@@ -266,7 +235,7 @@ public final class ChannelOptions {
return formats;
}
public @Nullable String defaultFormatName() {
public @NonNull String defaultFormatName() {
if (this.defaultFormatName == null || this.defaultFormatName.isEmpty()) {
return this.defaultOptions().defaultFormatName();
}