TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java

444 lines
13 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.util;
2011-10-13 23:07:52 +00:00
2014-08-23 18:19:25 +00:00
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.Field;
2012-11-13 01:42:30 +00:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
2014-08-23 18:19:25 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
2017-10-13 18:38:05 +00:00
import java.util.Iterator;
2014-08-23 18:19:25 +00:00
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
2017-10-13 18:38:05 +00:00
import static me.totalfreedom.totalfreedommod.util.FUtil.CHAT_RAINBOW;
import static me.totalfreedom.totalfreedommod.util.FUtil.FOUNDER;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
2011-10-13 23:07:52 +00:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
2011-10-13 23:07:52 +00:00
public class FUtil
2011-10-13 23:07:52 +00:00
{
2017-10-13 18:38:05 +00:00
private static final Random RANDOM;
public static final String SAVED_FLAGS_FILENAME = "savedflags.dat";
2017-10-13 18:38:05 +00:00
public static final List<String> DEVELOPERS;
public static final List<String> FOUNDER;
public static String DATE_STORAGE_FORMAT;
public static final Map<String, ChatColor> CHAT_COLOR_NAMES;
public static final Map<String, ChatColor> CHAT_RAINBOW_NAMES;
public static List<String> BLOCKED_CODES;
public static final List<ChatColor> CHAT_COLOR_POOL;
public static final List<ChatColor> CHAT_RAINBOW;
private static Iterator<ChatColor> color;
private static final ChatColor[] BLOCKED = new ChatColor[]
{
ChatColor.MAGIC,
ChatColor.STRIKETHROUGH,
ChatColor.ITALIC,
ChatColor.UNDERLINE,
ChatColor.BLACK
};
static {
2017-10-13 18:38:05 +00:00
RANDOM = new Random();
2017-11-11 17:14:01 +00:00
DEVELOPERS = Arrays.asList("Madgeek1450", "Prozza", "WickedGamingUK", "aggelosQQ", "OxLemonxO", "Commodore64x", "Wild1145", "marcocorriero");
2017-10-13 18:38:05 +00:00
FOUNDER = Arrays.asList("markbyron");
FUtil.DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
CHAT_COLOR_NAMES = new HashMap<String, ChatColor>();
CHAT_RAINBOW_NAMES = new HashMap<String, ChatColor>();
FUtil.BLOCKED_CODES = new ArrayList<String>();
CHAT_COLOR_POOL = Arrays.asList(ChatColor.DARK_BLUE, ChatColor.DARK_GREEN, ChatColor.DARK_AQUA, ChatColor.DARK_RED, ChatColor.DARK_PURPLE, ChatColor.GOLD, ChatColor.BLUE, ChatColor.GREEN, ChatColor.AQUA, ChatColor.RED, ChatColor.LIGHT_PURPLE, ChatColor.YELLOW);
CHAT_RAINBOW = Arrays.asList(ChatColor.DARK_RED, ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, ChatColor.GREEN, ChatColor.DARK_GREEN, ChatColor.AQUA, ChatColor.DARK_AQUA, ChatColor.BLUE, ChatColor.DARK_BLUE, ChatColor.DARK_PURPLE, ChatColor.LIGHT_PURPLE);
for (final ChatColor chatColor : FUtil.CHAT_COLOR_POOL) {
FUtil.CHAT_COLOR_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
}
2017-10-13 18:38:05 +00:00
for (final ChatColor chatColor : FUtil.CHAT_RAINBOW) {
FUtil.CHAT_RAINBOW_NAMES.put(chatColor.name().toLowerCase().replace("_", ""), chatColor);
}
FUtil.color = FUtil.CHAT_RAINBOW.iterator();
}
private static final Pattern REGEX = Pattern.compile(ChatColor.COLOR_CHAR + "[" + StringUtils.join(BLOCKED, "") + "]", Pattern.CASE_INSENSITIVE);
2011-10-24 02:43:52 +00:00
private FUtil()
2011-10-24 02:43:52 +00:00
{
}
public static void cancel(BukkitTask task)
{
if (task == null)
{
return;
}
try
{
task.cancel();
}
catch (Exception ex)
{
}
}
2011-10-24 02:43:52 +00:00
public static void bcastMsg(String message, ChatColor color)
2011-10-13 23:07:52 +00:00
{
FLog.info(message, true);
2011-10-13 23:07:52 +00:00
2013-08-14 14:01:42 +00:00
for (Player player : Bukkit.getOnlinePlayers())
2011-10-13 23:07:52 +00:00
{
2013-08-14 14:01:42 +00:00
player.sendMessage((color == null ? "" : color) + message);
2011-10-13 23:07:52 +00:00
}
}
2011-10-24 02:43:52 +00:00
public static void bcastMsg(String message)
2011-10-13 23:07:52 +00:00
{
FUtil.bcastMsg(message, null);
2011-10-13 23:07:52 +00:00
}
2013-01-21 18:58:42 +00:00
// Still in use by listeners
public static void playerMsg(CommandSender sender, String message, ChatColor color)
{
sender.sendMessage(color + message);
}
2013-01-21 18:58:42 +00:00
// Still in use by listeners
public static void playerMsg(CommandSender sender, String message)
{
FUtil.playerMsg(sender, message, ChatColor.GRAY);
}
2015-05-13 12:52:01 +00:00
public static void setFlying(Player player, boolean flying)
{
player.setAllowFlight(true);
player.setFlying(flying);
}
public static void adminAction(String adminName, String action, boolean isRed)
{
FUtil.bcastMsg(adminName + " - " + action, (isRed ? ChatColor.RED : ChatColor.AQUA));
}
public static String formatLocation(Location location)
2011-10-13 23:07:52 +00:00
{
return String.format("%s: (%d, %d, %d)",
location.getWorld().getName(),
Math.round(location.getX()),
Math.round(location.getY()),
Math.round(location.getZ()));
2011-10-13 23:07:52 +00:00
}
2013-09-24 12:05:48 +00:00
public static boolean deleteFolder(final File file)
2011-10-14 05:31:21 +00:00
{
2013-09-24 12:05:48 +00:00
if (file.exists() && file.isDirectory())
2011-10-14 05:31:21 +00:00
{
2013-09-24 12:05:48 +00:00
return FileUtils.deleteQuietly(file);
2011-10-14 05:31:21 +00:00
}
2013-09-24 12:05:48 +00:00
return false;
2011-10-14 05:31:21 +00:00
}
2011-10-24 02:43:52 +00:00
public static void deleteCoreDumps()
{
final File[] coreDumps = new File(".").listFiles(new FileFilter()
{
@Override
2017-10-13 18:38:05 +00:00
public boolean accept(File file)
{
return file.getName().startsWith("java.core");
}
});
for (File dump : coreDumps)
{
FLog.info("Removing core dump file: " + dump.getName());
dump.delete();
}
}
public static Date parseDateOffset(String time)
{
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]*)?)?", Pattern.CASE_INSENSITIVE);
Matcher m = timePattern.matcher(time);
int years = 0;
int months = 0;
int weeks = 0;
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
boolean found = false;
while (m.find())
{
if (m.group() == null || m.group().isEmpty())
{
continue;
}
for (int i = 0; i < m.groupCount(); i++)
{
if (m.group(i) != null && !m.group(i).isEmpty())
{
found = true;
break;
}
}
if (found)
{
if (m.group(1) != null && !m.group(1).isEmpty())
{
years = Integer.parseInt(m.group(1));
}
if (m.group(2) != null && !m.group(2).isEmpty())
{
months = Integer.parseInt(m.group(2));
}
if (m.group(3) != null && !m.group(3).isEmpty())
{
weeks = Integer.parseInt(m.group(3));
}
if (m.group(4) != null && !m.group(4).isEmpty())
{
days = Integer.parseInt(m.group(4));
}
if (m.group(5) != null && !m.group(5).isEmpty())
{
hours = Integer.parseInt(m.group(5));
}
if (m.group(6) != null && !m.group(6).isEmpty())
{
minutes = Integer.parseInt(m.group(6));
}
if (m.group(7) != null && !m.group(7).isEmpty())
{
seconds = Integer.parseInt(m.group(7));
}
break;
}
}
if (!found)
{
return null;
}
Calendar c = new GregorianCalendar();
if (years > 0)
{
c.add(Calendar.YEAR, years);
}
if (months > 0)
{
c.add(Calendar.MONTH, months);
}
if (weeks > 0)
{
c.add(Calendar.WEEK_OF_YEAR, weeks);
}
if (days > 0)
{
c.add(Calendar.DAY_OF_MONTH, days);
}
if (hours > 0)
{
c.add(Calendar.HOUR_OF_DAY, hours);
}
if (minutes > 0)
{
c.add(Calendar.MINUTE, minutes);
}
if (seconds > 0)
{
c.add(Calendar.SECOND, seconds);
}
return c.getTime();
}
2012-09-18 00:13:13 +00:00
public static String playerListToNames(Set<OfflinePlayer> players)
{
List<String> names = new ArrayList<>();
2013-08-14 14:01:42 +00:00
for (OfflinePlayer player : players)
{
names.add(player.getName());
}
return StringUtils.join(names, ", ");
}
2012-11-13 01:42:30 +00:00
public static String dateToString(Date date)
{
return new SimpleDateFormat(DATE_STORAGE_FORMAT, Locale.ENGLISH).format(date);
}
public static Date stringToDate(String dateString)
2012-11-13 01:42:30 +00:00
{
try
{
return new SimpleDateFormat(DATE_STORAGE_FORMAT, Locale.ENGLISH).parse(dateString);
2012-11-13 01:42:30 +00:00
}
catch (ParseException pex)
2012-11-13 01:42:30 +00:00
{
return new Date(0L);
}
}
2012-11-18 03:57:24 +00:00
public static boolean isFromHostConsole(String senderName)
2012-11-18 03:57:24 +00:00
{
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
return ConfigEntry.HOST_SENDER_NAMES.getList().contains(senderName.toLowerCase());
}
public static boolean fuzzyIpMatch(String a, String b, int octets)
{
boolean match = true;
String[] aParts = a.split("\\.");
String[] bParts = b.split("\\.");
if (aParts.length != 4 || bParts.length != 4)
{
return false;
}
if (octets > 4)
{
octets = 4;
}
else if (octets < 1)
{
octets = 1;
}
for (int i = 0; i < octets && i < 4; i++)
{
if (aParts[i].equals("*") || bParts[i].equals("*"))
{
continue;
}
if (!aParts[i].equals(bParts[i]))
{
match = false;
break;
}
}
return match;
}
2014-05-16 13:39:40 +00:00
public static String getFuzzyIp(String ip)
{
final String[] ipParts = ip.split("\\.");
if (ipParts.length == 4)
{
return String.format("%s.%s.*.*", ipParts[0], ipParts[1]);
}
return ip;
}
//getField: Borrowed from WorldEdit
@SuppressWarnings("unchecked")
2017-10-13 18:38:05 +00:00
public static <T> T getField(Object from, String name)
{
Class<?> checkClass = from.getClass();
do
{
try
{
Field field = checkClass.getDeclaredField(name);
field.setAccessible(true);
return (T) field.get(from);
2017-10-13 18:38:05 +00:00
}
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
catch (NoSuchFieldException | IllegalAccessException ex)
{
}
} while (checkClass.getSuperclass() != Object.class
2017-10-13 18:38:05 +00:00
&& ((checkClass = checkClass.getSuperclass()) != null));
return null;
}
2017-10-13 18:38:05 +00:00
public static ChatColor randomChatColor() {
return FUtil.CHAT_COLOR_POOL.get(FUtil.RANDOM.nextInt(FUtil.CHAT_COLOR_POOL.size()));
}
public static ChatColor rainbowChatColor() {
if (FUtil.color.hasNext()) {
return FUtil.color.next();
}
FUtil.color = FUtil.CHAT_RAINBOW.iterator();
return FUtil.color.next();
}
2013-08-12 10:26:49 +00:00
public static String colorize(String string)
2013-08-12 10:26:49 +00:00
{
return ChatColor.translateAlternateColorCodes('&', string);
}
2013-09-24 12:05:48 +00:00
public static String StrictColorize(String string)
{
String string2 = ChatColor.translateAlternateColorCodes('&', string);
final Matcher matcher = REGEX.matcher(string2);
if (matcher.find())
{
final String filteredcolorize = matcher.replaceAll("&");
if(matcher.find(REGEX.matcher(ChatColor.BLACK)))
return filteredcolorize;
}
else
{
return string2;
}
}
public static Date getUnixDate(long unix)
{
return new Date(unix * 1000);
}
public static long getUnixTime()
{
return System.currentTimeMillis() / 1000L;
}
public static long getUnixTime(Date date)
{
if (date == null)
{
return 0;
}
return date.getTime() / 1000L;
}
public static String getNmsVersion()
{
String packageName = Bukkit.getServer().getClass().getPackage().getName();
return packageName.substring(packageName.lastIndexOf('.') + 1);
}
2011-10-13 23:07:52 +00:00
}