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