Walk this way

- Update instances using a walker object created at the top level
This commit is contained in:
Jozufozu 2024-09-14 17:46:54 -07:00
parent ed35c5a429
commit 5ffddeb221

View File

@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.ObjIntConsumer; import java.util.function.ObjIntConsumer;
@ -170,6 +171,16 @@ public final class InstanceTree {
} }
public void updateInstances(PoseStack poseStack) { public void updateInstances(PoseStack poseStack) {
// Need to use an anonymous class so it can reference this.
updateInstancesInner(poseStack, new Walker() {
@Override
public void accept(InstanceTree child) {
child.updateInstancesInner(poseStack, this);
}
});
}
private void updateInstancesInner(PoseStack poseStack, Walker walker) {
if (visible) { if (visible) {
poseStack.pushPose(); poseStack.pushPose();
translateAndRotate(poseStack); translateAndRotate(poseStack);
@ -179,9 +190,9 @@ public final class InstanceTree {
.setChanged(); .setChanged();
} }
for (InstanceTree child : children.values()) { // Use the bare HashMap.forEach because .values() always allocates a new collection.
child.updateInstances(poseStack); // We also don't want to store an array of children because that would statically use a lot more memory.
} children.forEach(walker);
poseStack.popPose(); poseStack.popPose();
} }
@ -292,4 +303,14 @@ public final class InstanceTree {
public interface ObjIntIntConsumer<T> { public interface ObjIntIntConsumer<T> {
void accept(T t, int i, int j); void accept(T t, int i, int j);
} }
// Helper interface for writing walking classes.
private interface Walker extends BiConsumer<String, InstanceTree> {
void accept(InstanceTree child);
@Override
default void accept(String name, InstanceTree child) {
accept(child);
}
}
} }