diff --git a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java index f4e677a..98b9f27 100644 --- a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java +++ b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/BridgeRaceChallengeEvent.java @@ -5,12 +5,19 @@ import de.fanta.challenge.score.ChallengePlayer; import de.fanta.challenge.score.Scorable; import de.fanta.challenge.score.ScoreManager; +import de.fanta.challenge.scoreboard.ChallengePlayerScoreboard; +import de.fanta.challenge.scoreboard.ChallengeScoreboard; import de.fanta.challenge.utils.ChatSkullAPI.ChatSkull; import de.fanta.challenge.utils.Config; import de.fanta.challengeutils.Color; import de.iani.cubesideutils.bukkit.world.EmptyChunkGenerator; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; @@ -40,6 +47,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Random; import java.util.UUID; @@ -57,6 +65,9 @@ private final Map> currentBarrier = new HashMap<>(); private static final Material barrierBlock = Material.GLASS; + private static ChallengeScoreboard challengeScoreboard; + private static final HashMap positionColors = new HashMap<>(); + @EventHandler public void onActivation(TimerChangedEvent event) { if (Objects.equals(Config.getString("event.type"), "bridgerace")) { @@ -76,6 +87,7 @@ plugin.getScoreManager().setScore(new ChallengePlayer(pp.getUniqueId()), 1); } updateBossBar(); + updatePlayerScoreBoard(); startItemSpawnTask(); setRunning(true); @@ -111,23 +123,25 @@ @EventHandler public void onPlayerJoin(PlayerJoinEvent e) { + if (isLoaded()) { + challengeScoreboard.addPlayer(e.getPlayer()); + createPlayerLocation(e.getPlayer()); + teleportPlayer(e.getPlayer()); + } if (isRunning()) { if (plugin.getVanish().isVanish(e.getPlayer())) { return; } - ChallengePlayer challengePlayer = new ChallengePlayer(e.getPlayer().getUniqueId()); - plugin.getScoreManager().join(challengePlayer); - if (plugin.getScoreManager().getScore(challengePlayer) >= 1) { + ScoreManager scoreManager = plugin.getScoreManager(); + ChallengePlayer cp = new ChallengePlayer(e.getPlayer().getUniqueId()); + scoreManager.join(cp); + if (scoreManager.getScore(cp) >= 1) { return; } - plugin.getScoreManager().setScore(challengePlayer, 1); + plugin.getScoreManager().updateScore(cp, 1); + updatePlayerScoreBoard(); updateBossBar(); } - - if (isLoaded()) { - createPlayerLocation(e.getPlayer()); - teleportPlayer(e.getPlayer()); - } } @EventHandler @@ -209,9 +223,9 @@ int score = scoreManager.getScore(new ChallengePlayer(placer.getUniqueId())); if (e.getBlock().getX() >= score) { scoreManager.setScore(new ChallengePlayer(placer.getUniqueId()), e.getBlock().getX()); + updateBossBar(); + updatePlayerScoreBoard(); } - - updateBossBar(); } } @@ -230,9 +244,17 @@ } }); + positionColors.put(1, TextColor.fromHexString("#09FF00")); + positionColors.put(2, TextColor.fromHexString("#61FF5C")); + positionColors.put(3, TextColor.fromHexString("#93FF8F")); + + challengeScoreboard = new ChallengeScoreboard(); + challengeScoreboard.setSidebarTitle(Component.text("Bridge Race", Color.GREEN)); + plugin.getServer().getScheduler().runTaskLater(plugin, () -> plugin.getVanish().getPlayerListWithoutVanishPlayers().forEach(pp -> { createPlayerLocation(pp); teleportPlayer(pp); + challengeScoreboard.addPlayer(pp); }), 1L); load = true; @@ -317,6 +339,96 @@ } } + private void updatePlayerScoreBoard() { + ArrayList globalLines = new ArrayList<>(); + ScoreManager scoreManager = plugin.getScoreManager(); + int globalLine = 0; + for (Scorable scorable : scoreManager.getByPositionRange(1, 10)) { + int score = scoreManager.getScore(scorable); + int pos = scoreManager.getPosition(scorable); + Style style; + if (pos <= 3) { + style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE), TextDecoration.BOLD); + } else { + style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE)); + } + globalLines.add(getScorebordLine(String.valueOf(pos), scorable.getName(), style, String.valueOf(score))); + globalLine++; + if (globalLine >= 10) { + break; + } + } + + globalLines.add(Component.empty()); + + for (Player player : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { + ChallengePlayerScoreboard playerScoreboard = challengeScoreboard.getPlayerScoreboard(player); + if (playerScoreboard != null) { + ArrayList lines = new ArrayList<>(globalLines); + List playerLines = getAdditionalScoreboardInfo(player); + lines.addAll(playerLines); + + if (lines.size() < 15) { + playerScoreboard.limitSidebarLines(lines.size()); + } + + int line = 0; + for (Component lineContent : lines) { + playerScoreboard.setSidebarLine(line, lineContent); + line++; + } + } + } + } + + private List getAdditionalScoreboardInfo(Player player) { + List components = new ArrayList<>(); + ScoreManager scoreManager = plugin.getScoreManager(); + ChallengePlayer cp = new ChallengePlayer(player.getUniqueId()); + int ownPosition = scoreManager.getPosition(cp); + int beforePosition = ownPosition - 1; + int afterPosition = ownPosition + 1; + + Optional beforeOptionalScore = Optional.empty(); + if (beforePosition > 0) { + beforeOptionalScore = scoreManager.getByPositon(beforePosition).stream().findFirst(); + } + + Scorable beforeScore = null; + if (beforeOptionalScore.isPresent()) { + beforeScore = beforeOptionalScore.get(); + } + + if (beforeScore != null) { + int pos = scoreManager.getPosition(beforeScore); + components.add(getScorebordLine(String.valueOf(pos), beforeScore.getName(), Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), String.valueOf(scoreManager.getScore(beforeScore)))); + } else { + components.add(getScorebordLine("?", "niemand", Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), "???")); + } + + int ownPos = scoreManager.getPosition(cp); + components.add(getScorebordLine(String.valueOf(ownPos), cp.getName(), Style.style(NamedTextColor.WHITE, TextDecoration.BOLD), String.valueOf(scoreManager.getScore(cp)))); + + Optional afterOptionalScore = scoreManager.getByPositon(afterPosition).stream().findFirst(); + Scorable afterScore = null; + if (afterOptionalScore.isPresent()) { + afterScore = afterOptionalScore.get(); + } + + if (afterScore != null) { + int pos = scoreManager.getPosition(afterScore); + components.add(getScorebordLine(String.valueOf(pos), afterScore.getName(), Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), String.valueOf(scoreManager.getScore(afterScore)))); + } else { + components.add(getScorebordLine("?", "niemand", Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), "???")); + } + + return components; + } + + private Component getScorebordLine(String pos, String playerName, Style nameTextStyle, String score) { + return Component.text(pos + ". ").append(Component.text(playerName, nameTextStyle)).append(Component.text(" " + score, Color.BLUE)); + } + private void resetBarrier(Player p, World world, List locations) { for (Location location : locations) { p.sendBlockChange(location, world.getBlockAt(location).getBlockData()); diff --git a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/DeathrunChallengeEventMonth.java b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/DeathrunChallengeEventMonth.java index 9b37d77..025477d 100644 --- a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/DeathrunChallengeEventMonth.java +++ b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/DeathrunChallengeEventMonth.java @@ -10,9 +10,9 @@ import de.fanta.challenge.utils.Config; import de.fanta.challenge.utils.CoordsTargeter; import de.fanta.challenge.utils.CustomFontUtil; -import de.fanta.challenge.utils.MinecraftTextUtils; import de.fanta.challenge.utils.Statistics; import de.fanta.challengeutils.Color; +import de.iani.cubesideutils.FontUtil; import de.iani.cubesideutils.bukkit.items.ItemGroups; import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.text.Component; @@ -21,6 +21,7 @@ import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; @@ -353,7 +354,7 @@ positionColors.put(3, TextColor.fromHexString("#93FF8F")); challengeScoreboard = new ChallengeScoreboard(); - challengeScoreboard.setSidebarTitle(CustomFontUtil.deathRunLogo); + challengeScoreboard.setSidebarTitle(Component.text(CustomFontUtil.deathRunLogo)); Statistics statistics = plugin.getStatistics(); statistics.setAllTotaleScores(); @@ -484,78 +485,93 @@ } private void updatePlayerScoreBoard() { + ArrayList globalLines = new ArrayList<>(); ScoreManager scoreManager = plugin.getScoreManager(); - plugin.getServer().getOnlinePlayers().forEach(player -> { + int globalLine = 0; + for (Scorable scorable : scoreManager.getByPositionRange(1, 10)) { + int score = scoreManager.getScore(scorable); + int pos = scoreManager.getPosition(scorable); + Style style; + if (pos <= 3) { + style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE), TextDecoration.BOLD); + } else { + style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE)); + } + globalLines.add(getScorebordLine(String.valueOf(pos), scorable.getName(), style, String.valueOf(score))); + globalLine++; + if (globalLine >= 10) { + break; + } + } + + globalLines.add(Component.empty()); + + for (Player player : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { ChallengePlayerScoreboard playerScoreboard = challengeScoreboard.getPlayerScoreboard(player); - int count = 0; - for (int i = 1; i < 5; i++) { - //TOP 5 - for (Scorable scorable : scoreManager.getByPositon(i)) { - if (count == 11) { - break; - } + if (playerScoreboard != null) { + ArrayList lines = new ArrayList<>(globalLines); + List playerLines = getAdditionalScoreboardInfo(player); + lines.addAll(playerLines); - if (scorable != null) { - int score = scoreManager.getScore(scorable); - if (score > 0) { - int pos = scoreManager.getPosition(scorable); - Style style; - if (pos <= 3) { - style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE), TextDecoration.BOLD); - } else { - style = Style.style(positionColors.getOrDefault(pos, NamedTextColor.WHITE)); - } - playerScoreboard.setSidebarLine(count, Component.text(pos + ". ").append(Component.text(scorable.getName() + " ", style)).append(Component.text(score, Color.BLUE))); - count++; - } + if (lines.size() < 15) { + playerScoreboard.limitSidebarLines(lines.size()); + } - } + int line = 0; + for (Component lineContent : lines) { + playerScoreboard.setSidebarLine(line, lineContent); + line++; } } + } + } - playerScoreboard.setSidebarLine(count, Component.empty()); - count++; - ChallengePlayer cp = new ChallengePlayer(player.getUniqueId()); - int ownPosition = scoreManager.getPosition(cp); - int beforePosition = ownPosition - 1; - int afterPosition = ownPosition + 1; + private List getAdditionalScoreboardInfo(Player player) { + List components = new ArrayList<>(); + ScoreManager scoreManager = plugin.getScoreManager(); + ChallengePlayer cp = new ChallengePlayer(player.getUniqueId()); + int ownPosition = scoreManager.getPosition(cp); + int beforePosition = ownPosition - 1; + int afterPosition = ownPosition + 1; - Optional beforeOptionalScore = Optional.empty(); - if (beforePosition > 0) { - beforeOptionalScore = scoreManager.getByPositon(beforePosition).stream().findFirst(); - } + Optional beforeOptionalScore = Optional.empty(); + if (beforePosition > 0) { + beforeOptionalScore = scoreManager.getByPositon(beforePosition).stream().findFirst(); + } - Optional afterOptionalScore = scoreManager.getByPositon(afterPosition).stream().findFirst(); + Scorable beforeScore = null; + if (beforeOptionalScore.isPresent()) { + beforeScore = beforeOptionalScore.get(); + } - Scorable beforeScore = null; - if (beforeOptionalScore.isPresent()) { - beforeScore = beforeOptionalScore.get(); - } + if (beforeScore != null) { + int pos = scoreManager.getPosition(beforeScore); + components.add(getScorebordLine(String.valueOf(pos), beforeScore.getName(), Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), String.valueOf(scoreManager.getScore(beforeScore)))); + } else { + components.add(getScorebordLine("?", "niemand", Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), "???")); + } - if (beforeScore != null) { - int pos = scoreManager.getPosition(beforeScore); - playerScoreboard.setSidebarLine(count, Component.text(pos + ". ").append(Component.text(beforeScore.getName() + " ", NamedTextColor.GRAY, TextDecoration.ITALIC)).append(Component.text(scoreManager.getScore(beforeScore), Color.BLUE))); - } else { - playerScoreboard.setSidebarLine(count, Component.text( "?. ").append(Component.text("niemand", NamedTextColor.GRAY, TextDecoration.ITALIC)).append(Component.text(" ???", Color.BLUE))); - } - count++; + int ownPos = scoreManager.getPosition(cp); + components.add(getScorebordLine(String.valueOf(ownPos), cp.getName(), Style.style(NamedTextColor.WHITE, TextDecoration.BOLD), String.valueOf(scoreManager.getScore(cp)))); - int ownPos = scoreManager.getPosition(cp); - playerScoreboard.setSidebarLine(count, Component.text(ownPos + ". ").append(Component.text(cp.getName() + " ", NamedTextColor.WHITE, TextDecoration.BOLD)).append(Component.text(scoreManager.getScore(cp), Color.BLUE))); - count++; + Optional afterOptionalScore = scoreManager.getByPositon(afterPosition).stream().findFirst(); + Scorable afterScore = null; + if (afterOptionalScore.isPresent()) { + afterScore = afterOptionalScore.get(); + } - Scorable afterScore = null; - if (afterOptionalScore.isPresent()) { - afterScore = afterOptionalScore.get(); - } + if (afterScore != null) { + int pos = scoreManager.getPosition(afterScore); + components.add(getScorebordLine(String.valueOf(pos), afterScore.getName(), Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), String.valueOf(scoreManager.getScore(afterScore)))); + } else { + components.add(getScorebordLine("?", "niemand", Style.style(NamedTextColor.DARK_GRAY, TextDecoration.ITALIC), "???")); + } - if (afterScore != null) { - int pos = scoreManager.getPosition(afterScore); - playerScoreboard.setSidebarLine(count, Component.text(pos + ". ").append(Component.text(afterScore.getName() + " ", NamedTextColor.GRAY, TextDecoration.ITALIC)).append(Component.text(scoreManager.getScore(afterScore), Color.BLUE))); - } else { - playerScoreboard.setSidebarLine(count, Component.text( "?. ").append(Component.text("niemand", NamedTextColor.GRAY, TextDecoration.ITALIC)).append(Component.text(" ???", Color.BLUE))); - } - }); + return components; + } + + private Component getScorebordLine(String pos, String playerName, Style nameTextStyle, String score) { + return Component.text(pos + ". ").append(Component.text(playerName, nameTextStyle)).append(Component.text(" " + score, Color.BLUE)); } private void updateBossBar() { @@ -597,7 +613,7 @@ // Erstelle den zentrierten Text Component spacesLeft = Component.text(" ".repeat(spacesOnLeft)); Component spacesRight = Component.text(" ".repeat(spacesOnRight)); - Component formattedText = Component.text("") + Component formattedText = Component.empty() .append(spacesLeft) .append(left1) .append(Component.text(" ")) @@ -609,7 +625,6 @@ .append(Component.text(" ")) .append(right2) .append(spacesRight); - return formattedText; } @@ -626,7 +641,7 @@ String text = textComponent.content(); int width = 0; for (char c : text.toCharArray()) { - width += MinecraftTextUtils.getCharWidth(c); + width += FontUtil.getCharWidth(c); } return width; } @@ -723,7 +738,7 @@ } return effects.stream() - .map(effect -> formatEffect(effect)) + .map(DeathrunChallengeEventMonth::formatEffect) .collect(Collectors.joining(", ")); } diff --git a/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java b/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java index 4e78a68..ea46ebb 100644 --- a/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java +++ b/Challenge/src/main/java/de/fanta/challenge/score/ScoreManager.java @@ -203,6 +203,14 @@ return positions.subList(start, end + 1).stream().map(ScoreData::getScorable).collect(Collectors.toSet()); } + public ArrayList getByPositionRange(int start, int end) { + ArrayList scorables = new ArrayList<>(); + for (int i = start; i <= end; i++) { + scorables.addAll(getByPositon(i)); + } + return scorables; + } + public void saveScores(@Nullable Player player) { saveScores(player, 0); } diff --git a/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengePlayerScoreboard.java b/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengePlayerScoreboard.java index ba51c72..b5bd72e 100644 --- a/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengePlayerScoreboard.java +++ b/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengePlayerScoreboard.java @@ -29,7 +29,7 @@ sidebarObjective = bukkitScoreboard.registerNewObjective("sidebar", "dummy", scoreboard.getSidebarTitle()); sidebarObjective.setDisplaySlot(DisplaySlot.SIDEBAR); - sidebarObjective.setDisplayName(scoreboard.getSidebarTitle()); + sidebarObjective.displayName(scoreboard.getSidebarTitle()); sidebarObjective.numberFormat(NumberFormat.blank()); Component[] lines = scoreboard.getSidebarLines(); @@ -58,8 +58,9 @@ } } - void resetScores(String entry) { - bukkitScoreboard.resetScores(entry); + public void resetScores(int line) { + Preconditions.checkArgument(line >= 0 && line < 15, "line >= 0 && line < 15"); + bukkitScoreboard.resetScores(getSidebarLineMarker(line)); } public void setSidebarLine(int line, Component component) { @@ -101,7 +102,7 @@ if (sidebarLines != null) { int lines = sidebarLines.length; for (int line = maxlines; line < lines; line++) { - String l = sidebarLines[line] + getSidebarLineMarker(line); + String l = getSidebarLineMarker(line); bukkitScoreboard.resetScores(l); } sidebarLines = maxlines == 0 ? null : Arrays.copyOf(sidebarLines, maxlines); @@ -112,7 +113,7 @@ if (sidebarLines != null) { int lines = sidebarLines.length; for (int line = 0; line < lines; line++) { - String l = sidebarLines[line] + getSidebarLineMarker(line); + String l = getSidebarLineMarker(line); bukkitScoreboard.resetScores(l); } sidebarLines = null; diff --git a/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengeScoreboard.java b/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengeScoreboard.java index 6362fa7..4d9e0ec 100644 --- a/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengeScoreboard.java +++ b/Challenge/src/main/java/de/fanta/challenge/scoreboard/ChallengeScoreboard.java @@ -1,6 +1,7 @@ package de.fanta.challenge.scoreboard; import com.google.common.base.Preconditions; +import de.fanta.challengeutils.Color; import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -10,12 +11,12 @@ import java.util.HashMap; public class ChallengeScoreboard { - private final static String DEFAULT_SIDEBAR_TITLE = ChatColor.BOLD.toString() + ChatColor.BLUE + "Event"; + private final static Component DEFAULT_SIDEBAR_TITLE = Component.text("Event", Color.BLUE); private final HashMap playerScoreboards; private final HashMap teams; - private String sidebarTitle; + private Component sidebarTitle; private Component[] sidebarLines; public ChallengeScoreboard() { @@ -50,6 +51,13 @@ return playerScoreboards.get(player); } + public void resetScores(int line) { + Preconditions.checkArgument(line >= 0 && line < 15, "line >= 0 && line < 15"); + for (ChallengePlayerScoreboard ps : getPlayerScoreboards()) { + ps.resetScores(line); + } + } + public void setSidebarLine(int line, Component s) { Preconditions.checkArgument(line >= 0 && line < 15, "line >= 0 && line < 15"); Preconditions.checkNotNull(s, "s may not be null"); @@ -83,18 +91,18 @@ sidebarTitle = DEFAULT_SIDEBAR_TITLE; for (ChallengePlayerScoreboard ps : getPlayerScoreboards()) { ps.clearSidebar(); - ps.getSidebarObjective().setDisplayName(sidebarTitle); + ps.getSidebarObjective().displayName(sidebarTitle); } } - public void setSidebarTitle(String sidebarTitle) { + public void setSidebarTitle(Component sidebarTitle) { this.sidebarTitle = sidebarTitle; for (ChallengePlayerScoreboard ps : getPlayerScoreboards()) { - ps.getSidebarObjective().setDisplayName(sidebarTitle); + ps.getSidebarObjective().displayName(sidebarTitle); } } - public String getSidebarTitle() { + public Component getSidebarTitle() { return sidebarTitle; } diff --git a/Challenge/src/main/java/de/fanta/challenge/utils/MinecraftTextUtils.java b/Challenge/src/main/java/de/fanta/challenge/utils/MinecraftTextUtils.java deleted file mode 100644 index 3d424ea..0000000 --- a/Challenge/src/main/java/de/fanta/challenge/utils/MinecraftTextUtils.java +++ /dev/null @@ -1,125 +0,0 @@ -package de.fanta.challenge.utils; - -import java.util.HashMap; -import java.util.Map; - -public class MinecraftTextUtils { - - private static final Map charWidths = new HashMap<>(); - - static { - charWidths.put('A', 5); - charWidths.put('B', 5); - charWidths.put('C', 5); - charWidths.put('D', 5); - charWidths.put('E', 5); - charWidths.put('F', 5); - charWidths.put('G', 5); - charWidths.put('H', 5); - charWidths.put('I', 3); - charWidths.put('J', 5); - charWidths.put('K', 5); - charWidths.put('L', 5); - charWidths.put('M', 5); - charWidths.put('N', 5); - charWidths.put('O', 5); - charWidths.put('P', 5); - charWidths.put('Q', 5); - charWidths.put('R', 5); - charWidths.put('S', 5); - charWidths.put('T', 5); - charWidths.put('U', 5); - charWidths.put('V', 5); - charWidths.put('W', 5); - charWidths.put('X', 5); - charWidths.put('Y', 5); - charWidths.put('Z', 5); - charWidths.put('a', 4); - charWidths.put('b', 4); - charWidths.put('c', 4); - charWidths.put('d', 4); - charWidths.put('e', 4); - charWidths.put('f', 3); - charWidths.put('g', 4); - charWidths.put('h', 4); - charWidths.put('i', 2); - charWidths.put('j', 3); - charWidths.put('k', 4); - charWidths.put('l', 2); - charWidths.put('m', 5); - charWidths.put('n', 4); - charWidths.put('o', 4); - charWidths.put('p', 4); - charWidths.put('q', 4); - charWidths.put('r', 3); - charWidths.put('s', 4); - charWidths.put('t', 3); - charWidths.put('u', 4); - charWidths.put('v', 4); - charWidths.put('w', 5); - charWidths.put('x', 4); - charWidths.put('y', 4); - charWidths.put('z', 4); - charWidths.put('0', 5); - charWidths.put('1', 3); - charWidths.put('2', 5); - charWidths.put('3', 5); - charWidths.put('4', 5); - charWidths.put('5', 5); - charWidths.put('6', 5); - charWidths.put('7', 5); - charWidths.put('8', 5); - charWidths.put('9', 5); - - charWidths.put(' ', 3); - charWidths.put('!', 2); - charWidths.put('"', 4); - charWidths.put('#', 5); - charWidths.put('$', 5); - charWidths.put('%', 5); - charWidths.put('&', 5); - charWidths.put('\'', 2); - charWidths.put('(', 3); - charWidths.put(')', 3); - charWidths.put('*', 5); - charWidths.put('+', 5); - charWidths.put(',', 2); - charWidths.put('-', 4); - charWidths.put('.', 2); - charWidths.put('/', 3); - charWidths.put(':', 2); - charWidths.put(';', 2); - charWidths.put('<', 5); - charWidths.put('=', 5); - charWidths.put('>', 5); - charWidths.put('?', 5); - charWidths.put('@', 5); - charWidths.put('[', 3); - charWidths.put('\\', 3); - charWidths.put(']', 3); - charWidths.put('^', 5); - charWidths.put('_', 5); - charWidths.put('`', 3); - charWidths.put('{', 4); - charWidths.put('|', 2); - charWidths.put('}', 4); - charWidths.put('~', 5); - - charWidths.put('ä', 4); - charWidths.put('ö', 4); - charWidths.put('ü', 4); - charWidths.put('ß', 5); - } - - public static int getCharWidth(char c) { - return charWidths.getOrDefault(c, 4); - } - - public static int getTextWidth(String text) { - int width = 0; - for (char c : text.toCharArray()) { - width += getCharWidth(c); - } - return width; - } -}