| 1 | package net.minecraft.client.gui.screens.dialog.body;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import java.util.HashMap;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.components.FocusableTextWidget;
|
| 12 | import net.minecraft.client.gui.components.ItemDisplayWidget;
|
| 13 | import net.minecraft.client.gui.layouts.LayoutElement;
|
| 14 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 15 | import net.minecraft.client.gui.screens.dialog.DialogScreen;
|
| 16 | import net.minecraft.network.chat.ClickEvent;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Style;
|
| 19 | import net.minecraft.server.dialog.body.DialogBody;
|
| 20 | import net.minecraft.server.dialog.body.ItemBody;
|
| 21 | import net.minecraft.server.dialog.body.PlainMessage;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class DialogBodyHandlers {
|
| 28 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 29 | private static final Map<MapCodec<? extends DialogBody>, DialogBodyHandler<?>> HANDLERS = new HashMap<>();
|
| 30 |
|
| 31 | private static <B extends DialogBody> void register(final MapCodec<B> type, final DialogBodyHandler<? super B> handler) {
|
| 32 | HANDLERS.put(type, handler);
|
| 33 | }
|
| 34 |
|
| 35 | private static <B extends DialogBody> @Nullable DialogBodyHandler<B> getHandler(final B body) {
|
| 36 | return (DialogBodyHandler<B>)HANDLERS.get(body.mapCodec());
|
| 37 | }
|
| 38 |
|
| 39 | public static <B extends DialogBody> @Nullable LayoutElement createBodyElement(final DialogScreen<?> screen, final B body) {
|
| 40 | DialogBodyHandler<B> handler = getHandler(body);
|
| 41 | if (handler == null) {
|
| 42 | LOGGER.warn("Unrecognized dialog body {}", body);
|
| 43 | return null;
|
| 44 | } else {
|
| 45 | return handler.createControls(screen, body);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public static void bootstrap() {
|
| 50 | register(PlainMessage.MAP_CODEC, new DialogBodyHandlers.PlainMessageHandler());
|
| 51 | register(ItemBody.MAP_CODEC, new DialogBodyHandlers.ItemHandler());
|
| 52 | }
|
| 53 |
|
| 54 | private static void runActionOnParent(final DialogScreen<?> parent, final @Nullable Style clickedStyle) {
|
| 55 | if (clickedStyle != null) {
|
| 56 | ClickEvent clickEvent = clickedStyle.getClickEvent();
|
| 57 | if (clickEvent != null) {
|
| 58 | parent.runAction(Optional.of(clickEvent));
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Environment(EnvType.CLIENT)
|
| 64 | private static class ItemHandler implements DialogBodyHandler<ItemBody> {
|
| 65 | public LayoutElement createControls(final DialogScreen<?> parent, final ItemBody item) {
|
| 66 | ItemStack displayStack = item.item().create();
|
| 67 | if (item.description().isPresent()) {
|
| 68 | PlainMessage description = item.description().get();
|
| 69 | LinearLayout layout = LinearLayout.horizontal().spacing(2);
|
| 70 | layout.defaultCellSetting().alignVerticallyMiddle();
|
| 71 | ItemDisplayWidget itemWidget = new ItemDisplayWidget(
|
| 72 | Minecraft.getInstance(), 0, 0, item.width(), item.height(), CommonComponents.EMPTY, displayStack, item.showDecorations(), item.showTooltip()
|
| 73 | );
|
| 74 | layout.addChild(itemWidget);
|
| 75 | layout.addChild(
|
| 76 | FocusableTextWidget.builder(description.contents(), parent.getFont())
|
| 77 | .maxWidth(description.width())
|
| 78 | .alwaysShowBorder(false)
|
| 79 | .backgroundFill(FocusableTextWidget.BackgroundFill.NEVER)
|
| 80 | .build()
|
| 81 | .setComponentClickHandler(style -> DialogBodyHandlers.runActionOnParent(parent, style))
|
| 82 | );
|
| 83 | return layout;
|
| 84 | } else {
|
| 85 | return new ItemDisplayWidget(
|
| 86 | Minecraft.getInstance(), 0, 0, item.width(), item.height(), displayStack.getHoverName(), displayStack, item.showDecorations(), item.showTooltip()
|
| 87 | );
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | @Environment(EnvType.CLIENT)
|
| 93 | private static class PlainMessageHandler implements DialogBodyHandler<PlainMessage> {
|
| 94 | public LayoutElement createControls(final DialogScreen<?> parent, final PlainMessage message) {
|
| 95 | return FocusableTextWidget.builder(message.contents(), parent.getFont())
|
| 96 | .maxWidth(message.width())
|
| 97 | .alwaysShowBorder(false)
|
| 98 | .backgroundFill(FocusableTextWidget.BackgroundFill.NEVER)
|
| 99 | .build()
|
| 100 | .setCentered(true)
|
| 101 | .setComponentClickHandler(style -> DialogBodyHandlers.runActionOnParent(parent, style));
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|