Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/client/java/red/jackf/chesttracker/impl/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ public static Optional<MemoryBankImpl> load(String id) {
return existing;

var level = Minecraft.getInstance().level;
var connection = Minecraft.getInstance().getConnection();
HolderLookup.Provider registries = null;

if (level != null) {
if (connection != null) {
registries = connection.registryAccess();
} else if (level != null) {
registries = level.registryAccess();
}

Expand All @@ -94,9 +97,12 @@ public static void save(MemoryBankImpl bank) {
}

var level = Minecraft.getInstance().level;
var connection = Minecraft.getInstance().getConnection();
HolderLookup.Provider registries = null;

if (level != null) {
if (connection != null) {
registries = connection.registryAccess();
} else if (level != null) {
registries = level.registryAccess();
}

Expand Down
23 changes: 22 additions & 1 deletion src/client/java/red/jackf/chesttracker/impl/util/ModCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@
* Codecs for classes that aren't ours
*/
public class ModCodecs {
private static final Codec<DataComponentPatch> SAFE_COMPONENT_PATCH_CODEC = new Codec<>() {
@Override
public <T> DataResult<Pair<DataComponentPatch, T>> decode(DynamicOps<T> ops, T input) {
DataResult<Pair<DataComponentPatch, T>> res = DataComponentPatch.CODEC.decode(ops, input);
if (res.isError()) {
return DataResult.success(Pair.of(DataComponentPatch.EMPTY, input));
}
return res;
}

@Override
public <T> DataResult<T> encode(DataComponentPatch input, DynamicOps<T> ops, T prefix) {
DataResult<T> res = DataComponentPatch.CODEC.encode(input, ops, prefix);
if (res.isError()) {
FileUtil.LOGGER.warn("Failed to encode item DataComponentPatch ({}); falling back to empty patch for item", res.error().get().message());
return DataComponentPatch.CODEC.encode(DataComponentPatch.EMPTY, ops, prefix);
}
return res;
}
};

/**
* Identical to {@link ItemStack#OPTIONAL_CODEC}, but will not enforce a max stack size of 99.
*/
Expand All @@ -35,7 +56,7 @@ public class ModCodecs {
// Item.CODEC.fieldOf("id").forGetter(ItemStack::getItem),
Item.CODEC.fieldOf("id").forGetter(stack -> BuiltInRegistries.ITEM.wrapAsHolder(stack.getItem())),
ExtraCodecs.POSITIVE_INT.fieldOf("count").orElse(1).forGetter(ItemStack::getCount),
DataComponentPatch.CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter(ItemStack::getComponentsPatch)
SAFE_COMPONENT_PATCH_CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter(ItemStack::getComponentsPatch)
).apply(instance, (Holder<Item> itemHolder, Integer count, DataComponentPatch patch) -> {
ItemStack stack = new ItemStack(itemHolder, count);
stack.applyComponents(patch);
Expand Down
Loading