| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.EnumMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.Util;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public class EntityAttachments {
|
| 13 | private final Map<EntityAttachment, List<Vec3>> attachments;
|
| 14 |
|
| 15 | private EntityAttachments(final Map<EntityAttachment, List<Vec3>> attachments) {
|
| 16 | this.attachments = attachments;
|
| 17 | }
|
| 18 |
|
| 19 | public static EntityAttachments createDefault(final float width, final float height) {
|
| 20 | return builder().build(width, height);
|
| 21 | }
|
| 22 |
|
| 23 | public static EntityAttachments.Builder builder() {
|
| 24 | return new EntityAttachments.Builder();
|
| 25 | }
|
| 26 |
|
| 27 | public EntityAttachments scale(final float x, final float y, final float z) {
|
| 28 | return new EntityAttachments(Util.makeEnumMap(EntityAttachment.class, attachment -> {
|
| 29 | List<Vec3> list = new ArrayList<>();
|
| 30 |
|
| 31 | for (Vec3 vec3 : this.attachments.get(attachment)) {
|
| 32 | list.add(vec3.multiply(x, y, z));
|
| 33 | }
|
| 34 |
|
| 35 | return list;
|
| 36 | }));
|
| 37 | }
|
| 38 |
|
| 39 | public @Nullable Vec3 getNullable(final EntityAttachment attachment, final int index, final float rotY) {
|
| 40 | List<Vec3> points = this.attachments.get(attachment);
|
| 41 | return index >= 0 && index < points.size() ? transformPoint(points.get(index), rotY) : null;
|
| 42 | }
|
| 43 |
|
| 44 | public Vec3 get(final EntityAttachment attachment, final int index, final float rotY) {
|
| 45 | Vec3 point = this.getNullable(attachment, index, rotY);
|
| 46 | if (point == null) {
|
| 47 | throw new IllegalStateException("Had no attachment point of type: " + attachment + " for index: " + index);
|
| 48 | } else {
|
| 49 | return point;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | public Vec3 getAverage(final EntityAttachment attachment) {
|
| 54 | List<Vec3> points = this.attachments.get(attachment);
|
| 55 | if (points != null && !points.isEmpty()) {
|
| 56 | Vec3 sum = Vec3.ZERO;
|
| 57 |
|
| 58 | for (Vec3 point : points) {
|
| 59 | sum = sum.add(point);
|
| 60 | }
|
| 61 |
|
| 62 | return sum.scale(1.0F / points.size());
|
| 63 | } else {
|
| 64 | throw new IllegalStateException("No attachment points of type: PASSENGER");
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public Vec3 getClamped(final EntityAttachment attachment, final int index, final float rotY) {
|
| 69 | List<Vec3> points = this.attachments.get(attachment);
|
| 70 | if (points.isEmpty()) {
|
| 71 | throw new IllegalStateException("Had no attachment points of type: " + attachment);
|
| 72 | }
|
| 73 |
|
| 74 | Vec3 point = points.get(Mth.clamp(index, 0, points.size() - 1));
|
| 75 | return transformPoint(point, rotY);
|
| 76 | }
|
| 77 |
|
| 78 | private static Vec3 transformPoint(final Vec3 point, final float rotY) {
|
| 79 | return point.yRot(-rotY * (float) (Math.PI / 180.0));
|
| 80 | }
|
| 81 |
|
| 82 | public static class Builder {
|
| 83 | private final Map<EntityAttachment, List<Vec3>> attachments = new EnumMap<>(EntityAttachment.class);
|
| 84 |
|
| 85 | private Builder() {
|
| 86 | }
|
| 87 |
|
| 88 | public EntityAttachments.Builder attach(final EntityAttachment attachment, final float x, final float y, final float z) {
|
| 89 | return this.attach(attachment, new Vec3(x, y, z));
|
| 90 | }
|
| 91 |
|
| 92 | public EntityAttachments.Builder attach(final EntityAttachment attachment, final Vec3 point) {
|
| 93 | this.attachments.computeIfAbsent(attachment, a -> new ArrayList<>(1)).add(point);
|
| 94 | return this;
|
| 95 | }
|
| 96 |
|
| 97 | public EntityAttachments build(final float width, final float height) {
|
| 98 | Map<EntityAttachment, List<Vec3>> attachments = Util.makeEnumMap(EntityAttachment.class, attachment -> {
|
| 99 | List<Vec3> points = this.attachments.get(attachment);
|
| 100 | return points == null ? attachment.createFallbackPoints(width, height) : List.copyOf(points);
|
| 101 | });
|
| 102 | return new EntityAttachments(attachments);
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|