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 2ad5a59c0587706f79bbf4053a0f964a14ef3b10..67a84fc3f5ad4568c8bd97c5c8af45625f7823a4 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 c695281d78adb51aa40288109a5f1699410c4e06..de16d14a50552a5dec63475af19c6f24b7495efa 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 eab866a598704a167e597a0a0349929fa4c48d70..d59ef282a59bd6146cc3a84015b7aae7c9642a39 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 1058f927e6784cd2d94471e41c5e9bbefa53282b..beb4869ffb4d33b7a8ad007ebb0f2895cc66e4ec 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(""); } }