From d8f8f6532a26ab15c68345453a8d9ca818fdb8e2 Mon Sep 17 00:00:00 2001 From: Draycia Date: Mon, 7 Dec 2020 23:15:10 -0800 Subject: [PATCH] Context changes --- .../java/net/draycia/carbon/api/Context.java | 77 +++++++++++++++---- .../carbon/api/config/ChannelOptions.java | 55 +++---------- 2 files changed, 72 insertions(+), 60 deletions(-) diff --git a/CarbonChat-API/src/main/java/net/draycia/carbon/api/Context.java b/CarbonChat-API/src/main/java/net/draycia/carbon/api/Context.java index f36e869c..bac3c69a 100644 --- a/CarbonChat-API/src/main/java/net/draycia/carbon/api/Context.java +++ b/CarbonChat-API/src/main/java/net/draycia/carbon/api/Context.java @@ -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 List asList() { - return (List) this.value(); + public List asList() { + return Arrays.asList(this.value.split(",")); } public Number asNumber() { - return (Number) this.value(); + return Double.valueOf(this.value()); } } diff --git a/CarbonChat-API/src/main/java/net/draycia/carbon/api/config/ChannelOptions.java b/CarbonChat-API/src/main/java/net/draycia/carbon/api/config/ChannelOptions.java index 730c5cf4..c0e06f46 100644 --- a/CarbonChat-API/src/main/java/net/draycia/carbon/api/config/ChannelOptions.java +++ b/CarbonChat-API/src/main/java/net/draycia/carbon/api/config/ChannelOptions.java @@ -148,6 +148,7 @@ public final class ChannelOptions { settings.defaultFormatName = "default"; settings.formats = Collections.singletonMap("default", "<> "); 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 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 contexts() { + public @NonNull Map contexts() { if (this.contexts == null) { return this.defaultOptions().contexts(); } @@ -194,18 +189,12 @@ public final class ChannelOptions { return this.contexts; } - public @Nullable Map contextsAndDefault() { + public @NonNull Map contextsAndDefault() { if (this.contexts == null) { return this.defaultOptions().contexts(); } - final Map defaultContexts = this.defaultOptions().contexts(); - - if (defaultContexts == null) { - return this.contexts; - } - - final Map contexts = new HashMap<>(defaultContexts); + final Map 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 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 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 defaultFormats = this.defaultOptions().formats(); - - if (defaultFormats == null) { - return this.formats; - } - - final Map formats = new HashMap<>(defaultFormats); + final Map 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(); }