Advertisement
Dutchjelly

Untitled

Mar 4th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. public class Translate extends JavaPlugin{
  2.  
  3.     public ArrayList<LanguageInfo> playerLanguages = new ArrayList<>();
  4.     @Override
  5.     public void onEnable(){
  6.         File file = new File(this.getDataFolder() + "\\languageInfo.yml");
  7.         if(!file.exists()){
  8.             createDirectory(file);
  9.         }
  10.        
  11.         //this.getCommand("language").setExecutor(new LanguageCommand(file, playerLanguages));
  12.         this.getServer().getPluginManager().registerEvents(new ChatListener(file, playerLanguages), this);
  13.        
  14.        
  15.     }
  16.     private void createDirectory(File file){
  17.         try{
  18.             file.getParentFile().mkdirs();
  19.             file.createNewFile();
  20.             FileOutputStream s = new FileOutputStream(file, false);
  21.             s.close();
  22.         }
  23.         catch(Exception e){
  24.             System.out.println("\033[0;33mCreation of file threw an error!\033[0m");
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30. public class LanguageInfo {
  31.  
  32.     private File config;
  33.     public Player player;
  34.     public LanguageInfo(File config, Player player){
  35.         this.config = config;
  36.         this.player = player;
  37.     }
  38.     private String userLan;
  39.     private List<String> allowed;
  40.     public String getLanguage(){
  41.         if(userLan == null){
  42.             FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
  43.             if(fileConfig != null){
  44.                 String lan = fileConfig.getString(player.getName() + ".Language");
  45.                 if(lan != null){
  46.                     userLan = lan;
  47.                     return lan;
  48.                 }
  49.                 else{
  50.                     fileConfig.set(player.getName() + ".Language", Language.ENGLISH);
  51.                     userLan = Language.ENGLISH;
  52.                     try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
  53.                 }
  54.             }
  55.             else{
  56.                 player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
  57.             }
  58.             return Language.ENGLISH;
  59.         }
  60.         return userLan;
  61.     }
  62.     public void setLanguage(String lan){
  63.         if(Language.getInstance().getNameLanguage(lan) != null){
  64.             userLan = lan;
  65.             FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
  66.             if(fileConfig != null){
  67.                 fileConfig.set(player.getName() + ".Language", lan);
  68.                 try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
  69.             }
  70.             else{
  71.                 player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
  72.             }
  73.         }
  74.         else{
  75.             player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + lan + " is not a valid language prefix code!");
  76.         }
  77.     }
  78.     public List<String> getAllowedLanguages(){
  79.         if(allowed == null){
  80.             FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
  81.             if(fileConfig != null){
  82.                 List<String> lans = fileConfig.getStringList(player.getName() + ".allowedLans");
  83.                 if(lans != null){
  84.                     allowed = lans;
  85.                     return lans;
  86.                 }
  87.             }
  88.             else{
  89.                 player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
  90.             }
  91.         }
  92.         else{
  93.             return allowed;
  94.         }
  95.         return new ArrayList<String>();
  96.     }
  97.     public void addAllowedLanguage(String lan){
  98.         if(Language.getInstance().getNameLanguage(lan) != null){
  99.             List<String> currentAllowedLans = getAllowedLanguages();
  100.             if(!currentAllowedLans.contains(lan) && !getLanguage().equals(lan)){
  101.                 currentAllowedLans.add(lan);
  102.                 allowed = currentAllowedLans;
  103.                 FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(config);
  104.                 if(fileConfig != null){
  105.                     fileConfig.set(player.getName() + ".allowedLans", currentAllowedLans);
  106.                     try {fileConfig.save(config);} catch (IOException e) {e.printStackTrace();}
  107.                 }
  108.                 else{
  109.                     player.sendMessage(ChatColor.RED + "" + ChatColor.UNDERLINE + "FileConfiguration returned null!");
  110.                 }
  111.             }
  112.             else{
  113.                 player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "That language is already allowed!");
  114.             }
  115.         }
  116.         else{
  117.             player.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + lan + " is not a valid language prefix code!");
  118.         }
  119.     }
  120. }
  121. public class ChatListener implements Listener {
  122.  
  123.     File config;
  124.     ArrayList<LanguageInfo> lans;
  125.     public ChatListener(File file, ArrayList<LanguageInfo> lans){
  126.         this.config = file;
  127.         this.lans = lans;
  128.     }
  129.     @EventHandler
  130.     public void onChat(AsyncPlayerChatEvent e){
  131.         if(!e.isCancelled()){
  132.             for(Player player : e.getRecipients()){
  133.                 for(LanguageInfo lanInfo : lans){
  134.                     if(lanInfo.player.equals(player)){
  135.                         e.setMessage(transLateMessage(e.getMessage(), lanInfo.getLanguage(), lanInfo.getAllowedLanguages()));
  136.                         return;
  137.                     }
  138.                 }
  139.                 LanguageInfo lanInfo = new LanguageInfo(config, player);
  140.                 lans.add(lanInfo);
  141.             }
  142.         }
  143.     }
  144.     private String transLateMessage(String message, String userLan, List<String> allowed){
  145.         String detectedLan = Translator.getInstance().detect(message);
  146.         if(!detectedLan.equals(userLan) && !allowed.contains(detectedLan)){
  147.             return (ChatColor.ITALIC + "(" + detectedLan + " -> " + userLan + ")" + ChatColor.RESET + Translator.getInstance().translate(message, detectedLan, userLan));
  148.         }
  149.         return message;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement