Skip to content

Commit

Permalink
Merge pull request #90 from InventivetalentDev/frame_switching
Browse files Browse the repository at this point in the history
Frame switching
  • Loading branch information
InventivetalentDev authored Nov 10, 2018
2 parents 74a41e7 + f7192f8 commit 631f844
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<groupId>org.inventivetalent</groupId>
<artifactId>animatedframes</artifactId>
<version>4.7.0-SNAPSHOT</version>
<version>4.8.0-SNAPSHOT</version>
<name>AnimatedFrames</name>

<build>
Expand Down Expand Up @@ -140,7 +140,7 @@
<dependency>
<groupId>org.inventivetalent</groupId>
<artifactId>mapmanager</artifactId>
<version>1.5.0-SNAPSHOT</version>
<version>1.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.inventivetalent</groupId>
Expand Down
105 changes: 81 additions & 24 deletions src/main/java/org/inventivetalent/animatedframes/AnimatedFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package org.inventivetalent.animatedframes;

import com.google.common.primitives.Ints;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import lombok.Data;
Expand All @@ -42,6 +43,7 @@
import org.bukkit.plugin.Plugin;
import org.inventivetalent.animatedframes.decoder.GifDecoder;
import org.inventivetalent.frameutil.BaseFrameMapAbstract;
import org.inventivetalent.mapmanager.ArrayImage;
import org.inventivetalent.mapmanager.MapManagerPlugin;
import org.inventivetalent.mapmanager.TimingsHelper;
import org.inventivetalent.mapmanager.controller.MapController;
Expand All @@ -57,6 +59,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -125,37 +128,80 @@ public void run() {
if (!imageLoaded) {
MapManager mapManager = ((MapManagerPlugin) Bukkit.getPluginManager().getPlugin("MapManager")).getMapManager();
try {
File file = plugin.frameManager.downloadOrGetImage(this.imageSource);
GifDecoder decoder = new GifDecoder();
decoder.read(new FileInputStream(file));

if ((this.length = decoder.getFrameCount()) <= 0) {
plugin.getLogger().info("Animation length for '" + getName() + "' is zero. Creating non-animated image.");
this.length = 1;

BufferedImage image = ImageIO.read(file);
image = scaleImage(image);
MapWrapper mapWrapper = mapManager.wrapMultiImage(image, this.height, this.width);
this.frameDelays = new int[] { 500 };
this.mapWrappers = new MapWrapper[] { mapWrapper };
image.flush();
File cacheDir = new File(new File(plugin.getDataFolder(), "cache"), this.name);
if (!cacheDir.exists()) {
cacheDir.mkdirs();

plugin.getLogger().info("Generating image data for " + getName() + "...");

File file = plugin.frameManager.downloadOrGetImage(this.imageSource);
GifDecoder decoder = new GifDecoder();
decoder.read(new FileInputStream(file));

if ((this.length = decoder.getFrameCount()) <= 0) {
plugin.getLogger().info("Animation length for '" + getName() + "' is zero. Creating non-animated image.");
this.length = 1;

BufferedImage image = ImageIO.read(file);
image = scaleImage(image);
MapWrapper mapWrapper = mapManager.wrapMultiImage(image, this.height, this.width);
this.frameDelays = new int[] { 500 };
this.mapWrappers = new MapWrapper[] { mapWrapper };
image.flush();

File cacheFile = new File(cacheDir, this.name + "_0.afc");
cacheFile.createNewFile();
try (FileOutputStream out = new FileOutputStream(cacheFile)) {
ArrayImage.writeToStream(mapWrapper.getContent(), out);
}
} else {
this.frameDelays = new int[this.length];
this.mapWrappers = new MapWrapper[this.length];
for (int i = 0; i < this.length; i++) {
BufferedImage image = scaleImage(decoder.getFrame(i));
int delay = decoder.getDelay(i);
this.frameDelays[i] = delay;
MapWrapper wrapper = mapManager.wrapMultiImage(image, this.height, this.width);
this.mapWrappers[i] = wrapper;
image.flush();

File cacheFile = new File(cacheDir, this.name + "_" + i + ".afc");
cacheFile.createNewFile();
try (FileOutputStream out = new FileOutputStream(cacheFile)) {
out.write(Ints.toByteArray(delay));
ArrayImage.writeToStream(wrapper.getContent(), out);
}
}
}

// Reset all images
for (Object object : decoder.frames) {
((GifDecoder.GifFrame) object).image.flush();
}
decoder.frames.clear();

} else {
plugin.getLogger().info("Reading " + getName() + " from cache...");

String[] fileList = cacheDir.list();
this.length = fileList.length;
this.frameDelays = new int[this.length];
this.mapWrappers = new MapWrapper[this.length];

for (int i = 0; i < this.length; i++) {
BufferedImage image = scaleImage(decoder.getFrame(i));
this.frameDelays[i] = decoder.getDelay(i);
this.mapWrappers[i] = mapManager.wrapMultiImage(image, this.height, this.width);
image.flush();
File cacheFile = new File(cacheDir, this.name + "_" + i + ".afc");
cacheFile.createNewFile();
try (FileInputStream in = new FileInputStream(cacheFile)) {
byte[] lengthBytes = new byte[4];
in.read(lengthBytes, 0, 4);
this.frameDelays[i] = Ints.fromByteArray(lengthBytes);

ArrayImage arrayImage = ArrayImage.readFromStream(in);
this.mapWrappers[i]=mapManager.wrapMultiImage(arrayImage, this.height, this.width);
}
}
}

// Reset all images
for (Object object : decoder.frames) {
((GifDecoder.GifFrame) object).image.flush();
}
decoder.frames.clear();

imageLoaded = true;
} catch (IOException e) {
plugin.getLogger().log(Level.SEVERE, "Failed to load image '" + getName() + "'", e);
Expand Down Expand Up @@ -385,6 +431,17 @@ public void clearFrames() {
}
}

protected MapWrapper[] getWrappers() {
return this.mapWrappers;
}

protected void setContent(MapWrapper[] wrappers, int[] delays) {
this.length = wrappers.length;
this.frameDelays = delays;
this.currentFrame = Math.min(this.currentFrame, this.length - 1);
this.mapWrappers = wrappers;
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.inventivetalent.update.spiget.UpdateCallback;
import org.inventivetalent.update.spiget.comparator.VersionComparator;

import java.io.File;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -77,6 +78,9 @@ public void onEnable() {
Bukkit.getPluginManager().registerEvents(interactListener = new InteractListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this);

File cacheDir = new File(getDataFolder(), "cache");
if (!cacheDir.exists()) { cacheDir.mkdirs(); }

getLogger().fine("Waiting 2 seconds before loading data...");
Bukkit.getScheduler().runTaskLaterAsynchronously(this, new Runnable() {
@Override
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/inventivetalent/animatedframes/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,31 @@ public void frameprevious(final CommandSender sender, final String name) {
}
}

@Command(name = "framecontent",
aliases = {
"afcontent"
},
usage = "<Target Name> <Source Name>",
description = "Set the content of a frame to the contents of another",
min = 2,
max = 2,
fallbackPrefix = "animatedframes")
@Permission("animatedframes.framecontent")
public void framecontent(final CommandSender sender, final String target, final String source) {
if (!plugin.frameManager.doesFrameExist(target)) {
sender.sendMessage("Target frame not found");
return;
}
if (!plugin.frameManager.doesFrameExist(source)) {
sender.sendMessage("Source frame not found");
return;
}
final AnimatedFrame targetFrame = plugin.frameManager.getFrame(target);
final AnimatedFrame sourceFrame = plugin.frameManager.getFrame(source);

targetFrame.setContent(sourceFrame.getWrappers(), sourceFrame.getFrameDelays());
}

@Command(name = "placeframes",
aliases = {
"afplace",
Expand Down

0 comments on commit 631f844

Please sign in to comment.