193 lines
6.8 KiB
Java
193 lines
6.8 KiB
Java
package com.songoda.ultimatemoderation.utils;
|
|
|
|
import com.songoda.core.compatibility.ServerVersion;
|
|
import com.songoda.ultimatemoderation.UltimateModeration;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.ChatColor;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.World;
|
|
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(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ?
|
|
Material.LEGACY_STAINED_GLASS_PANE : Material.valueOf("STAINED_GLASS_PANE"), 1, (short) randomNum);
|
|
} else {
|
|
glass = new ItemStack(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13) ?
|
|
Material.LEGACY_STAINED_GLASS_PANE : Material.valueOf("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 formatTitle(String text) {
|
|
if (text == null || text.equals(""))
|
|
return "";
|
|
if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
|
|
if (text.length() > 31)
|
|
text = text.substring(0, 29) + "...";
|
|
}
|
|
text = formatText(text);
|
|
return text;
|
|
}
|
|
|
|
public static String makeReadable(Long time) {
|
|
if (time == null)
|
|
return "";
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
long days = TimeUnit.MILLISECONDS.toDays(time);
|
|
long hours = TimeUnit.MILLISECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(time));
|
|
long minutes = TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time));
|
|
long seconds = TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time));
|
|
|
|
if (days != 0L)
|
|
sb.append(" ").append(days).append("d");
|
|
if (hours != 0L)
|
|
sb.append(" ").append(hours).append("h");
|
|
if (minutes != 0L)
|
|
sb.append(" ").append(minutes).append("m");
|
|
if (seconds != 0L)
|
|
sb.append(" ").append(seconds).append("s");
|
|
return sb.toString().trim();
|
|
}
|
|
|
|
|
|
public static long parseTime(String input) {
|
|
long result = 0;
|
|
StringBuilder number = new StringBuilder();
|
|
for (int i = 0; i < input.length(); i++) {
|
|
char c = input.charAt(i);
|
|
if (Character.isDigit(c)) {
|
|
number.append(c);
|
|
} else if (Character.isLetter(c) && (number.length() > 0)) {
|
|
result += convert(Integer.parseInt(number.toString()), c);
|
|
number = new StringBuilder();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static long convert(long 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 || location.getWorld() == 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;
|
|
}
|
|
|
|
}
|