Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/world/bentobox/level/util/ConversationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
@NonNull String question,
@Nullable String successMessage)
{
if (user.getPlayer().isConversing())
{
// Bukkit queues conversations per player, so starting another one here would
// stack prompts that later replay as cancel/prompt spam (#451). Repeat the
// question for the pending conversation instead.
user.closeInventory();
user.getPlayer().sendRawMessage(user.getTranslation("level.conversations.prefix") + question);

Check failure on line 52 in src/main/java/world/bentobox/level/util/ConversationUtils.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "level.conversations.prefix" 3 times.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ-udkA7hylc2f8zvDP_&open=AZ-udkA7hylc2f8zvDP_&pullRequest=452
return;
}

// Text input message.
StringPrompt stringPrompt = new StringPrompt()
{
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/world/bentobox/level/util/ConversationUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package world.bentobox.level.util;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.function.Consumer;

import org.bukkit.conversations.Conversation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import world.bentobox.bentobox.api.user.User;
import world.bentobox.level.CommonTestSetup;

/**
* Tests for {@link ConversationUtils}.
*/
class ConversationUtilsTest extends CommonTestSetup {

private User user;

@Override
@BeforeEach
protected void setUp() throws Exception {
super.setUp();
// The conversation timeout canceller starts a scheduler timer on build.
when(plugin.getServer()).thenReturn(server);
user = User.getInstance(p);
}

/**
* A player with no active conversation should get a new one.
*/
@Test
void testCreateStringInputStartsConversation() {
when(p.isConversing()).thenReturn(false);

Consumer<String> consumer = value -> {};
ConversationUtils.createStringInput(consumer, user, "question?", "done");

verify(p).beginConversation(any(Conversation.class));

Check warning on line 44 in src/test/java/world/bentobox/level/util/ConversationUtilsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this call to a deprecated class, it has been marked for removal.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ-udkDRhylc2f8zvDQA&open=AZ-udkDRhylc2f8zvDQA&pullRequest=452
}

/**
* Clicking the search button while a conversation is already pending must not
* queue a second conversation (#451) — it should just repeat the question.
*/
@Test
void testCreateStringInputDoesNotQueueSecondConversation() {
when(p.isConversing()).thenReturn(true);

Consumer<String> consumer = value -> {};
ConversationUtils.createStringInput(consumer, user, "question?", "done");

verify(p, never()).beginConversation(any(Conversation.class));

Check warning on line 58 in src/test/java/world/bentobox/level/util/ConversationUtilsTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this call to a deprecated class, it has been marked for removal.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_Level&issues=AZ-udkDRhylc2f8zvDQB&open=AZ-udkDRhylc2f8zvDQB&pullRequest=452
verify(p).closeInventory();
verify(p).sendRawMessage(anyString());
}
}
Loading