generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/client/java/net/vinrobot/mcemote/client/providers/BTTVEmote.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package net.vinrobot.mcemote.client.providers; | ||
|
||
import net.minecraft.client.texture.NativeImage; | ||
import net.vinrobot.mcemote.api.bttv.Emote; | ||
import net.vinrobot.mcemote.client.helpers.NativeImageHelper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
public class BTTVEmote implements net.vinrobot.mcemote.client.font.Emote { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(BTTVEmote.class); | ||
private static final int DEFAULT_SIZE = 28; | ||
|
||
private final Emote emote; | ||
|
||
public BTTVEmote(Emote emote) { | ||
this.emote = emote; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.emote.code(); | ||
} | ||
|
||
@Override | ||
public int getWidth() { | ||
final int width = this.emote.width(); | ||
return width > 0 ? width : DEFAULT_SIZE; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
final int height = this.emote.height(); | ||
return height > 0 ? height : DEFAULT_SIZE; | ||
} | ||
|
||
@Override | ||
public Frame[] loadFrames() throws IOException { | ||
if (this.emote.animated()) { | ||
LOGGER.error("Animated BTTV emotes are not supported yet."); | ||
} | ||
|
||
final URL url = new URL("https://cdn.betterttv.net/emote/" + this.emote.id() + "/1x"); | ||
final BufferedImage image = Objects.requireNonNull(ImageIO.read(url)); | ||
|
||
final int expectedWidth = getWidth(), expectedHeight = getHeight(); | ||
final int actualWidth = image.getWidth(), actualHeight = image.getHeight(); | ||
if (expectedWidth != actualWidth || expectedHeight != actualHeight) { | ||
final String expectedSize = expectedWidth + "x" + expectedHeight; | ||
final String actualSize = actualWidth + "x" + actualHeight; | ||
LOGGER.error("BTTV emote " + getName() + " has unexpected size " + actualSize + " (expected " + expectedSize + ")."); | ||
} | ||
|
||
final NativeImage nativeImage = NativeImageHelper.fromBufferedImage(image); | ||
return new Frame[]{new Frame(nativeImage)}; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/client/java/net/vinrobot/mcemote/client/providers/BTTVGlobalEmoteProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.vinrobot.mcemote.client.providers; | ||
|
||
import net.vinrobot.mcemote.api.bttv.BetterTTVService; | ||
import net.vinrobot.mcemote.api.bttv.Emote; | ||
import net.vinrobot.mcemote.config.Configuration; | ||
|
||
import java.util.Arrays; | ||
|
||
public class BTTVGlobalEmoteProvider implements IEmoteProvider { | ||
@Override | ||
public int priority() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public void registerEmotes(final Configuration config, final IEmoteRegistry registry) throws Exception { | ||
final BetterTTVService service = new BetterTTVService(); | ||
|
||
final Emote[] emoteSets = service.fetchGlobalEmoteSet(); | ||
|
||
Arrays.stream(emoteSets) | ||
.map(BTTVEmote::new) | ||
.forEach(registry::registerEmote); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/client/java/net/vinrobot/mcemote/client/providers/BTTVUserEmoteProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package net.vinrobot.mcemote.client.providers; | ||
|
||
import net.vinrobot.mcemote.api.bttv.BetterTTVService; | ||
import net.vinrobot.mcemote.api.bttv.Provider; | ||
import net.vinrobot.mcemote.api.bttv.UserResponse; | ||
import net.vinrobot.mcemote.config.Configuration; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
public class BTTVUserEmoteProvider implements IEmoteProvider { | ||
@Override | ||
public void registerEmotes(final Configuration config, final IEmoteRegistry registry) throws Exception { | ||
final String twitchId = config.twitchId().get(); | ||
if (twitchId.isEmpty()) { | ||
return; | ||
} | ||
|
||
final BetterTTVService service = new BetterTTVService(); | ||
|
||
final UserResponse emoteSets = service.fetchUserEmoteSet(Provider.TWITCH, twitchId); | ||
|
||
Stream.concat(Arrays.stream(emoteSets.channelEmotes()), Arrays.stream(emoteSets.sharedEmotes())) | ||
.map(BTTVEmote::new) | ||
.forEach(registry::registerEmote); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/net/vinrobot/mcemote/api/bttv/BetterTTVService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.vinrobot.mcemote.api.bttv; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
public class BetterTTVService { | ||
public static final String BASE_API = "https://api.betterttv.net/3/cached"; | ||
public static final URI GLOBAL_EMOTE_SET_URI = URI.create(BASE_API + "/emotes/global"); | ||
|
||
private final HttpClient httpClient; | ||
private final Gson gson = new Gson(); | ||
|
||
public BetterTTVService() { | ||
this(HttpClient.newHttpClient()); | ||
} | ||
|
||
public BetterTTVService(HttpClient httpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
public Emote[] fetchGlobalEmoteSet() throws IOException, InterruptedException { | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.uri(GLOBAL_EMOTE_SET_URI) | ||
.build(); | ||
|
||
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
|
||
return gson.fromJson(httpResponse.body(), Emote[].class); | ||
} | ||
|
||
public UserResponse fetchUserEmoteSet(Provider provider, String userId) throws IOException, InterruptedException { | ||
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.uri(URI.create(BASE_API + "/users/" + provider.path + "/" + userId)) | ||
.build(); | ||
|
||
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
|
||
return gson.fromJson(httpResponse.body(), UserResponse.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.vinrobot.mcemote.api.bttv; | ||
|
||
public record Emote( | ||
String id, | ||
String code, | ||
String imageType, | ||
boolean animated, | ||
int width, | ||
int height | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package net.vinrobot.mcemote.api.bttv; | ||
|
||
public enum Provider { | ||
TWITCH("twitch"), | ||
YOUTUBE("youtube"); | ||
|
||
public final String path; | ||
|
||
Provider(String path) { | ||
this.path = path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.vinrobot.mcemote.api.bttv; | ||
|
||
public record User( | ||
String id, | ||
String name, | ||
String displayName, | ||
String providerId | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/vinrobot/mcemote/api/bttv/UserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.vinrobot.mcemote.api.bttv; | ||
|
||
public record UserResponse( | ||
String id, | ||
String[] bots, | ||
String avatar, | ||
Emote[] channelEmotes, | ||
Emote[] sharedEmotes | ||
) { | ||
} |