| 1 | package net.minecraft.client.gui.screens.dialog;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.Map;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.gui.screens.Screen;
|
| 9 | import net.minecraft.server.dialog.ConfirmationDialog;
|
| 10 | import net.minecraft.server.dialog.Dialog;
|
| 11 | import net.minecraft.server.dialog.DialogListDialog;
|
| 12 | import net.minecraft.server.dialog.MultiActionDialog;
|
| 13 | import net.minecraft.server.dialog.NoticeDialog;
|
| 14 | import net.minecraft.server.dialog.ServerLinksDialog;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class DialogScreens {
|
| 19 | private static final Map<MapCodec<? extends Dialog>, DialogScreens.Factory<?>> FACTORIES = new HashMap<>();
|
| 20 |
|
| 21 | private static <T extends Dialog> void register(final MapCodec<T> type, final DialogScreens.Factory<? super T> factory) {
|
| 22 | FACTORIES.put(type, factory);
|
| 23 | }
|
| 24 |
|
| 25 | public static <T extends Dialog> @Nullable DialogScreen<T> createFromData(
|
| 26 | final T dialog, final @Nullable Screen previousScreen, final DialogConnectionAccess connectionAccess
|
| 27 | ) {
|
| 28 | DialogScreens.Factory<T> factory = (DialogScreens.Factory<T>)FACTORIES.get(dialog.codec());
|
| 29 | return factory != null ? factory.create(previousScreen, dialog, connectionAccess) : null;
|
| 30 | }
|
| 31 |
|
| 32 | public static void bootstrap() {
|
| 33 | register(ConfirmationDialog.MAP_CODEC, SimpleDialogScreen::new);
|
| 34 | register(NoticeDialog.MAP_CODEC, SimpleDialogScreen::new);
|
| 35 | register(DialogListDialog.MAP_CODEC, DialogListDialogScreen::new);
|
| 36 | register(MultiActionDialog.MAP_CODEC, MultiButtonDialogScreen::new);
|
| 37 | register(ServerLinksDialog.MAP_CODEC, ServerLinksDialogScreen::new);
|
| 38 | }
|
| 39 |
|
| 40 | @FunctionalInterface
|
| 41 | @Environment(EnvType.CLIENT)
|
| 42 | public interface Factory<T extends Dialog> {
|
| 43 | DialogScreen<T> create(@Nullable Screen previousScreen, T data, DialogConnectionAccess connectionAccess);
|
| 44 | }
|
| 45 | }
|
| 46 |
|