Skip to content

Commit

Permalink
Tidy up item util
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Sep 8, 2024
1 parent 83d6348 commit 7b4a1d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package de.florianmichael.viafabricplus.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import de.florianmichael.viafabricplus.ViaFabricPlus;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/de/florianmichael/viafabricplus/util/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@

import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;

public class ItemUtil {

private static final String VV_IDENTIFIER = "VV|" + Protocol1_10To1_11.class.getSimpleName(); // ItemRewriter#nbtTagName

/**
* Returns the actual amount of items in the stack, versions older or equal to 1.10 can have negative stack sizes
* which are not represented by {@link ItemStack#getCount()}.
*
* @param stack The stack to get the count from
* @return The actual amount of items in the stack
*/
public static int getCount(final ItemStack stack) {
final NbtCompound tag = getTagOrNull(stack);
if (tag != null && tag.contains(VV_IDENTIFIER)) {
Expand All @@ -37,12 +45,14 @@ public static int getCount(final ItemStack stack) {
}
}

// Via 1.20.5->.3 will always put the original item data into CUSTOM_DATA if it's not empty.
// ViaVersion's 1.20.5 -> 1.20.3 protocol will save the original item nbt inside custom data to later restore
// it for creative clients, we can use this to get nbt stored in older protocols as well
public static NbtCompound getTagOrNull(final ItemStack stack) {
if (!stack.contains(DataComponentTypes.CUSTOM_DATA)) {
return null;
final NbtComponent tag = stack.get(DataComponentTypes.CUSTOM_DATA);
if (tag != null) {
return tag.copyNbt();
} else {
return stack.get(DataComponentTypes.CUSTOM_DATA).getNbt();
return null;
}
}

Expand Down

0 comments on commit 7b4a1d0

Please sign in to comment.