Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Translate extends JavaPlugin{
- public ArrayList<LanguageInfo> playerLanguages = new ArrayList<>();
- @Override
- public void onEnable(){
- File file = new File(this.getDataFolder() + "\\languageInfo.yml");
- if(!file.exists()){
- createDirectory(file);
- }
- //this.getCommand("language").setExecutor(new LanguageCommand(file, playerLanguages));
- this.getServer().getPluginManager().registerEvents(new ChatListener(file, playerLanguages), this);
- }
- private void createDirectory(File file){
- try{
- file.getParentFile().mkdirs();
- file.createNewFile();
- FileOutputStream s = new FileOutputStream(file, false);
- s.close();
- }
- catch(Exception e){
- System.out.println("\033[0;33mCreation of file threw an error!\033[0m");
- }
- }
- }
- public class LanguageInfo {
- private File config;
- public Player player;
- public LanguageInfo(File config, Player player){
- this.config = config;
- this.player = player;
- }
- private String userLan;
- private List<String> allowed;
- public String getLanguage(){
- if(userLan == null){
- FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
- if(fileConfig != null){
- String lan = fileConfig.getString(player.getName() + ".Language");
- if(lan != null){
- userLan = lan;
- return lan;
- }
- else{
- fileConfig.set(player.getName() + ".Language", Language.ENGLISH);
- userLan = Language.ENGLISH;
- try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
- }
- }
- else{
- player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
- }
- return Language.ENGLISH;
- }
- return userLan;
- }
- public void setLanguage(String lan){
- if(Language.getInstance().getNameLanguage(lan) != null){
- userLan = lan;
- FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
- if(fileConfig != null){
- fileConfig.set(player.getName() + ".Language", lan);
- try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
- }
- else{
- player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
- }
- }
- else{
- player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + lan + " is not a valid language prefix code!");
- }
- }
- public List<String> getAllowedLanguages(){
- if(allowed == null){
- FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
- if(fileConfig != null){
- List<String> lans = fileConfig.getStringList(player.getName() + ".allowedLans");
- if(lans != null){
- allowed = lans;
- return lans;
- }
- }
- else{
- player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
- }
- }
- else{
- return allowed;
- }
- return new ArrayList<String>();
- }
- public void addAllowedLanguage(String lan){
- if(Language.getInstance().getNameLanguage(lan) != null){
- List<String> currentAllowedLans = getAllowedLanguages();
- if(!currentAllowedLans.contains(lan) && !getLanguage().equals(lan)){
- currentAllowedLans.add(lan);
- allowed = currentAllowedLans;
- FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
- if(fileConfig != null){
- fileConfig.set(player.getName() + ".allowedLans", currentAllowedLans);
- try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
- }
- else{
- player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
- }
- }
- else{
- player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "That language is already allowed!");
- }
- }
- else{
- player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + lan + " is not a valid language prefix code!");
- }
- }
- }
- public class ChatListener implements Listener {
- File config;
- ArrayList<LanguageInfo> lans;
- public ChatListener(File file, ArrayList<LanguageInfo> lans){
- this.config = file;
- this.lans = lans;
- }
- @EventHandler
- public void onChat(AsyncPlayerChatEvent e){
- if(!e.isCancelled()){
- for(Player player : e.getRecipients()){
- for(LanguageInfo lanInfo : lans){
- if(lanInfo.player.equals(player)){
- e.setMessage(transLateMessage(e.getMessage(), lanInfo.getLanguage(), lanInfo.getAllowedLanguages()));
- return;
- }
- }
- LanguageInfo lanInfo = new LanguageInfo(config, player);
- lans.add(lanInfo);
- }
- }
- }
- private String transLateMessage(String message, String userLan, List<String> allowed){
- String detectedLan = Translator.getInstance().detect(message);
- if(!detectedLan.equals(userLan) && !allowed.contains(detectedLan)){
- return (ChatColor.ITALIC + "(" + detectedLan + " -> " + userLan + ")" + ChatColor.RESET + Translator.getInstance().translate(message, detectedLan, userLan));
- }
- return message;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement