Tidy up item util

This commit is contained in:
FlorianMichael 2024-09-08 13:13:51 +02:00
parent 83d6348f0d
commit 7b4a1d008e
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
2 changed files with 14 additions and 5 deletions

View File

@ -19,7 +19,6 @@
package de.florianmichael.viafabricplus.screen; package de.florianmichael.viafabricplus.screen;
import com.mojang.blaze3d.systems.RenderSystem;
import de.florianmichael.viafabricplus.ViaFabricPlus; import de.florianmichael.viafabricplus.ViaFabricPlus;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;

View File

@ -21,6 +21,7 @@ package de.florianmichael.viafabricplus.util;
import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11; import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11;
import net.minecraft.component.DataComponentTypes; import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
@ -28,6 +29,13 @@ public class ItemUtil {
private static final String VV_IDENTIFIER = "VV|" + Protocol1_10To1_11.class.getSimpleName(); // ItemRewriter#nbtTagName 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) { public static int getCount(final ItemStack stack) {
final NbtCompound tag = getTagOrNull(stack); final NbtCompound tag = getTagOrNull(stack);
if (tag != null && tag.contains(VV_IDENTIFIER)) { if (tag != null && tag.contains(VV_IDENTIFIER)) {
@ -37,12 +45,14 @@ public class ItemUtil {
} }
} }
// 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) { public static NbtCompound getTagOrNull(final ItemStack stack) {
if (!stack.contains(DataComponentTypes.CUSTOM_DATA)) { final NbtComponent tag = stack.get(DataComponentTypes.CUSTOM_DATA);
return null; if (tag != null) {
return tag.copyNbt();
} else { } else {
return stack.get(DataComponentTypes.CUSTOM_DATA).getNbt(); return null;
} }
} }