From 0a041c951d4c40987fbd7fe0f55b50be843ad036 Mon Sep 17 00:00:00 2001 From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com> Date: Sat, 17 Aug 2024 22:30:21 +0200 Subject: [PATCH] Update item data sanitization (#11227) --- ...oversized-item-data-in-equipment-and.patch | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch b/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch index 565c374326..63db36b61d 100644 --- a/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch +++ b/patches/server/Prevent-sending-oversized-item-data-in-equipment-and.patch @@ -13,17 +13,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ +package io.papermc.paper.util; + -+import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.UnaryOperator; ++import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; ++import net.minecraft.util.Mth; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.component.BundleContents; +import net.minecraft.world.item.component.ChargedProjectiles; +import net.minecraft.world.item.component.ItemContainerContents; ++import org.apache.commons.lang3.math.Fraction; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + @@ -48,26 +50,33 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + if (projectiles.isEmpty()) { + return projectiles; + } -+ final List items = projectiles.getItems(); -+ final List sanitized = new ArrayList<>(); -+ for (int i = 0; i < Math.min(items.size(), 3); i++) { -+ // we want to preserve item type as vanilla client can change visuals based on type -+ sanitized.add(new ItemStack(items.get(i).getItemHolder())); -+ } -+ return ChargedProjectiles.of(sanitized); ++ ++ return ChargedProjectiles.of(List.of( ++ new ItemStack(projectiles.contains(Items.FIREWORK_ROCKET) ? Items.FIREWORK_ROCKET : Items.ARROW) ++ )); + } + + private static BundleContents sanitizeBundleContents(final BundleContents contents) { ++ if (contents.isEmpty()) { ++ return contents; ++ } ++ + // Bundles change their texture based on their fullness. -+ int sizeUsed = 0; -+ for (final ItemStack item : contents.items()) { -+ final int scale = 64 / item.getMaxStackSize(); -+ sizeUsed += scale * item.getCount(); ++ // A bundles content weight may be anywhere from 0 to, basically, infinity. ++ // A weight of 1 is the usual maximum case ++ int sizeUsed = Mth.mulAndTruncate(contents.weight(), 64); ++ // Early out, *most* bundles should not be overfilled above a weight of one. ++ if (sizeUsed <= 64) return new BundleContents(List.of(new ItemStack(Items.PAPER, Math.max(1, sizeUsed)))); ++ ++ final List sanitizedRepresentation = new ObjectArrayList<>(sizeUsed / 64 + 1); ++ while (sizeUsed > 0) { ++ final int stackCount = Math.min(64, sizeUsed); ++ sanitizedRepresentation.add(new ItemStack(Items.PAPER, stackCount)); ++ sizeUsed -= stackCount; + } + // Now we add a single fake item that uses the same amount of slots as all other items. -+ final List items = new ArrayList<>(); -+ items.add(new ItemStack(Items.PAPER, sizeUsed)); -+ return new BundleContents(items); ++ // Ensure that potentially overstacked bundles are not represented by empty (count=0) itemstacks. ++ return new BundleContents(sanitizedRepresentation); + } + + private static StreamCodec codec(final StreamCodec delegate, final UnaryOperator sanitizer) { @@ -109,7 +118,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + private DataSanitizationUtil() { + } -+ +} diff --git a/src/main/java/net/minecraft/core/component/DataComponents.java b/src/main/java/net/minecraft/core/component/DataComponents.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644