Skip to content

Commit

Permalink
Include component and stack data when Itemstack#save and Fluidstack#s…
Browse files Browse the repository at this point in the history
…ave throws from codec errors (#1297)
  • Loading branch information
baileyholl authored Jul 14, 2024
1 parent 0b58662 commit 9e0523b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
20 changes: 20 additions & 0 deletions patches/net/minecraft/world/item/ItemStack.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@
if (player != null && interactionresult.indicateItemUse()) {
player.awardStat(Stats.ITEM_USED.get(item));
}
@@ -382,7 +_,8 @@
if (this.isEmpty()) {
throw new IllegalStateException("Cannot encode empty ItemStack");
} else {
- return CODEC.encode(this, p_331900_.createSerializationContext(NbtOps.INSTANCE), p_330830_).getOrThrow();
+ // Neo: Logs extra information about this ItemStack on error
+ return net.neoforged.neoforge.common.util.DataComponentUtil.wrapEncodingExceptions(this, CODEC, p_331900_, p_330830_);
}
}

@@ -390,7 +_,8 @@
if (this.isEmpty()) {
throw new IllegalStateException("Cannot encode empty ItemStack");
} else {
- return CODEC.encodeStart(p_332160_.createSerializationContext(NbtOps.INSTANCE), this).getOrThrow();
+ // Neo: Logs extra information about this ItemStack on error
+ return net.neoforged.neoforge.common.util.DataComponentUtil.wrapEncodingExceptions(this, CODEC, p_332160_);
}
}

@@ -399,7 +_,7 @@
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.common.util;

import com.mojang.serialization.Codec;
import net.minecraft.Util;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentHolder;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.Nullable;

public class DataComponentUtil {
/**
* Wraps encoding exceptions and adds additional logging for a DataComponentHolder that failed to save.
*/
public static <T extends DataComponentHolder> Tag wrapEncodingExceptions(T componentHolder, Codec<T> codec, HolderLookup.Provider provider, Tag tag) {
try {
return codec.encode(componentHolder, provider.createSerializationContext(NbtOps.INSTANCE), tag).getOrThrow();
} catch (Exception exception) {
logDataComponentSaveError(componentHolder, exception, tag);
throw exception;
}
}

/**
* Wraps encoding exceptions and adds additional logging for a DataComponentHolder that failed to save.
*/
public static <T extends DataComponentHolder> Tag wrapEncodingExceptions(T componentHolder, Codec<T> codec, HolderLookup.Provider provider) {
try {
return codec.encodeStart(provider.createSerializationContext(NbtOps.INSTANCE), componentHolder).getOrThrow();
} catch (Exception exception) {
logDataComponentSaveError(componentHolder, exception, null);
throw exception;
}
}

/**
* Logs component information and tag data for a DataComponentHolder that failed to save.
* See {@link ItemStack#save } or {@link FluidStack#save }
*
* <pre>
* Example:
* Error saving [1 minecraft:dirt]. Original cause: java.lang.NullPointerException
* With components:
* {
* neoforge:test=>Test[s=null]
* minecraft:max_stack_size=>64
* minecraft:lore=>ItemLore[lines=[], styledLines=[]]
* minecraft:enchantments=>ItemEnchantments{enchantments={}, showInTooltip=true}
* minecraft:repair_cost=>0
* minecraft:attribute_modifiers=>ItemAttributeModifiers[modifiers=[], showInTooltip=true]
* minecraft:rarity=>COMMON
* }
* With tag: {}
* </pre>
*/
public static void logDataComponentSaveError(DataComponentHolder componentHolder, Exception original, @Nullable Tag tag) {
StringBuilder cause = new StringBuilder("Error saving [" + componentHolder + "]. Original cause: " + original);

cause.append("\nWith components:\n{");
componentHolder.getComponents().forEach((component) -> {
cause.append("\n\t").append(component);
});
cause.append("\n}");
if (tag != null) {
cause.append("\nWith tag: ").append(tag);
}
Util.logAndPauseIfInIde(cause.toString());
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/neoforged/neoforge/fluids/FluidStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.common.MutableDataComponentHolder;
import net.neoforged.neoforge.common.util.DataComponentUtil;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

Expand Down Expand Up @@ -270,7 +271,7 @@ public Tag save(HolderLookup.Provider lookupProvider, Tag prefix) {
if (this.isEmpty()) {
throw new IllegalStateException("Cannot encode empty FluidStack");
} else {
return CODEC.encode(this, lookupProvider.createSerializationContext(NbtOps.INSTANCE), prefix).getOrThrow();
return DataComponentUtil.wrapEncodingExceptions(this, CODEC, lookupProvider, prefix);
}
}

Expand All @@ -283,7 +284,7 @@ public Tag save(HolderLookup.Provider lookupProvider) {
if (this.isEmpty()) {
throw new IllegalStateException("Cannot encode empty FluidStack");
} else {
return CODEC.encodeStart(lookupProvider.createSerializationContext(NbtOps.INSTANCE), this).getOrThrow();
return DataComponentUtil.wrapEncodingExceptions(this, CODEC, lookupProvider);
}
}

Expand Down

0 comments on commit 9e0523b

Please sign in to comment.