Skip to content

Commit

Permalink
custom language handler support
Browse files Browse the repository at this point in the history
  • Loading branch information
tim03we committed Dec 16, 2024
1 parent 654e4ee commit bb97d60
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 75 deletions.
9 changes: 7 additions & 2 deletions src/main/java/ovis/futureplots/FuturePlots.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import ovis.futureplots.components.bstats.Metrics;
import ovis.futureplots.components.provider.Provider;
import ovis.futureplots.components.util.*;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.provider.LanguageProvider;
import ovis.futureplots.listener.plot.*;
import ovis.futureplots.generator.PlotGenerator;
import ovis.futureplots.generator.PlotStage;
Expand All @@ -42,7 +44,6 @@
import ovis.futureplots.manager.PlayerNameFunction;
import ovis.futureplots.manager.PlotManager;
import ovis.futureplots.components.util.async.TaskExecutor;
import ovis.futureplots.components.util.language.Language;

import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -101,6 +102,9 @@ public class FuturePlots extends PluginBase {

private static FuturePlots instance;

@Getter
private static LanguageProvider languageProvider;


public static FuturePlots getInstance() {
return instance;
Expand Down Expand Up @@ -184,7 +188,8 @@ public void onEnable() {
registerListener();
server.getPluginManager().registerEvents(new PlotLevelRegistrationListener(this), this);

Language.init();
languageProvider = LanguageManager.init();
languageProvider.init();
checkVersion();
registerCommands();

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ovis/futureplots/commands/SubCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import cn.nukkit.utils.Config;
import lombok.Getter;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.components.util.language.Language;

import javax.annotation.Nullable;
import java.util.*;
Expand Down Expand Up @@ -139,9 +139,9 @@ public boolean hasPermission(Player player) {

protected String translate(CommandSender sender, String key, Object... replacements) {
if(sender instanceof Player) {
return new Language(((Player) sender).getLoginChainData().getLanguageCode()).message(key, replacements);
return new LanguageManager(((Player) sender).getLoginChainData().getLanguageCode()).message(key, replacements);
}
return new Language(FuturePlots.getSettings().getLanguage()).message(key, replacements);
return new LanguageManager(FuturePlots.getSettings().getDefaultLanguage()).message(key, replacements);
}

protected String translate(CommandSender sender, TranslationKey key, Object... replacements) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/ovis/futureplots/commands/sub/FlagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import cn.nukkit.Player;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockState;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandEnum;
import cn.nukkit.command.data.CommandParamType;
Expand All @@ -38,7 +37,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @modified Tim tim03we, Ovis Development (2024)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import cn.nukkit.command.CommandSender;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.commands.SubCommand;
import ovis.futureplots.components.util.language.Language;
import ovis.futureplots.components.util.language.TranslationKey;

/**
Expand All @@ -42,7 +41,7 @@ public ReloadCommand(FuturePlots plugin) {
@Override
public void execute(CommandSender sender, String command, String[] args) {
Player player = (Player) sender;
Language.init();
FuturePlots.getLanguageProvider().init();

player.sendMessage(this.translate(player, TranslationKey.RELOAD_SUCCESS));
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ovis/futureplots/components/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
@Getter
public class Settings {

private String language;
private boolean defaultLangEnabled;
private String defaultLanguage;
private String provider;
private boolean debugEnabled;
private boolean metricsEnabled;
Expand All @@ -45,7 +46,8 @@ public class Settings {
public void init() {
Config config = FuturePlots.getInstance().getConfig();

language = config.getString("default_language");
defaultLangEnabled = !config.getBoolean("enable_player_language");
defaultLanguage = config.getString("default_language");
provider = config.getString("provider");
debugEnabled = config.getBoolean("debug");
metricsEnabled = config.getBoolean("metrics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
*
*/

package ovis.futureplots.components.util.language;
package ovis.futureplots.components.util.language.handler;

import cn.nukkit.utils.Config;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.provider.LanguageProvider;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @author Tim tim03we, Ovis Development (2024)
* @author Tim tim03we, Ovis Development (2024)
*/
public class Language {
public class MainHandler implements LanguageProvider {

private static String version = "2.0.0";
public static HashMap<String, HashMap<String, String>> messages = new HashMap<>();
private static HashMap<String, HashMap<String, String>> messages = new HashMap<>();
private static final String[] official_langs = new String[]{"en_US", "de_DE"};

private static String[] official_langs = new String[]{"en_US", "de_DE"};

public static void init() {
@Override
public void init() {
messages.clear();
new File(FuturePlots.getInstance().getDataFolder() + "/lang/").mkdirs();
for (String languageKey : official_langs) {
Expand Down Expand Up @@ -72,35 +73,9 @@ public static void init() {
FuturePlots.getInstance().getLogger().info("All languages cached...");
}

private final String locale;

public Language(String locale) {
this.locale = locale;
}


/*
public static String translate(boolean prefix, String key, Object... replacements) {
String message;
if(prefix) {
message = get(key);
} else {
message = getNoPrefix(key);
}
if(replacements == null) {
return message;
}
int i = 1;
for (Object replacement : replacements) {
message = message.replace("%" + i, String.valueOf(replacement));
i++;
}
return message;
}
*/

public String message(String key, Object... replacements) {
String message = get(key);
@Override
public String message(String locale, String key, Object... replacements) {
String message = messages.getOrDefault(locale, new HashMap<>()).getOrDefault(key, "null");
if (replacements == null)
return message;
int i = 1;
Expand All @@ -110,16 +85,5 @@ public String message(String key, Object... replacements) {
}
return message;
}

public String message(TranslationKey key, Object... replacements) {
return message(key.getKey(), replacements);
}

private String getValidLocale() {
return Language.messages.get(this.locale) != null ? this.locale : "en_US";
}

private String get(String key) {
return (String)((HashMap) Language.messages.get(getValidLocale())).getOrDefault(key, "null");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024 tim03we, Ovis Development
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ovis.futureplots.components.util.language.manager;

import lombok.Getter;
import lombok.Setter;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.provider.LanguageProvider;
import ovis.futureplots.components.util.language.handler.MainHandler;
import ovis.futureplots.components.util.language.TranslationKey;

/**
* @author Tim tim03we, Ovis Development (2024)
*/
public class LanguageManager {

@Getter
@Setter
private static LanguageProvider handler;

public static LanguageProvider init() {
if(handler == null) {
handler = new MainHandler();
}
return handler;
}

private String locale;

public LanguageManager(String locale) {
this.locale = locale;
}

public String message(TranslationKey translationKey, Object... replacements) {
return message(translationKey.getKey(), replacements);
}

public String message(String key, Object... replacements) {
if(FuturePlots.getSettings().isDefaultLangEnabled()) {
this.locale = FuturePlots.getSettings().getDefaultLanguage();
}
return handler.message(this.locale, key, replacements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 tim03we, Ovis Development
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ovis.futureplots.components.util.language.provider;

/**
* @author Tim tim03we, Ovis Development (2024)
*/
public interface LanguageProvider {

void init();

String message(String locale, String key, Object... replacements);
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import cn.nukkit.registry.Registries;
import lombok.RequiredArgsConstructor;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.Language;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.components.util.PlotLevelRegistration;

Expand All @@ -44,7 +44,7 @@ public class PlotLevelRegistrationListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onChat(PlayerChatEvent event) {
final Player player = event.getPlayer();
Language language = new Language(player.getLoginChainData().getLanguageCode());
LanguageManager language = new LanguageManager(player.getLoginChainData().getLanguageCode());
if (this.plugin.getLevelRegistrationMap().containsKey(player)) {
event.setCancelled(true);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ovis/futureplots/listener/plot/BlockBreak.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import cn.nukkit.event.block.BlockBreakEvent;
import lombok.RequiredArgsConstructor;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.manager.PlotManager;
import ovis.futureplots.components.util.Plot;
import ovis.futureplots.components.util.Utils;
import ovis.futureplots.components.util.language.Language;

/**
* @modified Tim tim03we, Ovis Development (2024)
Expand All @@ -55,7 +55,7 @@ public void on(BlockBreakEvent event) {

if(plot.getHomePosition() != null && plot.getHomePosition().distance(event.getBlock()) < 5) {
event.setCancelled(true);
Language language = new Language(player.getLoginChainData().getLanguageCode());
LanguageManager language = new LanguageManager(player.getLoginChainData().getLanguageCode());
player.sendMessage(language.message(TranslationKey.TOO_CLOSE_TO_HOME));
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ovis/futureplots/listener/plot/BlockPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cn.nukkit.event.block.BlockPlaceEvent;
import lombok.RequiredArgsConstructor;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.Language;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.manager.PlotManager;
import ovis.futureplots.components.util.Plot;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void on(BlockPlaceEvent event) {

if(plot.getHomePosition() != null && plot.getHomePosition().distance(event.getBlock()) < 5) {
event.setCancelled(true);
Language language = new Language(player.getLoginChainData().getLanguageCode());
LanguageManager language = new LanguageManager(player.getLoginChainData().getLanguageCode());
player.sendMessage(language.message(TranslationKey.TOO_CLOSE_TO_HOME));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cn.nukkit.event.player.PlayerBucketEmptyEvent;
import lombok.RequiredArgsConstructor;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.Language;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.manager.PlotManager;
import ovis.futureplots.components.util.Plot;
Expand Down Expand Up @@ -54,7 +54,7 @@ public void on(PlayerBucketEmptyEvent event) {

if(plot.getHomePosition() != null && plot.getHomePosition().distance(event.getBlockClicked()) < 5) {
event.setCancelled(true);
Language language = new Language(player.getLoginChainData().getLanguageCode());
LanguageManager language = new LanguageManager(player.getLoginChainData().getLanguageCode());
player.sendMessage(language.message(TranslationKey.TOO_CLOSE_TO_HOME));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cn.nukkit.event.player.PlayerBucketFillEvent;
import lombok.RequiredArgsConstructor;
import ovis.futureplots.FuturePlots;
import ovis.futureplots.components.util.language.Language;
import ovis.futureplots.components.util.language.manager.LanguageManager;
import ovis.futureplots.components.util.language.TranslationKey;
import ovis.futureplots.manager.PlotManager;
import ovis.futureplots.components.util.Plot;
Expand Down Expand Up @@ -54,7 +54,7 @@ public void on(PlayerBucketFillEvent event) {

if(plot.getHomePosition() != null && plot.getHomePosition().distance(event.getBlockClicked()) < 5) {
event.setCancelled(true);
Language language = new Language(player.getLoginChainData().getLanguageCode());
LanguageManager language = new LanguageManager(player.getLoginChainData().getLanguageCode());
player.sendMessage(language.message(TranslationKey.TOO_CLOSE_TO_HOME));
}
} else {
Expand Down
Loading

0 comments on commit bb97d60

Please sign in to comment.