Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Handle exceptions when loading accessories from NBT
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalalb committed May 8, 2021
1 parent 16e975b commit f1e494a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/api/java/doggytalents/api/registry/AccessoryInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,26 @@ public final void writeInstance(CompoundNBT compound) {
this.getAccessory().write(this, compound);
}

/**
* Reads the accessory from the given NBT data. If the accessory id is not
* valid or an exception is thrown during loading then an empty optional
* is returned.
*/
public static Optional<AccessoryInstance> readInstance(CompoundNBT compound) {
ResourceLocation rl = ResourceLocation.tryCreate(compound.getString("type"));
if (DoggyTalentsAPI.ACCESSORIES.containsKey(rl)) {
Accessory type = DoggyTalentsAPI.ACCESSORIES.getValue(rl);
return Optional.of(type.read(compound));
} else {
ResourceLocation rl = null;
try {
rl = ResourceLocation.tryCreate(compound.getString("type"));
if (DoggyTalentsAPI.ACCESSORIES.containsKey(rl)) {
Accessory type = DoggyTalentsAPI.ACCESSORIES.getValue(rl);
return Optional.of(type.read(compound));
} else {
DoggyTalentsAPI.LOGGER.warn("Failed to load accessory {}", compound);
}
} catch (Exception e) {
DoggyTalentsAPI.LOGGER.warn("Failed to load accessory {}", rl);
return Optional.empty();
}

return Optional.empty();
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit f1e494a

Please sign in to comment.