Skip to content

Fix entities with persist=false copied in an invalid state #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ private static String getEntityId(Entity entity) {
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
entity.save(tag);
private static boolean readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
return entity.save(tag);
}

private static Block getBlockFromType(BlockType blockType) {
Expand Down Expand Up @@ -471,15 +471,12 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();

// Do not allow creating of passenger entity snapshots, passengers are included in the vehicle entity
if (mcEntity.isPassenger()) {
return null;
}

String id = getEntityId(mcEntity);

net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
readEntityIntoTag(mcEntity, tag);
if (!readEntityIntoTag(mcEntity, tag)) {
return null;
}
return new BaseEntity(
EntityTypes.get(id),
LazyReference.from(() -> (LinCompoundTag) toNative(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ private static String getEntityId(Entity entity) {
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
entity.save(tag);
private static boolean readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
return entity.save(tag);
}

private static Block getBlockFromType(BlockType blockType) {
Expand Down Expand Up @@ -471,15 +471,12 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();

// Do not allow creating of passenger entity snapshots, passengers are included in the vehicle entity
if (mcEntity.isPassenger()) {
return null;
}

String id = getEntityId(mcEntity);

net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
readEntityIntoTag(mcEntity, tag);
if (!readEntityIntoTag(mcEntity, tag)) {
return null;
}
return new BaseEntity(
EntityTypes.get(id),
LazyReference.from(() -> (LinCompoundTag) toNative(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ private static String getEntityId(Entity entity) {
* @param entity the entity
* @param tag the tag
*/
private static void readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
entity.save(tag);
private static boolean readEntityIntoTag(Entity entity, net.minecraft.nbt.CompoundTag tag) {
return entity.save(tag);
}

private static Block getBlockFromType(BlockType blockType) {
Expand Down Expand Up @@ -471,15 +471,12 @@ public BaseEntity getEntity(org.bukkit.entity.Entity entity) {
CraftEntity craftEntity = ((CraftEntity) entity);
Entity mcEntity = craftEntity.getHandle();

// Do not allow creating of passenger entity snapshots, passengers are included in the vehicle entity
if (mcEntity.isPassenger()) {
return null;
}

String id = getEntityId(mcEntity);

net.minecraft.nbt.CompoundTag tag = new net.minecraft.nbt.CompoundTag();
readEntityIntoTag(mcEntity, tag);
if (!readEntityIntoTag(mcEntity, tag)) {
return null;
}
return new BaseEntity(
EntityTypes.get(id),
LazyReference.from(() -> (LinCompoundTag) toNative(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ public FabricEntity(net.minecraft.world.entity.Entity entity) {
@Override
public BaseEntity getState() {
net.minecraft.world.entity.Entity entity = entityRef.get();
if (entity == null || entity.isPassenger()) {
if (entity == null) {
return null;
}
ResourceLocation id = FabricWorldEdit.getRegistry(Registries.ENTITY_TYPE).getKey(entity.getType());
CompoundTag tag = new CompoundTag();
entity.saveWithoutId(tag);
if (!entity.save(tag)) {
return null;
}
ResourceLocation id = FabricWorldEdit.getRegistry(Registries.ENTITY_TYPE).getKey(entity.getType());
return new BaseEntity(
EntityTypes.get(id.toString()),
LazyReference.from(() -> NBTConverter.fromNative(tag))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ public NeoForgeEntity(net.minecraft.world.entity.Entity entity) {
@Override
public BaseEntity getState() {
net.minecraft.world.entity.Entity entity = entityRef.get();
if (entity == null || entity.isPassenger()) {
if (entity == null) {
return null;
}
ResourceLocation id = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType());
CompoundTag tag = new CompoundTag();
entity.saveWithoutId(tag);
return new BaseEntity(EntityTypes.get(id.toString()), LazyReference.from(() -> NBTConverter.fromNative(tag)));
if (!entity.save(tag)) {
return null;
}
ResourceLocation id = BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType());
return new BaseEntity(
EntityTypes.get(id.toString()),
LazyReference.from(() -> NBTConverter.fromNative(tag))
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public BaseEntity getState() {
if (entity == null || entity.vehicle().isPresent()) {
return null;
}
EntityType entityType = EntityType.REGISTRY.get(entity.type().key(RegistryTypes.ENTITY_TYPE).asString());
org.spongepowered.api.entity.EntityType<?> spongeEntityType = entity.type();
if (spongeEntityType.isTransient()) {
return null;
}
EntityType entityType = EntityType.REGISTRY.get(spongeEntityType.key(RegistryTypes.ENTITY_TYPE).asString());
if (entityType == null) {
return null;
}
Expand Down
Loading