Skip to content

Commit

Permalink
chore: do not throw exception when there are unknown block entities &…
Browse files Browse the repository at this point in the history
… entities
  • Loading branch information
smartcmd committed Mar 23, 2024
1 parent 6e97395 commit 6aad18d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.allaymc.api.blockentity;

import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.blockentity.init.SimpleBlockEntityInitInfo;
import org.allaymc.api.blockentity.registry.BlockEntityTypeRegistry;
import org.allaymc.api.world.Dimension;
Expand All @@ -12,11 +13,15 @@
*
* @author daoge_cmd
*/
@Slf4j
public final class BlockEntityHelper {
public static BlockEntity fromNBT(Dimension dimension, NbtMap nbt) {
var id = nbt.getString("id");
var blockEntityType = BlockEntityTypeRegistry.getRegistry().get(id);
Objects.requireNonNull(blockEntityType, "Unknown block entity type: " + id);
if (blockEntityType == null) {
log.warn("Unknown block entity type: " + id);
return null;
}
return blockEntityType.createBlockEntity(SimpleBlockEntityInitInfo.builder().dimension(dimension).nbt(nbt).build());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.allaymc.api.entity;

import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.utils.Identifier;
import org.allaymc.api.entity.init.SimpleEntityInitInfo;
import org.allaymc.api.entity.registry.EntityTypeRegistry;
Expand All @@ -13,10 +14,15 @@
*
* @author daoge_cmd
*/
@Slf4j
public final class EntityHelper {
public static Entity fromNBT(Dimension dimension, NbtMap nbt) {
var identifier = new Identifier(nbt.getString("identifier"));
var entityType = Objects.requireNonNull(EntityTypeRegistry.getRegistry().get(identifier), "Unknown entity type " + identifier);
var entityType = EntityTypeRegistry.getRegistry().get(identifier);
if (entityType == null) {
log.warn("Unknown entity type " + identifier);
return null;
}
return entityType.createEntity(SimpleEntityInitInfo.builder().dimension(dimension).nbt(nbt).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void beforeSetChunk(Dimension dimension) {
if (blockEntityNbtList != null && !blockEntityNbtList.isEmpty()) {
for (var n : blockEntityNbtList) {
BlockEntity entity = BlockEntityHelper.fromNBT(dimension, n);
if (entity == null) continue;
Position3ic position = entity.getPosition();
var key = HashUtils.hashChunkXYZ(position.x() & 15, position.y(), position.z() & 15);
this.blockEntities.put(key, entity);
Expand All @@ -84,6 +85,7 @@ public void afterSetChunk(Dimension dimension) {
if (entityNbtList != null && !entityNbtList.isEmpty()) {
for (var n : entityNbtList) {
Entity entity = EntityHelper.fromNBT(dimension, n);
if (entity == null) continue;
dimension.getEntityService().addEntity(entity);
}
entityNbtList = null;
Expand Down

0 comments on commit 6aad18d

Please sign in to comment.