Skip to content

Commit

Permalink
Potentially better handling for #313.
Browse files Browse the repository at this point in the history
Error messages when attempting to save including the block position and
dimension instead of just straight up crashing.

Properly check for a null `tileId` when loading and generate a new
`tileId` if it's null, instead of if it's just not loaded properly. I'm
not sure if it's ever possible for this to take place, but it's worth
it.

Finally, if a player opens a container which doesn't have a generated
`tileId` for whatever reason, inform them of that fact.
  • Loading branch information
noobanidus committed Mar 30, 2024
1 parent 8ea5055 commit 9229f4c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
Expand Up @@ -211,7 +211,8 @@ public void load(CompoundTag compound) {
}
if (compound.hasUUID("tileId")) {
this.tileId = compound.getUUID("tileId");
} else if (this.tileId == null) {
}
if (this.tileId == null) {
getTileId();
}
if (compound.contains("LootrOpeners")) {
Expand Down
Expand Up @@ -117,7 +117,8 @@ public void load(CompoundTag compound) {
}
if (compound.hasUUID("tileId")) {
this.tileId = compound.getUUID("tileId");
} else if (this.tileId == null) {
}
if (this.tileId == null) {
getTileId();
}
if (compound.contains("LootrOpeners")) {
Expand Down
Expand Up @@ -232,7 +232,8 @@ public void load(CompoundTag compound) {
}
if (compound.hasUUID("tileId")) {
this.tileId = compound.getUUID("tileId");
} else if (this.tileId == null) {
}
if (this.tileId == null) {
getTileId();
}
if (compound.contains("LootrOpeners")) {
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/noobanidus/mods/lootr/data/ChestData.java
Expand Up @@ -78,6 +78,9 @@ public static String ID(UUID id) {
}

public static Supplier<ChestData> ref_id(ResourceKey<Level> dimension, BlockPos pos, UUID id, NonNullList<ItemStack> base) {
if (id == null) {
throw new IllegalArgumentException("Can't create ChestData for custom container in dimension '" + dimension + "' at '" + pos + "' with a null id.");
}
return () -> {
ChestData data = new ChestData(ID(id));
data.pos = pos;
Expand All @@ -94,6 +97,9 @@ public static Supplier<ChestData> ref_id(ResourceKey<Level> dimension, BlockPos
}

public static Supplier<ChestData> id(ResourceKey<Level> dimension, BlockPos pos, UUID id) {
if (id == null) {
throw new IllegalArgumentException("Can't create ChestData for container in dimension '" + dimension + "' at '" + pos + "' with a null id.");
}
return () -> {
ChestData data = new ChestData(ID(id));
data.pos = pos;
Expand All @@ -107,6 +113,9 @@ public static Supplier<ChestData> id(ResourceKey<Level> dimension, BlockPos pos,
}

public static Supplier<ChestData> entity(ResourceKey<Level> dimension, BlockPos pos, UUID entityId) {
if (entityId == null) {
throw new IllegalArgumentException("Can't create ChestData for minecart in dimension '" + dimension + "' at '" + pos + "' with a null entityId.");
}
return () -> {
ChestData data = new ChestData(ID(entityId));
data.pos = pos;
Expand Down Expand Up @@ -199,15 +208,7 @@ public SpecialChestInventory createInventory(ServerPlayer player, LootFiller fil
result = new SpecialChestInventory(this, items, cart.getDisplayName());
lootTable = cart.lootTable;
} else {
/* if (world.dimension() != dimension) {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if (server == null) {
return null;
}
world = server.getLevel(dimension);
}*/

if (/*world == null || */tile == null) {
if (tile == null) {
return null;
}

Expand Down Expand Up @@ -327,7 +328,11 @@ public CompoundTag save(CompoundTag compound) {
} else {
LootrAPI.LOG.error("Attempted to save a data file with no `dimension`: '" + key + "'");
}
compound.putUUID("uuid", uuid);
if (uuid != null) {
compound.putUUID("uuid", uuid);
} else {
throw new IllegalStateException("Attempted to save a data file with no `uuid`: '" + key + "'. Located in dimension '" + dimension + "' at '" + pos + "'. This is an unrecoverable error.");
}
compound.putBoolean("custom", custom);
compound.putBoolean("entity", entity);
if (reference != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/noobanidus/mods/lootr/util/ChestUtil.java
Expand Up @@ -78,6 +78,10 @@ public static void handleLootChest(Block block, Level level, BlockPos pos, Playe
BlockEntity te = level.getBlockEntity(pos);
if (te instanceof ILootBlockEntity tile) {
UUID tileId = tile.getTileId();
if (tileId == null) {
player.displayClientMessage(Component.translatable("lootr.message.invalid_block").setStyle(Style.EMPTY.withColor(TextColor.fromLegacyFormat(ChatFormatting.RED)).withBold(true)), true);
return;
}
if (DataStorage.isDecayed(tileId)) {
level.destroyBlock(pos, true);
notifyDecay(player, tileId);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/lootr/lang/en_us.json
Expand Up @@ -13,6 +13,7 @@
"lootr.message.refresh_in": "This container will refresh its contents in %s seconds.",
"lootr.message.refresh_start": "The container will refresh with new contents in %s seconds!",
"lootr.message.invalid_table": "The loot table for this container [%s] resolved to empty and loot could not be generated. Check your `latest.log` for errors.",
"lootr.message.invalid_block": "The container that you have tried to open is invalid as it has not generated its identifier.",
"block.lootr.lootr_chest": "Loot Chest",
"block.lootr.lootr_trapped_chest": "Loot Chest",
"block.lootr.lootr_barrel": "Loot Barrel",
Expand Down

0 comments on commit 9229f4c

Please sign in to comment.