|
| 1 | +package me.kodysimpson.simpapi.conversations; |
| 2 | + |
| 3 | +import me.kodysimpson.simpapi.colors.ColorTranslator; |
| 4 | +import org.bukkit.conversations.*; |
| 5 | +import org.bukkit.entity.Player; |
| 6 | +import org.bukkit.plugin.Plugin; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.jetbrains.annotations.Range; |
| 10 | + |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Helper class for construction of conversations. |
| 16 | + * Wraps {@link ConversationFactory} to make building conversations with players |
| 17 | + * significantly less tedious and boiler-plate prone. |
| 18 | + * |
| 19 | + * @see ConversationOptions |
| 20 | + * @author muunitnocQ |
| 21 | + */ |
| 22 | +public final class ConversationStarter { |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructs a conversation with the first prompt for a given player under the authority of the provided plugin, |
| 26 | + * with optionally attached data. |
| 27 | + * |
| 28 | + * @param plugin The plugin owning this conversation instance |
| 29 | + * @param player Who the conversation is for |
| 30 | + * @param firstPrompt The prompt to start off on |
| 31 | + * @param options Optional fine-grained configuration of the conversation. Refer to {@link ConversationOptions} |
| 32 | + * @param initialData Optional session data to start off with. Refer to {@link ConversationContext#getAllSessionData()} |
| 33 | + * @return The conversation, not yet started. See {@link Conversation#begin()} |
| 34 | + */ |
| 35 | + public static @NotNull Conversation getForPlayer( |
| 36 | + @NotNull Plugin plugin, |
| 37 | + @NotNull Player player, |
| 38 | + @NotNull Prompt firstPrompt, |
| 39 | + @Nullable ConversationOptions options, |
| 40 | + @Nullable Map<Object, Object> initialData |
| 41 | + ) { |
| 42 | + if (options == null) options = ConversationOptions.DEFAULT_OPTIONS; |
| 43 | + |
| 44 | + ConversationFactory factory = new ConversationFactory(plugin) |
| 45 | + .withFirstPrompt(firstPrompt) |
| 46 | + .withLocalEcho(options.localEcho) |
| 47 | + .withPrefix(options.prefix) |
| 48 | + .addConversationAbandonedListener(new PrefixedAbandonedListener(options.rawPrefix)) |
| 49 | + .withConversationCanceller(new ArrayMatchCanceller(options.escapeWordsCaseSensitive, options.escapeWords)) |
| 50 | + .withInitialSessionData(initialData == null ? Collections.emptyMap() : initialData) |
| 51 | + .withTimeout(options.timeOut); |
| 52 | + Conversation convo = factory.buildConversation(player); |
| 53 | + |
| 54 | + new ServerReloadListener(plugin, convo); |
| 55 | + |
| 56 | + return convo; |
| 57 | + } |
| 58 | + |
| 59 | + //I really want records, koby |
| 60 | + //java 8 makes me go grrr |
| 61 | + |
| 62 | + /** |
| 63 | + * Represents the configurable options in a conversation. |
| 64 | + * |
| 65 | + * @see ConversationOptions#ConversationOptions(String, boolean, boolean, int, boolean, String...) |
| 66 | + */ |
| 67 | + public final static class ConversationOptions { |
| 68 | + |
| 69 | + //Applied if the options for ConversationStarter#getForPlayer is null |
| 70 | + private static final ConversationOptions DEFAULT_OPTIONS = new ConversationOptions( |
| 71 | + null, |
| 72 | + true, |
| 73 | + false, |
| 74 | + 60, |
| 75 | + false, |
| 76 | + "cancel", "exit", "quit", "escape", "done" |
| 77 | + ); |
| 78 | + |
| 79 | + /** |
| 80 | + * Shown before every line of output in the conversation. |
| 81 | + * Supports Minecraft vanilla colors and hex colors in the format &#RRGGBB. |
| 82 | + * |
| 83 | + * @see ColorTranslator#translateColorCodes(String) |
| 84 | + */ |
| 85 | + public final ConversationPrefix prefix; |
| 86 | + private final String rawPrefix; |
| 87 | + public final boolean localEcho; |
| 88 | + public final boolean modal; |
| 89 | + public final int timeOut; |
| 90 | + public final boolean escapeWordsCaseSensitive; |
| 91 | + public final String[] escapeWords; |
| 92 | + |
| 93 | + /** |
| 94 | + * Describes all options a conversation can have. |
| 95 | + * |
| 96 | + * @param prefix Shown before every line of output in the conversation |
| 97 | + * Defaults to "" if null is passed |
| 98 | + * @param localEcho Should the player's input be displayed back to them? |
| 99 | + * @param modal Determines if the player can see other players chatting during the conversation |
| 100 | + * @param timeOut An automatic time-out in which the conversation will cancel upon receiving no input from the player |
| 101 | + * Clamps to 0 as a minimum. |
| 102 | + * @param escapeWordsCaseSensitive Should escape words be case sensitive? See next parameter for details |
| 103 | + * @param escapeWords Words that, when entered as input, will force the conversation to be abandoned. |
| 104 | + */ |
| 105 | + public ConversationOptions( |
| 106 | + @Nullable String prefix, |
| 107 | + boolean localEcho, |
| 108 | + boolean modal, |
| 109 | + @Range(from = 0, to = Integer.MAX_VALUE) int timeOut, |
| 110 | + boolean escapeWordsCaseSensitive, |
| 111 | + String... escapeWords |
| 112 | + ) { |
| 113 | + if (prefix == null) { |
| 114 | + this.rawPrefix = ""; |
| 115 | + this.prefix = new NullConversationPrefix(); |
| 116 | + } else { |
| 117 | + this.rawPrefix = ColorTranslator.translateColorCodes(prefix); |
| 118 | + this.prefix = (context) -> rawPrefix; |
| 119 | + } |
| 120 | + |
| 121 | + this.localEcho = localEcho; |
| 122 | + this.modal = modal; |
| 123 | + this.timeOut = timeOut; |
| 124 | + this.escapeWordsCaseSensitive = escapeWordsCaseSensitive; |
| 125 | + |
| 126 | + if (escapeWords == null || escapeWords.length < 1) { |
| 127 | + this.escapeWords = new String[]{ |
| 128 | + "cancel", "exit", "quit", "escape", "done" |
| 129 | + }; |
| 130 | + } else this.escapeWords = escapeWords; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments