| 1 | package net.minecraft.client.model.geom.builders;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.Map.Entry;
|
| 9 | import java.util.function.UnaryOperator;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.model.geom.ModelPart;
|
| 14 | import net.minecraft.client.model.geom.PartPose;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class PartDefinition {
|
| 18 | private final List<CubeDefinition> cubes;
|
| 19 | private final PartPose partPose;
|
| 20 | private final Map<String, PartDefinition> children = Maps.newHashMap();
|
| 21 |
|
| 22 | PartDefinition(final List<CubeDefinition> cubes, final PartPose partPose) {
|
| 23 | this.cubes = cubes;
|
| 24 | this.partPose = partPose;
|
| 25 | }
|
| 26 |
|
| 27 | public PartDefinition addOrReplaceChild(final String name, final CubeListBuilder cubes, final PartPose partPose) {
|
| 28 | PartDefinition child = new PartDefinition(cubes.getCubes(), partPose);
|
| 29 | return this.addOrReplaceChild(name, child);
|
| 30 | }
|
| 31 |
|
| 32 | public PartDefinition addOrReplaceChild(final String name, final PartDefinition child) {
|
| 33 | PartDefinition previous = this.children.put(name, child);
|
| 34 | if (previous != null) {
|
| 35 | child.children.putAll(previous.children);
|
| 36 | }
|
| 37 |
|
| 38 | return child;
|
| 39 | }
|
| 40 |
|
| 41 | public PartDefinition clearRecursively() {
|
| 42 | for (String name : this.children.keySet()) {
|
| 43 | this.clearChild(name).clearRecursively();
|
| 44 | }
|
| 45 |
|
| 46 | return this;
|
| 47 | }
|
| 48 |
|
| 49 | public PartDefinition clearChild(final String name) {
|
| 50 | PartDefinition child = this.children.get(name);
|
| 51 | if (child == null) {
|
| 52 | throw new IllegalArgumentException("No child with name: " + name);
|
| 53 | } else {
|
| 54 | return this.addOrReplaceChild(name, CubeListBuilder.create(), child.partPose);
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public void retainPartsAndChildren(final Set<String> parts) {
|
| 59 | for (Entry<String, PartDefinition> entry : this.children.entrySet()) {
|
| 60 | PartDefinition child = entry.getValue();
|
| 61 | if (!parts.contains(entry.getKey())) {
|
| 62 | this.addOrReplaceChild(entry.getKey(), CubeListBuilder.create(), child.partPose).retainPartsAndChildren(parts);
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | public void retainExactParts(final Set<String> parts) {
|
| 68 | for (Entry<String, PartDefinition> entry : this.children.entrySet()) {
|
| 69 | PartDefinition child = entry.getValue();
|
| 70 | if (parts.contains(entry.getKey())) {
|
| 71 | child.clearRecursively();
|
| 72 | } else {
|
| 73 | this.addOrReplaceChild(entry.getKey(), CubeListBuilder.create(), child.partPose).retainExactParts(parts);
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public ModelPart bake(final int texScaleX, final int texScaleY) {
|
| 79 | Object2ObjectArrayMap<String, ModelPart> bakedChildren = this.children
|
| 80 | .entrySet()
|
| 81 | .stream()
|
| 82 | .collect(Collectors.toMap(Entry::getKey, e -> ((PartDefinition)e.getValue()).bake(texScaleX, texScaleY), (a, b) -> a, Object2ObjectArrayMap::new));
|
| 83 | List<ModelPart.Cube> bakedCubes = this.cubes.stream().map(definition -> definition.bake(texScaleX, texScaleY)).toList();
|
| 84 | ModelPart result = new ModelPart(bakedCubes, bakedChildren);
|
| 85 | result.setInitialPose(this.partPose);
|
| 86 | result.loadPose(this.partPose);
|
| 87 | return result;
|
| 88 | }
|
| 89 |
|
| 90 | public PartDefinition getChild(final String name) {
|
| 91 | return this.children.get(name);
|
| 92 | }
|
| 93 |
|
| 94 | public Set<Entry<String, PartDefinition>> getChildren() {
|
| 95 | return this.children.entrySet();
|
| 96 | }
|
| 97 |
|
| 98 | public PartDefinition transformed(final UnaryOperator<PartPose> function) {
|
| 99 | PartDefinition newPart = new PartDefinition(this.cubes, function.apply(this.partPose));
|
| 100 | newPart.children.putAll(this.children);
|
| 101 | return newPart;
|
| 102 | }
|
| 103 | }
|
| 104 |
|