Skip to content

Commit

Permalink
More tool initialization fixes
Browse files Browse the repository at this point in the history
Fix recursion in the tool stack constructor due to vanilla calling veriffyTagAfterLoad and setDamage/getDamage in the setter
Fix tools that are missing materials building stats before receiving materials
  • Loading branch information
KnightMiner committed May 22, 2022
1 parent 982acbf commit 643768d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private static ToolStack from(ItemStack stack, boolean copyNbt) {
if (nbt == null) {
nbt = new CompoundTag();
if (!copyNbt) {
stack.setTag(nbt);
// bypass the setter as vanilla insists on setting damage values there, along with verifying the tag
// both are things we will do later, doing so now causes us to recursively call this method (though not infinite)
stack.tag = nbt;
}
} else if (copyNbt) {
nbt = nbt.copy();
Expand Down Expand Up @@ -742,7 +744,8 @@ public static void verifyTag(Item item, CompoundTag tag, ToolDefinition definiti
}

// resolve all material redirects
if (tag.contains(ToolStack.TAG_MATERIALS, Tag.TAG_LIST)) {
boolean hasMaterials = tag.contains(ToolStack.TAG_MATERIALS, Tag.TAG_LIST);
if (hasMaterials) {
MaterialIdNBT stored = MaterialIdNBT.readFromNBT(tag.getList(ToolStack.TAG_MATERIALS, Tag.TAG_STRING));
MaterialIdNBT resolved = stored.resolveRedirects();
if (resolved != stored) {
Expand All @@ -752,6 +755,8 @@ public static void verifyTag(Item item, CompoundTag tag, ToolDefinition definiti
// rebuild stats
ToolStack tool = ToolStack.from(item, definition, tag);
tool.ensureSlotsBuilt();
tool.rebuildStats();
if (hasMaterials || !definition.isMultipart()) {
tool.rebuildStats();
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,6 @@ public net.minecraft.resources.ResourceLocation m_135841_(Ljava/lang/String;)Z #

# Condition context for material manager
public net.minecraft.server.ReloadableServerResources f_206849_ # tagManager

# Prevent recursion when setting tool stack tag
public net.minecraft.world.item.ItemStack f_41590_ # tag

0 comments on commit 643768d

Please sign in to comment.