/* * Decompiled with CFR 0.152. * * Could not load the following classes: * com.mojang.brigadier.exceptions.CommandSyntaxException * com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType */ package net.minecraft.commands.arguments.item; import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import net.minecraft.core.Holder; import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.TypedDataComponent; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.resources.Identifier; import net.minecraft.resources.RegistryOps; import net.minecraft.resources.ResourceKey; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; public class ItemInput { private static final Dynamic2CommandExceptionType ERROR_STACK_TOO_BIG = new Dynamic2CommandExceptionType((item, count) -> Component.translatableEscape("arguments.item.overstacked", item, count)); private final Holder item; private final DataComponentPatch components; public ItemInput(Holder item, DataComponentPatch components) { this.item = item; this.components = components; } public Item getItem() { return this.item.value(); } public ItemStack createItemStack(int count, boolean checkSize) throws CommandSyntaxException { ItemStack result = new ItemStack(this.item, count); result.applyComponents(this.components); if (checkSize && count > result.getMaxStackSize()) { throw ERROR_STACK_TOO_BIG.create((Object)this.getItemName(), (Object)result.getMaxStackSize()); } return result; } public String serialize(HolderLookup.Provider registries) { StringBuilder result = new StringBuilder(this.getItemName()); String serializedComponents = this.serializeComponents(registries); if (!serializedComponents.isEmpty()) { result.append('['); result.append(serializedComponents); result.append(']'); } return result.toString(); } private String serializeComponents(HolderLookup.Provider registries) { RegistryOps ops = registries.createSerializationContext(NbtOps.INSTANCE); return this.components.entrySet().stream().flatMap(entry -> { DataComponentType type = (DataComponentType)entry.getKey(); Identifier key = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type); if (key == null) { return Stream.empty(); } Optional value = (Optional)entry.getValue(); if (value.isPresent()) { TypedDataComponent typedComponent = TypedDataComponent.createUnchecked(type, value.get()); return typedComponent.encodeValue(ops).result().stream().map(tag -> key.toString() + "=" + String.valueOf(tag)); } return Stream.of("!" + key.toString()); }).collect(Collectors.joining(String.valueOf(','))); } private String getItemName() { return this.item.unwrapKey().map(ResourceKey::identifier).orElseGet(() -> "unknown[" + String.valueOf(this.item) + "]").toString(); } }