diff --git a/src/main/java/com/comandante/creeper/server/MultiLineInputHandler.java b/src/main/java/com/comandante/creeper/server/MultiLineInputHandler.java index 79213610ec57fc598ec1c43b5812a2943d797b02..6b3a7f3f7f14982d9f85c5a6eba9712ecbcf5334 100644 --- a/src/main/java/com/comandante/creeper/server/MultiLineInputHandler.java +++ b/src/main/java/com/comandante/creeper/server/MultiLineInputHandler.java @@ -19,11 +19,11 @@ public class MultiLineInputHandler extends SimpleChannelUpstreamHandler { try { CreeperSession creeperSession = (CreeperSession) e.getChannel().getAttachment(); String message = (String) e.getMessage(); - if (message.equals("DONE")) { + if (message.equalsIgnoreCase("DONE")) { e.getChannel().getPipeline().addLast(UUID.randomUUID().toString(), creeperSession.getGrabMultiLineInput().get().getValue()); return; } - gameManager.getMultiLineInputManager().addToMultiLine(creeperSession.getGrabMultiLineInput().get().getKey(), message); + gameManager.getMultiLineInputManager().addToMultiLine(creeperSession.getGrabMultiLineInput().get().getKey(), message + "\r\n"); } finally { e.getChannel().getPipeline().remove(ctx.getHandler()); super.messageReceived(ctx, e); diff --git a/src/main/java/com/comandante/creeper/server/MultiLineInputManager.java b/src/main/java/com/comandante/creeper/server/MultiLineInputManager.java index 3b0fef9cbe1d47a04b285ebdd3bc26f42033ba88..139e2b6118d04f6e1e424e5d209ecb32b3a9192b 100644 --- a/src/main/java/com/comandante/creeper/server/MultiLineInputManager.java +++ b/src/main/java/com/comandante/creeper/server/MultiLineInputManager.java @@ -1,6 +1,8 @@ package com.comandante.creeper.server; +import com.google.common.base.Joiner; import com.google.common.collect.Maps; +import org.apache.commons.lang3.ArrayUtils; import java.util.Map; import java.util.UUID; @@ -10,11 +12,11 @@ public class MultiLineInputManager { private final Map<UUID, StringBuilder> multiLineInputs = Maps.newConcurrentMap(); public void addToMultiLine(UUID uuid, String input) { - multiLineInputs.get(uuid).append(input).append("\r\n"); + multiLineInputs.get(uuid).append(input); } public String retrieveMultiLineInput(UUID uuid) { - return multiLineInputs.remove(uuid).toString(); + return removeTrailingBlankLines(multiLineInputs.remove(uuid).toString()); } public UUID createNewMultiLineInput() { @@ -22,4 +24,15 @@ public class MultiLineInputManager { multiLineInputs.put(retrievalId, new StringBuilder()); return retrievalId; } + + private String removeTrailingBlankLines(String s) { + String[] split = s.split("\r\n"); + String s1 = split[split.length - 1].replaceAll("(\\r|\\n)", ""); + if (s1.isEmpty()) { + String[] strings = ArrayUtils.removeElement(split, split.length - 1); + return Joiner.on("\r\n").join(strings).replaceAll("(\\r|\\n)", ""); + } else { + return s.replaceAll("(\\r|\\n)", ""); + } + } } diff --git a/src/main/java/com/comandante/creeper/server/command/admin/DescriptionCommand.java b/src/main/java/com/comandante/creeper/server/command/admin/DescriptionCommand.java index f886ef3c9ee2b35131c35a398c350c03ab142c8d..0371e3cf49c5fedcdcba6e1210f3009c0e8f4582 100644 --- a/src/main/java/com/comandante/creeper/server/command/admin/DescriptionCommand.java +++ b/src/main/java/com/comandante/creeper/server/command/admin/DescriptionCommand.java @@ -41,7 +41,7 @@ public class DescriptionCommand extends Command { return; } final String playerId = extractPlayerId(session); - gameManager.getChannelUtils().write(playerId, "\n\n ENTERING MULTI LINE INPUT MODE. TYPE \"DONE\" ON AN EMPTY LINE TO EXIT"); + gameManager.getChannelUtils().write(playerId, "You are now in multi-line mode. Type \"done\" on an empty line to exit and save.\r\n"); session.setGrabMultiLineInput(Optional.of( new CreeperEntry<UUID, Command>(gameManager.getMultiLineInputManager().createNewMultiLineInput(), this))); } finally {