Skip to content

Commit 54fd86f

Browse files
committed
fix: model load and keyframe (#180)
1 parent 1b1de86 commit 54fd86f

File tree

9 files changed

+93
-14
lines changed

9 files changed

+93
-14
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/src/main/java/kr/toxicity/model/api/animation/AnimationStateHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private boolean updateKeyframe(@NotNull Iterator<TreeIterator> iterator, @NotNul
150150
}
151151

152152
private boolean setAfterKeyframe(@Nullable KeyframeData next) {
153-
if (value(afterKeyframe) == value(next)) return false;
153+
if (equals(afterKeyframe, next)) return false;
154154
setConsumer.accept(
155155
value(beforeKeyframe = afterKeyframe),
156156
value(afterKeyframe = next)
@@ -159,6 +159,12 @@ private boolean setAfterKeyframe(@Nullable KeyframeData next) {
159159
return true;
160160
}
161161

162+
private boolean equals(@Nullable KeyframeData from, @Nullable KeyframeData to) {
163+
if (from == null && to == null) return true;
164+
if (from == null || to == null) return false;
165+
return from.value == to.value && from.realTime == to.realTime;
166+
}
167+
162168
private @Nullable T value(@Nullable KeyframeData data) {
163169
return data == null ? null : data.value;
164170
}

api/src/main/java/kr/toxicity/model/api/bone/BoneName.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public boolean equals(Object o) {
5151
public int hashCode() {
5252
return rawName.hashCode();
5353
}
54+
55+
@Override
56+
public @NotNull String toString() {
57+
return rawName;
58+
}
5459
}

api/src/main/java/kr/toxicity/model/api/data/raw/ModelAnimation.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jetbrains.annotations.Nullable;
2626

2727
import java.util.*;
28+
import java.util.stream.Collectors;
2829

2930
import static kr.toxicity.model.api.util.CollectionUtil.associate;
3031

@@ -49,12 +50,20 @@ public record ModelAnimation(
4950
/**
5051
* Converts raw animation to blueprint animation
5152
* @param meta meta
53+
* @param availableUUIDs available uuids
5254
* @param children children
5355
* @param placeholder placeholder
5456
* @return converted animation
5557
*/
56-
public @NotNull BlueprintAnimation toBlueprint(@NotNull ModelMeta meta, @NotNull List<BlueprintChildren> children, @NotNull ModelPlaceholder placeholder) {
57-
var map = new HashMap<>(animators());
58+
public @NotNull BlueprintAnimation toBlueprint(
59+
@NotNull ModelMeta meta,
60+
@NotNull Set<String> availableUUIDs,
61+
@NotNull List<BlueprintChildren> children,
62+
@NotNull ModelPlaceholder placeholder
63+
) {
64+
var map = animators().entrySet().stream()
65+
.filter(e -> availableUUIDs.contains(e.getKey()))
66+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
5867
var script = Optional.ofNullable(map.remove("effects"))
5968
.filter(ModelAnimator::isNotEmpty)
6069
.map(a -> toScript(a, placeholder))

api/src/main/java/kr/toxicity/model/api/data/raw/ModelChildren.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.Map;
1818
import java.util.Objects;
19+
import java.util.stream.Stream;
1920

2021
import static kr.toxicity.model.api.util.CollectionUtil.filterIsInstance;
2122
import static kr.toxicity.model.api.util.CollectionUtil.mapToList;
@@ -55,6 +56,18 @@ else if (json.isJsonObject()) {
5556
@NotNull Map<String, ModelGroup> groupMap
5657
);
5758

59+
/**
60+
* Flattens this children tree
61+
* @return flatten stream
62+
*/
63+
@NotNull Stream<ModelChildren> flatten();
64+
65+
/**
66+
* Gets uuid
67+
* @return uuid
68+
*/
69+
@NotNull String uuid();
70+
5871
/**
5972
* A raw element's uuid.
6073
* @param uuid uuid
@@ -67,6 +80,11 @@ record ModelUUID(@NotNull String uuid) implements ModelChildren {
6780
) {
6881
return new BlueprintChildren.BlueprintElement(Objects.requireNonNull(elementMap.get(uuid())));
6982
}
83+
84+
@Override
85+
public @NotNull Stream<ModelChildren> flatten() {
86+
return Stream.of(this);
87+
}
7088
}
7189

7290
/**
@@ -86,7 +104,7 @@ record ModelOutliner(
86104
) {
87105
var child = mapToList(children, c -> c.toBlueprint(elementMap, groupMap));
88106
var filtered = filterIsInstance(child, BlueprintChildren.BlueprintElement.class).toList();
89-
var selectedGroup = groupMap.getOrDefault(group.uuid(), group);
107+
var selectedGroup = groupMap.getOrDefault(uuid(), group);
90108
return new BlueprintChildren.BlueprintGroup(
91109
BoneTagRegistry.parse(selectedGroup.name()),
92110
selectedGroup.origin(),
@@ -95,5 +113,18 @@ record ModelOutliner(
95113
filtered.isEmpty() ? selectedGroup.visibility() : filtered.stream().anyMatch(element -> element.element().visibility())
96114
);
97115
}
116+
117+
@Override
118+
public @NotNull Stream<ModelChildren> flatten() {
119+
return Stream.concat(
120+
Stream.of(this),
121+
children.stream().flatMap(ModelChildren::flatten)
122+
);
123+
}
124+
125+
@Override
126+
public @NotNull String uuid() {
127+
return group.uuid();
128+
}
98129
}
99130
}

api/src/main/java/kr/toxicity/model/api/data/raw/ModelData.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21-
import static kr.toxicity.model.api.util.CollectionUtil.associate;
22-
import static kr.toxicity.model.api.util.CollectionUtil.mapToList;
21+
import static kr.toxicity.model.api.util.CollectionUtil.*;
2322

2423
/**
2524
* Raw BlockBench model's data.
@@ -65,12 +64,16 @@ public record ModelData(
6564
var elementMap = associate(elements(), ModelElement::uuid);
6665
var groupMap = associate(groups(), ModelGroup::uuid);
6766
var group = mapToList(outliner(), children -> children.toBlueprint(elementMap, groupMap));
67+
var availableUUIDs = mapToSet(
68+
filterIsInstance(outliner().stream().flatMap(ModelChildren::flatten), ModelChildren.ModelOutliner.class),
69+
ModelChildren.ModelOutliner::uuid
70+
);
6871
return new ModelBlueprint(
6972
name,
7073
resolution(),
7174
mapToList(textures(), ModelTexture::toBlueprint),
7275
group,
73-
associate(animations().stream().map(raw -> raw.toBlueprint(meta, group, placeholder)), BlueprintAnimation::name)
76+
associate(animations().stream().map(raw -> raw.toBlueprint(meta, availableUUIDs, group, placeholder)), BlueprintAnimation::name)
7477
);
7578
}
7679

api/src/main/java/kr/toxicity/model/api/pack/PackResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Set<PackByte> overlays(@NotNull PackOverlay overlay) {
9696
return get != null ? Collections.unmodifiableSet(get) : Collections.emptySet();
9797
}
9898

99-
public Stream<PackByte> stream() {
99+
public @NotNull Stream<PackByte> stream() {
100100
return Stream.concat(
101101
overlays.values().stream().flatMap(Collection::stream),
102102
assets.stream()

api/src/main/java/kr/toxicity/model/api/util/CollectionUtil.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import org.jetbrains.annotations.NotNull;
1616
import org.jetbrains.annotations.Unmodifiable;
1717

18-
import java.util.Collection;
19-
import java.util.Collections;
20-
import java.util.List;
21-
import java.util.Map;
18+
import java.util.*;
2219
import java.util.concurrent.atomic.AtomicInteger;
2320
import java.util.function.Function;
2421
import java.util.function.Predicate;
@@ -79,6 +76,34 @@ public static <E, R> List<R> mapToList(@NotNull Stream<E> stream, @NotNull Funct
7976
return stream.map(mapper).toList();
8077
}
8178

79+
/**
80+
* Maps stream to set
81+
* @param collection collection
82+
* @param mapper mapper
83+
* @return unmodifiable set
84+
* @param <E> element
85+
* @param <R> return value
86+
*/
87+
@NotNull
88+
@Unmodifiable
89+
public static <E, R> Set<R> mapToSet(@NotNull Collection<E> collection, @NotNull Function<E, R> mapper) {
90+
return collection.isEmpty() ? Collections.emptySet() : mapToSet(collection.stream(), mapper);
91+
}
92+
93+
/**
94+
* Maps stream to set
95+
* @param stream stream
96+
* @param mapper mapper
97+
* @return unmodifiable set
98+
* @param <E> element
99+
* @param <R> return value
100+
*/
101+
@NotNull
102+
@Unmodifiable
103+
public static <E, R> Set<R> mapToSet(@NotNull Stream<E> stream, @NotNull Function<E, R> mapper) {
104+
return stream.map(mapper).collect(Collectors.toUnmodifiableSet());
105+
}
106+
82107
/**
83108
* Maps stream to JSON
84109
* @param collection collection

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
kotlin = "2.2.20"
2+
kotlin = "2.2.21"
33
paperweight = "2.0.0-beta.19"
44
resourcefactory = "1.3.1"
55

0 commit comments

Comments
 (0)