diff --git a/multification-core/src/com/eternalcode/multification/notice/Notice.java b/multification-core/src/com/eternalcode/multification/notice/Notice.java index bbaa65c..e1c09a0 100644 --- a/multification-core/src/com/eternalcode/multification/notice/Notice.java +++ b/multification-core/src/com/eternalcode/multification/notice/Notice.java @@ -26,14 +26,17 @@ public class Notice { private final Map, NoticePart> parts = new LinkedHashMap<>(); + private List> cachedParts; protected Notice(Map, NoticePart> parts) { this.parts.putAll(parts); } public List> parts() { - return this.parts.values().stream() - .toList(); + if (cachedParts == null) { + cachedParts = List.copyOf(this.parts.values()); + } + return cachedParts; } public static Notice of(NoticeKey key, T content) { @@ -261,7 +264,8 @@ public B bossBar(BossBar.Color color, BossBar.Overlay overlay, Duration duration } public B bossBar(BossBar.Color color, Duration duration, String message) { - return this.withPart(NoticeKey.BOSS_BAR, new BossBarContent(color, Optional.empty(), duration, OptionalDouble.empty(), message)); + return this.withPart(NoticeKey.BOSS_BAR, + new BossBarContent(color, Optional.empty(), duration, OptionalDouble.empty(), message)); } } diff --git a/multification-core/src/com/eternalcode/multification/notice/NoticeBroadcastImpl.java b/multification-core/src/com/eternalcode/multification/notice/NoticeBroadcastImpl.java index 881a660..670a3f9 100644 --- a/multification-core/src/com/eternalcode/multification/notice/NoticeBroadcastImpl.java +++ b/multification-core/src/com/eternalcode/multification/notice/NoticeBroadcastImpl.java @@ -19,7 +19,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; @@ -78,13 +77,10 @@ public B player(UUID player) { @Override @CheckReturnValue public B players(Iterable players) { - Set viewers = new HashSet<>(); - for (UUID player : players) { - viewers.add(this.viewerProvider.player(player)); + this.viewers.add(this.viewerProvider.player(player)); } - this.viewers.addAll(viewers); return this.getThis(); } @@ -258,11 +254,10 @@ private void sendTranslatedMessages(LanguageViewersIndex viewersIndex, T continue; } + Set languageViewers = viewersIndex.getViewers(language); TranslatedFormatter translatedFormatter = this.prepareFormatterForLanguage(language); for (Notice notice : notificationsForLang) { - Set languageViewers = viewersIndex.getViewers(language); - for (VIEWER viewer : languageViewers) { Audience audience = audienceConverter.convert(viewer); @@ -298,23 +293,42 @@ private TranslatedNoticesIndex prepareTranslatedNotices(Set languages) { }); } + private final Map formatterCache = new HashMap<>(); + protected TranslatedFormatter prepareFormatterForLanguage(Locale language) { + TranslatedFormatter cached = formatterCache.get(language); + if (cached != null && !cached.isDirty()) { + return cached; + } + TRANSLATION translation = this.translationProvider.provide(language); Formatter translatedFormatter = new Formatter(); for (Map.Entry> entry : this.placeholders.entrySet()) { - translatedFormatter.register(entry.getKey(), () -> entry.getValue().extract(translation)); + String value = entry.getValue().extract(translation); + translatedFormatter.register(entry.getKey(), value); } - return new TranslatedFormatter(translatedFormatter); + TranslatedFormatter formatter = new TranslatedFormatter(translatedFormatter); + formatterCache.put(language, formatter); + return formatter; } protected class TranslatedFormatter { protected final Formatter translatedPlaceholders; + private final int expectedFormatterCount; + private final int expectedPlaceholderCount; protected TranslatedFormatter(Formatter translatedPlaceholders) { this.translatedPlaceholders = translatedPlaceholders; + this.expectedFormatterCount = NoticeBroadcastImpl.this.formatters.size(); + this.expectedPlaceholderCount = NoticeBroadcastImpl.this.placeholders.size(); + } + + protected boolean isDirty() { + return NoticeBroadcastImpl.this.formatters.size() != expectedFormatterCount + || NoticeBroadcastImpl.this.placeholders.size() != expectedPlaceholderCount; } public String format(String text, VIEWER viewer) { diff --git a/multification-core/src/com/eternalcode/multification/notice/resolver/NoticeResolverRegistry.java b/multification-core/src/com/eternalcode/multification/notice/resolver/NoticeResolverRegistry.java index cfb9ac0..dcbb901 100644 --- a/multification-core/src/com/eternalcode/multification/notice/resolver/NoticeResolverRegistry.java +++ b/multification-core/src/com/eternalcode/multification/notice/resolver/NoticeResolverRegistry.java @@ -45,8 +45,7 @@ public Optional> deserialize(String key, NoticeSerdes if (deserializeResult != null) { return Optional.of(deserializeResult); } - } - catch (Throwable exception) { + } catch (Exception exception) { illegalArgumentException.addSuppressed(exception); } }