minecraft-src/net/minecraft/world/entity/EntityAttachments.java
2025-07-04 01:41:11 +03:00

102 lines
3.3 KiB
Java

package net.minecraft.world.entity;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
public class EntityAttachments {
private final Map<EntityAttachment, List<Vec3>> attachments;
EntityAttachments(Map<EntityAttachment, List<Vec3>> attachments) {
this.attachments = attachments;
}
public static EntityAttachments createDefault(float width, float height) {
return builder().build(width, height);
}
public static EntityAttachments.Builder builder() {
return new EntityAttachments.Builder();
}
public EntityAttachments scale(float xScale, float yScale, float zScale) {
Map<EntityAttachment, List<Vec3>> map = new EnumMap(EntityAttachment.class);
for (Entry<EntityAttachment, List<Vec3>> entry : this.attachments.entrySet()) {
map.put((EntityAttachment)entry.getKey(), scalePoints((List<Vec3>)entry.getValue(), xScale, yScale, zScale));
}
return new EntityAttachments(map);
}
private static List<Vec3> scalePoints(List<Vec3> attachmentPoints, float xScale, float yScale, float zScale) {
List<Vec3> list = new ArrayList(attachmentPoints.size());
for (Vec3 vec3 : attachmentPoints) {
list.add(vec3.multiply(xScale, yScale, zScale));
}
return list;
}
@Nullable
public Vec3 getNullable(EntityAttachment attachment, int index, float yRot) {
List<Vec3> list = (List<Vec3>)this.attachments.get(attachment);
return index >= 0 && index < list.size() ? transformPoint((Vec3)list.get(index), yRot) : null;
}
public Vec3 get(EntityAttachment attachment, int index, float yRot) {
Vec3 vec3 = this.getNullable(attachment, index, yRot);
if (vec3 == null) {
throw new IllegalStateException("Had no attachment point of type: " + attachment + " for index: " + index);
} else {
return vec3;
}
}
public Vec3 getClamped(EntityAttachment attachment, int index, float yRot) {
List<Vec3> list = (List<Vec3>)this.attachments.get(attachment);
if (list.isEmpty()) {
throw new IllegalStateException("Had no attachment points of type: " + attachment);
} else {
Vec3 vec3 = (Vec3)list.get(Mth.clamp(index, 0, list.size() - 1));
return transformPoint(vec3, yRot);
}
}
private static Vec3 transformPoint(Vec3 point, float yRot) {
return point.yRot(-yRot * (float) (Math.PI / 180.0));
}
public static class Builder {
private final Map<EntityAttachment, List<Vec3>> attachments = new EnumMap(EntityAttachment.class);
Builder() {
}
public EntityAttachments.Builder attach(EntityAttachment attachment, float x, float y, float z) {
return this.attach(attachment, new Vec3(x, y, z));
}
public EntityAttachments.Builder attach(EntityAttachment attachment, Vec3 poas) {
((List)this.attachments.computeIfAbsent(attachment, entityAttachment -> new ArrayList(1))).add(poas);
return this;
}
public EntityAttachments build(float width, float height) {
Map<EntityAttachment, List<Vec3>> map = new EnumMap(EntityAttachment.class);
for (EntityAttachment entityAttachment : EntityAttachment.values()) {
List<Vec3> list = (List<Vec3>)this.attachments.get(entityAttachment);
map.put(entityAttachment, list != null ? List.copyOf(list) : entityAttachment.createFallbackPoints(width, height));
}
return new EntityAttachments(map);
}
}
}