diff --git a/src/client/java/net/vinrobot/mcemote/client/font/Frames.java b/src/client/java/net/vinrobot/mcemote/client/font/AnimatedGlyph.java similarity index 75% rename from src/client/java/net/vinrobot/mcemote/client/font/Frames.java rename to src/client/java/net/vinrobot/mcemote/client/font/AnimatedGlyph.java index 2012685..1016036 100644 --- a/src/client/java/net/vinrobot/mcemote/client/font/Frames.java +++ b/src/client/java/net/vinrobot/mcemote/client/font/AnimatedGlyph.java @@ -1,15 +1,15 @@ package net.vinrobot.mcemote.client.font; -import net.minecraft.client.font.Glyph; - import java.time.Duration; +import java.time.Instant; import java.util.stream.Stream; +import net.minecraft.client.font.Glyph; -public class Frames { +public class AnimatedGlyph { private final Frame[] frames; private final Duration loopTime; - public Frames(Frame[] frames) { + public AnimatedGlyph(Frame[] frames) { this.frames = frames; this.loopTime = Stream.of(frames) .map(Frame::duration) @@ -17,19 +17,19 @@ public Frames(Frame[] frames) { .orElse(Duration.ofDays(1)); } - private static Duration modulo(Duration a, Duration b) { + private static Duration modulo(Instant a, Duration b) { // TODO: Improve using Duration::getSeconds and Duration::getNano - return a.minus(b.multipliedBy(a.toMillis() / b.toMillis())); + return Duration.ofMillis(a.toEpochMilli() % b.toMillis()); } - public Frame getFrameAt(Duration at) { + public Glyph getGlyphAt(Instant at) { final Duration time = modulo(at, loopTime); Duration current = Duration.ZERO; for (Frame frame : frames) { current = current.plus(frame.duration()); if (current.compareTo(time) > 0) { - return frame; + return frame.image(); } } diff --git a/src/client/java/net/vinrobot/mcemote/client/font/EmoteFontStorage.java b/src/client/java/net/vinrobot/mcemote/client/font/EmoteFontStorage.java index 15547a3..5f6b7a0 100644 --- a/src/client/java/net/vinrobot/mcemote/client/font/EmoteFontStorage.java +++ b/src/client/java/net/vinrobot/mcemote/client/font/EmoteFontStorage.java @@ -1,29 +1,26 @@ package net.vinrobot.mcemote.client.font; +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.font.BuiltinEmptyGlyph; import net.minecraft.client.font.FontStorage; import net.minecraft.client.font.Glyph; import net.minecraft.client.font.GlyphRenderer; -import net.minecraft.client.texture.NativeImage; import net.minecraft.client.texture.TextureManager; import net.minecraft.util.Identifier; import net.vinrobot.mcemote.MinecraftEmoteMod; -import net.vinrobot.mcemote.client.helpers.NativeImageHelper; import net.vinrobot.mcemote.client.text.EmotesManager; -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; - @Environment(EnvType.CLIENT) public class EmoteFontStorage extends FontStorage { public static final Identifier IDENTIFIER = new Identifier("mcemote.fonts", "emotes"); public static final float GLYPH_HEIGHT = 9; private final EmotesManager emotesManager; - private final Map framesCache = new HashMap<>(); + private final Map framesCache = new HashMap<>(); private final Map glyphRendererCache = new HashMap<>(); public EmoteFontStorage(TextureManager textureManager, EmotesManager emotesManager) { @@ -34,17 +31,16 @@ public EmoteFontStorage(TextureManager textureManager, EmotesManager emotesManag @Override public Glyph getGlyph(int codePoint, boolean validateAdvance) { try { - return this.framesCache.computeIfAbsent(codePoint, this::loadAnimationManager) - .getFrameAt(Duration.ofMillis(System.currentTimeMillis())) - .image(); + return this.framesCache.computeIfAbsent(codePoint, this::loadAnimatedGlyph) + .getGlyphAt(Instant.now()); } catch (RuntimeException ex) { return BuiltinEmptyGlyph.MISSING; } } - private Frames loadAnimationManager(Integer integer) { + private AnimatedGlyph loadAnimatedGlyph(int codePoint) { try { - final Emote emote = this.emotesManager.getByCodePoint(integer).orElseThrow(); + final Emote emote = this.emotesManager.getByCodePoint(codePoint).orElseThrow(); final int width = emote.getWidth(); final int height = emote.getHeight(); @@ -52,14 +48,14 @@ private Frames loadAnimationManager(Integer integer) { final float oversample = height / GLYPH_HEIGHT; final Emote.Frame[] frames = emote.loadFrames(); - final Frames.Frame[] animatedFrames = new Frames.Frame[frames.length]; + final AnimatedGlyph.Frame[] animatedFrames = new AnimatedGlyph.Frame[frames.length]; for (int i = 0; i < frames.length; i++) { final Emote.Frame frame = frames[i]; final Glyph glyph = new NativeImageGlyph(frame.image(), advance, oversample); - animatedFrames[i] = new Frames.Frame(glyph, frame.duration()); + animatedFrames[i] = new AnimatedGlyph.Frame(glyph, frame.duration()); } - return new Frames(animatedFrames); + return new AnimatedGlyph(animatedFrames); } catch (Exception ex) { MinecraftEmoteMod.LOGGER.error("Unable to load emote", ex); throw new RuntimeException(ex);