From 3fc4f7bff7bb6f425192dc15102a52419360e0ee Mon Sep 17 00:00:00 2001
From: Chris Kearney <chris@kearneymail.com>
Date: Sun, 14 Aug 2016 11:59:22 -0700
Subject: [PATCH] simple spells command, and some fixes to the print horizontal
 strings crap

---
 .../comandante/creeper/ConfigureCommands.java |  1 +
 .../com/comandante/creeper/CreeperUtils.java  | 22 ++++++++++---
 .../creeper/blackjack/BlackJack.java          |  5 +++
 .../creeper/command/SpellsCommand.java        | 33 +++++++++++++++++++
 .../com/comandante/creeper/player/Player.java |  5 +++
 .../comandante/creeper/CreeperUtilsTest.java  | 18 +++++++---
 6 files changed, 76 insertions(+), 8 deletions(-)
 create mode 100644 src/main/java/com/comandante/creeper/command/SpellsCommand.java

diff --git a/src/main/java/com/comandante/creeper/ConfigureCommands.java b/src/main/java/com/comandante/creeper/ConfigureCommands.java
index 5c989f2d..f51e2801 100755
--- a/src/main/java/com/comandante/creeper/ConfigureCommands.java
+++ b/src/main/java/com/comandante/creeper/ConfigureCommands.java
@@ -93,5 +93,6 @@ public class ConfigureCommands {
         creeperCommandRegistry.addCommand(new KillTallyCommand(gameManager));
         creeperCommandRegistry.addCommand(new CompareCommand(gameManager));
         creeperCommandRegistry.addCommand(new CardsCommand(gameManager));
+        creeperCommandRegistry.addCommand(new SpellsCommand(gameManager));
     }
 }
diff --git a/src/main/java/com/comandante/creeper/CreeperUtils.java b/src/main/java/com/comandante/creeper/CreeperUtils.java
index 1b1c557f..89c0a574 100644
--- a/src/main/java/com/comandante/creeper/CreeperUtils.java
+++ b/src/main/java/com/comandante/creeper/CreeperUtils.java
@@ -22,7 +22,7 @@ public class CreeperUtils {
     public static String printStringsNextToEachOther(List<String> strings, String seperator) {
         // Find the "longest", meaning most newlines string.
         final String[] maxHeightLine = {strings.get(0)};
-        strings.stream().forEach(s -> {
+        strings.forEach(s -> {
             int height = s.split("[\\r\\n]+").length;
             if (maxHeightLine[0] != null && height > maxHeightLine[0].split("[\\r\\n]+").length) {
                 maxHeightLine[0] = s;
@@ -34,14 +34,16 @@ public class CreeperUtils {
         // Make them all even length.  This is terrible streams api usage.
         List<List<String>> textToJoin = strings.stream()
                 .map((Function<String, List<String>>) s -> Lists.newArrayList(s.split("[\\r\\n]+"))).map(strings1 -> {
+                    String asciiColorPattern = "\u001B\\[[;\\d]*m";
                     strings1.forEach(s -> {
-                        if (s.length() > maxLineLength[0]) {
-                            maxLineLength[0] = s.replaceAll("\u001B\\[[;\\d]*m", "").length();
+                        if (s.replaceAll(asciiColorPattern, "").length() > maxLineLength[0]) {
+                            maxLineLength[0] = s.replaceAll(asciiColorPattern, "").length();
                         }
                     });
                     List<String> newStrings = Lists.newArrayList();
                     strings1.forEach(s -> {
-                        int diff = maxLineLength[0] - s.replaceAll("\u001B\\[[;\\d]*m", "").length();
+                        String cleaned = s.replaceAll(asciiColorPattern, "");
+                        int diff = maxLineLength[0] - cleaned.length();
                         for (int i = 0; i < diff; i++) {
                             s += " ";
                         }
@@ -83,4 +85,16 @@ public class CreeperUtils {
         }
     }
 
+    public static String trimTrailingBlanks( String str)
+    {
+        if( str == null)
+            return null;
+        int len = str.length();
+        for( ; len > 0; len--)
+        {
+            if( ! Character.isWhitespace( str.charAt( len - 1)))
+                break;
+        }
+        return str.substring( 0, len);
+    }
 }
diff --git a/src/main/java/com/comandante/creeper/blackjack/BlackJack.java b/src/main/java/com/comandante/creeper/blackjack/BlackJack.java
index c8efce59..cd927bbc 100644
--- a/src/main/java/com/comandante/creeper/blackjack/BlackJack.java
+++ b/src/main/java/com/comandante/creeper/blackjack/BlackJack.java
@@ -58,6 +58,11 @@ public class BlackJack {
     }
 
     public static void main(String[] args) {
+
+        String test = "   │7        │   ";
+        String s = CreeperUtils.trimTrailingBlanks(test);
+        System.out.println(s);
+
         Deck deck = new Deck();
         deck.shuffle();
         List<Card> cards = Lists.newArrayList(deck.next(), deck.next(), deck.next(), deck.next());
diff --git a/src/main/java/com/comandante/creeper/command/SpellsCommand.java b/src/main/java/com/comandante/creeper/command/SpellsCommand.java
new file mode 100644
index 00000000..abd87149
--- /dev/null
+++ b/src/main/java/com/comandante/creeper/command/SpellsCommand.java
@@ -0,0 +1,33 @@
+package com.comandante.creeper.command;
+
+import com.comandante.creeper.managers.GameManager;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.MessageEvent;
+
+import java.util.Arrays;
+import java.util.List;
+
+
+public class SpellsCommand extends Command {
+    final static List<String> validTriggers = Arrays.asList("spells");
+    final static String description = "Lists the spells that a player has learned.";
+    final static String correctUsage = "show <item_name>";
+
+    public SpellsCommand(GameManager gameManager) {
+        super(gameManager, validTriggers, description, correctUsage);
+    }
+
+    @Override
+    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
+        execCommand(ctx, e, () -> {
+            StringBuilder sb = new StringBuilder();
+            List<String> learnedSpells = player.getLearnedSpells();
+            if (learnedSpells.size() == 0) {
+                write("You haven't learned any spells." + "\r\n");
+                return;
+            }
+            learnedSpells.forEach(s -> sb.append(s).append("\r\n"));
+            write(sb.append("\r\n").toString());
+        });
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/comandante/creeper/player/Player.java b/src/main/java/com/comandante/creeper/player/Player.java
index ad9b14d7..52586b0d 100755
--- a/src/main/java/com/comandante/creeper/player/Player.java
+++ b/src/main/java/com/comandante/creeper/player/Player.java
@@ -329,6 +329,11 @@ public class Player extends CreeperEntity {
         }
     }
 
+    public List<String> getLearnedSpells() {
+        PlayerMetadata playerMetadata = getPlayerMetadata();
+        return Lists.newArrayList(playerMetadata.getLearnedSpells());
+    }
+
     public void addInventoryId(String inventoryId) {
         synchronized (interner.intern(playerId)) {
             PlayerMetadata playerMetadata = getPlayerMetadata();
diff --git a/src/test/com/comandante/creeper/CreeperUtilsTest.java b/src/test/com/comandante/creeper/CreeperUtilsTest.java
index 24a8484b..1b91ef6e 100644
--- a/src/test/com/comandante/creeper/CreeperUtilsTest.java
+++ b/src/test/com/comandante/creeper/CreeperUtilsTest.java
@@ -1,5 +1,7 @@
 package com.comandante.creeper;
 
+import com.comandante.creeper.Items.ItemType;
+import com.comandante.creeper.entity.EntityManager;
 import com.comandante.creeper.managers.GameManager;
 import com.comandante.creeper.player.*;
 import com.google.common.collect.Lists;
@@ -15,7 +17,10 @@ public class CreeperUtilsTest {
 
     @Test
     public void testCombineStrings() throws Exception {
-        PlayerMetadata playerMetadata = new PlayerMetadata("usertest", "Testtest", Main.createPlayerId("usertest"), PlayerStats.DEFAULT_PLAYER.createStats(), 0, Sets.newHashSet(PlayerRole.MORTAL), new String[0], 0, new String[0], Maps.newHashMap());
+        String[] strings = new String[2];
+        strings[0] = "feet";
+        strings[1] = "hand";
+        PlayerMetadata playerMetadata = new PlayerMetadata("usertest", "Testtest", Main.createPlayerId("usertest"), PlayerStats.DEFAULT_PLAYER.createStats(), 0, Sets.newHashSet(PlayerRole.MORTAL), strings, 0, new String[0], Maps.newHashMap());
         GameManager gameManager = mock(GameManager.class);
         StatsModifierFactory statsModifierFactory = mock(StatsModifierFactory.class);
         when(statsModifierFactory.getStatsModifier(Matchers.any())).thenReturn(PlayerStats.DEFAULT_PLAYER.createStats());
@@ -23,13 +28,18 @@ public class CreeperUtilsTest {
         PlayerManager playerManager = mock(PlayerManager.class);
         when(playerManager.getPlayerMetadata(Matchers.any())).thenReturn(playerMetadata);
         when(gameManager.getPlayerManager()).thenReturn(playerManager);
+        EntityManager entityManager = mock(EntityManager.class);
+        when(entityManager.getItemEntity(Matchers.startsWith("feet"))).thenReturn(ItemType.BERSEKER_BOOTS.create());
+        when(entityManager.getItemEntity(Matchers.startsWith("hand"))).thenReturn(ItemType.BERSERKER_BATON.create());
+        when(gameManager.getEntityManager()).thenReturn(entityManager);
+        Player usertest = new Player("usertest", gameManager);
 
+        String s = CreeperUtils.printStringsNextToEachOther(Lists.newArrayList(usertest.getLookString(), usertest.getLookString()), "");
 
-        String usertest = new Player("usertest", gameManager).getLookString();
+        System.out.println(s);
 
-        String s = CreeperUtils.printStringsNextToEachOther(Lists.newArrayList(usertest, usertest), " | ");
 
-        System.out.println(s);
+        System.out.println(usertest.getLookString().replaceAll("\u001B\\[[;\\d]*m", ""));
 
 
     }
-- 
GitLab