mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 04:46:37 +00:00
优化实体包构造速度
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 乐魂
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user