| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Map.Entry;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import net.minecraft.ChatFormatting;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.world.item.Item;
|
| 16 | import net.minecraft.world.item.TooltipFlag;
|
| 17 | import net.minecraft.world.level.block.BeehiveBlock;
|
| 18 | import net.minecraft.world.level.block.Block;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 21 | import net.minecraft.world.level.block.state.properties.Property;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | public record BlockItemStateProperties(Map<String, String> properties) implements TooltipProvider {
|
| 25 | public static final BlockItemStateProperties EMPTY = new BlockItemStateProperties(Map.of());
|
| 26 | public static final Codec<BlockItemStateProperties> CODEC = Codec.unboundedMap(Codec.STRING, Codec.STRING)
|
| 27 | .xmap(BlockItemStateProperties::new, BlockItemStateProperties::properties);
|
| 28 | private static final StreamCodec<ByteBuf, Map<String, String>> PROPERTIES_STREAM_CODEC = ByteBufCodecs.map(
|
| 29 | Object2ObjectOpenHashMap::new, ByteBufCodecs.STRING_UTF8, ByteBufCodecs.STRING_UTF8
|
| 30 | );
|
| 31 | public static final StreamCodec<ByteBuf, BlockItemStateProperties> STREAM_CODEC = PROPERTIES_STREAM_CODEC.map(
|
| 32 | BlockItemStateProperties::new, BlockItemStateProperties::properties
|
| 33 | );
|
| 34 |
|
| 35 | public <T extends Comparable<T>> BlockItemStateProperties with(final Property<T> property, final T value) {
|
| 36 | return new BlockItemStateProperties(Util.copyAndPut(this.properties, property.getName(), property.getName(value)));
|
| 37 | }
|
| 38 |
|
| 39 | public <T extends Comparable<T>> BlockItemStateProperties with(final Property<T> property, final BlockState state) {
|
| 40 | return this.with(property, state.getValue(property));
|
| 41 | }
|
| 42 |
|
| 43 | public <T extends Comparable<T>> @Nullable T get(final Property<T> property) {
|
| 44 | String value = this.properties.get(property.getName());
|
| 45 | return value == null ? null : property.getValue(value).orElse(null);
|
| 46 | }
|
| 47 |
|
| 48 | public BlockState apply(BlockState state) {
|
| 49 | StateDefinition<Block, BlockState> stateDefinition = state.getBlock().getStateDefinition();
|
| 50 |
|
| 51 | for (Entry<String, String> entry : this.properties.entrySet()) {
|
| 52 | Property<?> property = stateDefinition.getProperty(entry.getKey());
|
| 53 | if (property != null) {
|
| 54 | state = updateState(state, property, entry.getValue());
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return state;
|
| 59 | }
|
| 60 |
|
| 61 | private static <T extends Comparable<T>> BlockState updateState(final BlockState state, final Property<T> property, final String value) {
|
| 62 | return property.getValue(value).map(v -> state.setValue(property, v)).orElse(state);
|
| 63 | }
|
| 64 |
|
| 65 | public boolean isEmpty() {
|
| 66 | return this.properties.isEmpty();
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void addToTooltip(final Item.TooltipContext context, final Consumer<Component> consumer, final TooltipFlag flag, final DataComponentGetter components) {
|
| 71 | Integer honeyLevel = this.get(BeehiveBlock.HONEY_LEVEL);
|
| 72 | if (honeyLevel != null) {
|
| 73 | consumer.accept(Component.translatable("container.beehive.honey", honeyLevel, 5).withStyle(ChatFormatting.GRAY));
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|