Advertisement
Guest User

Hi @mbaxter Hi @tom

a guest
Aug 11th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.59 KB | None | 0 0
  1. package me.jophestus.JOPHWarn;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.List;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.OfflinePlayer;
  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.plugin.java.JavaPlugin;
  18. import org.bukkit.command.ConsoleCommandSender;
  19.  
  20. public class JOPHWarn extends JavaPlugin {
  21.     Logger log = Logger.getLogger("Minecraft");
  22.     private FileConfiguration customConfig = null;
  23.     private File customConfigFile = null;
  24.  
  25.     public void reloadCustomConfig() {
  26.         if (customConfigFile == null) {
  27.             customConfigFile = new File(getDataFolder(), "warnings.yml");
  28.         }
  29.         customConfig = YamlConfiguration.loadConfiguration(customConfigFile);
  30.  
  31.         // Look for defaults in the jar
  32.         InputStream defConfigStream = this.getResource("warnings.yml");
  33.         if (defConfigStream != null) {
  34.             YamlConfiguration defConfig = YamlConfiguration
  35.                     .loadConfiguration(defConfigStream);
  36.             customConfig.setDefaults(defConfig);
  37.         }
  38.     }
  39.  
  40.     public FileConfiguration getCustomConfig() {
  41.         if (customConfig == null) {
  42.             this.reloadCustomConfig();
  43.         }
  44.         return customConfig;
  45.     }
  46.  
  47.     public void saveCustomConfig() {
  48.         if (customConfig == null || customConfigFile == null) {
  49.             return;
  50.         }
  51.         try {
  52.             getCustomConfig().save(customConfigFile);
  53.         } catch (IOException ex) {
  54.             this.getLogger().log(Level.SEVERE,
  55.                     "Could not save config to " + customConfigFile, ex);
  56.         }
  57.     }
  58.  
  59.     public void onEnable() {
  60.         SetupConfig();
  61.         loadConfiguration();
  62.  
  63.     }
  64.  
  65.     private void SetupConfig() {
  66.  
  67.         getConfig().options().copyDefaults(true);
  68.         saveDefaultConfig();
  69.     }
  70.  
  71.     private void loadConfiguration() {
  72.  
  73.         getCustomConfig().options().copyDefaults(true);
  74.         saveCustomConfig();
  75.     }
  76.  
  77.     public void onDisable() {
  78.     }
  79.  
  80.     public boolean onCommand(CommandSender sender, Command command,
  81.             String label, String[] args) {
  82.  
  83.         if (command.getName().equalsIgnoreCase("warn")) {
  84.  
  85.             ConsoleCommandSender console = Bukkit.getServer()
  86.                     .getConsoleSender();
  87.             int maxwarnings = this.getConfig().getInt("kickafter");
  88.             int maxwarningsBan = getConfig().getInt("banafter");
  89.             int custom1 = getConfig().getInt("custom1warns");
  90.             int custom2 = getConfig().getInt("custom2warns");
  91.             int custom3 = getConfig().getInt("custom3warns");
  92.             int custom4 = getConfig().getInt("custom4warns");
  93.             int custom5 = getConfig().getInt("custom5warns");
  94.             int custom6 = getConfig().getInt("custom6warns");
  95.  
  96.             String custom1command = getConfig().getString("custom1command");
  97.             String custom2command = getConfig().getString("custom2command");
  98.             String custom3command = getConfig().getString("custom3command");
  99.             String custom4command = getConfig().getString("custom4command");
  100.             String custom5command = getConfig().getString("custom5command");
  101.             String custom6command = getConfig().getString("custom6command");
  102.  
  103.             if (args.length == 0 || args.length == 1) {
  104.                 sender.sendMessage(ChatColor.RED
  105.                         + "[JOPHWarn] "
  106.                         + ChatColor.GREEN
  107.                         + "I'm sorry "
  108.                         + sender.getName()
  109.                         + ", You haven't provided enough args for this command.");
  110.  
  111.                 return false;
  112.             }
  113.             StringBuilder b = new StringBuilder();
  114.             for (int i = 1; i < args.length; i++) {
  115.                 if (i != 1)
  116.                     b.append(" ");
  117.                 b.append(args[i]);
  118.             }
  119.             Player warnee = Bukkit.getServer().getPlayer(args[0]);
  120.             List<String> warnings = getCustomConfig().getStringList(
  121.                     warnee.getName());
  122.     if (warnee == null) {
  123.                
  124.                 OfflinePlayer offline = Bukkit.getServer()
  125.                         .getOfflinePlayer(args[0]);
  126.                 warnings.add(b.toString() + " - By: " + sender.getName());
  127.                 getCustomConfig().set(offline.getName() + "offline", warnings);
  128.                 saveCustomConfig();
  129.                 reloadCustomConfig();
  130.             }
  131.            
  132.        
  133.                        
  134.             custom1command = custom1command.replace("%p", warnee.getName());
  135.             custom2command = custom2command.replace("%p", warnee.getName());
  136.             custom3command = custom3command.replace("%p", warnee.getName());
  137.             custom4command = custom4command.replace("%p", warnee.getName());
  138.             custom5command = custom5command.replace("%p", warnee.getName());
  139.             custom6command = custom6command.replace("%p", warnee.getName());
  140.  
  141.             if (sender.hasPermission("JOPHWarn.warn")) {
  142.  
  143.                 sender.sendMessage(ChatColor.RED + "[JOPHWarn] "
  144.                         + ChatColor.GREEN + "Warning sent to "
  145.                         + warnee.getName());
  146.                 warnee.sendMessage(ChatColor.BLACK
  147.                         + "+++++++++++++++++++++++++++++++++++++++");
  148.                 warnee.sendMessage(ChatColor.RED + "You have been warned by: "
  149.                         + ChatColor.BLUE + sender.getName() + ChatColor.RED
  150.                         + " for:");
  151.                 warnee.sendMessage(ChatColor.GREEN + b.toString());
  152.                 warnee.sendMessage(ChatColor.BLACK
  153.                         + "+++++++++++++++++++++++++++++++++++++++");
  154.            
  155.                 warnings.add(b.toString() + " - By: " + sender.getName());
  156.                 getCustomConfig().set(warnee.getName(), warnings);
  157.                 saveCustomConfig();
  158.                 reloadCustomConfig();
  159.                 int warningCount = warnings.size();
  160.                 if (getConfig().getBoolean("enablekick", true)) {
  161.                     if (warningCount == maxwarnings) {
  162.                         warnee.kickPlayer(getConfig().getString("kickmessage"));
  163.                         this.log.info(warnee.getName()
  164.                                 + " reached the max warnings amount and was kicked");
  165.                     }
  166.                 }
  167.                 if (getConfig().getBoolean("enableban", true)) {
  168.                     if (warningCount == maxwarningsBan) {
  169.                         Bukkit.getOfflinePlayer(args[0]).setBanned(true);
  170.                         if (getServer().getPlayer(args[0]) != null) {
  171.                             warnee.setBanned(true);
  172.                             warnee.kickPlayer(getConfig().getString(
  173.                                     "banmessage"));
  174.                             this.log.info(warnee.getName()
  175.                                     + " reached the max warnings amount and was banned");
  176.                         }
  177.  
  178.                     }
  179.                 }
  180.                 if (getConfig().getBoolean("enablecustom1", true)) {
  181.                     if (warningCount == custom1) {
  182.  
  183.                         Bukkit.dispatchCommand(console, custom1command);
  184.                     }
  185.                 }
  186.                 if (getConfig().getBoolean("enablecustom2", true)) {
  187.                     if (warningCount == custom2) {
  188.  
  189.                         Bukkit.dispatchCommand(console, custom2command);
  190.                     }
  191.                 }
  192.                 if (getConfig().getBoolean("enablecustom3", true)) {
  193.                     if (warningCount == custom3) {
  194.  
  195.                         Bukkit.dispatchCommand(console, custom3command);
  196.                     }
  197.                 }
  198.                 if (getConfig().getBoolean("enablecustom4", true)) {
  199.                     if (warningCount == custom4) {
  200.  
  201.                         Bukkit.dispatchCommand(console, custom4command);
  202.                     }
  203.                 }
  204.                 if (getConfig().getBoolean("enablecustom5", true)) {
  205.                     if (warningCount == custom5) {
  206.  
  207.                         Bukkit.dispatchCommand(console, custom5command);
  208.                     }
  209.                 }
  210.                 if (getConfig().getBoolean("enablecustom6", true)) {
  211.                     if (warningCount == custom6) {
  212.  
  213.                         Bukkit.dispatchCommand(console, custom6command);
  214.                     }
  215.                 }
  216.  
  217.                 this.log.info("JOPHWarn:: " + sender.getName() + " warned "
  218.                         + warnee.getName() + " for:");
  219.                 this.log.info("JOPHWarn:: " + b.toString());
  220.  
  221.             } else {
  222.                 sender.sendMessage(ChatColor.RED + "[JOPHWarn] "
  223.                         + ChatColor.GREEN + "I'm sorry " + sender.getName()
  224.                         + ", You can't do that.");
  225.             }
  226.  
  227.         }
  228.  
  229.         if (command.getName().equalsIgnoreCase("warnings")) {
  230.             if (sender.hasPermission("JOPHWarn.view")) {
  231.  
  232.                 if (args[0].equalsIgnoreCase("view")) {
  233.                     reloadCustomConfig();
  234.                     List<String> warns = getCustomConfig().getStringList(
  235.                             args[1]);
  236.                     sender.sendMessage(ChatColor.BLACK
  237.                             + "+++++++++++++++++++++++++++++++++++++++");
  238.                     sender.sendMessage(ChatColor.GREEN + "Viewing " + args[1]
  239.                             + "'s Warnings");
  240.                     for (String s : warns) {
  241.  
  242.                         sender.sendMessage(ChatColor.GOLD + s);
  243.  
  244.                     }
  245.                     sender.sendMessage(ChatColor.BLACK
  246.                             + "+++++++++++++++++++++++++++++++++++++++");
  247.  
  248.                 }
  249.             }
  250.             if (sender.hasPermission("JOPHWarn.warnings.clearall")) {
  251.                 if (args[0].equalsIgnoreCase("clear")) {
  252.                     getCustomConfig().set(args[1], null);
  253.                     saveCustomConfig();
  254.                     reloadCustomConfig();
  255.                     sender.sendMessage(ChatColor.RED + "[JOPHWarn]"
  256.                             + ChatColor.GREEN + " You have cleared " + args[1]
  257.                             + "'s warnings");
  258.                 }
  259.             }
  260.         }
  261.         if (command.getName().equalsIgnoreCase("infract")) {
  262.  
  263.             if (sender.hasPermission("JOPHWarn.infract")) {
  264.  
  265.                 if (args.length == 0 || args.length == 1) {
  266.                     sender.sendMessage(ChatColor.RED
  267.                             + "[JOPHWarn] "
  268.                             + ChatColor.GREEN
  269.                             + "I'm sorry "
  270.                             + sender.getName()
  271.                             + ", You haven't provided enough args for this command.");
  272.  
  273.                 }
  274.             }
  275.         }
  276.         return super.onCommand(sender, command, label, args);
  277.     }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement