diff --git a/src/client/java/net/vinrobot/mcemote/client/MinecraftEmoteModClient.java b/src/client/java/net/vinrobot/mcemote/client/MinecraftEmoteModClient.java index af1a5a4..d81fc9a 100644 --- a/src/client/java/net/vinrobot/mcemote/client/MinecraftEmoteModClient.java +++ b/src/client/java/net/vinrobot/mcemote/client/MinecraftEmoteModClient.java @@ -8,6 +8,8 @@ import net.vinrobot.mcemote.client.providers.STVGlobalEmoteProvider; import net.vinrobot.mcemote.client.providers.STVUserEmoteProvider; import net.vinrobot.mcemote.client.text.EmotesManager; +import net.vinrobot.mcemote.config.Configuration; +import net.vinrobot.mcemote.config.impl.ConfigurationImpl; import webpdecoderjn.WebPLoader; import java.io.IOException; @@ -16,7 +18,6 @@ public class MinecraftEmoteModClient implements ClientModInitializer { public static final EmotesManager EMOTES_MANAGER = new EmotesManager(); - public static final String SCRAPIE_TWITCH_ID = "40646018"; @Override public void onInitializeClient() { @@ -27,10 +28,11 @@ public void onInitializeClient() { MinecraftEmoteMod.LOGGER.error("Failed to initialize WebPDecoder", e); } + final Configuration config = new ConfigurationImpl(); final IEmoteProvider[] providers = new IEmoteProvider[]{ new STVGlobalEmoteProvider(), - new STVUserEmoteProvider(SCRAPIE_TWITCH_ID), - new FFZRoomEmoteProvider(SCRAPIE_TWITCH_ID), + new STVUserEmoteProvider(), + new FFZRoomEmoteProvider(), }; int codePoint = 100; @@ -42,7 +44,7 @@ public void onInitializeClient() { // "Register" emotes in a temporary list // If an exception is thrown, the emotes will not be added to the manager final List emotes = new ArrayList<>(); - provider.registerEmotes(emotes::add); + provider.registerEmotes(config, emotes::add); for (final Emote emote : emotes) { EMOTES_MANAGER.addEmote(codePoint++, emote); diff --git a/src/client/java/net/vinrobot/mcemote/client/providers/FFZRoomEmoteProvider.java b/src/client/java/net/vinrobot/mcemote/client/providers/FFZRoomEmoteProvider.java index e75477a..5074dac 100644 --- a/src/client/java/net/vinrobot/mcemote/client/providers/FFZRoomEmoteProvider.java +++ b/src/client/java/net/vinrobot/mcemote/client/providers/FFZRoomEmoteProvider.java @@ -3,21 +3,21 @@ import net.vinrobot.mcemote.api.FrankerFaceZService; import net.vinrobot.mcemote.api.ffz.Platform; import net.vinrobot.mcemote.client.font.impl.FFZEmote; +import net.vinrobot.mcemote.config.Configuration; import java.io.IOException; public class FFZRoomEmoteProvider implements IEmoteProvider { - private final String userId; - - public FFZRoomEmoteProvider(String userId) { - this.userId = userId; - } - @Override - public void registerEmotes(IEmoteRegistry registry) throws IOException, InterruptedException { + public void registerEmotes(Configuration config, IEmoteRegistry registry) throws IOException, InterruptedException { + final String twitchId = config.twitchId().get(); + if (twitchId.isEmpty()) { + return; + } + final FrankerFaceZService service = new FrankerFaceZService(); - service.fetchRoom(Platform.TWITCH, this.userId) + service.fetchRoom(Platform.TWITCH, twitchId) .sets().values().stream() .flatMap(emoteSet -> emoteSet.emoticons().stream()) .map(FFZEmote::new) diff --git a/src/client/java/net/vinrobot/mcemote/client/providers/IEmoteProvider.java b/src/client/java/net/vinrobot/mcemote/client/providers/IEmoteProvider.java index 737cd35..82d8e84 100644 --- a/src/client/java/net/vinrobot/mcemote/client/providers/IEmoteProvider.java +++ b/src/client/java/net/vinrobot/mcemote/client/providers/IEmoteProvider.java @@ -1,5 +1,7 @@ package net.vinrobot.mcemote.client.providers; +import net.vinrobot.mcemote.config.Configuration; + public interface IEmoteProvider { - void registerEmotes(IEmoteRegistry registry) throws Exception; + void registerEmotes(Configuration config, IEmoteRegistry registry) throws Exception; } diff --git a/src/client/java/net/vinrobot/mcemote/client/providers/STVGlobalEmoteProvider.java b/src/client/java/net/vinrobot/mcemote/client/providers/STVGlobalEmoteProvider.java index b86eda1..0e354a7 100644 --- a/src/client/java/net/vinrobot/mcemote/client/providers/STVGlobalEmoteProvider.java +++ b/src/client/java/net/vinrobot/mcemote/client/providers/STVGlobalEmoteProvider.java @@ -2,12 +2,13 @@ import net.vinrobot.mcemote.api.SevenTVService; import net.vinrobot.mcemote.client.font.impl.SevenTVEmote; +import net.vinrobot.mcemote.config.Configuration; import java.io.IOException; public class STVGlobalEmoteProvider implements IEmoteProvider { @Override - public void registerEmotes(IEmoteRegistry registry) throws IOException, InterruptedException { + public void registerEmotes(Configuration config, IEmoteRegistry registry) throws IOException, InterruptedException { final SevenTVService service = new SevenTVService(); service.fetchGlobalEmoteSet() diff --git a/src/client/java/net/vinrobot/mcemote/client/providers/STVUserEmoteProvider.java b/src/client/java/net/vinrobot/mcemote/client/providers/STVUserEmoteProvider.java index a0d42e2..d0dab36 100644 --- a/src/client/java/net/vinrobot/mcemote/client/providers/STVUserEmoteProvider.java +++ b/src/client/java/net/vinrobot/mcemote/client/providers/STVUserEmoteProvider.java @@ -3,21 +3,21 @@ import net.vinrobot.mcemote.api.SevenTVService; import net.vinrobot.mcemote.api.seventv.Platform; import net.vinrobot.mcemote.client.font.impl.SevenTVEmote; +import net.vinrobot.mcemote.config.Configuration; import java.io.IOException; public class STVUserEmoteProvider implements IEmoteProvider { - private final String userId; - - public STVUserEmoteProvider(String userId) { - this.userId = userId; - } - @Override - public void registerEmotes(IEmoteRegistry registry) throws IOException, InterruptedException { + public void registerEmotes(Configuration config, IEmoteRegistry registry) throws IOException, InterruptedException { + final String twitchId = config.twitchId().get(); + if (twitchId.isEmpty()) { + return; + } + final SevenTVService service = new SevenTVService(); - service.fetchUser(Platform.TWITCH, this.userId) + service.fetchUser(Platform.TWITCH, twitchId) .emote_set().emotes().stream() .map(SevenTVEmote::new) .forEach(registry::registerEmote); diff --git a/src/main/java/net/vinrobot/mcemote/config/Configuration.java b/src/main/java/net/vinrobot/mcemote/config/Configuration.java new file mode 100644 index 0000000..dad9083 --- /dev/null +++ b/src/main/java/net/vinrobot/mcemote/config/Configuration.java @@ -0,0 +1,5 @@ +package net.vinrobot.mcemote.config; + +public interface Configuration { + Option twitchId(); +} diff --git a/src/main/java/net/vinrobot/mcemote/config/Option.java b/src/main/java/net/vinrobot/mcemote/config/Option.java new file mode 100644 index 0000000..72be209 --- /dev/null +++ b/src/main/java/net/vinrobot/mcemote/config/Option.java @@ -0,0 +1,74 @@ +package net.vinrobot.mcemote.config; + +import net.vinrobot.mcemote.config.impl.OptionImpl; + +import java.util.Optional; + +public interface Option { + static Option of(final T defaultValue) { + return new OptionImpl<>(defaultValue); + } + + static Option of(final T defaultValue, Optional value) { + final Option option = of(defaultValue); + option.set(value); + return option; + } + + static Option of(final T defaultValue, T value) { + final Option option = of(defaultValue); + option.set(value); + return option; + } + + /** + * Set the value of this option. + * + * @param value The optional value to set. + * @return This option. + */ + Option set(Optional value); + + /** + * Set the value of this option. + * + * @param value The value to set. Must not be null. + * @return This option. + */ + default Option set(T value) { + return this.set(Optional.of(value)); + } + + /** + * Reset the value of this option. + * + * @return This option. + */ + default Option reset() { + return this.set(Optional.empty()); + } + + /** + * Get the value of this option. + * Returns the default value if the value is not set. + * + * @return The value of this option. + */ + default T get() { + return this.getRaw().orElseGet(this::getDefault); + } + + /** + * Get the raw value of this option. + * + * @return The raw value of this option. + */ + Optional getRaw(); + + /** + * Get the default value of this option. + * + * @return The default value of this option. + */ + T getDefault(); +} diff --git a/src/main/java/net/vinrobot/mcemote/config/impl/ConfigurationImpl.java b/src/main/java/net/vinrobot/mcemote/config/impl/ConfigurationImpl.java new file mode 100644 index 0000000..657825e --- /dev/null +++ b/src/main/java/net/vinrobot/mcemote/config/impl/ConfigurationImpl.java @@ -0,0 +1,15 @@ +package net.vinrobot.mcemote.config.impl; + +import net.vinrobot.mcemote.config.Configuration; +import net.vinrobot.mcemote.config.Option; + +public final class ConfigurationImpl implements Configuration { + private static final String SCRAPIE_TWITCH_ID = "40646018"; + + private final Option twitchId = Option.of(SCRAPIE_TWITCH_ID); + + @Override + public Option twitchId() { + return this.twitchId; + } +} diff --git a/src/main/java/net/vinrobot/mcemote/config/impl/OptionImpl.java b/src/main/java/net/vinrobot/mcemote/config/impl/OptionImpl.java new file mode 100644 index 0000000..ca247d2 --- /dev/null +++ b/src/main/java/net/vinrobot/mcemote/config/impl/OptionImpl.java @@ -0,0 +1,31 @@ +package net.vinrobot.mcemote.config.impl; + +import net.vinrobot.mcemote.config.Option; + +import java.util.Objects; +import java.util.Optional; + +public class OptionImpl implements Option { + private final T defaultValue; + private Optional value = Optional.empty(); + + public OptionImpl(final T defaultValue) { + this.defaultValue = defaultValue; + } + + @Override + public Option set(final Optional value) { + this.value = Objects.requireNonNull(value); + return this; + } + + @Override + public Optional getRaw() { + return this.value; + } + + @Override + public T getDefault() { + return this.defaultValue; + } +}