Skip to content

Commit

Permalink
Create basic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Aug 7, 2023
1 parent d771d06 commit 19a6c62
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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<Emote> emotes = new ArrayList<>();
provider.registerEmotes(emotes::add);
provider.registerEmotes(config, emotes::add);

for (final Emote emote : emotes) {
EMOTES_MANAGER.addEmote(codePoint++, emote);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/vinrobot/mcemote/config/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.vinrobot.mcemote.config;

public interface Configuration {
Option<String> twitchId();
}
74 changes: 74 additions & 0 deletions src/main/java/net/vinrobot/mcemote/config/Option.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.vinrobot.mcemote.config;

import net.vinrobot.mcemote.config.impl.OptionImpl;

import java.util.Optional;

public interface Option<T> {
static <T> Option<T> of(final T defaultValue) {
return new OptionImpl<>(defaultValue);
}

static <T> Option<T> of(final T defaultValue, Optional<T> value) {
final Option<T> option = of(defaultValue);
option.set(value);
return option;
}

static <T> Option<T> of(final T defaultValue, T value) {
final Option<T> 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<T> set(Optional<T> value);

/**
* Set the value of this option.
*
* @param value The value to set. Must not be null.
* @return This option.
*/
default Option<T> set(T value) {
return this.set(Optional.of(value));
}

/**
* Reset the value of this option.
*
* @return This option.
*/
default Option<T> 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<T> getRaw();

/**
* Get the default value of this option.
*
* @return The default value of this option.
*/
T getDefault();
}
Original file line number Diff line number Diff line change
@@ -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<String> twitchId = Option.of(SCRAPIE_TWITCH_ID);

@Override
public Option<String> twitchId() {
return this.twitchId;
}
}
31 changes: 31 additions & 0 deletions src/main/java/net/vinrobot/mcemote/config/impl/OptionImpl.java
Original file line number Diff line number Diff line change
@@ -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<T> implements Option<T> {
private final T defaultValue;
private Optional<T> value = Optional.empty();

public OptionImpl(final T defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public Option<T> set(final Optional<T> value) {
this.value = Objects.requireNonNull(value);
return this;
}

@Override
public Optional<T> getRaw() {
return this.value;
}

@Override
public T getDefault() {
return this.defaultValue;
}
}

0 comments on commit 19a6c62

Please sign in to comment.