Add method to get real item count

This commit is contained in:
RaphiMC 2024-06-30 19:22:04 +02:00
parent 22c01812a8
commit d370e8bc41
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 13 additions and 12 deletions

View File

@ -19,31 +19,20 @@
package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item;
import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11;
import de.florianmichael.viafabricplus.util.ItemUtil;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(DrawContext.class)
public abstract class MixinDrawContext {
@Unique
private static final String viaFabricPlus$vvIdentifier = "VV|" + Protocol1_10To1_11.class.getSimpleName(); // ItemRewriter#nbtTagName
@Redirect(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;getCount()I"))
private int handleNegativeItemCount(ItemStack instance) {
final NbtCompound tag = ItemUtil.getTagOrNull(instance);
if (tag != null && tag.contains(viaFabricPlus$vvIdentifier)) {
return tag.getInt(viaFabricPlus$vvIdentifier);
} else {
return instance.getCount();
}
return ItemUtil.getCount(instance);
}
@Redirect(method = "drawItemInSlot(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V", at = @At(value = "INVOKE", target = "Ljava/lang/String;valueOf(I)Ljava/lang/String;", remap = false))

View File

@ -19,12 +19,24 @@
package de.florianmichael.viafabricplus.util;
import com.viaversion.viaversion.protocols.v1_10to1_11.Protocol1_10To1_11;
import net.minecraft.component.DataComponentTypes;
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
public static int getCount(final ItemStack stack) {
final NbtCompound tag = getTagOrNull(stack);
if (tag != null && tag.contains(VV_IDENTIFIER)) {
return tag.getInt(VV_IDENTIFIER);
} else {
return stack.getCount();
}
}
// Via 1.20.5->.3 will always put the original item data into CUSTOM_DATA if it's not empty.
public static NbtCompound getTagOrNull(final ItemStack stack) {
if (!stack.contains(DataComponentTypes.CUSTOM_DATA)) {