From 088989c775b49e6d4b5c738a7193c30877053c1a Mon Sep 17 00:00:00 2001 From: wagyourtail Date: Sat, 19 Sep 2020 17:15:43 -0700 Subject: [PATCH] fix gifs, and some stuff with pair/tuple --- .../craftpresence/impl/ImageFrame.java | 13 +++++ .../cdagaming/craftpresence/impl/Pair.java | 6 ++- .../cdagaming/craftpresence/impl/Tuple.java | 9 ++-- .../craftpresence/utils/ImageUtils.java | 50 ++++++++++++++----- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/ImageFrame.java b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/ImageFrame.java index 2ad5a59c05..67a84fc3f5 100644 --- a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/ImageFrame.java +++ b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/ImageFrame.java @@ -31,6 +31,7 @@ public class ImageFrame { private final BufferedImage image; private final String disposal; private final int width, height; + private long renderTime = 0; public ImageFrame (BufferedImage image, int delay, String disposal, int width, int height){ this.image = image; @@ -67,4 +68,16 @@ public class ImageFrame { public int getHeight() { return height; } + + public long getRenderTime() { + return renderTime; + } + + public void setRenderTime(long renderTime) { + this.renderTime = renderTime; + } + + public boolean shouldRenderNext() { + return System.currentTimeMillis() - renderTime > delay * 10; + } } diff --git a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Pair.java b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Pair.java index c695281d78..de16d14a50 100644 --- a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Pair.java +++ b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Pair.java @@ -73,8 +73,9 @@ public class Pair { * * @param first the first element to be applied */ - public void setFirst(final U first) { + public U setFirst(final U first) { this.first = first; + return first; } /** @@ -91,8 +92,9 @@ public class Pair { * * @param second the second element to be applied */ - public void setSecond(final V second) { + public V setSecond(final V second) { this.second = second; + return second; } /** diff --git a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Tuple.java b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Tuple.java index eab866a598..d59ef282a5 100644 --- a/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Tuple.java +++ b/src/main/java/com/gitlab/cdagaming/craftpresence/impl/Tuple.java @@ -80,8 +80,9 @@ public class Tuple { * * @param first the first element to be applied */ - public void setFirst(final T first) { + public T setFirst(final T first) { this.first = first; + return first; } /** @@ -98,8 +99,9 @@ public class Tuple { * * @param second the second element to be applied */ - public void setSecond(final U second) { + public U setSecond(final U second) { this.second = second; + return second; } /** @@ -116,8 +118,9 @@ public class Tuple { * * @param third the third element to be applied */ - public void setThird(final V third) { + public V setThird(final V third) { this.third = third; + return third; } /** diff --git a/src/main/java/com/gitlab/cdagaming/craftpresence/utils/ImageUtils.java b/src/main/java/com/gitlab/cdagaming/craftpresence/utils/ImageUtils.java index 1058f927e6..beb4869ffb 100644 --- a/src/main/java/com/gitlab/cdagaming/craftpresence/utils/ImageUtils.java +++ b/src/main/java/com/gitlab/cdagaming/craftpresence/utils/ImageUtils.java @@ -77,7 +77,7 @@ public class ImageUtils { *

* Format: textureName;[[textureInputType, textureObj], imageData, textureData] */ - private static final Map, Pair>, ResourceLocation>> cachedImages = Maps.newHashMap(); + private static final Map, Pair>, List>> cachedImages = Maps.newHashMap(); /** * The thread used for Url Image Events to take place within */ @@ -92,7 +92,7 @@ public class ImageUtils { final Pair> request = urlRequests.take(); final boolean isGif = request.getFirst().endsWith(".gif"); - Pair> bufferData = cachedImages.get(request.getFirst()).getSecond(); + Pair> bufferData = cachedImages.get(request.getFirst()).getSecond(); if (bufferData == null) { // Setup Initial Data bufferData = new Pair<>(0, Lists.newArrayList()); @@ -117,7 +117,7 @@ public class ImageUtils { for (ImageFrame frame : frames) { try { - bufferData.getSecond().add(frame.getImage()); + bufferData.getSecond().add(frame); } catch (Exception ex) { if (ModUtils.IS_VERBOSE) { ex.printStackTrace(); @@ -125,9 +125,10 @@ public class ImageUtils { } } } else { - bufferData.getSecond().add(ImageIO.read(streamData)); + bufferData.getSecond().add(new ImageFrame(ImageIO.read(streamData))); } cachedImages.get(request.getFirst()).setSecond(bufferData); + cachedImages.get(request.getFirst()).setThird(new ArrayList<>(bufferData.getSecond().size())); } } catch (Exception ex) { if (ModUtils.IS_VERBOSE) { @@ -241,7 +242,7 @@ public class ImageUtils { } } - final Pair> bufferData = cachedImages.get(textureName).getSecond(); + final Pair> bufferData = cachedImages.get(textureName).getSecond(); final boolean shouldRepeat = textureName.endsWith(".gif"); final boolean doesContinue = bufferData == null ? false : bufferData.getFirst() < bufferData.getSecond().size() - 1; @@ -249,18 +250,41 @@ public class ImageUtils { if (bufferData == null || bufferData.getSecond() == null || bufferData.getSecond().isEmpty()) { return new ResourceLocation(""); } else if (textureName != null) { - final DynamicTexture dynTexture = new DynamicTexture(bufferData.getSecond().get(bufferData.getFirst())); + List resources = cachedImages.get(textureName).getThird(); + if (bufferData.getFirst() < resources.size()) { + ResourceLocation loc = resources.get(bufferData.getFirst()); + if (bufferData.getSecond().get(bufferData.getFirst()).shouldRenderNext()) { + if (doesContinue) { + bufferData.getSecond().get(bufferData.setFirst(bufferData.getFirst() + 1)).setRenderTime(System.currentTimeMillis()); + } else if (shouldRepeat) { + bufferData.getSecond().get(bufferData.setFirst(0)).setRenderTime(System.currentTimeMillis()); + } + } + return loc; + } + final DynamicTexture dynTexture = new DynamicTexture(bufferData.getSecond().get(bufferData.getFirst()).getImage()); final ResourceLocation cachedTexture = CraftPresence.instance.getRenderManager().renderEngine.getDynamicTextureLocation(textureName + (textureName.endsWith(".gif") ? "_" + cachedImages.get(textureName).getFirst().getFirst() : ""), dynTexture); - if (doesContinue) { - bufferData.setFirst(bufferData.getFirst() + 1); - } else if (shouldRepeat) { - bufferData.setFirst(0); + if (bufferData.getSecond().get(bufferData.getFirst()).shouldRenderNext()) { + if (doesContinue) { + bufferData.getSecond().get(bufferData.setFirst(bufferData.getFirst() + 1)).setRenderTime(System.currentTimeMillis()); + } else if (shouldRepeat) { + bufferData.setFirst(0); + } } - cachedImages.get(textureName).setSecond(bufferData); - cachedImages.get(textureName).setThird(cachedTexture); + resources.add(cachedTexture); + return cachedTexture; + } + } else if (textureName != null) { + List resources = cachedImages.get(textureName).getThird(); + if (0 < resources.size()) { + return resources.get(0); } + final DynamicTexture dynTexture = new DynamicTexture(bufferData.getSecond().get(0).getImage()); + final ResourceLocation cachedTexture = CraftPresence.instance.getRenderManager().renderEngine.getDynamicTextureLocation(textureName + (textureName.endsWith(".gif") ? "_" + cachedImages.get(textureName).getFirst().getFirst() : ""), dynTexture); + resources.add(cachedTexture); + return cachedTexture; } - return cachedImages.get(textureName).getThird(); + return new ResourceLocation(""); } } -- GitLab