Skip to content

Commit

Permalink
Add new locale provider PLAYER_CHOICE allow player to set individuall…
Browse files Browse the repository at this point in the history
…y their language by using command
  • Loading branch information
TeksuSiK committed Sep 19, 2023
1 parent 411c1c0 commit 27ec220
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 40 deletions.
48 changes: 32 additions & 16 deletions src/main/java/pl/teksusik/customskins/CustomSkinsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.yaml.bukkit.YamlBukkitConfigurer;
import eu.okaeri.i18n.configs.LocaleConfigManager;
Expand All @@ -19,6 +21,7 @@
import pl.teksusik.customskins.configuration.MessageConfiguration;
import pl.teksusik.customskins.configuration.PluginConfiguration;
import pl.teksusik.customskins.i18n.BI18n;
import pl.teksusik.customskins.i18n.locale.PlayerChoiceLocaleProvider;
import pl.teksusik.customskins.i18n.locale.FixedLocaleProvider;
import pl.teksusik.customskins.i18n.locale.LocaleProviderType;
import pl.teksusik.customskins.i18n.locale.PlayerLocaleProvider;
Expand All @@ -41,17 +44,21 @@
public class CustomSkinsPlugin extends JavaPlugin implements Module {
private final File pluginConfigurationFile = new File(getDataFolder(), "config.yml");
private PluginConfiguration pluginConfiguration;

private final File langDirectory = new File(getDataFolder(), "lang");

private Storage skinStorage;
private SkinService skinService;

private Injector injector;

@Override
public void onEnable() {
Injector injector = Guice.createInjector(this);
this.injector = Guice.createInjector(this);

this.skinStorage = injector.getInstance(Storage.class);
this.skinService = injector.getInstance(SkinService.class);
this.registerLocaleProviders(this.injector.getInstance(BI18n.class));
this.skinStorage = this.injector.getInstance(Storage.class);
this.skinService = this.injector.getInstance(SkinService.class);

PaperCommandManager paperCommandManager = new PaperCommandManager(this);
paperCommandManager.registerCommand(injector.getInstance(SkinCommand.class));
Expand Down Expand Up @@ -80,18 +87,7 @@ public MessageConfiguration loadMessageConfiguration() {

private BI18n loadI18n() {
BI18n i18n = new BI18n();
Locale defaultLocale = Locale.forLanguageTag(this.pluginConfiguration.getLocale());
if (defaultLocale == null) {
defaultLocale = Locale.ENGLISH;
}

i18n.setDefaultLocale(defaultLocale);

if (this.pluginConfiguration.getLocaleProvider() == LocaleProviderType.PLAYER) {
i18n.registerLocaleProvider(new PlayerLocaleProvider(defaultLocale));
} else if (this.pluginConfiguration.getLocaleProvider() == LocaleProviderType.FIXED) {
i18n.registerLocaleProvider(new FixedLocaleProvider(defaultLocale));
}
i18n.setDefaultLocale(this.getDefaultLocale());

if (!this.langDirectory.exists()) {
this.langDirectory.mkdir();
Expand Down Expand Up @@ -127,6 +123,16 @@ private BI18n loadI18n() {
return i18n;
}

private void registerLocaleProviders(BI18n i18n) {
if (this.pluginConfiguration.getLocaleProvider() == LocaleProviderType.PLAYER_CHOICE) {
i18n.registerLocaleProvider(this.injector.getInstance(PlayerChoiceLocaleProvider.class));
} else if (this.pluginConfiguration.getLocaleProvider() == LocaleProviderType.PLAYER) {
i18n.registerLocaleProvider(this.injector.getInstance(PlayerLocaleProvider.class));
} else if (this.pluginConfiguration.getLocaleProvider() == LocaleProviderType.FIXED) {
i18n.registerLocaleProvider(this.injector.getInstance(FixedLocaleProvider.class));
}
}

private Storage loadStorage() {
final File sqliteFile = new File(getDataFolder(), this.pluginConfiguration.getSqliteFile());
if (this.pluginConfiguration.getStorageType().equals(StorageType.SQLITE)) {
Expand Down Expand Up @@ -171,12 +177,22 @@ public void configure(Binder binder) {
binder.bind(Logger.class).toInstance(this.getSLF4JLogger());
binder.bind(PluginConfiguration.class).toInstance(this.loadPluginConfiguration());
binder.bind(MessageConfiguration.class).toInstance(this.loadMessageConfiguration());
binder.bind(BI18n.class).toInstance(this.loadI18n());
binder.bind(Storage.class).toInstance(this.loadStorage());
binder.bind(Locale.class).annotatedWith(Names.named("defaultLocale")).toInstance(this.getDefaultLocale());
binder.bind(BI18n.class).toInstance(loadI18n());
binder.bind(BukkitAudiences.class).toInstance(BukkitAudiences.create(this));
binder.bind(MineskinClient.class).toInstance(new MineskinClient("CustomSkins"));
}

public Locale getDefaultLocale() {
Locale defaultLocale = Locale.forLanguageTag(this.pluginConfiguration.getLocale());
if (defaultLocale == null) {
defaultLocale = Locale.ENGLISH;
}

return defaultLocale;
}

public Storage getSkinStorage() {
return skinStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class MessageConfiguration extends LocaleConfig {
"\n<green>- <reset>/skins list <gold>- <reset>Shows off skin list" +
"\n<green>- <reset>/skins wear <name> <gold>- <reset>Dresses up skin from list" +
"\n<green>- <reset>/skins add <name> <URL> <Model> <gold>- <reset>Creates skin from URL" +
"\n<green>- <reset>/skins delete <name> <gold>- <reset>Removes skin from list";
"\n<green>- <reset>/skins delete <name> <gold>- <reset>Removes skin from list" +
"\n<green>- <reset>/skins locale <locale> <gold>- <reset>Set locale to specified. Use without any argument to see available locales";
@Comment("Skins available message")
private String skinsAvailable = "<green>Skins available: <reset>{skins}";
@Comment("Skin not exists message")
Expand All @@ -30,6 +31,12 @@ public class MessageConfiguration extends LocaleConfig {
private String badUsage = "<dark_red>Error: <red>Incorrect usage. Check correct usage: <reset>/skins";
@Comment("Upload skin message")
private String skinUploaded = "<green>You successfully uploaded <reset>{skin} <green>skin";
@Comment("Available locales message")
private String availableLocales = "<yellow>Available locales: <reset>{locales}";
@Comment("Invalid locale message")
private String invalidLocale = "<dark_red>Error: <red>Locale <reset>{locale} does not exists. Available locales: <reset>{locales}";
@Comment("Set locale message")
private String localeChanged = "<green>You successfully changed your locale to <reset>{locale}";

public String getHelpCommands() {
return helpCommands;
Expand Down Expand Up @@ -66,4 +73,16 @@ public String getBadUsage() {
public String getSkinUploaded() {
return skinUploaded;
}

public String getAvailableLocales() {
return availableLocales;
}

public String getInvalidLocale() {
return invalidLocale;
}

public String getLocaleChanged() {
return localeChanged;
}
}
12 changes: 12 additions & 0 deletions src/main/java/pl/teksusik/customskins/i18n/BI18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import eu.okaeri.placeholders.message.CompiledMessage;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;


public class BI18n extends CustomMEOCI18n<AdventureMessage> {
@Inject
private BukkitAudiences adventure;
Expand All @@ -20,4 +25,11 @@ public BukkitMessageDispatcher get(String key) {
public AdventureMessage assembleMessage(Placeholders placeholders, CompiledMessage compiled) {
return AdventureMessage.of(placeholders, compiled);
}

public List<String> getAvailableLocales() {
return this.configs.keySet()
.stream()
.map(Locale::toLanguageTag)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package pl.teksusik.customskins.i18n.locale;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import eu.okaeri.i18n.locale.LocaleProvider;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

public class FixedLocaleProvider implements LocaleProvider<Object> {
private final Locale locale;

public FixedLocaleProvider(Locale locale) {
this.locale = locale;
}
@Inject
@Named("defaultLocale")
private Locale locale;

@Override
public boolean supports(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package pl.teksusik.customskins.i18n.locale;

public enum LocaleProviderType {
PLAYER, FIXED;
PLAYER_CHOICE, PLAYER, FIXED;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pl.teksusik.customskins.i18n.locale;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import eu.okaeri.i18n.locale.LocaleProvider;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import pl.teksusik.customskins.storage.Storage;

import java.util.Locale;
import java.util.Optional;

public class PlayerChoiceLocaleProvider implements LocaleProvider<Player> {
@Inject
private Storage storage;
@Inject
@Named("defaultLocale")
private Locale fallbackLocale;

@Override
public boolean supports(Class<?> type) {
return Player.class.isAssignableFrom(type);
}

@Override
public @Nullable Locale getLocale(Player entity) {
Optional<String> preferableLocale = this.storage.findLocale(entity.getUniqueId());
if (preferableLocale.isEmpty()) {
return this.fallbackLocale;
}

Locale locale = Locale.forLanguageTag(preferableLocale.get().replace("_", "-"));
if (locale == null) {
return this.fallbackLocale;
}

return locale;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package pl.teksusik.customskins.i18n.locale;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import eu.okaeri.i18n.locale.LocaleProvider;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

public class PlayerLocaleProvider implements LocaleProvider<Player> {
private final Locale fallbackLocale;

public PlayerLocaleProvider(Locale fallbackLocale) {
this.fallbackLocale = fallbackLocale;
}
@Inject
@Named("defaultLocale")
private Locale fallbackLocale;

@Override
public boolean supports(Class<?> type) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/pl/teksusik/customskins/skin/SkinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pl.teksusik.customskins.storage.Storage;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@CommandAlias("skin|skins|customskins")
Expand Down Expand Up @@ -120,4 +121,31 @@ public void onDelete(Player player, String[] args) {
public void onClear(Player player) {
this.skinService.clearSkin(player);
}

@Subcommand("locale")
@Syntax("<locale>")
public void onLocale(Player player, String[] args) {
if (args.length != 1) {
String availableLocales = String.join(", ", this.i18n.getAvailableLocales());
this.i18n.get(this.messages.getAvailableLocales())
.with("locales", availableLocales)
.sendTo(player);
return;
}

Locale locale = Locale.forLanguageTag(args[0].replace("_", "-"));
if (locale == null || !(this.i18n.getConfigs().containsKey(locale))) {
String availableLocales = String.join(", ", this.i18n.getAvailableLocales());
this.i18n.get(this.messages.getInvalidLocale())
.with("locale", args[0])
.with("locales", availableLocales)
.sendTo(player);
return;
}

String newLocale = this.skinStorage.setLocale(player.getUniqueId(), locale.toLanguageTag());
this.i18n.get(this.messages.getLocaleChanged())
.with("locale", newLocale)
.sendTo(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class SkinService {
@Inject
private MineskinClient mineskinClient;


public void setSkin(Player player, CustomSkin customSkin) {
PlayerProfile profile = player.getPlayerProfile();
ProfileProperty textures = new ProfileProperty("textures", customSkin.getTexture(), customSkin.getSignature());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/pl/teksusik/customskins/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pl.teksusik.customskins.skin.CustomSkin;

import java.util.Collection;
import java.util.Locale;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -12,4 +13,7 @@ public interface Storage {
CustomSkin insertSkin(CustomSkin skin);
void deleteSkin(CustomSkin skin);
int countSkins();

Optional<String> findLocale(UUID owner);
String setLocale(UUID owner, String locale);
}
Loading

0 comments on commit 27ec220

Please sign in to comment.