org.bukkit.util.ChatPaginator Java Examples

The following examples show how to use org.bukkit.util.ChatPaginator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndexHelpTopic.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
public String getFullText(CommandSender sender) {
    StringBuilder sb = new StringBuilder();

    if (preamble != null) {
        sb.append(buildPreamble(sender));
        sb.append("\n");
    }

    for (HelpTopic topic : allTopics) {
        if (topic.canSee(sender)) {
            String lineStr = buildIndexLine(sender, topic).replace("\n", ". ");
            if (sender instanceof Player && lineStr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) {
                sb.append(lineStr.substring(0, ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - 3));
                sb.append("...");
            } else {
                sb.append(lineStr);
            }
            sb.append("\n");
        }
    }
    return sb.toString();
}
 
Example #2
Source File: HelpCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    sender.sendMessage(ChatColor.AQUA + plugin.getName() + "-Help");

    int maxWidth = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH;
    if (!(sender instanceof Player)) {
        maxWidth = Integer.MAX_VALUE;
    }

    for (Entry<String, Map<String, Object>> entry : plugin.getDescription().getCommands().entrySet()) {
        String commandKey = entry.getKey();
        Map<String, Object> value = entry.getValue();

        String description = ' ' + value.getOrDefault("description", "No description").toString();
        String usage = ((String) value.getOrDefault("usage", '/' + commandKey)).replace("<command>", commandKey);

        TextComponent component = createCommandHelp(usage, description, maxWidth);
        LagCommand.send(sender, component);
    }

    return true;
}
 
Example #3
Source File: HelpCommand.java    From LagMonitor with MIT License 6 votes vote down vote up
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    sender.sendMessage(ChatColor.AQUA + plugin.getName() + "-Help");

    int maxWidth = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH;
    if (!(sender instanceof Player)) {
        maxWidth = Integer.MAX_VALUE;
    }

    for (Entry<String, Map<String, Object>> entry : plugin.getDescription().getCommands().entrySet()) {
        String commandKey = entry.getKey();
        Map<String, Object> value = entry.getValue();

        String description = ' ' + value.getOrDefault("description", "No description").toString();
        String usage = ((String) value.getOrDefault("usage", '/' + commandKey)).replace("<command>", commandKey);

        TextComponent component = createCommandHelp(usage, description, maxWidth);
        LagCommand.send(sender, component);
    }

    return true;
}
 
Example #4
Source File: ImageMessage.java    From AnnihilationPro with MIT License 5 votes vote down vote up
public ImageMessage appendCenteredText(String... text) {
    for (int y = 0; y < lines.length; y++) {
        if (text.length > y) {
            int len = ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH - lines[y].length();
            lines[y] = lines[y] + center(text[y], len);
        } else {
            return this;
        }
    }
    return this;
}
 
Example #5
Source File: HelpCommand.java    From LagMonitor with MIT License 5 votes vote down vote up
private TextComponent createCommandHelp(String usage, String description, int maxWidth) {
    TextComponent usageComponent = new TextComponent(usage);
    usageComponent.setColor(ChatColor.DARK_AQUA);

    TextComponent descriptionComponent = new TextComponent(description);
    descriptionComponent.setColor(ChatColor.GOLD);
    int totalLen = usage.length() + description.length();
    if (totalLen > maxWidth) {
        int newDescLength = maxWidth - usage.length() - 3 - 1;
        if (newDescLength < 0) {
            newDescLength = 0;
        }

        String shortDesc = description.substring(0, newDescLength) + "...";
        descriptionComponent.setText(shortDesc);

        ComponentBuilder hoverBuilder = new ComponentBuilder("");

        String[] separated = ChatPaginator.wordWrap(description, HOVER_MAX_LENGTH);
        for (String line : separated) {
            hoverBuilder.append(line + '\n');
            hoverBuilder.color(ChatColor.GOLD);
        }

        descriptionComponent.setHoverEvent(new HoverEvent(Action.SHOW_TEXT, hoverBuilder.create()));
    } else {
        descriptionComponent.setText(description);
    }

    usageComponent.addExtra(descriptionComponent);
    return usageComponent;
}
 
Example #6
Source File: HelpCommand.java    From LagMonitor with MIT License 5 votes vote down vote up
private TextComponent createCommandHelp(String usage, String description, int maxWidth) {
    TextComponent usageComponent = new TextComponent(usage);
    usageComponent.setColor(ChatColor.DARK_AQUA);

    TextComponent descriptionComponent = new TextComponent(description);
    descriptionComponent.setColor(ChatColor.GOLD);
    int totalLen = usage.length() + description.length();
    if (totalLen > maxWidth) {
        int newDescLength = maxWidth - usage.length() - 3 - 1;
        if (newDescLength < 0) {
            newDescLength = 0;
        }

        String shortDesc = description.substring(0, newDescLength) + "...";
        descriptionComponent.setText(shortDesc);

        ComponentBuilder hoverBuilder = new ComponentBuilder("");

        String[] separated = ChatPaginator.wordWrap(description, HOVER_MAX_LENGTH);
        for (String line : separated) {
            hoverBuilder.append(line + '\n');
            hoverBuilder.color(ChatColor.GOLD);
        }

        descriptionComponent.setHoverEvent(new HoverEvent(Action.SHOW_TEXT, hoverBuilder.create()));
    } else {
        descriptionComponent.setText(description);
    }

    usageComponent.addExtra(descriptionComponent);
    return usageComponent;
}
 
Example #7
Source File: HelpCommand.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    if (!testPermission(sender)) return true;

    String command;
    int pageNumber;
    int pageHeight;
    int pageWidth;

    if (args.length == 0) {
        command = "";
        pageNumber = 1;
    } else if (NumberUtils.isDigits(args[args.length - 1])) {
        command = StringUtils.join(ArrayUtils.subarray(args, 0, args.length - 1), " ");
        try {
            pageNumber = NumberUtils.createInteger(args[args.length - 1]);
        } catch (NumberFormatException exception) {
            pageNumber = 1;
        }
        if (pageNumber <= 0) {
            pageNumber = 1;
        }
    } else {
        command = StringUtils.join(args, " ");
        pageNumber = 1;
    }

    if (sender instanceof ConsoleCommandSender) {
        pageHeight = ChatPaginator.UNBOUNDED_PAGE_HEIGHT;
        pageWidth = ChatPaginator.UNBOUNDED_PAGE_WIDTH;
    } else {
        pageHeight = ChatPaginator.CLOSED_CHAT_PAGE_HEIGHT - 1;
        pageWidth = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH;
    }

    HelpMap helpMap = Bukkit.getServer().getHelpMap();
    HelpTopic topic = helpMap.getHelpTopic(command);

    if (topic == null) {
        topic = helpMap.getHelpTopic("/" + command);
    }

    if (topic == null) {
        topic = findPossibleMatches(command);
    }

    if (topic == null || !topic.canSee(sender)) {
        sender.sendMessage(ChatColor.RED + "No help for " + command);
        return true;
    }

    ChatPaginator.ChatPage page = ChatPaginator.paginate(topic.getFullText(sender), pageNumber, pageWidth, pageHeight);

    StringBuilder header = new StringBuilder();
    header.append(ChatColor.YELLOW);
    header.append("--------- ");
    header.append(ChatColor.WHITE);
    header.append("Help: ");
    header.append(topic.getName());
    header.append(" ");
    if (page.getTotalPages() > 1) {
        header.append("(");
        header.append(page.getPageNumber());
        header.append("/");
        header.append(page.getTotalPages());
        header.append(") ");
    }
    header.append(ChatColor.YELLOW);
    for (int i = header.length(); i < ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH; i++) {
        header.append("-");
    }
    sender.sendMessage(header.toString());

    sender.sendMessage(page.getLines());

    return true;
}
 
Example #8
Source File: ListGamesCommand.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean execute(CommandSender sender, ArrayList<String> args) {
  if (!sender.hasPermission("bw." + this.getPermission())) {
    return false;
  }

  String paginate;
  int page = 1;
  ArrayList<Game> showedGames = new ArrayList<Game>();

  if (args != null) {
    if (args.size() == 0 || args.size() > 1) {
      paginate = "1";
    } else {
      paginate = args.get(0);
      if (paginate.isEmpty()) {
        paginate = "1";
      }

      if (!Utils.isNumber(paginate)) {
        paginate = "1";
      }
    }
  } else {
    paginate = "1";
  }

  page = Integer.parseInt(paginate);
  StringBuilder sb = new StringBuilder();
  sender.sendMessage(ChatColor.GREEN + "---------- Bedwars Games ----------");

  List<Game> games = BedwarsRel.getInstance().getGameManager().getGames();
  for (Game game : games) {
    GameCheckCode code = game.checkGame();
    if (code != GameCheckCode.OK && !sender.hasPermission("bw.setup")) {
      continue;
    }

    showedGames.add(game);
    int players = 0;
    if (game.getState() == GameState.RUNNING) {
      players = game.getCurrentPlayerAmount();
    } else {
      players = game.getPlayers().size();
    }

    sb.append(ChatColor.YELLOW
        + ((code != GameCheckCode.OK) ? ChatColor.RED + game.getName() + ChatColor.YELLOW
        : game.getName())
        + " - " + game.getRegion().getName() + " - "
        + BedwarsRel._l(sender, "sign.gamestate." + game.getState().toString().toLowerCase())
        + ChatColor.YELLOW
        + " - " + BedwarsRel._l(sender, "sign.players") + ": " + ChatColor.WHITE + "["
        + ChatColor.YELLOW
        + players + ChatColor.WHITE + "/" + ChatColor.YELLOW + game.getMaxPlayers()
        + ChatColor.WHITE + "]\n");
  }

  if (showedGames.size() == 0) {
    sb.append(ChatColor.RED + BedwarsRel._l(sender, "errors.nogames"));
  }

  ChatPage chatPage = ChatPaginator.paginate(sb.toString(), page);
  for (String line : chatPage.getLines()) {
    sender.sendMessage(line);
  }
  sender.sendMessage(ChatColor.GREEN + "---------- "
      + BedwarsRel._l(sender, "default.pages",
      ImmutableMap.of("current", String.valueOf(chatPage.getPageNumber()), "max",
          String.valueOf(chatPage.getTotalPages())))
      + " ----------");

  return true;
}