AlanGomes

Untitled

Feb 28th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.12 KB | None | 0 0
  1. package com.brinedev.fixer;
  2.  
  3. import java.io.File;
  4. import java.text.Normalizer;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.ChatColor;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.EventHandler;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.player.AsyncPlayerChatEvent;
  20. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22.  
  23. public class Fixer extends JavaPlugin implements Listener {
  24.  
  25.     private File customYml = null;
  26.     private FileConfiguration customConfig = null;
  27.    
  28.     public void onEnable() {
  29.         saveDefaultConfig();
  30.         saveResource("corretor.yml", false);
  31.         customYml=new File(getDataFolder()+"/corretor.yml");
  32.         customConfig=YamlConfiguration.loadConfiguration(customYml);
  33.         Bukkit.getPluginManager().registerEvents(this, this);
  34.         Bukkit.getConsoleSender().sendMessage("§a[BrineFixer] Plugin criado por BrineDev, não edite este plugin!");
  35.     }
  36.    
  37.     public FileConfiguration getCorretor() {
  38.         return customConfig;
  39.     }
  40.    
  41.     public static String removerAcentos(String str) {
  42.         return Normalizer.normalize(str, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
  43.     }
  44.    
  45.     public boolean checa(String str1, String str2) {
  46.         String str12=removerAcentos(str1);
  47.         String str22=removerAcentos(str2);
  48.         boolean igual=str12.equalsIgnoreCase(str22);
  49.         if (igual==true)return true;
  50.         double porcentagem=StringSimilarity.similaridade(str12, str22);
  51.         double minimo=getConfig().getDouble("Similaridade", 75.0);
  52.         if (porcentagem >=minimo) {
  53.             return true;
  54.         } else {
  55.             return false;
  56.         }
  57.     }
  58.    
  59.     public char getFromUnicode(String unicode) {
  60.         if (unicode==null) return '*';
  61.         unicode=unicode.replace("+", "");
  62.         if (!unicode.startsWith("\\")) {
  63.             unicode="\\"+unicode;
  64.         }
  65.         try {
  66.         return (char) Integer.parseInt(unicode.substring(2), 16);
  67.         } catch (Exception e) {
  68.         return '*';
  69.         }
  70.     }
  71.    
  72.     public String emoticonizer(String message) {
  73.         if (!getConfig().getBoolean("Emoticons")) return message;
  74.         if (getConfig().getStringList("EmoticonsLista")==null) return message;
  75.         for (String s:getConfig().getStringList("EmoticonsLista")) {
  76.             String[] dados=s.split("=");
  77.             if (dados.length<2) continue;
  78.             message=message.replace(dados[0], getFromUnicode(dados[1])+"");
  79.         }
  80.         return message;
  81.     }
  82.    
  83.     public boolean checaDV(String mensagem) {
  84.         if (!getConfig().getBoolean("AntiDV")) return false;
  85.         if (getConfig().getBoolean("IngorarEspacos")) {
  86.             mensagem=mensagem.replace(" ", "");
  87.         }
  88.         for (String regex:getConfig().getStringList("Avancado.ReGex")) {
  89.             try {
  90.             Pattern replace = Pattern.compile(regex);
  91.             Matcher matcher = replace.matcher(mensagem);
  92.             if (matcher.matches()) {
  93.                 return true;
  94.             }
  95.             } catch (Exception e){}
  96.         } return false;
  97.     }
  98.    
  99.     public String marcar(String mensagem, Player p, String cmd) {
  100.         if (!getConfig().getBoolean("Marcar")) return mensagem;
  101.         String msgnova="";
  102.         for (String palavra:mensagem.split(" ")) {
  103.             if (palavra==null) continue;
  104.                 if (palavra.length()>1) {
  105.                     String nome=palavra;
  106.                     if (Bukkit.getPlayer(nome)!=null) {
  107.                         nome=Bukkit.getPlayer(nome).getName();
  108.                         p.sendMessage(mensagem);
  109.                         if (msgnova.equalsIgnoreCase("")) {
  110.                             msgnova="§b"+nome+getCor(cmd);
  111.                         } else {
  112.                             msgnova=msgnova+" §b"+nome+getCor(cmd);
  113.                         }
  114.                     } else {
  115.                         if (msgnova.equalsIgnoreCase("")) {
  116.                             msgnova=palavra;
  117.                         } else {
  118.                             msgnova=msgnova+" "+palavra;
  119.                         }
  120.                     }
  121.                 } else {
  122.                     if (msgnova.equalsIgnoreCase("")) {
  123.                         msgnova=palavra;
  124.                     } else {
  125.                         msgnova=msgnova+" "+palavra;
  126.                     }
  127.                 }
  128.         }
  129.         return msgnova;
  130.     }
  131.    
  132.     public String corrigir(String palavra) {
  133.         for (String c:getCorretor().getStringList("Fix")) {
  134.             try {
  135.                 String[] correcao=c.split("->");
  136.                 if (correcao.length>1) {
  137.                 if (checa(correcao[0], palavra)) {
  138.                     return correcao[1];
  139.                 }
  140.                 }
  141.             } catch (Exception e) {}
  142.         }
  143.         return palavra;
  144.     }
  145.    
  146.     public ChatColor getCor(String cmd) {
  147.         ChatColor rtn=ChatColor.WHITE;
  148.         for (String c:getConfig().getStringList("Cores")) {
  149.             if (c.split(":")[0].equalsIgnoreCase(cmd)) {
  150.                 String cor=c.split(":")[1];
  151.                 if (ChatColor.valueOf(cor)!=null) {
  152.                     rtn=ChatColor.valueOf(cor);
  153.                 }
  154.             }
  155.         }
  156.         return rtn;
  157.     }
  158.    
  159.     protected String gerarAsterisco(int length, char charToFill) {
  160.           if (length > 0) {
  161.             char[] array = new char[length];
  162.             Arrays.fill(array, charToFill);
  163.             return new String(array);
  164.           }
  165.           return "";
  166.         }
  167.    
  168.     public String block(String msg) {
  169.         String novamsg=msg;
  170.         for (String p:getConfig().getStringList("Palavroes")) {
  171.             p=p.replaceAll("([\\\\\\.\\[\\{\\(\\*\\+\\?\\^\\$\\|])", "\\\\$1");
  172.             novamsg=novamsg.replaceAll("(?i)\\b("+p+")+\\b", gerarAsterisco(p.length(), '*'));
  173.         }
  174.         return novamsg;
  175.     }
  176.    
  177.     @EventHandler
  178.     public void onChat(AsyncPlayerChatEvent event) {
  179.         String mensagem=event.getMessage();
  180.         String msgnova="";
  181.         for (String palavra:mensagem.split(" ")) {
  182.             if (msgnova.equalsIgnoreCase("")) {
  183.                 msgnova=corrigir(palavra);
  184.             } else {
  185.                 msgnova=msgnova+" "+corrigir(palavra);
  186.             }
  187.         }
  188.         if (checaDV(msgnova)) {
  189.             event.getPlayer().sendMessage(getConfig().getString("MensagemDV").replace("&", "§"));
  190.             event.setCancelled(true);
  191.             return;
  192.         }
  193.         msgnova=marcar(msgnova,event.getPlayer(),"CHAT");
  194.         if (getConfig().getBoolean("BlockCaps")) {
  195.             msgnova=msgnova.toLowerCase();
  196.         }
  197.         if (getConfig().getBoolean("RemoverAcentos")) {
  198.             msgnova=removerAcentos(msgnova);
  199.         }
  200.         if (getConfig().getBoolean("ForcePonto")) {
  201.             if (!msgnova.endsWith(".")) msgnova=msgnova+".";
  202.         }
  203.         if (getConfig().getBoolean("ForceUpper")) {
  204.             msgnova=primeiraLetra(msgnova);
  205.         }
  206.         msgnova=block(msgnova);
  207.         msgnova=emoticonizer(msgnova);
  208.         event.setMessage(msgnova);
  209.     }
  210.    
  211.     public String primeiraLetra(String original){
  212.         if(original.length() == 0)
  213.             return original;
  214.         return original.substring(0, 1).toUpperCase() + original.substring(1);
  215.     }
  216.    
  217.     @EventHandler
  218.     public void onCmd(PlayerCommandPreprocessEvent event) {
  219.         boolean tem=false;
  220.         String cmdformato="";
  221.         for (String cmd:getConfig().getStringList("LerComandos")) {
  222.             if (cmd.split(" ")[0].equalsIgnoreCase(event.getMessage().split(" ")[0])) {
  223.                 tem=true;
  224.                 cmdformato=cmd;
  225.             }
  226.         }
  227.         if (tem) {
  228.         String[] msg=event.getMessage().split(" ");
  229.         String[] cmd=cmdformato.split(" ");
  230.         int i=cmd.length;
  231.         if (msg.length<=cmd.length) return;
  232.         String mensagem="";
  233.         while (i!=msg.length) {
  234.             if (mensagem.equalsIgnoreCase("")) {
  235.                 mensagem=msg[i];
  236.             } else {
  237.                 mensagem=mensagem+" "+msg[i];
  238.             }
  239.             i++;
  240.         }
  241.         String base="";
  242.         i=0;
  243.         while(i!=cmd.length) {
  244.             if (base.equalsIgnoreCase("")) {
  245.                 base=event.getMessage().split(" ")[i];
  246.             } else {
  247.                 base=base+" "+event.getMessage().split(" ")[i];
  248.             }
  249.             i++;
  250.         }
  251.         String msgnova="";
  252.         for (String palavra:mensagem.split(" ")) {
  253.             if (msgnova.equalsIgnoreCase("")) {
  254.                 msgnova=corrigir(palavra);
  255.             } else {
  256.                 msgnova=msgnova+" "+corrigir(palavra);
  257.             }
  258.         }
  259.         if (checaDV(msgnova)) {
  260.             event.getPlayer().sendMessage(getConfig().getString("MensagemDV").replace("&", "§"));
  261.             event.setCancelled(true);
  262.             return;
  263.         }
  264.         msgnova=marcar(msgnova,event.getPlayer(),event.getMessage().split(" ")[0].toLowerCase());
  265.         if (getConfig().getBoolean("BlockCaps")) {
  266.             msgnova=msgnova.toLowerCase();
  267.         }
  268.         if (getConfig().getBoolean("ForcePonto")) {
  269.             if (!msgnova.endsWith(".")) msgnova=msgnova+".";
  270.         }
  271.         if (getConfig().getBoolean("RemoverAcentos")) {
  272.             msgnova=removerAcentos(msgnova);
  273.         }
  274.         if (getConfig().getBoolean("ForceUpper")) {
  275.             msgnova=primeiraLetra(msgnova);
  276.         }
  277.         msgnova=block(msgnova);
  278.         msgnova=emoticonizer(msgnova);
  279.         event.setMessage(base+" "+msgnova);
  280.         }
  281.     }
  282.    
  283.     public void saveCorretor() {
  284.         try {
  285.             customConfig.save(customYml);
  286.             } catch (Exception e) {
  287.             }
  288.     }
  289.  
  290.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
  291.         if (!sender.isOp()) {
  292.             sender.sendMessage("§cVoce nao tem permissao.");
  293.             return false;
  294.         }
  295.         if (cmd.getName().equalsIgnoreCase("limparchat")) {
  296.             int i=0;
  297.             while (i!=200) {
  298.                 for (Player p:Bukkit.getOnlinePlayers()) {
  299.                     p.sendMessage("");
  300.                 }
  301.                 i++;
  302.             }
  303.             for (Player p:Bukkit.getOnlinePlayers()) {
  304.                 p.sendMessage("§9Chat limpo por §b"+sender.getName()+"§9!");
  305.             }
  306.             return false;
  307.         }
  308.         if (args.length==0) {
  309.             sender.sendMessage("§aBrineFixer * Comandos:");
  310.             sender.sendMessage("§a/brinefixer reload * Recarregar sistema");
  311.             sender.sendMessage("§a/brinefixer addpalavra <original> <novo> * Adicionar nova correção");
  312.             return false;
  313.         }
  314.         if (args[0].equalsIgnoreCase("reload")) {
  315.             customYml=null;
  316.             customConfig=null;
  317.             customYml=new File(getDataFolder()+"/corretor.yml");
  318.             customConfig=YamlConfiguration.loadConfiguration(customYml);
  319.             reloadConfig();
  320.             sender.sendMessage("§aConfig recarregada!");
  321.         } else if (args[0].equalsIgnoreCase("addpalavra")) {
  322.             if (args.length<3) {
  323.                 sender.sendMessage("§cUso: /brinefixer addpalavra <original> <novo>");
  324.                 return false;
  325.             }
  326.             String correcao=args[1]+"->"+args[2];
  327.             if (correcao.contains("'") || correcao.contains("\"")) {
  328.                 sender.sendMessage("§cCorreção inválida!");
  329.                 return false;
  330.             }
  331.             List<String> correcoes=getCorretor().getStringList("Fix");
  332.             if (!correcoes.contains(correcao)) {
  333.                 correcoes.add(correcao);
  334.             }
  335.             getCorretor().set("Fix", correcoes);
  336.             saveCorretor();
  337.             sender.sendMessage("§aCorreção adicionada!");
  338.             sender.sendMessage("§a"+args[1]+" -> "+args[2]);
  339.         } else {
  340.             sender.sendMessage("§aBrineFixer * Comandos:");
  341.             sender.sendMessage("§a/brinefixer reload * Recarregar sistema");
  342.             sender.sendMessage("§a/brinefixer addpalavra <original> <novo> * Adicionar nova correção");
  343.             return false;
  344.         }
  345.         return false;
  346.     }
  347.    
  348. }
Advertisement
Add Comment
Please, Sign In to add comment