diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 7ff27f264a..75fb8d537d 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -58,6 +58,7 @@ import org.bukkit.command.SimpleCommandMap; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; +import org.bukkit.conversations.Conversable; import org.bukkit.craftbukkit.help.SimpleHelpMap; import org.bukkit.craftbukkit.inventory.CraftFurnaceRecipe; import org.bukkit.craftbukkit.inventory.CraftInventoryCustom; @@ -443,7 +444,15 @@ public final class CraftServer implements Server { } // NOTE: Should only be called from MinecraftServer.b() - public boolean dispatchCommand(CommandSender sender, ServerCommand serverCommand) { + public boolean dispatchServerCommand(CommandSender sender, ServerCommand serverCommand) { + if (sender instanceof Conversable) { + Conversable conversable = (Conversable)sender; + + if (conversable.isConversing()) { + conversable.acceptConversationInput(serverCommand.command); + return true; + } + } return dispatchCommand(sender, serverCommand.command); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java b/paper-server/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java index f1deb305c9..c5c817c8ec 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/command/ColouredConsoleSender.java @@ -42,16 +42,17 @@ public class ColouredConsoleSender extends CraftConsoleCommandSender { @Override public void sendMessage(String message) { if (terminal.isANSISupported()) { - String result = message; - - for (ChatColor color : colors) { - if (replacements.containsKey(color)) { - result = result.replaceAll(color.toString(), replacements.get(color)); - } else { - result = result.replaceAll(color.toString(), ""); + if (!conversationTracker.isConversingModaly()) { + String result = message; + for (ChatColor color : colors) { + if (replacements.containsKey(color)) { + result = result.replaceAll(color.toString(), replacements.get(color)); + } else { + result = result.replaceAll(color.toString(), ""); + } } + System.out.println(result + ANSICodes.attrib(0)); } - System.out.println(result + ANSICodes.attrib(0)); } else { super.sendMessage(message); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/command/CraftConsoleCommandSender.java b/paper-server/src/main/java/org/bukkit/craftbukkit/command/CraftConsoleCommandSender.java index dbcfddc9ea..00f18d1d91 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/command/CraftConsoleCommandSender.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/command/CraftConsoleCommandSender.java @@ -2,17 +2,25 @@ package org.bukkit.craftbukkit.command; import org.bukkit.ChatColor; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.conversations.Conversation; +import org.bukkit.craftbukkit.conversations.ConversationTracker; /** * Represents CLI input from a console */ public class CraftConsoleCommandSender extends ServerCommandSender implements ConsoleCommandSender { + protected ConversationTracker conversationTracker = new ConversationTracker(); + protected CraftConsoleCommandSender() { super(); } public void sendMessage(String message) { + sendRawMessage(message); + } + + public void sendRawMessage(String message) { System.out.println(ChatColor.stripColor(message)); } @@ -33,4 +41,20 @@ public class CraftConsoleCommandSender extends ServerCommandSender implements Co public void setOp(boolean value) { throw new UnsupportedOperationException("Cannot change operator status of server console"); } + + public boolean beginConversation(Conversation conversation) { + return conversationTracker.beginConversation(conversation); + } + + public void abandonConversation(Conversation conversation) { + conversationTracker.abandonConversation(conversation); + } + + public void acceptConversationInput(String input) { + conversationTracker.acceptConversationInput(input); + } + + public boolean isConversing() { + return conversationTracker.isConversing(); + } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/conversations/ConversationTracker.java b/paper-server/src/main/java/org/bukkit/craftbukkit/conversations/ConversationTracker.java new file mode 100644 index 0000000000..46771728df --- /dev/null +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/conversations/ConversationTracker.java @@ -0,0 +1,62 @@ +package org.bukkit.craftbukkit.conversations; + +import org.bukkit.conversations.Conversation; + +import java.util.Deque; +import java.util.LinkedList; + +/** + */ +public class ConversationTracker { + + private Deque conversationQueue = new LinkedList(); + + public synchronized boolean beginConversation(Conversation conversation) { + if (!conversationQueue.contains(conversation)) { + conversationQueue.addLast(conversation); + if (conversationQueue.getFirst() == conversation) { + conversation.begin(); + conversation.outputNextPrompt(); + return true; + } + } + return true; + } + + public synchronized void abandonConversation(Conversation conversation) { + if (!conversationQueue.isEmpty()) { + if (conversationQueue.getFirst() == conversation) { + conversation.abandon(); + } + if (conversationQueue.contains(conversation)) { + conversationQueue.remove(conversation); + } + if (!conversationQueue.isEmpty()) { + conversationQueue.getFirst().outputNextPrompt(); + } + } + } + + public synchronized void abandonAllConversations() { + + Deque oldQueue = conversationQueue; + conversationQueue = new LinkedList(); + for(Conversation conversation : oldQueue) { + conversation.abandon(); + } + } + + public synchronized void acceptConversationInput(String input) { + if (isConversing()) { + conversationQueue.getFirst().acceptInput(input); + } + } + + public synchronized boolean isConversing() { + return !conversationQueue.isEmpty(); + } + + public synchronized boolean isConversingModaly() { + return isConversing() && conversationQueue.getFirst().isModal(); + } +} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index b065d47dfd..039cf9b57f 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -37,6 +37,8 @@ import org.bukkit.Statistic; import org.bukkit.World; import org.bukkit.block.BlockFace; import org.bukkit.configuration.serialization.DelegateDeserialization; +import org.bukkit.conversations.Conversation; +import org.bukkit.craftbukkit.conversations.ConversationTracker; import org.bukkit.craftbukkit.CraftEffect; import org.bukkit.craftbukkit.CraftOfflinePlayer; import org.bukkit.craftbukkit.CraftServer; @@ -59,6 +61,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player { private long firstPlayed = 0; private long lastPlayed = 0; private boolean hasPlayedBefore = false; + private ConversationTracker conversationTracker = new ConversationTracker(); private Set channels = new HashSet(); private Map hiddenPlayers = new MapMaker().softValues().makeMap(); private int hash = 0; @@ -133,9 +136,11 @@ public class CraftPlayer extends CraftHumanEntity implements Player { } public void sendMessage(String message) { - this.sendRawMessage(message); + if (!conversationTracker.isConversingModaly()) { + this.sendRawMessage(message); + } } - + public void sendMessage(String[] messages) { for (String message : messages) { sendMessage(message); @@ -692,6 +697,22 @@ public class CraftPlayer extends CraftHumanEntity implements Player { data.setLong("lastPlayed", System.currentTimeMillis()); } + public boolean beginConversation(Conversation conversation) { + return conversationTracker.beginConversation(conversation); + } + + public void abandonConversation(Conversation conversation) { + conversationTracker.abandonConversation(conversation); + } + + public void acceptConversationInput(String input) { + conversationTracker.acceptConversationInput(input); + } + + public boolean isConversing() { + return conversationTracker.isConversing(); + } + public void sendPluginMessage(Plugin source, String channel, byte[] message) { StandardMessenger.validatePluginMessage(server.getMessenger(), source, channel, message); @@ -773,4 +794,8 @@ public class CraftPlayer extends CraftHumanEntity implements Player { getHandle().setContainerData(container, prop.getId(), value); return true; } -} + + public void disconnect(String reason) { + conversationTracker.abandonAllConversations(); + } +} \ No newline at end of file