Skip to content

Commit

Permalink
Error handler at create entity
Browse files Browse the repository at this point in the history
  • Loading branch information
nk2IsHere committed Jan 7, 2025
1 parent 29ca7bb commit e3a606f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loader_version=0.16.9


# Mod Properties
mod_version = 1.6.0
mod_version = 1.6.1
maven_group = eu.nk2
archives_base_name = apathy

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/eu/nk2/apathy/ApathyMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private EntityType.EntityFactory<Entity> getEntityFactory(EntityType<Entity> ent
return ((ApathyMixinEntityTypeAccessor) entityType).getFactory();
} catch (Exception e) {
logger.error(
"For {}: ",
"Getting factory for {}: ",
entityType,
e
);
Expand All @@ -51,7 +51,7 @@ private void setEntityFactory(
((ApathyMixinEntityTypeAccessor) entityType).apathy$setCustomFactory(entityFactory);
} catch (Exception e) {
logger.error(
"For {}: ",
"Setting factory for {}: ",
entityType,
e
);
Expand All @@ -76,7 +76,7 @@ private Set<Goal> getGoalSetFromMobEntitySelector(
return ((ApathyMixinGoalSelectorAccessor) goalSelector).getGoals();
} catch (Exception e) {
logger.error(
"For {}: ",
"Getting {} for {}: ",
mobEntity,
e
);
Expand Down Expand Up @@ -216,9 +216,19 @@ public void onInitialize() {
.forEach(entityTypeToFactory -> setEntityFactory(
entityTypeToFactory.getLeft(),
(type, world) -> {
var entity = entityTypeToFactory
.getRight()
.create(type, world);
Entity entity;
try {
entity = entityTypeToFactory
.getRight()
.create(type, world);
} catch (Exception e) {
logger.error(
"Creating entity for {}: ",
entityTypeToFactory.getLeft(),
e
);
return null;
}

if (!(entity instanceof MobEntity mobEntity)) {
return entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public void createInjection(
SpawnReason reason,
CallbackInfoReturnable<T> cir
) {
EntityType<T> entityType = (EntityType<T>) (Object) this;
if(customFactory != null) cir.setReturnValue(customFactory.create(entityType, world));
var entityType = (EntityType<T>) (Object) this;
var factory = customFactory != null ? customFactory : getFactory();

cir.setReturnValue(
entityType.isEnabled(world.getEnabledFeatures())
? factory.create(entityType, world)
: null
);
}
}

0 comments on commit e3a606f

Please sign in to comment.