Skip to content

Commit

Permalink
GH-579 Add repository for language. Remove user settings. (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi committed Jan 17, 2025
1 parent 4ce2e55 commit 8936fa3
Show file tree
Hide file tree
Showing 50 changed files with 409 additions and 312 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.eternalcode.core.feature.language;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

public class Language {

Expand All @@ -13,35 +14,37 @@ public class Language {
public static final Language DEFAULT = Language.fromLocale(Locale.ROOT);

private final String lang;
private final List<String> aliases;
private final Set<String> aliases;

public Language(String lang, List<String> aliases) {
this.lang = lang;
this.aliases = new ArrayList<>(aliases);
this.aliases = new LinkedHashSet<>(aliases);
}

public String getLang() {
return this.lang;
}

public List<String> getAliases() {
return Collections.unmodifiableList(this.aliases);
public Set<String> getAliases() {
return Collections.unmodifiableSet(this.aliases);
}

public boolean isEquals(Language other) {
if (this.lang.equals(other.lang)) {
return true;
}

for (String alias : this.aliases) {
if (alias.equals(other.lang)) {
return true;
}
if (this.lang.startsWith(other.lang) || other.lang.startsWith(this.lang)) {
return true;
}

for (String otherAlias : other.aliases) {
if (alias.equals(otherAlias)) {
return true;
}
if (this.aliases.contains(other.lang)) {
return true;
}

for (String otherAlias : other.aliases) {
if (this.aliases.contains(otherAlias)) {
return true;
}
}

Expand Down Expand Up @@ -71,7 +74,7 @@ public static Language fromLocale(Locale locale) {
}

public Locale toLocale() {
return new Locale(this.lang);
return Locale.of(this.lang);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.eternalcode.core.feature.language;

import java.util.UUID;

public interface LanguageProvider {

Language getDefaultLanguage(UUID player);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.eternalcode.core.feature.language;

import java.util.Locale;
import org.bukkit.entity.Player;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public interface LanguageService {

Locale getPlayerLanguage(Player player);
void setDefaultProvider(LanguageProvider defaultProvider);

LanguageProvider getDefaultProvider();

CompletableFuture<Language> getLanguage(UUID playerUniqueId);

Language getLanguageNow(UUID playerUniqueId);

CompletableFuture<Void> setLanguage(UUID playerUniqueId, Language language);

CompletableFuture<Void> setDefaultLanguage(UUID playerUniqueId);

void setPlayerLanguage(Player player, Locale locale);
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package com.eternalcode.core.bridge.litecommand.argument;

import com.eternalcode.core.feature.language.Language;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.Viewer;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public abstract class AbstractViewerArgument<T> extends ArgumentResolver<CommandSender, T> {

protected final ViewerService viewerService;
protected final TranslationManager translationManager;

protected AbstractViewerArgument(ViewerService viewerService, TranslationManager translationManager) {
this.viewerService = viewerService;
protected AbstractViewerArgument(TranslationManager translationManager) {
this.translationManager = translationManager;
}

@Override
protected ParseResult<T> parse(Invocation<CommandSender> invocation, Argument<T> context, String argument) {
Viewer viewer = this.viewerService.any(invocation.sender());
Translation translation = this.translationManager.getMessages(viewer.getLanguage());
if (invocation.sender() instanceof Player player) {
Translation translation = this.translationManager.getMessages(player.getUniqueId());
return this.parse(invocation, argument, translation);
}

Translation translation = this.translationManager.getMessages(Language.DEFAULT);
return this.parse(invocation, argument, translation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -21,8 +20,8 @@
class EnchantmentArgument extends AbstractViewerArgument<Enchantment> {

@Inject
EnchantmentArgument(ViewerService viewerService, TranslationManager translationManager) {
super(viewerService, translationManager);
EnchantmentArgument(TranslationManager translationManager) {
super(translationManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -23,8 +22,8 @@ class GameModeArgument extends AbstractViewerArgument<GameMode> {
private final GameModeArgumentSettings gameModeArgumentSettings;

@Inject
GameModeArgument(ViewerService viewerService, TranslationManager translationManager, GameModeArgumentSettings gameModeArgumentSettings) {
super(viewerService, translationManager);
GameModeArgument(TranslationManager translationManager, GameModeArgumentSettings gameModeArgumentSettings) {
super(translationManager);
this.gameModeArgumentSettings = gameModeArgumentSettings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eternalcode.core.notice.NoticeTextType;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -19,8 +18,8 @@
class NoticeTypeArgument extends AbstractViewerArgument<NoticeTextType> {

@Inject
NoticeTypeArgument(ViewerService viewerService, TranslationManager translationManager) {
super(viewerService, translationManager);
NoticeTypeArgument(TranslationManager translationManager) {
super(translationManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -25,12 +24,11 @@ public class PlayerArgument extends AbstractViewerArgument<Player> {

@Inject
public PlayerArgument(
ViewerService viewerService,
TranslationManager translationManager,
Server server,
VanishService vanishService
) {
super(viewerService, translationManager);
super(translationManager);
this.server = server;
this.vanishService = vanishService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.user.User;
import com.eternalcode.core.user.UserManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -23,8 +22,8 @@ class UserArgument extends AbstractViewerArgument<User> {
private final UserManager userManager;

@Inject
UserArgument(ViewerService viewerService, TranslationManager translationManager, Server server, UserManager userManager) {
super(viewerService, translationManager);
UserArgument(TranslationManager translationManager, Server server, UserManager userManager) {
super(translationManager);
this.server = server;
this.userManager = userManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void onAfkSwitch(AfkSwitchEvent event) {
}

User user = this.userManager.getOrCreate(playerUUID, player.getName());
Translation translation = this.translationManager.getMessages(user);
Translation translation = this.translationManager.getMessages(user.getUniqueId());

Component component = this.miniMessage.deserialize(translation.afk().afkKickReason());
player.kickPlayer(legacySection().serialize(component));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.util.DurationUtil;
import com.eternalcode.core.viewer.ViewerService;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
Expand All @@ -18,12 +17,10 @@
class AftPlaceholderSetup {

private final TranslationManager translationManager;
private final ViewerService viewerService;

@Inject
AftPlaceholderSetup(TranslationManager translationManager, ViewerService viewerService) {
AftPlaceholderSetup(TranslationManager translationManager) {
this.translationManager = translationManager;
this.viewerService = viewerService;
}

@Subscribe(EternalInitializeEvent.class)
Expand All @@ -34,7 +31,7 @@ void setUpPlaceholders(PlaceholderRegistry placeholderRegistry, AfkService afkSe
placeholderRegistry.registerPlaceholder(PlaceholderReplacer.of(
"afk_formatted",
player -> {
Translation messages = this.translationManager.getMessages(this.viewerService.player(player.getUniqueId()));
Translation messages = this.translationManager.getMessages(player.getUniqueId());
return afkService.isAfk(player.getUniqueId()) ?
messages.afk().afkEnabledPlaceholder() : messages.afk().afkDisabledPlaceholder();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.eternalcode.annotations.scan.command.DescriptionDocs;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.feature.language.Language;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.user.UserManager;
import com.eternalcode.commons.adventure.AdventureUtil;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
Expand All @@ -24,26 +22,20 @@ class DisposalCommand {
private final NoticeService noticeService;
private final MiniMessage miniMessage;
private final TranslationManager translationManager;
private final UserManager userManager;
private final Server server;

@Inject
DisposalCommand(MiniMessage miniMessage, TranslationManager translationManager, UserManager userManager, Server server, NoticeService noticeService) {
DisposalCommand(MiniMessage miniMessage, TranslationManager translationManager, Server server, NoticeService noticeService) {
this.miniMessage = miniMessage;
this.translationManager = translationManager;
this.userManager = userManager;
this.server = server;
this.noticeService = noticeService;
}

@Execute
@DescriptionDocs(description = "Opens a disposal")
void execute(@Context Player player) {
Language language = this.userManager.getUser(player.getUniqueId())
.map(user -> user.getSettings().getLanguage())
.orElse(Language.DEFAULT);

Translation translation = this.translationManager.getMessages(language);
Translation translation = this.translationManager.getMessages(player.getUniqueId());
Component component = this.miniMessage.deserialize(translation.inventory().disposalTitle());
String serialize = AdventureUtil.SECTION_SERIALIZER.serialize(component);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -22,8 +21,8 @@ public class EnchantArgument extends AbstractViewerArgument<Integer> {
public static final String KEY = "enchant-level";

@Inject
public EnchantArgument(ViewerService viewerService, TranslationManager translationManager) {
super(viewerService, translationManager);
public EnchantArgument(TranslationManager translationManager) {
super(translationManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -22,8 +21,8 @@ class GiveArgument extends AbstractViewerArgument<Integer> {
static final String KEY = "item-amount";

@Inject
public GiveArgument(ViewerService viewerService, TranslationManager translationManager) {
super(viewerService, translationManager);
public GiveArgument(TranslationManager translationManager) {
super(translationManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.translation.Translation;
import com.eternalcode.core.translation.TranslationManager;
import com.eternalcode.core.viewer.ViewerService;
import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.invocation.Invocation;
Expand All @@ -22,8 +21,8 @@ class ItemLoreArgument extends AbstractViewerArgument<Integer> {
static final String KEY = "item-lore";

@Inject
public ItemLoreArgument(ViewerService viewerService, TranslationManager translationManager) {
super(viewerService, translationManager);
public ItemLoreArgument(TranslationManager translationManager) {
super(translationManager);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class ButcherArgument extends AbstractViewerArgument<Integer> {
static final String KEY = "chunks";
private final PluginConfiguration pluginConfiguration;
private final NoticeService noticeService;
private final ViewerService viewerService;

@Inject
ButcherArgument(ViewerService viewerService, TranslationManager translationManager, PluginConfiguration pluginConfiguration, NoticeService noticeService) {
super(viewerService, translationManager);
ButcherArgument(TranslationManager translationManager, PluginConfiguration pluginConfiguration, NoticeService noticeService, ViewerService viewerService) {
super(translationManager);
this.pluginConfiguration = pluginConfiguration;
this.noticeService = noticeService;
this.viewerService = viewerService;
}

@Override
Expand Down
Loading

0 comments on commit 8936fa3

Please sign in to comment.