Advertisement
mttprvst13

TellMe Main File

Mar 21st, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.24 KB | None | 0 0
  1. package me.mttprvst13;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6.  
  7. import net.milkbowl.vault.chat.Chat;
  8.  
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.Location;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.plugin.RegisteredServiceProvider;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16.  
  17. import ru.tehkode.permissions.PermissionGroup;
  18. import ru.tehkode.permissions.PermissionUser;
  19. import ru.tehkode.permissions.bukkit.PermissionsEx;
  20.  
  21. import com.onarandombox.MultiverseCore.MultiverseCore;
  22. import com.onarandombox.MultiverseCore.api.MultiverseWorld;
  23.  
  24. public class Main extends JavaPlugin {
  25.  
  26.     Logger log = this.getLogger();
  27.     ChatListener cl = new ChatListener(this);
  28.     private MultiverseCore core;
  29.     private boolean mv;
  30.     private MultiverseWorld mvWorld;
  31.     private String worldName;
  32.     public static Chat chat = null;
  33.  
  34.     public void onEnable() {
  35.  
  36.         this.saveDefaultConfig();
  37.  
  38.         this.getServer().getPluginManager().registerEvents(cl, this);
  39.  
  40.         if (this.getServer().getPluginManager()
  41.                 .isPluginEnabled("Multiverse-Core")) {
  42.             mv = true;
  43.             core = (MultiverseCore) getServer().getPluginManager().getPlugin(
  44.                     "Multiverse-Core");
  45.         }
  46.  
  47.         if (this.getServer().getPluginManager().isPluginEnabled("Vault")) {
  48.  
  49.             setupChat();
  50.  
  51.         }
  52.  
  53.         log.info("Enabled!");
  54.  
  55.     }
  56.  
  57.     public void onDisable() {
  58.  
  59.         log.info("Disabled.");
  60.  
  61.     }
  62.  
  63.     public List<Player> getLocalRecipients(Player sender, String message,
  64.             double range) {
  65.         Location playerLocation = sender.getLocation();
  66.         List<Player> recipients = new LinkedList<Player>();
  67.         double squaredDistance = Math.pow(range, 2);
  68.         for (Player recipient : getServer().getOnlinePlayers()) {
  69.             // Recipient are not from same world
  70.             if (!recipient.getWorld().equals(sender.getWorld())) {
  71.                 continue;
  72.             }
  73.             if (playerLocation.distanceSquared(recipient.getLocation()) > squaredDistance) {
  74.                 continue;
  75.             }
  76.             recipients.add(recipient);
  77.         }
  78.         return recipients;
  79.     }
  80.  
  81.     private boolean setupChat() {
  82.         RegisteredServiceProvider<Chat> chatProvider = getServer()
  83.                 .getServicesManager().getRegistration(
  84.                         net.milkbowl.vault.chat.Chat.class);
  85.         if (chatProvider != null) {
  86.             chat = chatProvider.getProvider();
  87.         }
  88.  
  89.         return (chat != null);
  90.     }
  91.  
  92.     public String formatChat(Player player, String format) {
  93.  
  94.         if (mv) {
  95.  
  96.             mvWorld = core.getMVWorldManager().getMVWorld(player.getWorld());
  97.  
  98.         }
  99.         if (mvWorld != null) {
  100.             log.info(mvWorld.getName() + " - " + format);
  101.             format.replaceAll("%MVWORLD%", mvWorld.getName());
  102.  
  103.         } else {
  104.  
  105.             format.replaceAll("%MVWORLD", "");
  106.  
  107.         }
  108.  
  109.         worldName = player.getWorld().getName();
  110.  
  111.         String[] stuff = getStuff(player);
  112.  
  113.         format = format
  114.                 .replaceAll("%PREFIX%", stuff[0])
  115.                 .replaceAll("%SUFFIX%", stuff[1])
  116.                 .replaceAll("%WORLD%", worldName)
  117.                 .replaceAll("%UUID%", player.getUniqueId().toString())
  118.                 // for people who really want UUIDs in chat
  119.                 .replaceAll("%PLAYER%", player.getName())
  120.                 .replaceAll("%DISPLAYNAME%", player.getDisplayName())
  121.                 .replaceAll("%GROUP%", stuff[2]);
  122.  
  123.         return format;
  124.  
  125.     }
  126.  
  127.     public String colorize(String string) {
  128.         if (string == null) {
  129.             return "";
  130.         }
  131.  
  132.         return string.replaceAll("&([a-z0-9])", "\u00A7$1");
  133.     }
  134.  
  135.     @SuppressWarnings("deprecation")
  136.     public String[] getStuff(Player player) {
  137.  
  138.         String prefix = null;
  139.         String suffix = null;
  140.         String group = null;
  141.         if (getServer().getPluginManager().getPlugin("PermissionsEx") != null) {
  142.  
  143.             PermissionUser user = PermissionsEx.getUser(player.getName());
  144.             PermissionGroup[] groups = user.getGroups();
  145.             prefix = "";
  146.             suffix = "";
  147.             group = "";
  148.  
  149.             for (PermissionGroup g : groups) {
  150.  
  151.                 prefix = prefix + "" + g.getOwnPrefix();
  152.                 suffix = suffix + "" + g.getOwnSuffix();
  153.                 group = group + "" + g.getName();
  154.  
  155.             }
  156.  
  157.         } else if (getServer().getPluginManager().getPlugin("bPermissions") != null) {
  158.  
  159.             prefix = Main.chat.getPlayerPrefix(player);
  160.             suffix = Main.chat.getPlayerSuffix(player);
  161.             group = chat.getPrimaryGroup(player);
  162.  
  163.         }
  164.  
  165.         String[] ret;
  166.         ret = new String[3];
  167.         ret[0] = prefix;
  168.         ret[1] = suffix;
  169.         ret[2] = group;
  170.  
  171.         return ret;
  172.  
  173.     }
  174.  
  175.     public List<Player> getSpies() {
  176.  
  177.         List<Player> recipients = new LinkedList<Player>();
  178.         for (Player p : getServer().getOnlinePlayers()) {
  179.  
  180.             if (p.hasPermission("tellme.spy")) {
  181.  
  182.                 recipients.add(p);
  183.  
  184.             }
  185.  
  186.         }
  187.  
  188.         return recipients;
  189.  
  190.     }
  191.  
  192.     public boolean onCommand(CommandSender sender, Command cmd, String label,
  193.             String[] args) {
  194.  
  195.         if (cmd.getName().equalsIgnoreCase("tmr")) {
  196.  
  197.             this.reloadConfig();
  198.  
  199.             sender.sendMessage(ChatColor.GREEN + "[TellMe] Reload Complete.");
  200.  
  201.         }
  202.        
  203.         if(cmd.getName().equalsIgnoreCase("/s") || cmd.getName().equalsIgnoreCase("/shout")){
  204.            
  205.             if(sender instanceof Player){
  206.                
  207.                 Player player = (Player) sender;
  208.                
  209.                 String msg = "";
  210.                
  211.                 for(String word : args){
  212.                    
  213.                     msg = msg + word + " ";
  214.                    
  215.                 }
  216.                
  217.                 player.chat("!" + msg);
  218.                 log.info("!" + msg);
  219.                
  220.             }else{
  221.                
  222.                 sender.sendMessage(ChatColor.RED + "You must be a player to do that command.");
  223.                
  224.             }
  225.            
  226.         }
  227.  
  228.         return false;
  229.  
  230.     }
  231.  
  232.     public boolean filter(Player player, String msg) {
  233.  
  234.         if (getConfig().getBoolean("Anti-Spam.Anti-Spam", true)) {
  235.  
  236.             String[] splitmsg = msg.split("");
  237.             int configcl = getConfig().getInt("Anti-Spam.Consecutive-Letters");
  238.             String ll = null;
  239.             int cl = 0;
  240.             for (String l : splitmsg) {
  241.  
  242.                 if (ll == null) {
  243.  
  244.                     ll = l;
  245.  
  246.                 } else {
  247.  
  248.                     if (ll.equalsIgnoreCase(l)) {
  249.  
  250.                         cl++;
  251.  
  252.                     } else {
  253.  
  254.                         cl = 0;
  255.  
  256.                     }
  257.  
  258.                     ll = l;
  259.  
  260.                     if (cl >= configcl) {
  261.  
  262.                         player.sendMessage(ChatColor.RED
  263.                                 + "You have done an excecive amount of consecutive letters.");
  264.                         return false;
  265.  
  266.                     }
  267.  
  268.                 }
  269.  
  270.             }
  271.  
  272.         }
  273.  
  274.         if (getConfig().getBoolean("Anti-Spam.CapsControl", true)) {
  275.  
  276.             String[] splitmsg = msg.split("");
  277.             int configcl = getConfig().getInt("Anti-Spam.Consecutive-Caps");
  278.             String lastLetter = null;
  279.             int conLetter = 0;
  280.             for (String letter : splitmsg) {
  281.  
  282.                 if (lastLetter == null) {
  283.  
  284.                     lastLetter = letter;
  285.  
  286.                 } else {
  287.  
  288.                     if (testIfCap(letter)) {
  289.  
  290.                         conLetter++;
  291.  
  292.                     } else {
  293.  
  294.                         conLetter = 0;
  295.  
  296.                     }
  297.                     lastLetter = letter;
  298.  
  299.                     if (conLetter >= configcl) {
  300.  
  301.                         player.sendMessage(ChatColor.RED
  302.                                 + "You have done an excecive amount of consecutive caps.");
  303.                         return false;
  304.  
  305.                     }
  306.  
  307.                 }
  308.  
  309.             }
  310.            
  311.             splitmsg = msg.split("");
  312.             configcl = getConfig().getInt("Anti-Spam.Total-Caps");
  313.             lastLetter = null;
  314.             conLetter = 0;
  315.             for (String letter1 : splitmsg) {
  316.                
  317.                 log.info("Letter: " + letter1);
  318.                
  319.                 if(letter1.equalsIgnoreCase("")){
  320.                    
  321.                     continue;
  322.                    
  323.                 }
  324.  
  325.                 if (testIfCap(letter1)) {
  326.  
  327.                     conLetter++;
  328.  
  329.                 }
  330.  
  331.                 if (conLetter >= configcl) {
  332.  
  333.                     player.sendMessage(ChatColor.RED
  334.                             + "You have done an excecive amount of caps.");
  335.                     return false;
  336.                    
  337.                 }
  338.  
  339.             }
  340.  
  341.         }
  342.  
  343.         return true;
  344.  
  345.     }
  346.  
  347.     public boolean testIfCap(String letter) {
  348.  
  349.         char c = letter.charAt(0);
  350.         if (c >= 97 && c <= 122) {
  351.             return false;
  352.         }
  353.  
  354.         return true;
  355.  
  356.     }
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement