9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 12:56:28 +00:00

优化实体包构造速度

This commit is contained in:
XiaoMoMi
2025-04-19 19:11:46 +08:00
parent 455712b59a
commit 50d0bb086d
13 changed files with 62 additions and 81 deletions

View File

@@ -30,16 +30,13 @@ items:
translation: 0,0.5,0
hitboxes:
- position: 0,0,0
width: 1
height: 1
type: shulker
direction: east
peek: 100
interactive: true
interaction-entity: true
seats:
- 0,0,-0.1 0
- position: 1,0,0
width: 1
height: 1
interactive: true
seats:
- 1,0,-0.1 0
loot:
template: "default:loot_table/basic"

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.bukkit.entity.data;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.util.ReflectionUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
@@ -98,11 +99,7 @@ public class EntityDataValue {
}
public static Object create(int id, Object serializer, Object value) {
try {
Object entityDataAccessor = Reflections.constructor$EntityDataAccessor.newInstance(id, serializer);
return Reflections.method$SynchedEntityData$DataValue$create.invoke(null, entityDataAccessor, value);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Object entityDataAccessor = FastNMS.INSTANCE.constructor$EntityDataAccessor(id, serializer);
return FastNMS.INSTANCE.method$SynchedEntityData$DataValue$create(entityDataAccessor, value);
}
}

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.bukkit.entity.furniture;
import net.momirealms.craftengine.bukkit.entity.data.ItemDisplayEntityData;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.entity.furniture.AbstractFurnitureElement;
import net.momirealms.craftengine.core.entity.furniture.Billboard;
@@ -33,17 +34,13 @@ public class BukkitFurnitureElement extends AbstractFurnitureElement {
}
@Override
public void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer<Object> packets) {
try {
Vector3f offset = conjugated.transform(new Vector3f(position()));
packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance(
entityId, UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw,
Reflections.instance$EntityType$ITEM_DISPLAY, 0, Reflections.instance$Vec3$Zero, 0
));
packets.accept(Reflections.constructor$ClientboundSetEntityDataPacket.newInstance(entityId, getCachedValues()));
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Failed to construct element spawn packet", e);
}
public void initPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer<Object> packets) {
Vector3f offset = conjugated.transform(new Vector3f(position()));
packets.accept(FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket(
entityId, UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw,
Reflections.instance$EntityType$ITEM_DISPLAY, 0, Reflections.instance$Vec3$Zero, 0
));
packets.accept(FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, getCachedValues()));
}
private synchronized List<Object> getCachedValues() {

View File

@@ -143,9 +143,6 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
elements.add(furnitureElement);
}
// add colliders
List<Collider> colliders = new ArrayList<>();
// external model providers
Optional<ExternalModel> externalModel;
if (placementArguments.containsKey("model-engine")) {
@@ -162,7 +159,6 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
for (Map<String, Object> config : hitboxConfigs) {
HitBox hitBox = HitBoxTypes.fromMap(config);
hitboxes.add(hitBox);
hitBox.optionalCollider().ifPresent(colliders::add);
}
if (hitboxes.isEmpty() && externalModel.isEmpty()) {
hitboxes.add(InteractionHitBox.DEFAULT);
@@ -180,7 +176,6 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
placements.put(anchorType, new CustomFurniture.Placement(
elements.toArray(new FurnitureElement[0]),
hitboxes.toArray(new HitBox[0]),
colliders.toArray(new Collider[0]),
rotationRule,
alignmentRule,
externalModel
@@ -189,7 +184,6 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
placements.put(anchorType, new CustomFurniture.Placement(
elements.toArray(new FurnitureElement[0]),
hitboxes.toArray(new HitBox[0]),
colliders.toArray(new Collider[0]),
RotationRule.ANY,
AlignmentRule.CENTER,
externalModel

View File

@@ -90,10 +90,12 @@ public class LoadedFurniture implements Furniture {
List<Object> packets = new ArrayList<>();
List<Object> minimizedPackets = new ArrayList<>();
List<Collider> colliders = new ArrayList<>();
for (FurnitureElement element : placement.elements()) {
int entityId = Reflections.instance$Entity$ENTITY_COUNTER.incrementAndGet();
fakeEntityIds.add(entityId);
element.addSpawnPackets(entityId, x, y, z, yaw, conjugated, packet -> {
element.initPackets(entityId, x, y, z, yaw, conjugated, packet -> {
packets.add(packet);
if (this.minimized) minimizedPackets.add(packet);
});
@@ -105,12 +107,12 @@ public class LoadedFurniture implements Furniture {
mainEntityIds.add(entityId);
this.hitBoxes.put(entityId, hitBox);
}
hitBox.addSpawnPackets(ids, x, y, z, yaw, conjugated, (packet, canBeMinimized) -> {
hitBox.initPacketsAndColliders(ids, x, y, z, yaw, conjugated, (packet, canBeMinimized) -> {
packets.add(packet);
if (this.minimized && !canBeMinimized) {
minimizedPackets.add(packet);
}
});
}, colliders::add);
}
try {
this.cachedSpawnPacket = FastNMS.INSTANCE.constructor$ClientboundBundlePacket(packets);
@@ -122,12 +124,12 @@ public class LoadedFurniture implements Furniture {
}
this.fakeEntityIds = fakeEntityIds;
this.entityIds = mainEntityIds;
int colliderSize = placement.colliders().length;
int colliderSize = colliders.size();
this.collisionEntities = new CollisionEntity[colliderSize];
if (colliderSize != 0) {
Object world = FastNMS.INSTANCE.field$CraftWorld$ServerLevel(this.location.getWorld());
for (int i = 0; i < colliderSize; i++) {
Collider collider = placement.colliders()[i];
Collider collider = colliders.get(i);
Vector3f offset = conjugated.transform(new Vector3f(collider.position()));
Vector3d offset1 = collider.point1();
Vector3d offset2 = collider.point2();

View File

@@ -48,14 +48,14 @@ public class CustomHitBox extends AbstractHitBox {
}
@Override
public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets) {
public void initPacketsAndColliders(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets, Consumer<Collider> collider) {
Vector3f offset = conjugated.transform(new Vector3f(position()));
try {
packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance(
packets.accept(FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket(
entityId[0], UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw,
FastNMS.INSTANCE.toNMSEntityType(this.entityType), 0, Reflections.instance$Vec3$Zero, 0
), true);
packets.accept(Reflections.constructor$ClientboundSetEntityDataPacket.newInstance(entityId[0], List.copyOf(this.cachedValues)), true);
packets.accept(FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId[0], List.copyOf(this.cachedValues)), true);
if (VersionHelper.isVersionNewerThan1_20_5() && this.scale != 1) {
Object attributeInstance = Reflections.constructor$AttributeInstance.newInstance(Reflections.instance$Holder$Attribute$scale, (Consumer<?>) (o) -> {});
Reflections.method$AttributeInstance$setBaseValue.invoke(attributeInstance, this.scale);

View File

@@ -8,6 +8,7 @@ import org.joml.Vector3f;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class HappyGhastHitBox extends AbstractHitBox {
@@ -29,7 +30,7 @@ public class HappyGhastHitBox extends AbstractHitBox {
}
@Override
public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets) {
public void initPacketsAndColliders(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets, Consumer<Collider> collider) {
// todo 乐魂
}

View File

@@ -1,6 +1,7 @@
package net.momirealms.craftengine.bukkit.entity.furniture.hitbox;
import net.momirealms.craftengine.bukkit.entity.data.InteractionEntityData;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.entity.furniture.*;
import net.momirealms.craftengine.core.util.Key;
@@ -13,6 +14,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class InteractionHitBox extends AbstractHitBox {
@@ -46,17 +48,13 @@ public class InteractionHitBox extends AbstractHitBox {
}
@Override
public void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets) {
public void initPacketsAndColliders(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets, Consumer<Collider> collider) {
Vector3f offset = conjugated.transform(new Vector3f(position()));
try {
packets.accept(Reflections.constructor$ClientboundAddEntityPacket.newInstance(
entityId[0], UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw,
Reflections.instance$EntityType$INTERACTION, 0, Reflections.instance$Vec3$Zero, 0
), true);
packets.accept(Reflections.constructor$ClientboundSetEntityDataPacket.newInstance(entityId[0], List.copyOf(this.cachedValues)), true);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Failed to construct interaction hitbox spawn packet", e);
}
packets.accept(FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket(
entityId[0], UUID.randomUUID(), x + offset.x, y + offset.y, z - offset.z, 0, yaw,
Reflections.instance$EntityType$INTERACTION, 0, Reflections.instance$Vec3$Zero, 0
), true);
packets.accept(FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId[0], List.copyOf(this.cachedValues)), true);
}
@Override

View File

@@ -625,15 +625,15 @@ public class Reflections {
)
);
public static final Constructor<?> constructor$ClientboundAddEntityPacket = requireNonNull(
ReflectionUtils.getConstructor(clazz$ClientboundAddEntityPacket,
int.class, UUID.class,
double.class, double.class, double.class,
float.class, float.class,
clazz$EntityType,
int.class, clazz$Vec3, double.class
)
);
// public static final Constructor<?> constructor$ClientboundAddEntityPacket = requireNonNull(
// ReflectionUtils.getConstructor(clazz$ClientboundAddEntityPacket,
// int.class, UUID.class,
// double.class, double.class, double.class,
// float.class, float.class,
// clazz$EntityType,
// int.class, clazz$Vec3, double.class
// )
// );
public static final Constructor<?> constructor$ClientboundRemoveEntitiesPacket = requireNonNull(
ReflectionUtils.getConstructor(clazz$ClientboundRemoveEntitiesPacket, int[].class)
@@ -685,10 +685,10 @@ public class Reflections {
)
);
public static final Constructor<?> constructor$ClientboundSetEntityDataPacket = requireNonNull(
ReflectionUtils.getConstructor(clazz$ClientboundSetEntityDataPacket,
int.class, List.class)
);
// public static final Constructor<?> constructor$ClientboundSetEntityDataPacket = requireNonNull(
// ReflectionUtils.getConstructor(clazz$ClientboundSetEntityDataPacket,
// int.class, List.class)
// );
public static final Class<?> clazz$EntityDataSerializers = requireNonNull(
ReflectionUtils.getClazz(
@@ -711,11 +711,11 @@ public class Reflections {
)
);
public static final Constructor<?> constructor$EntityDataAccessor = requireNonNull(
ReflectionUtils.getConstructor(
clazz$EntityDataAccessor, int.class, clazz$EntityDataSerializer
)
);
// public static final Constructor<?> constructor$EntityDataAccessor = requireNonNull(
// ReflectionUtils.getConstructor(
// clazz$EntityDataAccessor, int.class, clazz$EntityDataSerializer
// )
// );
public static final Class<?> clazz$SynchedEntityData = requireNonNull(
ReflectionUtils.getClazz(
@@ -737,11 +737,11 @@ public class Reflections {
)
);
public static final Method method$SynchedEntityData$DataValue$create = requireNonNull(
ReflectionUtils.getMethod(
clazz$SynchedEntityData$DataValue, clazz$SynchedEntityData$DataValue, clazz$EntityDataAccessor, Object.class
)
);
// public static final Method method$SynchedEntityData$DataValue$create = requireNonNull(
// ReflectionUtils.getMethod(
// clazz$SynchedEntityData$DataValue, clazz$SynchedEntityData$DataValue, clazz$EntityDataAccessor, Object.class
// )
// );
public static final Method method$Component$empty = requireNonNull(
ReflectionUtils.getStaticMethod(

View File

@@ -58,7 +58,6 @@ public class CustomFurniture {
public record Placement(FurnitureElement[] elements,
HitBox[] hitBoxes,
Collider[] colliders,
RotationRule rotationRule,
AlignmentRule alignmentRule,
Optional<ExternalModel> externalModel) {

View File

@@ -21,5 +21,5 @@ public interface FurnitureElement {
Vector3f position();
void addSpawnPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer<Object> packets);
void initPackets(int entityId, double x, double y, double z, float yaw, Quaternionf conjugated, Consumer<Object> packets);
}

View File

@@ -4,23 +4,19 @@ import net.momirealms.craftengine.core.util.Key;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
public interface HitBox {
Key type();
void addSpawnPackets(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets);
void initPacketsAndColliders(int[] entityId, double x, double y, double z, float yaw, Quaternionf conjugated, BiConsumer<Object, Boolean> packets, Consumer<Collider> collider);
int[] acquireEntityIds(Supplier<Integer> entityIdSupplier);
Seat[] seats();
Vector3f position();
default Optional<Collider> optionalCollider() {
return Optional.empty();
}
}

View File

@@ -51,7 +51,7 @@ byte_buddy_version=1.17.5
ahocorasick_version=0.6.3
snake_yaml_version=2.4
anti_grief_version=0.13
nms_helper_version=0.59.6
nms_helper_version=0.59.7
reactive_streams_version=1.0.4
amazon_awssdk_version=2.31.23
amazon_awssdk_eventstream_version=1.0.1