Facebook
From ds, 8 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 318
  1. package pl.myhard.core.tasks;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import java.util.GregorianCalendar;
  9. import java.util.HashMap;
  10. import java.util.LinkedHashMap;
  11. import java.util.Map;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. import org.bukkit.ChatColor;
  16. import org.bukkit.Material;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.command.ConsoleCommandSender;
  19. import org.bukkit.enchantments.Enchantment;
  20. import org.bukkit.entity.Entity;
  21. import org.bukkit.entity.Player;
  22. import org.bukkit.entity.Projectile;
  23. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  24. import org.bukkit.inventory.Inventory;
  25. import org.bukkit.inventory.ItemStack;
  26.  
  27. public final class Util
  28. {
  29.     private static final SimpleDateFormat dateFormat;
  30.     private static final SimpleDateFormat timeFormat;
  31.     private static final LinkedHashMap<Integer, String> values;
  32.    
  33.     public static String fixColor(final String s) {
  34.         if (s == null) {
  35.             return "";
  36.         }
  37.         return ChatColor.translateAlternateColorCodes('&', s);
  38.     }
  39.    
  40.     public static Collection<String> fixColor(final Collection<String> collection) {
  41.         final Collection<String> local = new ArrayList<String>();
  42.         for (final String s : collection) {
  43.             local.add(fixColor(s));
  44.         }
  45.         return local;
  46.     }
  47.    
  48.     public static String[] fixColor(final String[] array) {
  49.         for (int i = 0; i < array.length; ++i) {
  50.             array[i] = fixColor(array[i]);
  51.         }
  52.         return array;
  53.     }
  54.    
  55.     public static boolean sendMsg(final CommandSender sender, final String message, final String permission) {
  56.         if (sender instanceof ConsoleCommandSender) {
  57.             sendMsg(sender, message);
  58.         }
  59.         return permission != null && permission != "" && sender.hasPermission(permission) && sendMsg(sender, message);
  60.     }
  61.    
  62.     public static boolean sendMsg(final CommandSender sender, final String message) {
  63.         if (sender instanceof Player) {
  64.             if (message != null || message != "") {
  65.                 sender.sendMessage(fixColor(message));
  66.             }
  67.         }
  68.         else {
  69.             sender.sendMessage(ChatColor.stripColor(fixColor(message)));
  70.         }
  71.         return true;
  72.     }
  73.    
  74.     public static boolean sendMsg(final Collection<? extends CommandSender> collection, final String message) {
  75.         for (final CommandSender cs : collection) {
  76.             sendMsg(cs, message);
  77.         }
  78.         return true;
  79.     }
  80.    
  81.     public static boolean sendMsg(final Collection<? extends CommandSender> collection, final String message, final String permission) {
  82.         for (final CommandSender cs : collection) {
  83.             sendMsg(cs, message, permission);
  84.         }
  85.         return true;
  86.     }
  87.    
  88.     public static boolean containsIgnoreCase(final String[] array, final String element) {
  89.         for (final String s : array) {
  90.             if (s.equalsIgnoreCase(element)) {
  91.                 return true;
  92.             }
  93.         }
  94.         return false;
  95.     }
  96.    
  97.     public static Material getMaterial(final String idOrName) {
  98.         if (isInteger(idOrName)) {
  99.             return Material.getMaterial(Integer.parseInt(idOrName));
  100.         }
  101.         for (final Material m : Material.values()) {
  102.             if (m.name().replace("_", "").equalsIgnoreCase(idOrName)) {
  103.                 return m;
  104.             }
  105.         }
  106.         return null;
  107.     }
  108.    
  109.     public static ItemStack getItemStack(final Material m, final short data, final int amount, final HashMap<Enchantment, Integer> enchants) {
  110.         ItemStack item = null;
  111.         int a = 64;
  112.         if (amount >= 1) {
  113.             a = amount;
  114.         }
  115.         if (data > 0) {
  116.             item = new ItemStack(m, a);
  117.         }
  118.         else {
  119.             item = new ItemStack(m, a, data);
  120.         }
  121.         if (enchants != null) {
  122.             item.addUnsafeEnchantments((Map)enchants);
  123.         }
  124.         return item;
  125.     }
  126.    
  127.     public static void giveItems(final Player p, final ItemStack... items) {
  128.         final Inventory i = (Inventory)p.getInventory();
  129.         final HashMap<Integer, ItemStack> notStored = (HashMap<Integer, ItemStack>)i.addItem(items);
  130.         for (final Map.Entry<Integer, ItemStack> e : notStored.entrySet()) {
  131.             p.getWorld().dropItemNaturally(p.getLocation(), (ItemStack)e.getValue());
  132.         }
  133.     }
  134.    
  135.     public static Player getDamager(final EntityDamageByEntityEvent e) {
  136.         final Entity damager = e.getDamager();
  137.         if (damager instanceof Player) {
  138.             return (Player)damager;
  139.         }
  140.         if (damager instanceof Projectile) {
  141.             final Projectile p = (Projectile)damager;
  142.             if (p.getShooter() instanceof Player) {
  143.                 return (Player)p.getShooter();
  144.             }
  145.         }
  146.         return null;
  147.     }
  148.    
  149.     public static String secondsToString(int seconds) {
  150.         final StringBuilder sb = new StringBuilder();
  151.         for (final Map.Entry<Integer, String> e : Util.values.entrySet()) {
  152.             final int iDiv = seconds / e.getKey();
  153.             if (iDiv >= 1) {
  154.                 final int x = (int)Math.floor(iDiv);
  155.                 sb.append(x + e.getValue()).append(" ");
  156.                 seconds -= x * e.getKey();
  157.             }
  158.         }
  159.         return sb.toString();
  160.     }
  161.    
  162.     public static boolean isAlphaNumeric(final String s) {
  163.         return s.matches("^[a-zA-Z0-9_]*$");
  164.     }
  165.    
  166.     public static boolean isFloat(final String string) {
  167.         return Pattern.matches("([0-9]*)\\.([0-9]*)", string);
  168.     }
  169.    
  170.     public static boolean isInteger(final String string) {
  171.         return Pattern.matches("-?[0-9]+", string.subSequence(0, string.length()));
  172.     }
  173.    
  174.     public static String getDate(final long time) {
  175.         return Util.dateFormat.format(new Date(time));
  176.     }
  177.    
  178.     public static String getTime(final long time) {
  179.         return Util.timeFormat.format(new Date(time));
  180.     }
  181.    
  182.     public static ItemStack getItemStackFromString(final String itemstack) {
  183.         final String[] splits = itemstack.split("@");
  184.         final String type = splits[0];
  185.         final String data = (splits.length == 2) ? splits[1] : null;
  186.         if (data == null) {
  187.             return new ItemStack(Material.getMaterial(type), 1);
  188.         }
  189.         return new ItemStack(Material.getMaterial(type), 1, (short)Integer.parseInt(data));
  190.     }
  191.    
  192.     public static String getStringFromItemstack(final ItemStack itemstack) {
  193.         if (itemstack.getData().getData() > 0) {
  194.             return itemstack.getType().toString() + "@" + itemstack.getData().getData();
  195.         }
  196.         return itemstack.getType().toString();
  197.     }
  198.    
  199.     public static long parseDateDiff(final String time, final boolean future) {
  200.         try {
  201.             final Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?(?:([0-9]+)\\s*(?:s[a-z]*)?)?", 2);
  202.             final Matcher m = timePattern.matcher(time);
  203.             int years = 0;
  204.             int months = 0;
  205.             int weeks = 0;
  206.             int days = 0;
  207.             int hours = 0;
  208.             int minutes = 0;
  209.             int seconds = 0;
  210.             boolean found = false;
  211.             while (m.find()) {
  212.                 if (m.group() != null && !m.group().isEmpty()) {
  213.                     for (int i = 0; i < m.groupCount(); ++i) {
  214.                         if (m.group(i) != null && !m.group(i).isEmpty()) {
  215.                             found = true;
  216.                             break;
  217.                         }
  218.                     }
  219.                     if (!found) {
  220.                         continue;
  221.                     }
  222.                     if (m.group(1) != null && !m.group(1).isEmpty()) {
  223.                         years = Integer.parseInt(m.group(1));
  224.                     }
  225.                     if (m.group(2) != null && !m.group(2).isEmpty()) {
  226.                         months = Integer.parseInt(m.group(2));
  227.                     }
  228.                     if (m.group(3) != null && !m.group(3).isEmpty()) {
  229.                         weeks = Integer.parseInt(m.group(3));
  230.                     }
  231.                     if (m.group(4) != null && !m.group(4).isEmpty()) {
  232.                         days = Integer.parseInt(m.group(4));
  233.                     }
  234.                     if (m.group(5) != null && !m.group(5).isEmpty()) {
  235.                         hours = Integer.parseInt(m.group(5));
  236.                     }
  237.                     if (m.group(6) != null && !m.group(6).isEmpty()) {
  238.                         minutes = Integer.parseInt(m.group(6));
  239.                     }
  240.                     if (m.group(7) == null) {
  241.                         break;
  242.                     }
  243.                     if (m.group(7).isEmpty()) {
  244.                         break;
  245.                     }
  246.                     seconds = Integer.parseInt(m.group(7));
  247.                     break;
  248.                 }
  249.             }
  250.             if (!found) {
  251.                 return -1L;
  252.             }
  253.             final Calendar c = new GregorianCalendar();
  254.             if (years > 0) {
  255.                 c.add(1, years * (future ? 1 : -1));
  256.             }
  257.             if (months > 0) {
  258.                 c.add(2, months * (future ? 1 : -1));
  259.             }
  260.             if (weeks > 0) {
  261.                 c.add(3, weeks * (future ? 1 : -1));
  262.             }
  263.             if (days > 0) {
  264.                 c.add(5, days * (future ? 1 : -1));
  265.             }
  266.             if (hours > 0) {
  267.                 c.add(11, hours * (future ? 1 : -1));
  268.             }
  269.             if (minutes > 0) {
  270.                 c.add(12, minutes * (future ? 1 : -1));
  271.             }
  272.             if (seconds > 0) {
  273.                 c.add(13, seconds * (future ? 1 : -1));
  274.             }
  275.             final Calendar max = new GregorianCalendar();
  276.             max.add(1, 10);
  277.             if (c.after(max)) {
  278.                 return max.getTimeInMillis();
  279.             }
  280.             return c.getTimeInMillis();
  281.         }
  282.         catch (Exception e) {
  283.             return -1L;
  284.         }
  285.     }
  286.    
  287.     public static void main(final String[] args) {
  288.         System.out.println(getDate(1418431585969L));
  289.     }
  290.    
  291.     static {
  292.         dateFormat = new SimpleDateFormat("dd-MM-yyyy, HH:mm:ss");
  293.         timeFormat = new SimpleDateFormat("HH:mm:ss");
  294.         (values = new LinkedHashMap<Integer, String>(6)).put(31104000, "y");
  295.         Util.values.put(2592000, "msc");
  296.         Util.values.put(86400, "d");
  297.         Util.values.put(3600, "h");
  298.         Util.values.put(60, "min");
  299.         Util.values.put(1, "s");
  300.     }
  301. }
  302.