package net.minecraft.world.item.component; import com.mojang.serialization.Codec; import io.netty.buffer.ByteBuf; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.function.Consumer; import net.minecraft.ChatFormatting; import net.minecraft.Util; import net.minecraft.core.component.DataComponentGetter; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.world.item.Item; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.block.BeehiveBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.Property; import org.jetbrains.annotations.Nullable; public record BlockItemStateProperties(Map properties) implements TooltipProvider { public static final BlockItemStateProperties EMPTY = new BlockItemStateProperties(Map.of()); public static final Codec CODEC = Codec.unboundedMap(Codec.STRING, Codec.STRING) .xmap(BlockItemStateProperties::new, BlockItemStateProperties::properties); private static final StreamCodec> PROPERTIES_STREAM_CODEC = ByteBufCodecs.map( Object2ObjectOpenHashMap::new, ByteBufCodecs.STRING_UTF8, ByteBufCodecs.STRING_UTF8 ); public static final StreamCodec STREAM_CODEC = PROPERTIES_STREAM_CODEC.map( BlockItemStateProperties::new, BlockItemStateProperties::properties ); public > BlockItemStateProperties with(Property property, T value) { return new BlockItemStateProperties(Util.copyAndPut(this.properties, property.getName(), property.getName(value))); } public > BlockItemStateProperties with(Property property, BlockState state) { return this.with(property, state.getValue(property)); } @Nullable public > T get(Property property) { String string = (String)this.properties.get(property.getName()); return (T)(string == null ? null : property.getValue(string).orElse(null)); } public BlockState apply(BlockState state) { StateDefinition stateDefinition = state.getBlock().getStateDefinition(); for (Entry entry : this.properties.entrySet()) { Property property = stateDefinition.getProperty((String)entry.getKey()); if (property != null) { state = updateState(state, property, (String)entry.getValue()); } } return state; } private static > BlockState updateState(BlockState state, Property property, String propertyName) { return (BlockState)property.getValue(propertyName).map(comparable -> state.setValue(property, comparable)).orElse(state); } public boolean isEmpty() { return this.properties.isEmpty(); } @Override public void addToTooltip(Item.TooltipContext context, Consumer tooltipAdder, TooltipFlag flag, DataComponentGetter componentGetter) { Integer integer = this.get(BeehiveBlock.HONEY_LEVEL); if (integer != null) { tooltipAdder.accept(Component.translatable("container.beehive.honey", integer, 5).withStyle(ChatFormatting.GRAY)); } } }