Skip to content

Commit

Permalink
Better null handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
noobanidus committed Jan 24, 2024
1 parent 53b6618 commit ec47db2
Showing 1 changed file with 56 additions and 10 deletions.
66 changes: 56 additions & 10 deletions src/main/java/noobanidus/mods/lootr/data/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

@SuppressWarnings("unused")
public class DataStorage {
public static final String ID_OLD = "Lootr-AdvancementData";
public static final String SCORED_OLD = "Lootr-ScoreData";
Expand All @@ -49,6 +50,8 @@ public static DimensionDataStorage getDataStorage() {
return null;
}
ServerLevel overworld = server.overworld();
// Sometimes `overworld` returns null. I have no idea why.
//noinspection ConstantValue
if (overworld == null) {
LootrAPI.LOG.error("The Overworld is null at this stage; Lootr cannot fetch data storage.");
return null;
Expand Down Expand Up @@ -259,7 +262,11 @@ public static SpecialChestInventory getInventory(Level level, UUID uuid, BlockPo
return null;
}

ChestData data = getInstanceUuid((ServerLevel) level, pos, uuid);
ChestData data = getContainerData((ServerLevel) level, pos, uuid);
if (data == null) {
// Error messages are already generated by `getContainerData`
return null;
}
SpecialChestInventory inventory = data.getInventory(player);
if (inventory == null) {
inventory = data.createInventory(player, filler, sizeSupplier, displaySupplier, tableSupplier, seedSupplier);
Expand All @@ -274,7 +281,11 @@ public static SpecialChestInventory getInventory(Level level, UUID uuid, BlockPo
return null;
}

ChestData data = getInstanceUuid((ServerLevel) level, pos, uuid);
ChestData data = getContainerData((ServerLevel) level, pos, uuid);
if (data == null) {
// Error messages are already generated by `getContainerData`
return null;
}
SpecialChestInventory inventory = data.getInventory(player);
if (inventory == null) {
inventory = data.createInventory(player, filler, blockEntity, tableSupplier, seedSupplier);
Expand All @@ -289,7 +300,11 @@ public static SpecialChestInventory getInventory(Level world, UUID uuid, BlockPo
return null;
}

ChestData data = getInstanceUuid((ServerLevel) world, pos, uuid);
ChestData data = getContainerData((ServerLevel) world, pos, uuid);
if (data == null) {
// Error messages are already generated by `getContainerData`
return null;
}
SpecialChestInventory inventory = data.getInventory(player);
if (inventory == null) {
inventory = data.createInventory(player, filler, tile);
Expand All @@ -303,7 +318,11 @@ public static SpecialChestInventory getInventory(Level world, UUID uuid, NonNull
if (world.isClientSide || !(world instanceof ServerLevel)) {
return null;
}
ChestData data = getInstanceInventory((ServerLevel) world, pos, uuid, base);
ChestData data = getReferenceContainerData((ServerLevel) world, pos, uuid, base);
if (data == null) {
// Error messages are already generated by `getReferenceContainerData`
return null;
}
SpecialChestInventory inventory = data.getInventory(player);
if (inventory == null) {
inventory = data.createInventory(player, data.customInventory(), tile);
Expand All @@ -314,8 +333,19 @@ public static SpecialChestInventory getInventory(Level world, UUID uuid, NonNull

// TODO: This is non-optimal and can result in poorly loaded containers.
public static boolean clearInventories(UUID uuid) {
ServerLevel world = ServerLifecycleHooks.getCurrentServer().overworld();
DimensionDataStorage data = world.getDataStorage();
DimensionDataStorage data = getDataStorage();
if (data == null) {
// Errors are already generated in `getDataStorage`
return false;
}
// Server being null is already handled in `getDataStorage`
@SuppressWarnings("resource") ServerLevel world = ServerLifecycleHooks.getCurrentServer().overworld();
// This can actually be null on occasion.
//noinspection ConstantValue
if (world == null) {
LootrAPI.LOG.error("Overworld is null while attempting to clear inventories for '" + uuid.toString() + "'; Lootr cannot clear inventories.");
return false;
}
Path dataPath = world.getServer().getWorldPath(new LevelResource("data")).resolve("lootr");

List<String> ids = new ArrayList<>();
Expand Down Expand Up @@ -353,7 +383,11 @@ public static SpecialChestInventory getInventory(Level world, LootrChestMinecart
return null;
}

ChestData data = getInstance((ServerLevel) world, cart.blockPosition(), cart.getUUID());
ChestData data = getEntityData((ServerLevel) world, cart.blockPosition(), cart.getUUID());
if (data == null) {
// Error messages are already generated by `getEntityData`
return null;
}
SpecialChestInventory inventory = data.getInventory(player);
if (inventory == null) {
inventory = data.createInventory(player, filler, null);
Expand All @@ -367,7 +401,11 @@ public static void refreshInventory(Level level, BlockPos pos, UUID uuid, Server
return;
}

ChestData data = getInstanceUuid((ServerLevel) level, pos, uuid);
ChestData data = getContainerData((ServerLevel) level, pos, uuid);
if (data == null) {
// Error messages are already generated by `getContainerData`
return;
}
data.clear();
data.setDirty();
}
Expand All @@ -376,7 +414,11 @@ public static void refreshInventory(Level world, BlockPos pos, UUID uuid, NonNul
if (world.isClientSide() || !(world instanceof ServerLevel)) {
return;
}
ChestData data = getInstanceInventory((ServerLevel) world, pos, uuid, base);
ChestData data = getReferenceContainerData((ServerLevel) world, pos, uuid, base);
if (data == null) {
// Error messages are already generated by `getContainerReferenceData`
return;
}
data.clear();
data.setDirty();
}
Expand All @@ -386,7 +428,11 @@ public static void refreshInventory(Level world, LootrChestMinecartEntity cart,
return;
}

ChestData data = getInstance((ServerLevel) world, cart.blockPosition(), cart.getUUID());
ChestData data = getContainerData((ServerLevel) world, cart.blockPosition(), cart.getUUID());
if (data == null) {
// Error messages are already generated by `getContainerData`
return;
}
data.clear();
data.setDirty();
}
Expand Down

0 comments on commit ec47db2

Please sign in to comment.