Advertisement
Jnk1296

Ban-All

Aug 3rd, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.59 KB | None | 0 0
  1. /*
  2.  * Copyright © 2014 Jacob Keep (Jnk1296). All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  *  * Redistributions of source code must retain the above copyright notice,
  8.  *   this list of conditions and the following disclaimer.
  9.  *
  10.  *  * Redistributions in binary form must reproduce the above copyright notice,
  11.  *   this list of conditions and the following disclaimer in the documentation
  12.  *   and/or other materials provided with the distribution.
  13.  *
  14.  *  * Neither the name of JuNK Software nor the names of its contributors may
  15.  *   be used to endorse or promote products derived from this software without
  16.  *   specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. package net.risenphoenix.ipcheck.commands.ban;
  32.  
  33. import net.risenphoenix.commons.Plugin;
  34. import net.risenphoenix.commons.commands.Command;
  35. import net.risenphoenix.commons.commands.CommandType;
  36. import net.risenphoenix.ipcheck.IPCheck;
  37. import net.risenphoenix.ipcheck.actions.ActionBanAll;
  38. import net.risenphoenix.ipcheck.actions.ActionBroadcast;
  39. import net.risenphoenix.ipcheck.util.MessageParser;
  40. import org.bukkit.ChatColor;
  41. import org.bukkit.command.CommandSender;
  42. import org.bukkit.permissions.Permission;
  43.  
  44. import java.sql.Timestamp;
  45. import java.text.ParseException;
  46. import java.text.SimpleDateFormat;
  47. import java.util.ArrayList;
  48. import java.util.Date;
  49.  
  50. public class CmdBanAll extends Command {
  51.  
  52.     private IPCheck ipc;
  53.  
  54.     public CmdBanAll(final Plugin plugin, String[] callArgs, CommandType type) {
  55.         super(plugin, callArgs, type);
  56.  
  57.         this.ipc = IPCheck.getInstance();
  58.  
  59.         setName(this.getLocalString("CMD_BANALL"));
  60.         setHelp(this.getLocalString("HELP_BANALL"));
  61.         setSyntax("ipc banall <START_TIME> <STOP_TIME | now> [MESSAGE]");
  62.         setPermissions(new Permission[]{
  63.                 new Permission("ipcheck.use"),
  64.                 new Permission("ipcheck.ban"),
  65.                 new Permission("ipcheck.banall")});
  66.     }
  67.  
  68.     @Override
  69.     public void onExecute(CommandSender sender, String[] args) {
  70.         executeBan(sender, args, true);
  71.     }
  72.  
  73.     // Command-Specific Methods and Classes
  74.  
  75.     /* The executable code for Ban-All has been placed into this container
  76.      * method so that it can be accessed by Unban-all via getCommand() from the
  77.      * Command Manager. */
  78.     public void executeBan(CommandSender sender, String[] args, boolean ban) {
  79.         // Does the Configuration Allow this command to execute?
  80.         if (!this.getPlugin().getConfigurationManager()
  81.                 .getBoolean("should-manage-bans")) {
  82.             this.sendPlayerMessage(sender, this.getLocalString("DISABLE_ERR"));
  83.             return;
  84.         }
  85.  
  86.         // Parse Ban Message if one exists
  87.         String message = new MessageParser(args, 3).parseMessage();
  88.  
  89.         // Confirm Ban Message is not Empty
  90.         if (message == null || message.length() <= 0) message =
  91.                 IPCheck.getInstance().getConfigurationManager()
  92.                         .getString("ban-message");
  93.  
  94.         // Fetch database timestamp and parse it as a Date object
  95.         SimpleDateFormat sParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  96.         Date currentTime;
  97.  
  98.         // Parse the timestamp returned from the Database
  99.         try {
  100.             currentTime = sParse.parse(ipc.getDatabaseController()
  101.                     .getCurrentTimeStamp());
  102.         } catch (ParseException e) {
  103.             // If an error occurs while parsing, abort the command
  104.             this.sendPlayerMessage(sender, getLocalString("TIME_STAMP_ERR"));
  105.             return;
  106.         }
  107.  
  108.         /* Create two ArrayLists to hold ModifyItems, one for each of the two
  109.          * timestamps. setOne is the set for the older of the two time-stamps,
  110.          * while setTwo is the set for the newer of the time-stamps, and is
  111.          * optional in it's usage. */
  112.         ArrayList<ModifyItem> setOne = new ArrayList<ModifyItem>();
  113.         ArrayList<ModifyItem> setTwo = null;
  114.         boolean hasFirst = false; // Parse Control Flag
  115.  
  116.         // Fetch ModifyItems for each set if Arguments permit
  117.         for (int i = 1; i < args.length; i++) {
  118.             if (!hasFirst) {
  119.                 hasFirst = true;
  120.                 setOne = getModificationItems(args[i]);
  121.             } else {
  122.                 if (!args[i].equalsIgnoreCase("now")) {
  123.                     setTwo = getModificationItems(args[i]);
  124.                 }
  125.             }
  126.         }
  127.  
  128.         // Convert Dates to SQL Time-stamps
  129.         Timestamp tsOne = new Timestamp(modifyDateStamp(
  130.                 new Date(currentTime.getTime()), setOne).getTime());
  131.         Timestamp tsTwo = (setTwo != null) ? new Timestamp(modifyDateStamp(
  132.                 new Date(currentTime.getTime()), setTwo).getTime()) : null;
  133.  
  134.         // Convert Timestamps to Strings
  135.         String timeArgOne = sParse.format(tsOne);
  136.         String timeArgTwo = (tsTwo != null) ? sParse.format(tsTwo) :
  137.                 sParse.format(new Timestamp(currentTime.getTime()));
  138.  
  139.         // Execute Ban and retrieve affected account tally
  140.         Object[] results = new ActionBanAll(IPCheck.getInstance(), timeArgOne,
  141.                 timeArgTwo, message, ban).execute();
  142.  
  143.         // Get affected account tally from results
  144.         int count = (Integer) results[0];
  145.  
  146.         // Stats Link
  147.         if (ban) {
  148.             IPCheck.getInstance().getStatisticsObject().logPlayerBan(count);
  149.         } else {
  150.             IPCheck.getInstance().getStatisticsObject().logPlayerUnban(count);
  151.         }
  152.  
  153.         // Set up Broadcast Notification
  154.         ActionBroadcast ab;
  155.         String broadcastMsg;
  156.  
  157.         if (ban) {
  158.             if (count == 1) {
  159.                 broadcastMsg = ChatColor.GOLD + "Player " + ChatColor.RED +
  160.                         "%s" + ChatColor.GOLD + " was banned by " +
  161.                         ChatColor.GREEN + "%s" + ChatColor.GOLD + " for: %s";
  162.             } else {
  163.                 broadcastMsg = ChatColor.GOLD + "Player " + ChatColor.GREEN +
  164.                         "%s" + ChatColor.GOLD + " banned " + ChatColor.RED +
  165.                         "%s" + ChatColor.GOLD + " accounts for: " + "%s";
  166.             }
  167.         } else {
  168.             if (count == 1) {
  169.                 broadcastMsg = ChatColor.GOLD + "Player " + ChatColor.RED +
  170.                         "%s" + ChatColor.GOLD + " was banned by " +
  171.                         ChatColor.GREEN + "%s.";
  172.             } else {
  173.                 broadcastMsg = ChatColor.GOLD + "Player " + ChatColor.GREEN +
  174.                         "%s" + ChatColor.GOLD + " unbanned " + ChatColor.RED +
  175.                         "%s" + ChatColor.GOLD + " accounts.";
  176.             }
  177.         }
  178.  
  179.         // Determine which message should be shown by the broadcaster, if any.
  180.         if (count > 0) {
  181.             // Ban Messages
  182.             if (ban) {
  183.                 if (count == 1) {
  184.                     ab = new ActionBroadcast(broadcastMsg, new String[]{
  185.                             results[1].toString(), sender.getName(), message},
  186.                             new Permission[]{new Permission("ipcheck.seeban")},
  187.                             false);
  188.  
  189.                     // Execute Broadcast
  190.                     ab.execute();
  191.                 } else {
  192.                     ab = new ActionBroadcast(broadcastMsg, new String[]{
  193.                             sender.getName(), count + "", message},
  194.                             new Permission[]{new Permission("ipcheck.seeban")},
  195.                             false);
  196.  
  197.                     // Execute Broadcast
  198.                     ab.execute();
  199.                 }
  200.             // Unban Messages
  201.             } else {
  202.                 if (count == 1) {
  203.                     ab = new ActionBroadcast(broadcastMsg, new String[]{
  204.                             results[1].toString(), sender.getName()},
  205.                             new Permission[]{new Permission("ipcheck.seeban")},
  206.                             false);
  207.  
  208.                     // Execute Broadcast
  209.                     ab.execute();
  210.                 } else {
  211.                     ab = new ActionBroadcast(broadcastMsg, new String[]{
  212.                             sender.getName(), count + ""},
  213.                             new Permission[]{new Permission("ipcheck.seeban")},
  214.                             false);
  215.  
  216.                     // Execute Broadcast
  217.                     ab.execute();
  218.                 }
  219.             }
  220.         } else {
  221.             // If no accounts were modified, notify player of this.
  222.             this.sendPlayerMessage(sender, this.getLocalString("NO_MODIFY"));
  223.         }
  224.     }
  225.  
  226.     // ModifyItem Creator
  227.     private ArrayList<ModifyItem> getModificationItems(String arg) {
  228.         // Create Storage for finished ModifyItems
  229.         ArrayList<ModifyItem> modItems = new ArrayList<ModifyItem>();
  230.  
  231.         // Split Time Arguments into separate indices for proper parsing
  232.         String[] values = arg.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
  233.         int length = values.length;
  234.  
  235.         /* Confirm there are an even amount of indices to parse, else drop the
  236.          * odd index from the length size. */
  237.         if (values.length % 2 != 0) length--;
  238.  
  239.         // Control Flag denoting if arguments are formatted as 1d2h or d1h2
  240.         boolean startsNum = false;
  241.  
  242.         // Determine proper setting of Control Flag
  243.         try {
  244.             Integer.parseInt(values[0]);
  245.             startsNum = true;
  246.         } catch (NumberFormatException e) { /*ignore*/ }
  247.  
  248.         // Create ModifyItems
  249.         for (int i = 0; i < (length - 1); i += 2) {
  250.             // If the Control Flag is true, then arguments are formatted as 1d2h
  251.             if (startsNum) {
  252.                 modItems.add(new ModifyItem(values[i + 1],
  253.                         Integer.parseInt(values[i])));
  254.             } else {
  255.                 modItems.add(new ModifyItem(values[i],
  256.                         Integer.parseInt(values[i + 1])));
  257.             }
  258.         }
  259.  
  260.         // Return finished ModifyItems
  261.         return modItems;
  262.     }
  263.  
  264.     // ModifyItem Parser
  265.     private Date modifyDateStamp(Date date, ArrayList<ModifyItem> modItems) {
  266.         // Fetch date stamp from Date (divide by 1000 for easier modification)
  267.         long stamp = date.getTime() / 1000;
  268.  
  269.         // Loop through ModifyItems and edit date stamp
  270.         for (ModifyItem m : modItems) {
  271.             // Days
  272.             if (m.getModifier().equalsIgnoreCase("d")) {
  273.                 int val = m.getValue() * 86400; //Number of seconds in a day
  274.                 stamp -= val;
  275.                 continue;
  276.             }
  277.  
  278.             // Hours
  279.             if (m.getModifier().equalsIgnoreCase("h")) {
  280.                 int val = m.getValue() * 3600; // Number of seconds in an hour
  281.                 stamp -= val;
  282.                 continue;
  283.             }
  284.  
  285.             // Minutes
  286.             if (m.getModifier().equalsIgnoreCase("m")) {
  287.                 int val = m.getValue() * 60; // Number of seconds in a minute
  288.                 stamp -= val;
  289.                 continue;
  290.             }
  291.  
  292.             // Seconds
  293.             if (m.getModifier().equalsIgnoreCase("s")) {
  294.                 stamp -= m.getValue(); // Value is already in number of seconds
  295.                 continue;
  296.             }
  297.         }
  298.  
  299.         date = new Date(stamp * 1000); // Multiply by 1000 to fix original div.
  300.         return date;
  301.     }
  302.  
  303.     // ModifyItem used with argument parsing
  304.     private class ModifyItem {
  305.         private String modifier;
  306.         private int value;
  307.  
  308.         ModifyItem(String modifier, int value) {
  309.             this.modifier = modifier;
  310.             this.value = value;
  311.         }
  312.  
  313.         public String getModifier() {
  314.             return this.modifier;
  315.         }
  316.  
  317.         public int getValue() {
  318.             return this.value;
  319.         }
  320.     }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement