Gif loading
This commit is contained in:
@@ -157,37 +157,7 @@ public class Cloaks {
|
|||||||
|
|
||||||
public static IdentifierRef getCurrentCloakTexture(String cloakName) {
|
public static IdentifierRef getCurrentCloakTexture(String cloakName) {
|
||||||
if (Arrays.asList(ANIMATED_CLOAKS).contains(cloakName)) {
|
if (Arrays.asList(ANIMATED_CLOAKS).contains(cloakName)) {
|
||||||
List<GifFrame> frames = animatedCloaks.get(cloakName);
|
return IdentifierRef.ofSaturn("textures/cloaks/" + cloakName + ".gif");
|
||||||
if (frames == null || frames.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
long currentTime = System.currentTimeMillis();
|
|
||||||
long lastTime = lastFrameTime.getOrDefault(cloakName, currentTime);
|
|
||||||
int currentFrame = 0;
|
|
||||||
|
|
||||||
long elapsedTime = currentTime - lastTime;
|
|
||||||
long totalTime = 0;
|
|
||||||
for (int i = 0; i < frames.size(); i++) {
|
|
||||||
totalTime += frames.get(i).getDelayMs();
|
|
||||||
if (elapsedTime < totalTime) {
|
|
||||||
currentFrame = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elapsedTime >= totalTime) {
|
|
||||||
lastFrameTime.put(cloakName, currentTime);
|
|
||||||
currentFrame = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
IdentifierRef identifier = frames.get(currentFrame).getTextureId();
|
|
||||||
|
|
||||||
if (identifier == null) {
|
|
||||||
return IdentifierRef.ofSaturn("textures/cloaks/" + cloakName + ".png");
|
|
||||||
}
|
|
||||||
|
|
||||||
return identifier;
|
|
||||||
} else {
|
} else {
|
||||||
return IdentifierRef.ofSaturn("textures/cloaks/" + cloakName + ".png");
|
return IdentifierRef.ofSaturn("textures/cloaks/" + cloakName + ".png");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.io.InputStream;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
@@ -13,7 +14,7 @@ import org.saturnclient.common.provider.Providers;
|
|||||||
import org.saturnclient.common.ref.asset.IdentifierRef;
|
import org.saturnclient.common.ref.asset.IdentifierRef;
|
||||||
|
|
||||||
public class GifUtils {
|
public class GifUtils {
|
||||||
public static Map<String, GifData> loadedCache = new ConcurrentHashMap<>();
|
public static Map<String, Optional<GifData>> loadedCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private static final Queue<IdentifierRef> QUEUE = new ConcurrentLinkedQueue<>();
|
private static final Queue<IdentifierRef> QUEUE = new ConcurrentLinkedQueue<>();
|
||||||
private static volatile boolean RUNNING = false;
|
private static volatile boolean RUNNING = false;
|
||||||
@@ -23,16 +24,18 @@ public class GifUtils {
|
|||||||
String g = gif.toString();
|
String g = gif.toString();
|
||||||
|
|
||||||
if (!loadedCache.containsKey(g)) {
|
if (!loadedCache.containsKey(g)) {
|
||||||
loadedCache.put(g, null);
|
loadedCache.put(g, Optional.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadedCache.get(g) == null) {
|
Optional<GifData> loadedData = loadedCache.get(g);
|
||||||
|
|
||||||
|
if (loadedData.isEmpty()) {
|
||||||
return IdentifierRef.of(g.replace(".gif", ".png"));
|
return IdentifierRef.of(g.replace(".gif", ".png"));
|
||||||
}
|
}
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
GifData data = loadedCache.get(g);
|
GifData data = loadedData.get();
|
||||||
|
|
||||||
long t = now % data.totalDuration;
|
long t = now % data.totalDuration;
|
||||||
|
|
||||||
@@ -122,7 +125,7 @@ public class GifUtils {
|
|||||||
|
|
||||||
// Apply on main thread
|
// Apply on main thread
|
||||||
Providers.saturn.getClient().executeOnThread(() -> {
|
Providers.saturn.getClient().executeOnThread(() -> {
|
||||||
loadedCache.put(id.toString(), data);
|
loadedCache.put(id.toString(), Optional.of(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user