From 6e8a3e1626cfebd7cd26586cae516c481d864a96 Mon Sep 17 00:00:00 2001 From: Bobcat00 Date: Sat, 11 Jan 2020 01:05:06 +0100 Subject: [PATCH] Add support for list entries in sign templates. This commit adds support for listing arena players on arena signs by introducing the following _dynamic variables_: - `` the _nth_ live arena player - `` the _nth_ player in the lobby - `` the _nth_ ready player in the lobby - `` the _nth_ player in the lobby who hasn't readied up Each variable points to a list of players (sorted by name), and the `n` is the index into that list (1-indexed). This means that putting the variable `` in a template will result in the name of the first player in the list of lobby players who haven't readied up, sorted by player name. Implements #592 --- .../MobArena/signs/RendersTemplate.java | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java b/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java index 48f7188..0ff040f 100644 --- a/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java +++ b/src/main/java/com/garbagemule/MobArena/signs/RendersTemplate.java @@ -2,11 +2,25 @@ package com.garbagemule.MobArena.signs; import static java.lang.String.valueOf; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + import com.garbagemule.MobArena.framework.Arena; import org.bukkit.ChatColor; +import org.bukkit.entity.Player; class RendersTemplate { + // Regex Pattern for player list variables. + // Changes to the list types must also be made in getPlayerList. + // group(1) is the list type as a String. + // group(2) is player index+1 as a String. + private final Pattern playerListPattern = Pattern.compile("<(arena|lobby|ready|notready)-([1-9][0-9]?)>"); + String[] render(Template template, Arena arena) { String[] lines = getTemplateByState(template, arena); @@ -46,7 +60,8 @@ class RendersTemplate { } private String running(String line, Arena arena) { - return line + String result = replacePlayerListEntry(line, arena); + return result .replace("", valueOf(arena.getPlayerCount())) .replace("", valueOf(arena.getPlayersInArena().size())) .replace("", valueOf(arena.getPlayerCount() - arena.getPlayersInArena().size())) @@ -57,7 +72,8 @@ class RendersTemplate { } private String joining(String line, Arena arena) { - return line + String result = replacePlayerListEntry(line, arena); + return result .replace("", valueOf(arena.getPlayersInLobby().size())) .replace("", valueOf(arena.getPlayersInLobby().size())) .replace("", "-") @@ -66,6 +82,50 @@ class RendersTemplate { .replace("", valueOf(arena.getReadyPlayersInLobby().size())); } + private String replacePlayerListEntry(String line, Arena arena) { + Matcher matcher = playerListPattern.matcher(line); + if (!matcher.find()) { + return line; + } + List list = getNameList(matcher.group(1), arena); + int index = Integer.parseInt(matcher.group(2)) - 1; + + if (index < list.size()) { + String value = list.get(index); + return matcher.replaceFirst(value); + } else { + return matcher.replaceFirst(""); + } + } + + private List getNameList(String name, Arena arena) { + return getPlayerList(name, arena) + .stream() + .map(Player::getName) + .sorted() + .collect(Collectors.toList()); + } + + private Collection getPlayerList(String name, Arena arena) { + switch (name) { + case "arena": { + return arena.getPlayersInArena(); + } + case "lobby": { + return arena.getPlayersInLobby(); + } + case "ready": { + return arena.getReadyPlayersInLobby(); + } + case "notready": { + return arena.getNonreadyPlayers(); + } + default: { + return Collections.emptyList(); + } + } + } + private String truncate(String rendered) { if (rendered.length() <= 15) { return rendered;