private static String removeColors(String source) {

    for (ChatColor cc : ChatColor.values()) {
      source = source.replace(cc.toString(), "");
    }
    return source;
  }
Ejemplo n.º 2
0
 public static String parseColors(String str) {
   for (ChatColor color : ChatColor.values()) {
     String name = "\\[" + color.name().toUpperCase() + "]";
     str = str.replaceAll(name, color.toString());
   }
   return str;
 }
Ejemplo n.º 3
0
 @Test
 public void testRemoveColor() {
   List<String[]> items = new LinkedList<String[]>();
   for (String[] item :
       new String[][] {
         {"", ""},
         {" ", " "},
         {"&", "&"},
         {"&&", "&&"},
         {"o3rg7cbo'!§)=?%V823rg7c", "o3rg7cbo'!§)=?%V823rg7c"},
       }) {
     items.add(item);
   }
   String[][] generic =
       new String[][] {
         {"&/&/&/", ""},
         {"&/&/", ""},
         {" &/&/ ", "  "},
         {" &/&/", " "},
         {"&/", ""},
         {"&/ ", " "},
         {" &/", " "},
         {"123&/123", "123123"},
       };
   for (ChatColor color : ChatColor.values()) {
     char c = color.getChar();
     for (String[] pattern : generic) {
       items.add(new String[] {pattern[0].replace('/', c), pattern[1]});
     }
   }
   int i = 0;
   for (String[] item : items) {
     String input = item[0];
     String expectedOutput = item[1];
     String detail = "no details.";
     String output = "(ERROR)";
     try {
       output = ColorUtil.removeColors(input);
     } catch (Throwable t) {
       detail = t.getClass().getSimpleName() + ": " + t.getMessage();
     }
     if (!expectedOutput.equals(output)) {
       fail(
           "Wrong output at index "
               + i
               + " for input '"
               + input
               + "', expected '"
               + expectedOutput
               + "', but got instead: '"
               + output
               + "', details: "
               + detail);
     }
     i++;
   }
 }
Ejemplo n.º 4
0
 @Override
 @Localized("CRAZYCHATS.COMMAND.COLORHELP $ColorHelp$")
 public void command(final CommandSender sender, final String[] args) throws CrazyException {
   final StringBuilder builder = new StringBuilder();
   for (final ChatColor color : ChatColor.values())
     builder
         .append(color.toString())
         .append(Character.toUpperCase(color.getChar()))
         .append(color.getChar())
         .append(ChatColor.RESET);
   plugin.sendLocaleMessage("COMMAND.COLORHELP", sender, builder.toString());
 }
  public static String replaceColorCodes(String message) {
    ChatColor arr$[] = ChatColor.values();
    int len$ = arr$.length;
    for (int i$ = 0; i$ < len$; i$++) {
      ChatColor color = arr$[i$];
      message =
          message.replaceAll(
              String.format("&%c", new Object[] {Character.valueOf(color.getChar())}),
              color.toString());
    }

    return message;
  }
Ejemplo n.º 6
0
  private void attachColorLabels(PopupScreen popup) {

    int y = 5;

    for (ChatColor color : ChatColor.values()) {

      GenericLabel label = new GenericLabel();
      label.setText(
          color
              + color.name().replace('_', ' ').toLowerCase()
              + ChatColor.RESET
              + ": &"
              + color.getChar());
      label.setHeight(25).setWidth(80).setX(300).setY(y);
      popup.attachWidget(plugin, label);
      y += 10;
    }
  }
Ejemplo n.º 7
0
  // an algorithm to sort the players into their teams, there will be <playersPerTeam> in every team
  // , returting false in case of any problem.
  public boolean sortPlayers() {
    // 12 Teams limit
    if (this.teams > 12) return false;
    for (int i = 0; i < this.teams; i++) {
      Team team = new Team(ChatColor.values()[i], teamsNames[i], i);
      for (int q = 0; q < this.playersPerTeam; q++) {
        int tries = 0;
        while (tries < 50) {
          Random random = new Random();
          int x = random.nextInt(players.length);
          Player player = players[x];
          tries++;
          if (alreadyInTeam(player)) break;
          tries = 50;
          team.addPlayer(player);
          log.put(player.getName(), teamsNames[i]);
          player.sendMessage(Strings.TEAM_JOIN_MESSAGE);
        }
      }
    }

    return true;
  }
Ejemplo n.º 8
0
  static {
    ImmutableBiMap.Builder<ChatColor, String> builder = ImmutableBiMap.builder();
    for (ChatColor style : ChatColor.values()) {
      if (!style.isFormat()) {
        continue;
      }

      String styleName;
      switch (style) {
        case MAGIC:
          styleName = "obfuscated";
          break;
        case UNDERLINE:
          styleName = "underlined";
          break;
        default:
          styleName = style.name().toLowerCase();
          break;
      }

      builder.put(style, styleName);
    }
    stylesToNames = builder.build();
  }
Ejemplo n.º 9
0
 /**
  * Replace colors of a message.
  *
  * @param text the text
  * @return the string
  */
 public static String replaceColors(String text) {
   for (final ChatColor c : ChatColor.values())
     text = text.replace("&" + c.getChar(), c.toString());
   return text;
 }
 /**
  * This replace �0-�f with ChatColor.
  *
  * @param s the given String
  * @return string with ChatColor.
  */
 private String replaceColor(String s) {
   for (ChatColor c : ChatColor.values()) {
     s = s.replaceAll("�" + c.getChar(), "" + ChatColor.getByChar(c.getChar()));
   }
   return s;
 }
 private List<ChatColor> generateNewChatColors() {
   List<ChatColor> colors = new ArrayList<>();
   colors.addAll(Arrays.asList(ChatColor.values()));
   return colors;
 }
 private ChatColor fromList(List<ChatColor> chatColorList) {
   if (chatColorList.isEmpty()) {
     chatColorList.addAll(Arrays.asList(ChatColor.values()));
   }
   return chatColorList.remove(BubbleNetwork.getRandom().nextInt(chatColorList.size()));
 }
Ejemplo n.º 13
0
/**
 * A meta-class to handle all logging and input-related console improvements. Portions are heavily
 * based on CraftBukkit.
 */
public final class ConsoleManager {

  private static final String CONSOLE_DATE = "HH:mm:ss";
  private static final String FILE_DATE = "yyyy/MM/dd HH:mm:ss";
  private static final Logger logger = Logger.getLogger("");

  private final GlowServer server;
  private final Map<ChatColor, String> replacements = new EnumMap<>(ChatColor.class);
  private final ChatColor[] colors = ChatColor.values();

  private ConsoleReader reader;
  private ConsoleCommandSender sender;

  private boolean running = true;
  private boolean jLine = false;

  public ConsoleManager(GlowServer server) {
    this.server = server;

    // install Ansi code handler, which makes colors work on Windows
    AnsiConsole.systemInstall();

    for (Handler h : logger.getHandlers()) {
      logger.removeHandler(h);
    }

    // add log handler which writes to console
    logger.addHandler(new FancyConsoleHandler());

    // reader must be initialized before standard streams are changed
    try {
      reader = new ConsoleReader();
    } catch (IOException ex) {
      logger.log(Level.SEVERE, "Exception initializing console reader", ex);
    }
    reader.addCompleter(new CommandCompleter());

    // set system output streams
    System.setOut(new PrintStream(new LoggerOutputStream(Level.INFO), true));
    System.setErr(new PrintStream(new LoggerOutputStream(Level.WARNING), true));

    // set up colorization replacements
    replacements.put(
        ChatColor.BLACK,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString());
    replacements.put(
        ChatColor.DARK_BLUE,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString());
    replacements.put(
        ChatColor.DARK_GREEN,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString());
    replacements.put(
        ChatColor.DARK_AQUA,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString());
    replacements.put(
        ChatColor.DARK_RED,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString());
    replacements.put(
        ChatColor.DARK_PURPLE,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString());
    replacements.put(
        ChatColor.GOLD,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString());
    replacements.put(
        ChatColor.GRAY,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString());
    replacements.put(
        ChatColor.DARK_GRAY,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString());
    replacements.put(
        ChatColor.BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString());
    replacements.put(
        ChatColor.GREEN,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString());
    replacements.put(
        ChatColor.AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString());
    replacements.put(
        ChatColor.RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).bold().toString());
    replacements.put(
        ChatColor.LIGHT_PURPLE,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString());
    replacements.put(
        ChatColor.YELLOW,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString());
    replacements.put(
        ChatColor.WHITE,
        Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString());
    replacements.put(ChatColor.MAGIC, Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString());
    replacements.put(ChatColor.BOLD, Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString());
    replacements.put(
        ChatColor.STRIKETHROUGH, Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString());
    replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString());
    replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Ansi.Attribute.ITALIC).toString());
    replacements.put(ChatColor.RESET, Ansi.ansi().a(Ansi.Attribute.RESET).toString());
  }

  public ConsoleCommandSender getSender() {
    return sender;
  }

  public void startConsole(boolean jLine) {
    this.jLine = jLine;

    sender = new ColoredCommandSender();
    Thread thread = new ConsoleCommandThread();
    thread.setName("ConsoleCommandThread");
    thread.setDaemon(true);
    thread.start();
  }

  public void startFile(String logfile) {
    File parent = new File(logfile).getParentFile();
    if (!parent.isDirectory() && !parent.mkdirs()) {
      logger.warning("Could not create log folder: " + parent);
    }
    Handler fileHandler = new RotatingFileHandler(logfile);
    fileHandler.setFormatter(new DateOutputFormatter(FILE_DATE, false));
    logger.addHandler(fileHandler);
  }

  public void stop() {
    running = false;
    for (Handler handler : logger.getHandlers()) {
      handler.flush();
      handler.close();
    }
  }

  private String colorize(String string) {
    if (string.indexOf(ChatColor.COLOR_CHAR) < 0) {
      return string; // no colors in the message
    } else if (!jLine || !reader.getTerminal().isAnsiSupported()) {
      return ChatColor.stripColor(string); // color not supported
    } else {
      // colorize or strip all colors
      for (ChatColor color : colors) {
        if (replacements.containsKey(color)) {
          string = string.replaceAll("(?i)" + color.toString(), replacements.get(color));
        } else {
          string = string.replaceAll("(?i)" + color.toString(), "");
        }
      }
      return string + Ansi.ansi().reset().toString();
    }
  }

  private class CommandCompleter implements Completer {
    public int complete(final String buffer, int cursor, List<CharSequence> candidates) {
      try {
        List<String> completions =
            server
                .getScheduler()
                .syncIfNeeded(
                    new Callable<List<String>>() {
                      public List<String> call() throws Exception {
                        return server.getCommandMap().tabComplete(sender, buffer);
                      }
                    });
        if (completions == null) {
          return cursor; // no completions
        }
        candidates.addAll(completions);

        // location to position the cursor at (before autofilling takes place)
        return buffer.lastIndexOf(' ') + 1;
      } catch (Throwable t) {
        logger.log(Level.WARNING, "Error while tab completing", t);
        return cursor;
      }
    }
  }

  private class ConsoleCommandThread extends Thread {
    @Override
    public void run() {
      String command = "";
      while (running) {
        try {
          if (jLine) {
            command = reader.readLine(">", null);
          } else {
            command = reader.readLine();
          }

          if (command == null || command.trim().length() == 0) continue;

          server.getScheduler().runTask(null, new CommandTask(command.trim()));
        } catch (CommandException ex) {
          logger.log(Level.WARNING, "Exception while executing command: " + command, ex);
        } catch (Exception ex) {
          logger.log(Level.SEVERE, "Error while reading commands", ex);
        }
      }
    }
  }

  private class CommandTask implements Runnable {
    private final String command;

    public CommandTask(String command) {
      this.command = command;
    }

    public void run() {
      server.dispatchCommand(sender, EventFactory.onServerCommand(sender, command).getCommand());
    }
  }

  private class ColoredCommandSender implements ConsoleCommandSender {
    private final PermissibleBase perm = new PermissibleBase(this);

    ////////////////////////////////////////////////////////////////////////
    // CommandSender

    public String getName() {
      return "CONSOLE";
    }

    public void sendMessage(String text) {
      server.getLogger().info(text);
    }

    public void sendMessage(String[] strings) {
      for (String line : strings) {
        sendMessage(line);
      }
    }

    public GlowServer getServer() {
      return server;
    }

    public boolean isOp() {
      return true;
    }

    public void setOp(boolean value) {
      throw new UnsupportedOperationException("Cannot change operator status of server console");
    }

    ////////////////////////////////////////////////////////////////////////
    // Permissible

    public boolean isPermissionSet(String name) {
      return perm.isPermissionSet(name);
    }

    public boolean isPermissionSet(Permission perm) {
      return this.perm.isPermissionSet(perm);
    }

    public boolean hasPermission(String name) {
      return perm.hasPermission(name);
    }

    public boolean hasPermission(Permission perm) {
      return this.perm.hasPermission(perm);
    }

    public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
      return perm.addAttachment(plugin, name, value);
    }

    public PermissionAttachment addAttachment(Plugin plugin) {
      return perm.addAttachment(plugin);
    }

    public PermissionAttachment addAttachment(
        Plugin plugin, String name, boolean value, int ticks) {
      return perm.addAttachment(plugin, name, value, ticks);
    }

    public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
      return perm.addAttachment(plugin, ticks);
    }

    public void removeAttachment(PermissionAttachment attachment) {
      perm.removeAttachment(attachment);
    }

    public void recalculatePermissions() {
      perm.recalculatePermissions();
    }

    public Set<PermissionAttachmentInfo> getEffectivePermissions() {
      return perm.getEffectivePermissions();
    }

    ////////////////////////////////////////////////////////////////////////
    // Conversable

    @Override
    public boolean isConversing() {
      return false;
    }

    @Override
    public void acceptConversationInput(String input) {}

    @Override
    public boolean beginConversation(Conversation conversation) {
      return false;
    }

    @Override
    public void abandonConversation(Conversation conversation) {}

    @Override
    public void abandonConversation(
        Conversation conversation, ConversationAbandonedEvent details) {}

    @Override
    public void sendRawMessage(String message) {}
  }

  private static class LoggerOutputStream extends ByteArrayOutputStream {
    private final String separator = System.getProperty("line.separator");
    private final Level level;

    public LoggerOutputStream(Level level) {
      super();
      this.level = level;
    }

    @Override
    public synchronized void flush() throws IOException {
      super.flush();
      String record = this.toString();
      super.reset();

      if (record.length() > 0 && !record.equals(separator)) {
        logger.logp(level, "LoggerOutputStream", "log" + level, record);
      }
    }
  }

  private class FancyConsoleHandler extends ConsoleHandler {
    public FancyConsoleHandler() {
      setFormatter(new DateOutputFormatter(CONSOLE_DATE, true));
      setOutputStream(System.out);
    }

    @Override
    public synchronized void flush() {
      try {
        if (jLine) {
          reader.print(ConsoleReader.RESET_LINE + "");
          reader.flush();
          super.flush();
          try {
            reader.drawLine();
          } catch (Throwable ex) {
            reader.getCursorBuffer().clear();
          }
          reader.flush();
        } else {
          super.flush();
        }
      } catch (IOException ex) {
        logger.log(Level.SEVERE, "I/O exception flushing console output", ex);
      }
    }
  }

  private static class RotatingFileHandler extends StreamHandler {
    private final SimpleDateFormat dateFormat;
    private final String template;
    private final boolean rotate;
    private String filename;

    public RotatingFileHandler(String template) {
      this.template = template;
      rotate = template.contains("%D");
      dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      filename = calculateFilename();
      updateOutput();
    }

    private void updateOutput() {
      try {
        setOutputStream(new FileOutputStream(filename, true));
      } catch (IOException ex) {
        logger.log(Level.SEVERE, "Unable to open " + filename + " for writing", ex);
      }
    }

    private void checkRotate() {
      if (rotate) {
        String newFilename = calculateFilename();
        if (!filename.equals(newFilename)) {
          filename = newFilename;
          // note that the console handler doesn't see this message
          super.publish(new LogRecord(Level.INFO, "Log rotating to: " + filename));
          updateOutput();
        }
      }
    }

    private String calculateFilename() {
      return template.replace("%D", dateFormat.format(new Date()));
    }

    @Override
    public synchronized void publish(LogRecord record) {
      if (!isLoggable(record)) {
        return;
      }
      checkRotate();
      super.publish(record);
      super.flush();
    }

    @Override
    public synchronized void flush() {
      checkRotate();
      super.flush();
    }
  }

  private class DateOutputFormatter extends Formatter {
    private final SimpleDateFormat date;
    private final boolean color;

    public DateOutputFormatter(String pattern, boolean color) {
      this.date = new SimpleDateFormat(pattern);
      this.color = color;
    }

    @Override
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    public String format(LogRecord record) {
      StringBuilder builder = new StringBuilder();

      builder.append(date.format(record.getMillis()));
      builder.append(" [");
      builder.append(record.getLevel().getLocalizedName().toUpperCase());
      builder.append("] ");
      if (color) {
        builder.append(colorize(formatMessage(record)));
      } else {
        builder.append(formatMessage(record));
      }
      builder.append('\n');

      if (record.getThrown() != null) {
        StringWriter writer = new StringWriter();
        record.getThrown().printStackTrace(new PrintWriter(writer));
        builder.append(writer.toString());
      }

      return builder.toString();
    }
  }
}