-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathAnimationLoader.java
More file actions
91 lines (73 loc) · 3.18 KB
/
AnimationLoader.java
File metadata and controls
91 lines (73 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package io.github.foundationgames.animatica.animation;
import io.github.foundationgames.animatica.Animatica;
import io.github.foundationgames.animatica.util.Flags;
import io.github.foundationgames.animatica.util.exception.PropertyParseException;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.Identifier;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.BiConsumer;
public final class AnimationLoader implements SimpleSynchronousResourceReloadListener {
public static final String[] ANIM_PATHS = {
"animatica/anim",
"mcpatcher/anim",
"optifine/anim"
};
private static final Identifier ID = Animatica.id("animation_storage");
public static final AnimationLoader INSTANCE = new AnimationLoader();
private final Map<Identifier, Identifier> animationIds = new HashMap<>();
private AnimationLoader() {
}
private static void findAllMCPAnimations(ResourceManager manager, BiConsumer<Identifier, Resource> action) {
for (var path : ANIM_PATHS) {
manager.listResources(path, p -> p.getPath().endsWith(".properties")).forEach(action);
}
}
public @Nullable Identifier getAnimationId(Identifier id) {
return animationIds.get(id);
}
@Override
public Identifier getFabricId() {
return ID;
}
@Override
public void onResourceManagerReload(ResourceManager manager) {
this.animationIds.clear();
if (!Animatica.CONFIG.animatedTextures) {
return;
}
Flags.ALLOW_INVALID_ID_CHARS = true;
var animations = new HashMap<Identifier, List<AnimationMeta>>();
findAllMCPAnimations(manager, (id, resource) -> {
try {
try (var resourceInputStream = resource.open()) {
var ppt = new Properties();
ppt.load(resourceInputStream);
var anim = AnimationMeta.of(id, ppt);
var targetId = anim.target();
if (!animations.containsKey(targetId)) animations.put(targetId, new ArrayList<>());
animations.get(targetId).add(anim);
}
} catch (IOException | PropertyParseException e) {
Animatica.LOG.error(e.getMessage());
}
});
for (var targetId : animations.keySet()) {
AnimatedTexture.tryCreate(manager, targetId, animations.get(targetId))
.ifPresent(tex -> {
var animId = Identifier.fromNamespaceAndPath(targetId.getNamespace(), targetId.getPath() + "-anim");
this.animationIds.put(targetId, animId);
Minecraft.getInstance().getTextureManager().register(animId, tex);
});
}
Flags.ALLOW_INVALID_ID_CHARS = false;
}
}