Created
July 11, 2016 14:00
-
-
Save stormtrooper28/b6e5da8e14d44d29862c35681967ddc2 to your computer and use it in GitHub Desktop.
My 'Easy' Utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.storm28.guigame.Utils; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Comparator; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.LinkedHashMap; | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.Random; | |
| import java.util.regex.Pattern; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.Material; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.inventory.Inventory; | |
| import org.bukkit.inventory.ItemStack; | |
| import org.bukkit.inventory.meta.ItemMeta; | |
| public class Easy { | |
| public static String toScientificNotation(Long num) { | |
| String numb = num.toString(); | |
| if (numb.length() > 13) { | |
| numb = numb.charAt(0) + "." + numb.substring(1, 3) + " * 10^" + (numb.length() - 1); | |
| } else { | |
| int i = numb.length() - 3; | |
| while (i > 0) { | |
| numb = numb.substring(0, i) + "," + numb.substring(i); | |
| i -= 3; | |
| } | |
| } | |
| return numb; | |
| } | |
| /** | |
| * Change the title of the given inventory MAKE IT FOR THE GENERAL CASE AND | |
| * MOVE TO Easy.java @plan | |
| * | |
| * @param inv | |
| * Inventory to rename | |
| * @param p | |
| * Player to change the inventory of (can be null) | |
| * @param s | |
| * New Title | |
| * @return Given inventory with new title | |
| */ | |
| @Deprecated | |
| public Inventory setNewInventoryMessage(Inventory inv, Player p, String s) { | |
| Inventory newInv = Bukkit.createInventory(null, inv.getSize(), s); | |
| newInv.setContents(inv.getContents()); | |
| if (p != null && p.getOpenInventory().equals(inv)) | |
| p.openInventory(newInv); | |
| inv = newInv; | |
| return inv; | |
| } | |
| public static ItemStack item(Material mat) { | |
| if (mat != null) { | |
| return new ItemStack(mat); | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, String name) { | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat); | |
| ItemMeta itMeta = it.getItemMeta(); | |
| if (name != null) { | |
| itMeta.setDisplayName(name); | |
| it.setItemMeta(itMeta); | |
| } | |
| it.setItemMeta(itMeta); | |
| return it; | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, int dura) { | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat); | |
| it.setDurability((short) dura); | |
| return it; | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, int dura, String name) { | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat); | |
| ItemMeta itMeta = it.getItemMeta(); | |
| it.setDurability((short) dura); | |
| if (name != null) { | |
| itMeta.setDisplayName(name); | |
| it.setItemMeta(itMeta); | |
| } | |
| it.setItemMeta(itMeta); | |
| return it; | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, String name, List<String> lor) { | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat); | |
| ItemMeta itMeta = it.getItemMeta(); | |
| if (name != null) { | |
| itMeta.setDisplayName(name); | |
| it.setItemMeta(itMeta); | |
| } | |
| itMeta.setLore(lor); | |
| it.setItemMeta(itMeta); | |
| return it; | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, String name, String lor) { | |
| List<String> lores = new ArrayList<String>(); | |
| String lo = (String) lor; | |
| if (lo.contains("||")) { | |
| Collections.addAll(lores, lo.split(Pattern.quote("||"))); | |
| } else { | |
| lores.add((String) lo); | |
| } | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat); | |
| ItemMeta itMeta = it.getItemMeta(); | |
| if (name != null) { | |
| itMeta.setDisplayName(name); | |
| it.setItemMeta(itMeta); | |
| } | |
| if (lores != null) { | |
| itMeta.setLore(lores); | |
| it.setItemMeta(itMeta); | |
| } | |
| return it; | |
| } | |
| return null; | |
| } | |
| public static ItemStack item(Material mat, String name, String lor, int dura) { | |
| List<String> lores = new ArrayList<String>(); | |
| String lo = (String) lor; | |
| if (lo.contains("||")) { | |
| Collections.addAll(lores, lo.split(Pattern.quote("||"))); | |
| } else { | |
| lores.add((String) lo); | |
| } | |
| if (mat != null) { | |
| ItemStack it = new ItemStack(mat, 1, (short) dura); | |
| ItemMeta itMeta = it.getItemMeta(); | |
| if (name != null) { | |
| itMeta.setDisplayName(name); | |
| it.setItemMeta(itMeta); | |
| } | |
| if (lores != null) { | |
| itMeta.setLore(lores); | |
| it.setItemMeta(itMeta); | |
| } | |
| return it; | |
| } | |
| return null; | |
| } | |
| // Allow more advanced random in future | |
| // Less than or equal to is true | |
| public static Boolean wRand(int max, int chance) { // Weighted Binary Random | |
| // (yes/no) | |
| Random rand = new Random(); | |
| return (rand.nextInt(max) <= chance); | |
| } | |
| public static String parseTime(Long miliTime) { | |
| if (miliTime > 1000) { | |
| long secTime = (long) Math.ceil(miliTime / 1000); // mili -> second | |
| if (secTime < 60) { | |
| return secTime + " seconds"; | |
| } | |
| long miliSec = ((long) Math.ceil(miliTime / 1000)); | |
| secTime = miliSec % 60; // mili -> second -> leftovers | |
| long minTime = (miliSec - secTime) / 60; // mili -> second -> | |
| // minutes | |
| if (minTime < 60) { | |
| return minTime + " minutes and " + secTime + " seconds"; | |
| } // mili secs min leftover | |
| minTime = ((miliSec - secTime) / 60) % 60; // mili -> second -> | |
| // minutes -> leftovers | |
| long hourTime = ((miliSec - secTime) / 60) - minTime; // mili -> | |
| // second -> | |
| // minutes - | |
| // hours | |
| if (hourTime < 24) { | |
| return hourTime + " hours, " + minTime + " minutes, and " + secTime + " seconds"; | |
| } // days | |
| hourTime = ((miliSec - secTime) / 60) - minTime % 24; | |
| long dayTime = ((((miliSec - secTime) / 60) - minTime) - minTime) / 24; | |
| return dayTime + " days, " + hourTime + " hours, " + minTime + " minutes, and " + secTime + " seconds"; | |
| } | |
| return "a few moments"; | |
| } | |
| @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| public static LinkedHashMap ggLowToHighSort(HashMap map) { | |
| List list = new LinkedList(map.entrySet()); | |
| // Defined Custom Comparator here | |
| Collections.sort(list, new Comparator() { | |
| public int compare(Object o1, Object o2) { | |
| return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); | |
| } | |
| }); | |
| // Here I am copying the sorted list in HashMap | |
| // using LinkedHashMap to preserve the insertion order | |
| LinkedHashMap sortedHashMap = new LinkedHashMap(); | |
| for (Iterator it = list.iterator(); it.hasNext();) { | |
| Map.Entry entry = (Map.Entry) it.next(); | |
| sortedHashMap.put(entry.getKey(), entry.getValue()); | |
| } | |
| return sortedHashMap; | |
| } | |
| @SuppressWarnings({ "unchecked", "rawtypes" }) | |
| public static LinkedHashMap ggHighToLowSort(HashMap map) { | |
| List list = new LinkedList(map.entrySet()); | |
| // Defined Custom Comparator here | |
| Collections.sort(list, new Comparator() { | |
| public int compare(Object o1, Object o2) { | |
| return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue()); | |
| } | |
| }); | |
| // Here I am copying the sorted list in HashMap | |
| // using LinkedHashMap to preserve the insertion order | |
| LinkedHashMap sortedHashMap = new LinkedHashMap(); | |
| for (Iterator it = list.iterator(); it.hasNext();) { | |
| Map.Entry entry = (Map.Entry) it.next(); | |
| sortedHashMap.put(entry.getKey(), entry.getValue()); | |
| } | |
| return sortedHashMap; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment