From 369c8dae87bc108eca1bf98d8f1acc29c8f15c90 Mon Sep 17 00:00:00 2001 From: CyR1en Date: Wed, 25 Nov 2026 16:20:05 +0000 Subject: [PATCH 1/3] Stop expanding PlaceholderAPI placeholders in player-authored inline prompt text Inline tag text comes from the command line the player typed, so it no longer reaches the expansion delegate unless the player holds promptpaper.inline.placeholders. Preset text is still expanded. --- .../promptpaper/factory/PromptFactory.java | 23 ++++- .../factory/PromptPresentationExpander.java | 52 +++++++++- .../src/main/resources/paper-plugin.yml | 3 + .../factory/PromptFactoryTest.java | 36 ++++++- .../PromptPresentationExpanderTest.java | 95 +++++++++++++++++++ 5 files changed, 198 insertions(+), 11 deletions(-) diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java index d1c2e2d..e44ff0a 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java @@ -198,6 +198,18 @@ public InputScreen create(Player player, PromptDefinition def, DialogCompletionC return createExpanded(player, presentationExpander.expand(player, def), context); } + /** + * Builds the screen for a definition mapped from an inline tag. Inline text is player-authored, + * so it goes through {@link PromptPresentationExpander#expandInline} instead of the preset path. + */ + private InputScreen createInline( + Player player, PromptDefinition def, DialogCompletionContext context) { + plugin + .getPluginLogger() + .debug("PromptFactory.createInline: player=" + player.getName() + " type=" + def.type()); + return createExpanded(player, presentationExpander.expandInline(player, def), context); + } + /** * Builds the screen from an already-expanded definition. Never expands again — the public {@link * #create(Player, PromptDefinition, DialogCompletionContext)} is the only entry point allowed to @@ -238,12 +250,15 @@ public InputScreen createFromTag(Player player, PromptTag tag) { *
  • Presets look up the registry with the raw {@code displayText} (the id is a * semantic key, never expanded) and delegate to {@link #create}, which expands the resolved * definition once. - *
  • Inline dialogs / compound tags expand a presentation-only copy of the tag via + *
  • Inline dialogs / compound tags materialize a presentation-only copy of the tag via * {@link PromptPresentationExpander#expandInlineDialog} and build the dialog screen * directly from that copy. - *
  • Other inline tags map the raw tag and delegate to {@link #create}, which expands - * once. + *
  • Other inline tags map the raw tag and materialize it once via {@link + * PromptPresentationExpander#expandInline}. * + * + *

    Inline text is typed by the player, so placeholders in it are only resolved for players + * holding {@code promptpaper.inline.placeholders}; preset text is always resolved. */ public InputScreen createFromTag(Player player, PromptTag tag, DialogCompletionContext context) { if (tag == null) throw new IllegalArgumentException("PromptTag must not be null"); @@ -283,7 +298,7 @@ public InputScreen createFromTag(Player player, PromptTag tag, DialogCompletionC return wrapWithTagTitle(player, expandedTag, dialogScreen); } var def = InlineTagMapper.toPromptDefinition(tag, mappings); - return create(player, def, context); + return createInline(player, def, context); } private ChatPromptScreen createChat(Player player, ChatPrompt chat) { diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java index af70314..ce5f09c 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java @@ -50,6 +50,13 @@ * {@code title.main} / {@code title.sub}. * * + *

    Origin

    + * + *

    Only preset-sourced definitions ({@link #expand}) are sent to the expansion delegate. Inline + * tags are written on the command line by the player, so {@link #expandInline} and {@link + * #expandInlineDialog} leave their text unexpanded (length-bounded only) unless the player holds + * {@value #INLINE_PLACEHOLDER_PERMISSION}. + * *

    Never expanded

    * *

    Preset ids, tag keys, {@code rawTag}, filters, validator aliases, answer types, {@code @@ -65,6 +72,9 @@ final class PromptPresentationExpander { static final int MAX_DISPLAY_LENGTH = 1024; + /** Opt-in permission that lets a player's own inline prompt text reach the expansion delegate. */ + static final String INLINE_PLACEHOLDER_PERMISSION = "promptpaper.inline.placeholders"; + @FunctionalInterface interface TruncationListener { void onTruncate(Player player, int originalLength, int maxLength); @@ -72,6 +82,9 @@ interface TruncationListener { private final BiFunction expander; private final TruncationListener truncationListener; + // Same length bounding, but text never reaches the expansion delegate. Null on the literal + // instance itself. + private final PromptPresentationExpander literal; /** * @param expander the per-field expansion function (e.g. a PAPI hook). Injected so tests can use @@ -88,8 +101,19 @@ interface TruncationListener { */ PromptPresentationExpander( BiFunction expander, TruncationListener truncationListener) { + this(expander, truncationListener, true); + } + + private PromptPresentationExpander( + BiFunction expander, + TruncationListener truncationListener, + boolean withLiteral) { this.expander = Objects.requireNonNull(expander, "expander"); this.truncationListener = Objects.requireNonNull(truncationListener, "truncationListener"); + this.literal = + withLiteral + ? new PromptPresentationExpander((player, text) -> text, truncationListener, false) + : null; } /** @@ -121,7 +145,11 @@ static PromptPresentationExpander forPlugin(CommandPrompter plugin) { }); } - /** Expands every player-visible presentation field of the definition; semantic fields are raw. */ + /** + * Expands every player-visible presentation field of a preset-sourced definition; semantic + * fields are raw. Never pass a definition mapped from an inline tag here — use {@link + * #expandInline}. + */ PromptDefinition expand(Player player, PromptDefinition raw) { Objects.requireNonNull(raw, "raw"); return switch (raw) { @@ -169,15 +197,35 @@ private dev.cyr1en.promptpaper.preset.ConfirmationPrompt expandConfirmation( confirmation.behavior()); } + /** + * Materializes a definition mapped from an inline tag. Its text is player-authored, so it is + * passed through unexpanded unless the player holds {@value #INLINE_PLACEHOLDER_PERMISSION}. + */ + PromptDefinition expandInline(Player player, PromptDefinition raw) { + return forInline(player).expand(player, raw); + } + + private PromptPresentationExpander forInline(Player player) { + if (literal == null) return this; + return player != null && player.hasPermission(INLINE_PLACEHOLDER_PERMISSION) ? this : literal; + } + /** * Expands the presentation-only copy of an inline (compound) dialog tag: every sub-tag's {@code * displayText} (recursively) plus the {@code -t} title config. All parser/semantic components * ({@code rawTag}, {@code key}, {@code filter}, {@code validatorAlias}, {@code type}, {@code * sanitize}, {@code preset}) are copied through unchanged. + * + *

    The text is player-authored: it only reaches the expansion delegate when the player holds + * {@value #INLINE_PLACEHOLDER_PERMISSION}. */ PromptTag expandInlineDialog(Player player, PromptTag raw) { Objects.requireNonNull(raw, "raw"); - var subTags = raw.subTags().stream().map(sub -> expandInlineDialog(player, sub)).toList(); + return forInline(player).expandTag(player, raw); + } + + private PromptTag expandTag(Player player, PromptTag raw) { + var subTags = raw.subTags().stream().map(sub -> expandTag(player, sub)).toList(); return new PromptTag( raw.rawTag(), raw.key(), diff --git a/prompt-paper/src/main/resources/paper-plugin.yml b/prompt-paper/src/main/resources/paper-plugin.yml index d4ce5c1..00966f7 100644 --- a/prompt-paper/src/main/resources/paper-plugin.yml +++ b/prompt-paper/src/main/resources/paper-plugin.yml @@ -52,6 +52,9 @@ permissions: description: Allows using /commandprompter version promptpaper.use: description: Allows using prompt features + promptpaper.inline.placeholders: + description: Resolves PlaceholderAPI placeholders in inline prompt text typed by the player + default: false promptpaper.consoledelegate: description: Allows using /consoledelegate promptpaper.playerdelegate: diff --git a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptFactoryTest.java b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptFactoryTest.java index 50bebe7..7163c4b 100644 --- a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptFactoryTest.java +++ b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptFactoryTest.java @@ -814,18 +814,44 @@ void emptyTitleMainIsPreservedAndFallbackUsesExpandedPromptTextOnce() { } /** - * A non-dialog inline tag flows through {@link InlineTagMapper} on the raw tag and then {@link - * PromptFactory#create}, so the display text is expanded exactly once (never twice by the mapper - * and the factory). + * Inline tag text is typed by the player, so neither the mapped (non-dialog) path nor the inline + * dialog path may send it to the expansion delegate. */ @Test - void inlineTagDisplayTextIsExpandedExactlyOnce() { + void inlineTagDisplayTextNeverReachesExpander() { + List expanded = new ArrayList<>(); + var testFactory = factoryWithExpander(expanded); + + var anvil = new PromptTag("", "a", null, "Enter %player_name%"); + assertInstanceOf(AnvilPromptScreen.class, testFactory.createFromTag(createPlayer(), anvil)); + + var dialog = new PromptTag("", "d", "text", "Enter %player_name%"); + try { + testFactory.createFromTag(createPlayer("Other"), dialog); + } catch (NoClassDefFoundError ignored) { + // Paper's dialog API is absent from the test classpath; the tag is materialized before the + // dialog screen is constructed, so the assertion below still covers the dialog path. + } + + assertTrue(expanded.isEmpty(), "inline text reached the expander: " + expanded); + } + + /** + * With the opt-in permission, a non-dialog inline tag flows through {@link InlineTagMapper} on + * the raw tag and is expanded exactly once (never twice by the mapper and the factory). + */ + @Test + void inlineTagDisplayTextIsExpandedExactlyOnceWithOptInPermission() { var tag = new PromptTag("", "a", null, "Enter %player_name%"); List expanded = new ArrayList<>(); var testFactory = factoryWithExpander(expanded); + var player = createPlayer(); + player + .addAttachment(plugin) + .setPermission(PromptPresentationExpander.INLINE_PLACEHOLDER_PERMISSION, true); - var screen = testFactory.createFromTag(createPlayer(), tag); + var screen = testFactory.createFromTag(player, tag); assertInstanceOf(AnvilPromptScreen.class, screen); assertEquals(1, Collections.frequency(expanded, "Enter %player_name%")); diff --git a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java index e813292..e93be03 100644 --- a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java +++ b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import com.google.gson.JsonPrimitive; import dev.cyr1en.promptcore.PromptTag; @@ -27,6 +28,7 @@ import dev.cyr1en.promptpaper.preset.PromptDefinition; import dev.cyr1en.promptpaper.preset.SignPrompt; import dev.cyr1en.promptpaper.preset.UIButton; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -319,8 +321,99 @@ void dialogPreservesNullBaseAndNullActions() { // Inline (compound) dialog tags // ------------------------------------------------------------------ + private void grantInlinePlaceholders() { + when(player.hasPermission(PromptPresentationExpander.INLINE_PLACEHOLDER_PERMISSION)) + .thenReturn(true); + } + + @Test + void inlineTagNeverReachesDelegateButPresetDefinitionStillDoes() { + var calls = new ArrayList(); + var recording = + new PromptPresentationExpander( + (p, value) -> { + calls.add(value); + return "[" + value + "]"; + }); + var tag = + new PromptTag( + "", + "a", + null, + "%player_name%", + true, + null, + PromptTag.AnswerType.NONE, + List.of(), + false, + new TitleConfig("%server_name%", "%player_uuid%", 20)); + + var inline = + (AnvilPrompt) recording.expandInline(player, InlineTagMapper.toPromptDefinition(tag)); + var inlineDialog = recording.expandInlineDialog(player, tag); + + assertEquals("%player_name%", inline.promptText()); + assertEquals("%server_name%", inline.titleDisplay().main()); + assertEquals("%player_uuid%", inline.titleDisplay().sub()); + assertEquals("%player_name%", inlineDialog.displayText()); + assertEquals("%server_name%", inlineDialog.title().main()); + assertTrue(calls.isEmpty(), "inline text must never reach the expansion delegate: " + calls); + + var preset = + (ChatPrompt) + recording.expand( + player, + new ChatPrompt( + "chat", "p", "%player_name%", new CancelBehavior(false, "", false, ""), true)); + + assertEquals("[%player_name%]", preset.promptText()); + assertEquals(List.of("%player_name%"), calls); + } + + @Test + void inlineCompoundSubTagsNeverReachDelegate() { + var calls = new ArrayList(); + var recording = + new PromptPresentationExpander( + (p, value) -> { + calls.add(value); + return "[" + value + "]"; + }); + var row = new PromptTag("", "d", "text", "%player_name%"); + var block = + new PromptTag( + "", + "d", null, "", true, null, PromptTag.AnswerType.NONE, List.of(row), false); + + var out = recording.expandInlineDialog(player, block); + + assertEquals("%player_name%", out.subTags().get(0).displayText()); + assertTrue(calls.isEmpty()); + } + + @Test + void inlineTagIsExpandedForPlayersHoldingTheOptInPermission() { + grantInlinePlaceholders(); + var tag = new PromptTag("", "a", null, "%player_name%"); + + var out = (AnvilPrompt) expander.expandInline(player, InlineTagMapper.toPromptDefinition(tag)); + + assertEquals("[%player_name%]", out.promptText()); + } + + @Test + void unexpandedInlineTextIsStillLengthBounded() { + var text = "x".repeat(PromptPresentationExpander.MAX_DISPLAY_LENGTH + 10); + var tag = new PromptTag("", "a", null, text); + + var out = (AnvilPrompt) expander.expandInline(player, InlineTagMapper.toPromptDefinition(tag)); + + assertEquals(PromptPresentationExpander.MAX_DISPLAY_LENGTH, out.promptText().length()); + } + @Test void inlineDialogExpandsEverySubTagDisplayTextAndTitle() { + grantInlinePlaceholders(); var titleRow = new PromptTag("", "d", "title", "Header %a%"); var bodyRow = new PromptTag("", "d", "body", "Note %b%"); var textRow = new PromptTag("", "d", "text", "Enter %c%"); @@ -434,6 +527,7 @@ void itemExpandsPresentationFieldsExactlyOnce() { @Test void inlineDialogWithoutTitleConfigKeepsNullTitle() { + grantInlinePlaceholders(); var tag = new PromptTag("", "d", "text", "Enter %a%"); var out = expander.expandInlineDialog(player, tag); assertEquals("[Enter %a%]", out.displayText()); @@ -443,6 +537,7 @@ void inlineDialogWithoutTitleConfigKeepsNullTitle() { @Test void inlineDialogEmptyMainIsPreservedForFactoryFallback() { + grantInlinePlaceholders(); var tag = new PromptTag( "", From 1b9a0496f20d4c3f0a3ded081e2f313ba1aab641 Mon Sep 17 00:00:00 2001 From: CyR1en Date: Wed, 25 Nov 2026 16:25:47 +0000 Subject: [PATCH 2/3] Expand placeholders in console-delegated inline prompt text Inline tags in a /consoledelegate command line are written by the console, not typed by the recipient, so carry that origin from ScreenManager into the expander instead of gating on the recipient's opt-in permission. /playerdelegate and ordinary player commands stay untrusted. --- .../promptpaper/factory/PromptFactory.java | 24 +++++++--- .../factory/PromptPresentationExpander.java | 24 +++++++--- .../promptpaper/screen/ScreenManager.java | 35 +++++++++++++-- .../promptpaper/engine/ScreenManagerTest.java | 44 +++++++++++++++++-- .../PromptPresentationExpanderTest.java | 24 ++++++++++ 5 files changed, 133 insertions(+), 18 deletions(-) diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java index e44ff0a..e58be82 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java @@ -203,11 +203,12 @@ public InputScreen create(Player player, PromptDefinition def, DialogCompletionC * so it goes through {@link PromptPresentationExpander#expandInline} instead of the preset path. */ private InputScreen createInline( - Player player, PromptDefinition def, DialogCompletionContext context) { + Player player, PromptDefinition def, DialogCompletionContext context, boolean trustedOrigin) { plugin .getPluginLogger() .debug("PromptFactory.createInline: player=" + player.getName() + " type=" + def.type()); - return createExpanded(player, presentationExpander.expandInline(player, def), context); + return createExpanded( + player, presentationExpander.expandInline(player, def, trustedOrigin), context); } /** @@ -258,9 +259,22 @@ public InputScreen createFromTag(Player player, PromptTag tag) { * * *

    Inline text is typed by the player, so placeholders in it are only resolved for players - * holding {@code promptpaper.inline.placeholders}; preset text is always resolved. + * holding {@code promptpaper.inline.placeholders} or for a trusted origin (see the four-argument + * overload); preset text is always resolved. */ public InputScreen createFromTag(Player player, PromptTag tag, DialogCompletionContext context) { + return createFromTag(player, tag, context, false); + } + + /** + * As {@link #createFromTag(Player, PromptTag, DialogCompletionContext)}. + * + * @param trustedOrigin {@code true} when the command line holding the tag was written by the + * console ({@code /consoledelegate}) rather than typed by a player; placeholders in its + * inline text are then resolved. + */ + public InputScreen createFromTag( + Player player, PromptTag tag, DialogCompletionContext context, boolean trustedOrigin) { if (tag == null) throw new IllegalArgumentException("PromptTag must not be null"); if (tag.isPreset()) { // Lookup uses the raw displayText — a PAPI-looking preset id must never be expanded. @@ -293,12 +307,12 @@ public InputScreen createFromTag(Player player, PromptTag tag, DialogCompletionC if (isDialog) { // Expand the presentation-only copy of the tag, then build the dialog screen directly so // the expanded fields are materialized exactly once. - var expandedTag = presentationExpander.expandInlineDialog(player, tag); + var expandedTag = presentationExpander.expandInlineDialog(player, tag, trustedOrigin); var dialogScreen = new DialogPromptScreen(plugin, player, expandedTag, promptConfig, context); return wrapWithTagTitle(player, expandedTag, dialogScreen); } var def = InlineTagMapper.toPromptDefinition(tag, mappings); - return createInline(player, def, context); + return createInline(player, def, context, trustedOrigin); } private ChatPromptScreen createChat(Player player, ChatPrompt chat) { diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java index ce5f09c..62b1a2f 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java @@ -55,7 +55,8 @@ *

    Only preset-sourced definitions ({@link #expand}) are sent to the expansion delegate. Inline * tags are written on the command line by the player, so {@link #expandInline} and {@link * #expandInlineDialog} leave their text unexpanded (length-bounded only) unless the player holds - * {@value #INLINE_PLACEHOLDER_PERMISSION}. + * {@value #INLINE_PLACEHOLDER_PERMISSION} or the caller marks the tag as written by the console + * ({@code /consoledelegate}). * *

    Never expanded

    * @@ -202,11 +203,19 @@ private dev.cyr1en.promptpaper.preset.ConfirmationPrompt expandConfirmation( * passed through unexpanded unless the player holds {@value #INLINE_PLACEHOLDER_PERMISSION}. */ PromptDefinition expandInline(Player player, PromptDefinition raw) { - return forInline(player).expand(player, raw); + return expandInline(player, raw, false); } - private PromptPresentationExpander forInline(Player player) { - if (literal == null) return this; + /** + * @param trustedOrigin {@code true} when the inline tag was written by the console ({@code + * /consoledelegate}), not typed by a player; such text is expanded. + */ + PromptDefinition expandInline(Player player, PromptDefinition raw, boolean trustedOrigin) { + return forInline(player, trustedOrigin).expand(player, raw); + } + + private PromptPresentationExpander forInline(Player player, boolean trustedOrigin) { + if (literal == null || trustedOrigin) return this; return player != null && player.hasPermission(INLINE_PLACEHOLDER_PERMISSION) ? this : literal; } @@ -220,8 +229,13 @@ private PromptPresentationExpander forInline(Player player) { * {@value #INLINE_PLACEHOLDER_PERMISSION}. */ PromptTag expandInlineDialog(Player player, PromptTag raw) { + return expandInlineDialog(player, raw, false); + } + + /** As {@link #expandInlineDialog(Player, PromptTag)}; see {@link #expandInline} for the flag. */ + PromptTag expandInlineDialog(Player player, PromptTag raw, boolean trustedOrigin) { Objects.requireNonNull(raw, "raw"); - return forInline(player).expandTag(player, raw); + return forInline(player, trustedOrigin).expandTag(player, raw); } private PromptTag expandTag(Player player, PromptTag raw) { diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java index 90f300c..0466763 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java @@ -52,6 +52,30 @@ public class ScreenManager { @FunctionalInterface public interface ScreenCreator { InputScreen create(Player player, PromptTag tag, DialogCompletionContext context); + + /** + * @param trustedOrigin whether the command line was written by the console ({@code + * /consoledelegate}) instead of typed by a player + */ + default InputScreen create( + Player player, PromptTag tag, DialogCompletionContext context, boolean trustedOrigin) { + return create(player, tag, context); + } + } + + private static ScreenCreator factoryCreator(PromptFactory factory) { + return new ScreenCreator() { + @Override + public InputScreen create(Player player, PromptTag tag, DialogCompletionContext context) { + return factory.createFromTag(player, tag, context); + } + + @Override + public InputScreen create( + Player player, PromptTag tag, DialogCompletionContext context, boolean trustedOrigin) { + return factory.createFromTag(player, tag, context, trustedOrigin); + } + }; } public enum DispatchMode { @@ -84,7 +108,8 @@ public enum DispatchMode { public ScreenManager( CommandPrompter plugin, PromptEngine engine, PromptFactory factory, Scheduler scheduler) { - this(plugin, engine, factory, scheduler, factory != null ? factory::createFromTag : null, null); + this( + plugin, engine, factory, scheduler, factory != null ? factoryCreator(factory) : null, null); } public ScreenManager( @@ -139,7 +164,7 @@ public ScreenManager( this.factory = factory; this.scheduler = scheduler; this.screenCreator = - screenCreator != null ? screenCreator : (factory != null ? factory::createFromTag : null); + screenCreator != null ? screenCreator : (factory != null ? factoryCreator(factory) : null); this.playerExecutorFactory = playerExecutorFactory != null ? playerExecutorFactory @@ -525,10 +550,12 @@ private void showPrompt(Player player, PromptTag tag) { } } else { var context = buildCompletionContext(player, tag); + // A /consoledelegate command line was written by the console, not typed by this player. + var trustedOrigin = dispatchModes.get(uuid) == DispatchMode.CONSOLE; screen = screenCreator != null - ? screenCreator.create(player, tag, context) - : factory.createFromTag(player, tag, context); + ? screenCreator.create(player, tag, context, trustedOrigin) + : factory.createFromTag(player, tag, context, trustedOrigin); } if (screen == null) { diff --git a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java index 8dfa3b0..bc06d62 100644 --- a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java +++ b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java @@ -146,6 +146,40 @@ void startDelegatedSessionWithPromptsCreatesScreen() { assertTrue(screenManager.hasActiveScreen(player)); } + @Test + void onlyConsoleDelegatedInlineTagsAreCreatedWithTrustedOrigin() { + var origins = new java.util.ArrayList(); + var recording = + new ScreenManager.ScreenCreator() { + @Override + public dev.cyr1en.promptui.InputScreen create( + Player p, + dev.cyr1en.promptcore.PromptTag tag, + dev.cyr1en.promptpaper.screen.dialog.DialogCompletionContext context) { + return factory.createFromTag(p, tag, context); + } + + @Override + public dev.cyr1en.promptui.InputScreen create( + Player p, + dev.cyr1en.promptcore.PromptTag tag, + dev.cyr1en.promptpaper.screen.dialog.DialogCompletionContext context, + boolean trustedOrigin) { + origins.add(trustedOrigin); + return factory.createFromTag(p, tag, context, trustedOrigin); + } + }; + var manager = new ScreenManager(plugin, engine, factory, scheduler, recording, null); + + manager.startDelegatedSession( + createPlayer(), "/cmd ", ScreenManager.DispatchMode.CONSOLE, null); + manager.startSession(createPlayer(), "/cmd "); + manager.startDelegatedSession( + createPlayer(), "/cmd ", ScreenManager.DispatchMode.ATTACHMENT, "some.key"); + + assertEquals(java.util.List.of(true, false, false), origins); + } + @Test void handleChatInputProcessesAnswer() { var player = createPlayer(); @@ -489,7 +523,7 @@ void presetDialogWithTabCompletionBuildsCompletionContext() { java.util.Map.of())); var dummyScreen = org.mockito.Mockito.mock(dev.cyr1en.promptui.InputScreen.class); - when(mockFactory.createFromTag(any(Player.class), any(PromptTag.class), any())) + when(mockFactory.createFromTag(any(Player.class), any(PromptTag.class), any(), eq(false))) .thenReturn(dummyScreen); var player = createPlayer(); @@ -498,7 +532,8 @@ void presetDialogWithTabCompletionBuildsCompletionContext() { var captor = org.mockito.ArgumentCaptor.forClass( dev.cyr1en.promptpaper.screen.dialog.DialogCompletionContext.class); - verify(mockFactory).createFromTag(eq(player), any(PromptTag.class), captor.capture()); + verify(mockFactory) + .createFromTag(eq(player), any(PromptTag.class), captor.capture(), eq(false)); assertNotNull(captor.getValue()); assertEquals("/cmd ", captor.getValue().partialCommand()); @@ -529,7 +564,7 @@ void presetDialogWithoutTabCompletionPassesNullContext() { when(registry.getPrompt("my_conf_preset")).thenReturn(java.util.Optional.of(dialog)); var dummyScreen = org.mockito.Mockito.mock(dev.cyr1en.promptui.InputScreen.class); - when(mockFactory.createFromTag(any(Player.class), any(PromptTag.class), any())) + when(mockFactory.createFromTag(any(Player.class), any(PromptTag.class), any(), eq(false))) .thenReturn(dummyScreen); var player = createPlayer(); @@ -538,7 +573,8 @@ void presetDialogWithoutTabCompletionPassesNullContext() { var captor = org.mockito.ArgumentCaptor.forClass( dev.cyr1en.promptpaper.screen.dialog.DialogCompletionContext.class); - verify(mockFactory).createFromTag(eq(player), any(PromptTag.class), captor.capture()); + verify(mockFactory) + .createFromTag(eq(player), any(PromptTag.class), captor.capture(), eq(false)); assertNull(captor.getValue()); } diff --git a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java index e93be03..9e95414 100644 --- a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java +++ b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpanderTest.java @@ -326,6 +326,30 @@ private void grantInlinePlaceholders() { .thenReturn(true); } + @Test + void consoleAuthoredInlineTagIsExpandedWithoutRecipientPermission() { + var recording = new PromptPresentationExpander((p, value) -> "[" + value + "]"); + var tag = + new PromptTag( + "", + "a", + null, + "%player_name%", + true, + null, + PromptTag.AnswerType.NONE, + List.of(), + false, + null); + + var inline = + (AnvilPrompt) recording.expandInline(player, InlineTagMapper.toPromptDefinition(tag), true); + var inlineDialog = recording.expandInlineDialog(player, tag, true); + + assertEquals("[%player_name%]", inline.promptText()); + assertEquals("[%player_name%]", inlineDialog.displayText()); + } + @Test void inlineTagNeverReachesDelegateButPresetDefinitionStillDoes() { var calls = new ArrayList(); From 0fc3369ba07e1839c38372adf3acd1c0ec03a94d Mon Sep 17 00:00:00 2001 From: CyR1en Date: Thu, 26 Nov 2026 07:33:40 +0000 Subject: [PATCH 3/3] Treat /playerdelegate inline prompt text as console-authored PlayerDelegateCommand is restricted to ConsoleCommandSender, so DispatchMode.ATTACHMENT command lines are trusted like DispatchMode.CONSOLE. --- .../java/dev/cyr1en/promptpaper/factory/PromptFactory.java | 4 ++-- .../promptpaper/factory/PromptPresentationExpander.java | 4 ++-- .../java/dev/cyr1en/promptpaper/screen/ScreenManager.java | 7 ++++--- .../dev/cyr1en/promptpaper/engine/ScreenManagerTest.java | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java index e58be82..183d733 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptFactory.java @@ -270,8 +270,8 @@ public InputScreen createFromTag(Player player, PromptTag tag, DialogCompletionC * As {@link #createFromTag(Player, PromptTag, DialogCompletionContext)}. * * @param trustedOrigin {@code true} when the command line holding the tag was written by the - * console ({@code /consoledelegate}) rather than typed by a player; placeholders in its - * inline text are then resolved. + * console ({@code /consoledelegate}, {@code /playerdelegate}) rather than typed by a player; + * placeholders in its inline text are then resolved. */ public InputScreen createFromTag( Player player, PromptTag tag, DialogCompletionContext context, boolean trustedOrigin) { diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java index 62b1a2f..cfd0160 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/factory/PromptPresentationExpander.java @@ -56,7 +56,7 @@ * tags are written on the command line by the player, so {@link #expandInline} and {@link * #expandInlineDialog} leave their text unexpanded (length-bounded only) unless the player holds * {@value #INLINE_PLACEHOLDER_PERMISSION} or the caller marks the tag as written by the console - * ({@code /consoledelegate}). + * ({@code /consoledelegate}, {@code /playerdelegate}). * *

    Never expanded

    * @@ -208,7 +208,7 @@ PromptDefinition expandInline(Player player, PromptDefinition raw) { /** * @param trustedOrigin {@code true} when the inline tag was written by the console ({@code - * /consoledelegate}), not typed by a player; such text is expanded. + * /consoledelegate}, {@code /playerdelegate}), not typed by a player; such text is expanded. */ PromptDefinition expandInline(Player player, PromptDefinition raw, boolean trustedOrigin) { return forInline(player, trustedOrigin).expand(player, raw); diff --git a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java index 0466763..87ca308 100644 --- a/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java +++ b/prompt-paper/src/main/java/dev/cyr1en/promptpaper/screen/ScreenManager.java @@ -55,7 +55,7 @@ public interface ScreenCreator { /** * @param trustedOrigin whether the command line was written by the console ({@code - * /consoledelegate}) instead of typed by a player + * /consoledelegate}, {@code /playerdelegate}) instead of typed by a player */ default InputScreen create( Player player, PromptTag tag, DialogCompletionContext context, boolean trustedOrigin) { @@ -550,8 +550,9 @@ private void showPrompt(Player player, PromptTag tag) { } } else { var context = buildCompletionContext(player, tag); - // A /consoledelegate command line was written by the console, not typed by this player. - var trustedOrigin = dispatchModes.get(uuid) == DispatchMode.CONSOLE; + // Both delegate commands are console-only, so the console wrote this command line. + var mode = dispatchModes.get(uuid); + var trustedOrigin = mode == DispatchMode.CONSOLE || mode == DispatchMode.ATTACHMENT; screen = screenCreator != null ? screenCreator.create(player, tag, context, trustedOrigin) diff --git a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java index bc06d62..3d8628b 100644 --- a/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java +++ b/prompt-paper/src/test/java/dev/cyr1en/promptpaper/engine/ScreenManagerTest.java @@ -147,7 +147,7 @@ void startDelegatedSessionWithPromptsCreatesScreen() { } @Test - void onlyConsoleDelegatedInlineTagsAreCreatedWithTrustedOrigin() { + void onlyDelegatedInlineTagsAreCreatedWithTrustedOrigin() { var origins = new java.util.ArrayList(); var recording = new ScreenManager.ScreenCreator() { @@ -177,7 +177,7 @@ public dev.cyr1en.promptui.InputScreen create( manager.startDelegatedSession( createPlayer(), "/cmd ", ScreenManager.DispatchMode.ATTACHMENT, "some.key"); - assertEquals(java.util.List.of(true, false, false), origins); + assertEquals(java.util.List.of(true, false, true), origins); } @Test