mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-15 20:23:53 -07:00
[Bleeding] Added Conversations API. Addresses BUKKIT-864
By: rmichela <deltahat@gmail.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package org.bukkit.conversations;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class ConversationContextTest {
|
||||
@Test
|
||||
public void TestFromWhom() {
|
||||
Conversable conversable = new FakeConversable();
|
||||
ConversationContext context = new ConversationContext(null, conversable, new HashMap<Object, Object>());
|
||||
assertEquals(conversable, context.getForWhom());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestPlugin() {
|
||||
Conversable conversable = new FakeConversable();
|
||||
ConversationContext context = new ConversationContext(null, conversable, new HashMap<Object, Object>());
|
||||
assertEquals(null, context.getPlugin());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestSessionData() {
|
||||
Conversable conversable = new FakeConversable();
|
||||
Map session = new HashMap();
|
||||
session.put("key", "value");
|
||||
ConversationContext context = new ConversationContext(null, conversable, session);
|
||||
assertEquals("value", context.getSessionData("key"));
|
||||
}
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
package org.bukkit.conversations;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class ConversationTest {
|
||||
|
||||
@Test
|
||||
public void testBaseConversationFlow() {
|
||||
FakeConversable forWhom = new FakeConversable();
|
||||
Conversation conversation = new Conversation(null, forWhom, new FirstPrompt());
|
||||
|
||||
// Conversation not yet begun
|
||||
assertNull(forWhom.lastSentMessage);
|
||||
assertEquals(conversation.getForWhom(), forWhom);
|
||||
assertTrue(conversation.isModal());
|
||||
|
||||
// Begin the conversation
|
||||
conversation.begin();
|
||||
assertEquals("FirstPrompt", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.begunConversation);
|
||||
|
||||
// Send the first input
|
||||
conversation.acceptInput("FirstInput");
|
||||
assertEquals("SecondPrompt", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.abandonedConverstion);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversationFactory() {
|
||||
FakeConversable forWhom = new FakeConversable();
|
||||
NullConversationPrefix prefix = new NullConversationPrefix();
|
||||
ConversationFactory factory = new ConversationFactory(null)
|
||||
.withFirstPrompt(new FirstPrompt())
|
||||
.withModality(false)
|
||||
.withPrefix(prefix);
|
||||
Conversation conversation = factory.buildConversation(forWhom);
|
||||
|
||||
// Conversation not yet begun
|
||||
assertNull(forWhom.lastSentMessage);
|
||||
assertEquals(conversation.getForWhom(), forWhom);
|
||||
assertFalse(conversation.isModal());
|
||||
assertEquals(conversation.getPrefix(), prefix);
|
||||
|
||||
// Begin the conversation
|
||||
conversation.begin();
|
||||
assertEquals("FirstPrompt", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.begunConversation);
|
||||
|
||||
// Send the first input
|
||||
conversation.acceptInput("FirstInput");
|
||||
assertEquals("SecondPrompt", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.abandonedConverstion);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapeSequence() {
|
||||
FakeConversable forWhom = new FakeConversable();
|
||||
Conversation conversation = new Conversation(null, forWhom, new FirstPrompt());
|
||||
conversation.addConversationCanceller(new ExactMatchConversationCanceller("bananas"));
|
||||
|
||||
// Begin the conversation
|
||||
conversation.begin();
|
||||
assertEquals("FirstPrompt", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.begunConversation);
|
||||
|
||||
// Send the first input
|
||||
conversation.acceptInput("bananas");
|
||||
assertEquals("bananas", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.abandonedConverstion);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotPlayer() {
|
||||
FakeConversable forWhom = new FakeConversable();
|
||||
NullConversationPrefix prefix = new NullConversationPrefix();
|
||||
ConversationFactory factory = new ConversationFactory(null)
|
||||
.thatExcludesNonPlayersWithMessage("bye");
|
||||
Conversation conversation = factory.buildConversation(forWhom);
|
||||
|
||||
// Begin the conversation
|
||||
conversation.begin();
|
||||
assertEquals("bye", forWhom.lastSentMessage);
|
||||
assertEquals(conversation, forWhom.begunConversation);
|
||||
assertEquals(conversation, forWhom.abandonedConverstion);
|
||||
}
|
||||
|
||||
private class FirstPrompt extends StringPrompt {
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
return "FirstPrompt";
|
||||
}
|
||||
|
||||
public Prompt acceptInput(ConversationContext context, String input) {
|
||||
assertEquals("FirstInput", input);
|
||||
context.setSessionData("data", 10);
|
||||
return new SecondPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
private class SecondPrompt extends MessagePrompt {
|
||||
|
||||
@Override
|
||||
protected Prompt getNextPrompt(ConversationContext context) {
|
||||
return Prompt.END_OF_CONVERSATION;
|
||||
}
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
// Assert that session data passes from one prompt to the next
|
||||
assertEquals(context.getSessionData("data"), 10);
|
||||
return "SecondPrompt";
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
package org.bukkit.conversations;
|
||||
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class FakeConversable implements Conversable {
|
||||
public String lastSentMessage;
|
||||
public Conversation begunConversation;
|
||||
public Conversation abandonedConverstion;
|
||||
|
||||
public boolean isConversing() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void acceptConversationInput(String input) {
|
||||
|
||||
}
|
||||
|
||||
public boolean beginConversation(Conversation conversation) {
|
||||
begunConversation = conversation;
|
||||
conversation.outputNextPrompt();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void abandonConversation(Conversation conversation) {
|
||||
abandonedConverstion = conversation;
|
||||
}
|
||||
|
||||
public void sendRawMessage(String message) {
|
||||
lastSentMessage = message;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPermissionSet(String name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isPermissionSet(Permission perm) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasPermission(String name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasPermission(Permission perm) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public PermissionAttachment addAttachment(Plugin plugin) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removeAttachment(PermissionAttachment attachment) {
|
||||
|
||||
}
|
||||
|
||||
public void recalculatePermissions() {
|
||||
|
||||
}
|
||||
|
||||
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isOp() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setOp(boolean value) {
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,115 @@
|
||||
package org.bukkit.conversations;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class ValidatingPromptTest {
|
||||
|
||||
@Test
|
||||
public void TestBooleanPrompt() {
|
||||
TestBooleanPrompt prompt = new TestBooleanPrompt();
|
||||
assertTrue(prompt.isInputValid(null, "true"));
|
||||
assertFalse(prompt.isInputValid(null, "bananas"));
|
||||
prompt.acceptInput(null, "true");
|
||||
assertTrue(prompt.result);
|
||||
prompt.acceptInput(null, "no");
|
||||
assertFalse(prompt.result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestFixedSetPrompt() {
|
||||
TestFixedSetPrompt prompt = new TestFixedSetPrompt("foo", "bar");
|
||||
assertTrue(prompt.isInputValid(null, "foo"));
|
||||
assertFalse(prompt.isInputValid(null, "cheese"));
|
||||
prompt.acceptInput(null, "foo");
|
||||
assertEquals("foo", prompt.result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestNumericPrompt() {
|
||||
TestNumericPrompt prompt = new TestNumericPrompt();
|
||||
assertTrue(prompt.isInputValid(null, "1010220"));
|
||||
assertFalse(prompt.isInputValid(null, "tomato"));
|
||||
prompt.acceptInput(null, "1010220");
|
||||
assertEquals(1010220, prompt.result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestRegexPrompt() {
|
||||
TestRegexPrompt prompt = new TestRegexPrompt("a.c");
|
||||
assertTrue(prompt.isInputValid(null, "abc"));
|
||||
assertTrue(prompt.isInputValid(null, "axc"));
|
||||
assertFalse(prompt.isInputValid(null, "xyz"));
|
||||
prompt.acceptInput(null, "abc");
|
||||
assertEquals("abc", prompt.result);
|
||||
}
|
||||
|
||||
//TODO: TestPlayerNamePrompt()
|
||||
|
||||
private class TestBooleanPrompt extends BooleanPrompt {
|
||||
public boolean result;
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, boolean input) {
|
||||
result = input;
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestFixedSetPrompt extends FixedSetPrompt {
|
||||
public String result;
|
||||
|
||||
public TestFixedSetPrompt(String... fixedSet) {
|
||||
super(fixedSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, String input) {
|
||||
result = input;
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestNumericPrompt extends NumericPrompt {
|
||||
public Number result;
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
|
||||
result = input;
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestRegexPrompt extends RegexPrompt {
|
||||
public String result;
|
||||
|
||||
public TestRegexPrompt(String pattern) {
|
||||
super(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Prompt acceptValidatedInput(ConversationContext context, String input) {
|
||||
result = input;
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getPromptText(ConversationContext context) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.conversations.Conversation;
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Egg;
|
||||
import org.bukkit.entity.Entity;
|
||||
@@ -718,4 +719,20 @@ public class TestPlayer implements Player {
|
||||
public boolean setWindowProperty(Property prop, int value) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public boolean isConversing() {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public void acceptConversationInput(String input) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public boolean beginConversation(Conversation conversation) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
public void abandonConversation(Conversation conversation) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user