162 lines
5.5 KiB
Java
162 lines
5.5 KiB
Java
package com.songoda.ultimatemoderation.utils;
|
|
|
|
import com.songoda.ultimatemoderation.UltimateModeration;
|
|
import org.bukkit.*;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class Methods {
|
|
|
|
private static Map<String, Location> serializeCache = new HashMap<>();
|
|
|
|
public static ItemStack getGlass() {
|
|
UltimateModeration instance = UltimateModeration.getInstance();
|
|
return Methods.getGlass(instance.getConfig().getBoolean("Interfaces.Replace Glass Type 1 With Rainbow Glass"), instance.getConfig().getInt("Interfaces.Glass Type 1"));
|
|
}
|
|
|
|
public static ItemStack getBackgroundGlass(boolean type) {
|
|
UltimateModeration instance = UltimateModeration.getInstance();
|
|
if (type)
|
|
return getGlass(false, instance.getConfig().getInt("Interfaces.Glass Type 2"));
|
|
else
|
|
return getGlass(false, instance.getConfig().getInt("Interfaces.Glass Type 3"));
|
|
}
|
|
|
|
private static ItemStack getGlass(Boolean rainbow, int type) {
|
|
int randomNum = 1 + (int) (Math.random() * 6);
|
|
ItemStack glass;
|
|
if (rainbow) {
|
|
glass = new ItemStack(Material.LEGACY_STAINED_GLASS_PANE, 1, (short) randomNum);
|
|
} else {
|
|
glass = new ItemStack(Material.LEGACY_STAINED_GLASS_PANE, 1, (short) type);
|
|
}
|
|
ItemMeta glassmeta = glass.getItemMeta();
|
|
glassmeta.setDisplayName("§l");
|
|
glass.setItemMeta(glassmeta);
|
|
return glass;
|
|
}
|
|
|
|
public static String formatText(String text) {
|
|
if (text == null || text.equals(""))
|
|
return "";
|
|
return formatText(text, false);
|
|
}
|
|
|
|
public static String formatText(String text, boolean cap) {
|
|
if (text == null || text.equals(""))
|
|
return "";
|
|
if (cap)
|
|
text = text.substring(0, 1).toUpperCase() + text.substring(1);
|
|
return ChatColor.translateAlternateColorCodes('&', text);
|
|
}
|
|
|
|
public static String makeReadable(Long time) {
|
|
if (time == null)
|
|
return "";
|
|
return String.format("%dd %dh %dm",
|
|
TimeUnit.MILLISECONDS.toDays(time),
|
|
TimeUnit.MILLISECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(time)),
|
|
TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time)));
|
|
}
|
|
|
|
|
|
public static long parseTime(String input) {
|
|
long result = 0;
|
|
String number = "";
|
|
for (int i = 0; i < input.length(); i++) {
|
|
char c = input.charAt(i);
|
|
if (Character.isDigit(c)) {
|
|
number += c;
|
|
} else if (Character.isLetter(c) && !number.isEmpty()) {
|
|
result += convert(Integer.parseInt(number), c);
|
|
number = "";
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static long convert(int value, char unit) {
|
|
switch (unit) {
|
|
case 'd':
|
|
return value * 1000 * 60 * 60 * 24;
|
|
case 'h':
|
|
return value * 1000 * 60 * 60;
|
|
case 'm':
|
|
return value * 1000 * 60;
|
|
case 's':
|
|
return value * 1000;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
public static String convertToInvisibleString(String s) {
|
|
if (s == null || s.equals(""))
|
|
return "";
|
|
StringBuilder hidden = new StringBuilder();
|
|
for (char c : s.toCharArray()) hidden.append(ChatColor.COLOR_CHAR + "").append(c);
|
|
return hidden.toString();
|
|
}
|
|
|
|
/**
|
|
* Serializes the location of the block specified.
|
|
*
|
|
* @param b The block whose location is to be saved.
|
|
* @return The serialized data.
|
|
*/
|
|
public static String serializeLocation(Block b) {
|
|
if (b == null)
|
|
return "";
|
|
return serializeLocation(b.getLocation());
|
|
}
|
|
|
|
/**
|
|
* Serializes the location specified.
|
|
*
|
|
* @param location The location that is to be saved.
|
|
* @return The serialized data.
|
|
*/
|
|
public static String serializeLocation(Location location) {
|
|
if (location == null)
|
|
return "";
|
|
String w = location.getWorld().getName();
|
|
double x = location.getX();
|
|
double y = location.getY();
|
|
double z = location.getZ();
|
|
String str = w + ":" + x + ":" + y + ":" + z;
|
|
str = str.replace(".0", "").replace("/", "");
|
|
return str;
|
|
}
|
|
|
|
/**
|
|
* Deserializes a location from the string.
|
|
*
|
|
* @param str The string to parse.
|
|
* @return The location that was serialized in the string.
|
|
*/
|
|
public static Location unserializeLocation(String str) {
|
|
if (str == null || str.equals(""))
|
|
return null;
|
|
if (serializeCache.containsKey(str)) {
|
|
return serializeCache.get(str).clone();
|
|
}
|
|
String cacheKey = str;
|
|
str = str.replace("y:", ":").replace("z:", ":").replace("w:", "").replace("x:", ":").replace("/", ".");
|
|
List<String> args = Arrays.asList(str.split("\\s*:\\s*"));
|
|
|
|
World world = Bukkit.getWorld(args.get(0));
|
|
double x = Double.parseDouble(args.get(1)), y = Double.parseDouble(args.get(2)), z = Double.parseDouble(args.get(3));
|
|
Location location = new Location(world, x, y, z, 0, 0);
|
|
serializeCache.put(cacheKey, location.clone());
|
|
return location;
|
|
}
|
|
|
|
}
|