Better item validation

By: Thinkofdeath <thinkofdeath@spigotmc.org>
This commit is contained in:
CraftBukkit/Spigot 2014-07-02 23:35:51 +01:00
parent 804b118219
commit 4a1b3a723a

View file

@ -40,7 +40,7 @@
import net.minecraft.world.level.saveddata.maps.MapId;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.slf4j.Logger;
+
+// CraftBukkit start
+import java.util.Map;
+import java.util.Objects;
@ -71,7 +71,7 @@
+import org.bukkit.event.player.PlayerItemDamageEvent;
+import org.bukkit.event.world.StructureGrowEvent;
+// CraftBukkit end
+
public final class ItemStack implements DataComponentHolder {
private static final List<Component> OP_NBT_WARNING = List.of(Component.translatable("item.op_warning.line1").withStyle(ChatFormatting.RED, ChatFormatting.BOLD), Component.translatable("item.op_warning.line2").withStyle(ChatFormatting.RED), Component.translatable("item.op_warning.line3").withStyle(ChatFormatting.RED));
@ -89,7 +89,7 @@
return instance.group(Item.CODEC.fieldOf("id").forGetter(ItemStack::getItemHolder), DataComponentPatch.CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter((itemstack) -> {
return itemstack.components.asPatch();
})).apply(instance, (holder, datacomponentpatch) -> {
@@ -132,19 +161,25 @@
@@ -132,19 +161,29 @@
if (i <= 0) {
return ItemStack.EMPTY;
} else {
@ -115,11 +115,15 @@
} else {
registryfriendlybytebuf.writeVarInt(itemstack.getCount());
- null.ITEM_STREAM_CODEC.encode(registryfriendlybytebuf, itemstack.getItemHolder());
+ // Spigot start - filter
+ itemstack = itemstack.copy();
+ CraftItemStack.setItemMeta(itemstack, CraftItemStack.getItemMeta(itemstack));
+ // Spigot end
+ ITEM_STREAM_CODEC.encode(registryfriendlybytebuf, itemstack.getItemHolder()); // CraftBukkit - decompile error
DataComponentPatch.STREAM_CODEC.encode(registryfriendlybytebuf, itemstack.components.asPatch());
}
}
@@ -187,7 +222,7 @@
@@ -187,7 +226,7 @@
return dataresult.isError() ? dataresult.map((unit) -> {
return stack;
@ -128,7 +132,7 @@
int i = stack.getCount();
return "Item stack with stack size of " + i + " was larger than maximum: " + stack.getMaxStackSize();
@@ -294,8 +329,9 @@
@@ -294,8 +333,9 @@
j = itemstack.getMaxStackSize();
} while (i <= j);
@ -139,7 +143,7 @@
});
}
}
@@ -370,32 +406,190 @@
@@ -370,32 +410,190 @@
}
public InteractionResult useOn(UseOnContext context) {
@ -335,7 +339,7 @@
ItemStack itemstack = this.copy();
boolean flag = this.getUseDuration(user) <= 0;
InteractionResult enuminteractionresult = this.getItem().use(world, user, hand);
@@ -492,7 +686,22 @@
@@ -492,7 +690,22 @@
public void hurtAndBreak(int amount, ServerLevel world, @Nullable ServerPlayer player, Consumer<Item> breakCallback) {
int j = this.processDurabilityChange(amount, world, player);
@ -358,7 +362,7 @@
if (j != 0) {
this.applyDamage(this.getDamageValue() + j, player, breakCallback);
}
@@ -511,6 +720,11 @@
@@ -511,6 +724,11 @@
this.setDamageValue(damage);
if (this.isBroken()) {
Item item = this.getItem();
@ -370,7 +374,7 @@
this.shrink(1);
breakCallback.accept(item);
@@ -518,7 +732,7 @@
@@ -518,7 +736,7 @@
}
@ -379,7 +383,7 @@
if (player instanceof ServerPlayer entityplayer) {
int j = this.processDurabilityChange(amount, entityplayer.serverLevel(), entityplayer);
@@ -580,11 +794,11 @@
@@ -580,11 +798,11 @@
return this.getItem().getBarColor(this);
}
@ -393,7 +397,7 @@
return this.getItem().overrideOtherStackedOnMe(this, stack, slot, clickType, player, cursorStackReference);
}
@@ -592,8 +806,8 @@
@@ -592,8 +810,8 @@
Item item = this.getItem();
if (item.hurtEnemy(this, target, user)) {
@ -404,7 +408,7 @@
entityhuman.awardStat(Stats.ITEM_USED.get(item));
}
@@ -608,7 +822,7 @@
@@ -608,7 +826,7 @@
this.getItem().postHurtEnemy(this, target, user);
}
@ -413,7 +417,7 @@
Item item = this.getItem();
if (item.mineBlock(this, world, state, pos, miner)) {
@@ -617,11 +831,11 @@
@@ -617,11 +835,11 @@
}
@ -427,7 +431,7 @@
return this.getItem().interactLivingEntity(this, user, entity, hand);
}
@@ -736,7 +950,7 @@
@@ -736,7 +954,7 @@
}
@ -436,20 +440,21 @@
player.awardStat(Stats.ITEM_CRAFTED.get(this.getItem()), amount);
this.getItem().onCraftedBy(this, world, player);
}
@@ -770,6 +984,12 @@
return this.getItem().useOnRelease(this);
}
@@ -768,7 +986,13 @@
public boolean useOnRelease() {
return this.getItem().useOnRelease(this);
+ }
+
+ // CraftBukkit start
+ public void restorePatch(DataComponentPatch datacomponentpatch) {
+ this.components.restorePatch(datacomponentpatch);
+ }
}
+ // CraftBukkit end
+
@Nullable
public <T> T set(DataComponentType<? super T> type, @Nullable T value) {
return this.components.set(type, value);
@@ -858,7 +1078,7 @@
@@ -858,7 +1082,7 @@
}
private <T extends TooltipProvider> void addToTooltip(DataComponentType<T> componentType, Item.TooltipContext context, Consumer<Component> textConsumer, TooltipFlag type) {
@ -458,7 +463,7 @@
if (t0 != null) {
t0.addToTooltip(context, textConsumer, type);
@@ -866,7 +1086,7 @@
@@ -866,7 +1090,7 @@
}
@ -467,7 +472,7 @@
boolean flag = this.getItem().shouldPrintOpWarning(this, player);
if (!type.isCreative() && this.has(DataComponents.HIDE_TOOLTIP)) {
@@ -941,7 +1161,7 @@
@@ -941,7 +1165,7 @@
}
}
@ -476,7 +481,7 @@
ItemAttributeModifiers itemattributemodifiers = (ItemAttributeModifiers) this.getOrDefault(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
if (itemattributemodifiers.showInTooltip()) {
@@ -966,7 +1186,7 @@
@@ -966,7 +1190,7 @@
}
}
@ -485,7 +490,7 @@
double d0 = modifier.amount();
boolean flag = false;
@@ -1091,6 +1311,13 @@
@@ -1091,6 +1315,13 @@
EnchantmentHelper.forEachModifier(this, slot, attributeModifierConsumer);
}
@ -499,7 +504,7 @@
public Component getDisplayName() {
MutableComponent ichatmutablecomponent = Component.empty().append(this.getHoverName());
@@ -1153,7 +1380,7 @@
@@ -1153,7 +1384,7 @@
}
public void consume(int amount, @Nullable LivingEntity entity) {