2015-10-19 17:43:46 +00:00
package me.totalfreedom.totalfreedommod.util ;
2011-10-13 23:07:52 +00:00
2020-04-08 02:20:01 +00:00
import java.io.BufferedReader ;
import java.io.DataOutputStream ;
2014-08-23 18:19:25 +00:00
import java.io.File ;
2020-04-08 02:20:01 +00:00
import java.io.IOException ;
import java.io.InputStreamReader ;
2013-04-10 02:05:24 +00:00
import java.lang.reflect.Field ;
2020-09-12 03:27:26 +00:00
import java.net.HttpURLConnection ;
2020-04-08 02:20:01 +00:00
import java.net.URL ;
2012-11-13 01:42:30 +00:00
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
2021-02-26 21:26:13 +00:00
import java.time.Instant ;
2018-07-31 07:05:28 +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 ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Locale ;
import java.util.Map ;
2020-12-25 19:46:43 +00:00
import java.util.Objects ;
2018-07-31 07:05:28 +00:00
import java.util.Set ;
2021-01-07 19:29:47 +00:00
import java.util.SplittableRandom ;
2020-01-12 14:51:29 +00:00
import java.util.TimeZone ;
2020-04-08 02:20:01 +00:00
import java.util.UUID ;
2012-09-16 21:18:30 +00:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2020-04-22 08:23:51 +00:00
import me.totalfreedom.totalfreedommod.TotalFreedomMod ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
2014-11-29 19:16:00 +00:00
import org.apache.commons.io.FileUtils ;
2019-07-28 06:04:16 +00:00
import org.apache.commons.lang.StringUtils ;
2020-01-25 06:27:16 +00:00
import org.apache.commons.lang.WordUtils ;
2013-11-30 15:32:00 +00:00
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
2020-07-14 00:41:44 +00:00
import org.bukkit.Color ;
2013-11-30 15:32:00 +00:00
import org.bukkit.Location ;
2020-07-01 03:38:29 +00:00
import org.bukkit.Material ;
2013-11-30 15:32:00 +00:00
import org.bukkit.OfflinePlayer ;
2011-10-13 23:07:52 +00:00
import org.bukkit.command.CommandSender ;
2019-12-14 03:17:34 +00:00
import org.bukkit.entity.Entity ;
2013-11-30 15:32:00 +00:00
import org.bukkit.entity.Player ;
2020-07-01 01:51:06 +00:00
import org.bukkit.event.player.PlayerLoginEvent ;
2020-01-08 23:00:15 +00:00
import org.bukkit.inventory.ItemStack ;
import org.bukkit.inventory.meta.ItemMeta ;
2020-07-01 03:38:29 +00:00
import org.bukkit.scheduler.BukkitRunnable ;
2015-11-15 23:32:04 +00:00
import org.bukkit.scheduler.BukkitTask ;
2020-04-08 02:20:01 +00:00
import org.json.simple.JSONArray ;
2020-08-18 02:21:13 +00:00
import static org.bukkit.Bukkit.getServer ;
2011-10-13 23:07:52 +00:00
2015-10-19 17:43:46 +00:00
public class FUtil
2011-10-13 23:07:52 +00:00
{
2017-11-27 07:04:53 +00:00
2016-03-02 19:28:01 +00:00
public static final String SAVED_FLAGS_FILENAME = " savedflags.dat " ;
2020-08-20 01:19:15 +00:00
/ * See https : //github.com/TotalFreedom/License - None of the listed names may be removed.
Leaving this list here for anyone running TFM on a cracked server :
2020-10-02 09:27:06 +00:00
public static final List < String > DEVELOPERS = Arrays . asList ( " Madgeek1450 " , " Prozza " , " WickedGamingUK " , " Wild1145 " , " aggelosQQ " , " scripthead " , " Telesphoreo " , " CoolJWB " ) ;
2020-08-20 01:19:15 +00:00
* /
public static final List < String > DEVELOPERS = Arrays . asList (
" 1156a81a-23fb-435e-9aff-fe9c2ea7e82d " , // Madgeek1450
" f9a1982e-252e-4ed3-92ed-52b0506a39c9 " , // Prozza
" 90eb5d86-ed60-4165-a36e-bb77aa3c6664 " , // WickedGamingUK
" 604cbb51-842d-4b43-8b0a-d1d7c6cd2869 " , // Wild1145
" e67d77c4-fff9-4cea-94cc-9f1f1ab7806b " , // aggelosQQ
" 0061326b-8b3d-44c8-830a-5f2d59f5dc1b " , // scripthead
2020-12-01 00:03:51 +00:00
" 67ce0e28-3d6b-469c-ab71-304eec81b614 " , // CoolJWB
" 03b41e15-d03f-4025-86f5-f1812df200fa " , // elmon_
" d018f2b8-ce60-4672-a45f-e580e0331299 " , // speednt
" 458de06f-36a5-4e1b-aaa6-ec1d1751c5b6 " , // SupItsDillon
2021-01-26 01:36:58 +00:00
" c8e5af82-6aba-4dd7-83e8-474381380cc9 " , // Paldiu
" 38ea7c82-7bdc-4f48-b7fd-0e93fc26813d " , // AwesomePinch
" ba5aafba-9012-418f-9819-a7020d591068 " // TFTWPhoenix
2020-08-20 01:19:15 +00:00
) ;
2021-01-26 01:36:58 +00:00
public static final List < String > DEVELOPER_NAMES = Arrays . asList ( " Madgeek1450 " , " Prozza " , " WickedGamingUK " , " Wild1145 " , " aggelosQQ " , " scripthead " , " CoolJWB " , " elmon_ " , " speednt " , " SupItsDillon " , " Paldiu " , " AwesomePinch " , " TFTWPhoenix " ) ;
2017-11-27 07:04:53 +00:00
public static final Map < String , ChatColor > CHAT_COLOR_NAMES = new HashMap < > ( ) ;
public static final List < ChatColor > CHAT_COLOR_POOL = 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 ) ;
2020-12-25 20:57:10 +00:00
private static final SplittableRandom RANDOM = new SplittableRandom ( ) ;
2020-12-25 19:46:43 +00:00
private static final String CHARACTER_STRING = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ;
private static final Map < Integer , String > TIMEZONE_LOOKUP = new HashMap < > ( ) ;
public static String DATE_STORAGE_FORMAT = " EEE, d MMM yyyy HH:mm:ss Z " ;
2017-11-27 07:04:53 +00:00
static
{
for ( ChatColor chatColor : CHAT_COLOR_POOL )
{
CHAT_COLOR_NAMES . put ( chatColor . name ( ) . toLowerCase ( ) . replace ( " _ " , " " ) , chatColor ) ;
2017-10-13 18:38:05 +00:00
}
2020-01-12 14:51:29 +00:00
for ( int i = - 12 ; i < = 12 ; i + + )
{
String sec = String . valueOf ( i ) . replace ( " - " , " " ) ;
if ( i > - 10 & & i < 10 )
{
sec = " 0 " + sec ;
}
if ( i > = 0 )
{
sec = " + " + sec ;
}
else
{
sec = " - " + sec ;
}
TIMEZONE_LOOKUP . put ( i , " GMT " + sec + " :00 " ) ;
}
2011-11-08 00:29:33 +00:00
}
2011-10-24 02:43:52 +00:00
2015-11-15 23:32:04 +00:00
public static void cancel ( BukkitTask task )
{
if ( task = = null )
{
return ;
}
try
{
task . cancel ( ) ;
}
2020-12-25 19:46:43 +00:00
catch ( Exception ignored )
2015-11-15 23:32:04 +00:00
{
}
}
2018-05-13 19:49:13 +00:00
public static boolean isExecutive ( String name )
{
2021-02-25 21:42:30 +00:00
return ConfigEntry . SERVER_OWNERS . getStringList ( ) . contains ( name ) | | ConfigEntry . SERVER_EXECUTIVES . getStringList ( ) . contains ( name ) | | ConfigEntry . SERVER_ASSTISTANT_EXECUTIVES . getStringList ( ) . contains ( name ) ;
2018-05-13 19:49:13 +00:00
}
2020-08-20 01:19:15 +00:00
public static boolean isDeveloper ( Player player )
2019-08-27 23:25:50 +00:00
{
2021-01-03 18:16:41 +00:00
if ( Bukkit . getOnlineMode ( ) )
{
2021-01-03 18:04:23 +00:00
return DEVELOPERS . contains ( player . getUniqueId ( ) . toString ( ) ) ;
2021-01-03 18:16:41 +00:00
}
else
{
2021-01-03 18:23:10 +00:00
return DEVELOPER_NAMES . contains ( player . getName ( ) ) ;
2021-01-03 18:04:23 +00:00
}
2019-08-27 23:25:50 +00:00
}
2021-01-07 19:29:47 +00:00
2020-08-01 04:10:44 +00:00
public static boolean inDeveloperMode ( )
{
return ConfigEntry . DEVELOPER_MODE . getBoolean ( ) ;
}
2020-01-25 06:27:16 +00:00
public static String formatName ( String name )
{
return WordUtils . capitalizeFully ( name . replace ( " _ " , " " ) ) ;
}
public static String showS ( int count )
{
2020-08-01 04:10:44 +00:00
return ( count = = 1 ? " " : " s " ) ;
2020-01-25 06:27:16 +00:00
}
2019-01-29 04:57:41 +00:00
public static List < String > getPlayerList ( )
{
List < String > names = new ArrayList < > ( ) ;
for ( Player player : Bukkit . getOnlinePlayers ( ) )
{
2020-12-26 23:54:15 +00:00
if ( ! TotalFreedomMod . getPlugin ( ) . al . isVanished ( player . getName ( ) ) )
2020-04-22 08:23:51 +00:00
{
names . add ( player . getName ( ) ) ;
}
2019-01-29 04:57:41 +00:00
}
return names ;
}
2020-05-29 10:14:21 +00:00
public static String listToString ( List < String > list )
{
if ( list . size ( ) = = 0 )
{
return null ;
}
return String . join ( " , " , list ) ;
}
public static List < String > stringToList ( String string )
{
if ( string = = null )
{
return new ArrayList < > ( ) ;
}
return Arrays . asList ( string . split ( " , " ) ) ;
}
2020-07-14 19:00:22 +00:00
/ * *
* A way to get a sublist with a page index and a page size .
2020-11-30 23:17:27 +00:00
*
* @param list A list of objects that should be split into pages .
* @param size The size of the pages .
2020-07-14 19:00:22 +00:00
* @param index The page index , if outside of bounds error will be thrown . The page index starts at 0 as with all lists .
* @return A list of objects that is the page that has been selected from the previous last parameter .
* /
public static List < String > getPageFromList ( List < String > list , int size , int index )
{
try
{
if ( size > = list . size ( ) )
{
return list ;
}
else if ( size * ( index + 1 ) < = list . size ( ) )
{
return list . subList ( size * index , size * ( index + 1 ) ) ;
}
else
{
return list . subList ( size * index , ( size * index ) + ( list . size ( ) % size ) ) ;
}
}
catch ( IndexOutOfBoundsException e )
{
return new ArrayList < > ( ) ;
}
}
2019-01-29 04:57:41 +00:00
public static List < String > getAllMaterialNames ( )
{
List < String > names = new ArrayList < > ( ) ;
for ( Material material : Material . values ( ) )
{
names . add ( material . name ( ) ) ;
}
return names ;
}
2020-12-25 19:46:43 +00:00
@SuppressWarnings ( " unchecked " )
2020-04-08 02:20:01 +00:00
public static UUID nameToUUID ( String name )
{
try
{
JSONArray json = new JSONArray ( ) ;
json . add ( name ) ;
2020-06-30 07:25:38 +00:00
List < String > headers = new ArrayList < > ( ) ;
headers . add ( " Accept:application/json " ) ;
headers . add ( " Content-Type:application/json " ) ;
2020-09-14 09:36:25 +00:00
Response response = sendRequest ( " https://api.mojang.com/profiles/minecraft " , " POST " , headers , json . toString ( ) ) ;
2020-04-08 02:20:01 +00:00
// Don't care how stupid this looks, couldn't find anything to parse a json string to something readable in java with something not horrendously huge, maybe im just retarded
Pattern pattern = Pattern . compile ( " (?<= \" id \" : \" )[a-f0-9].{31} " ) ;
2020-09-14 09:36:25 +00:00
Matcher matcher = pattern . matcher ( response . getMessage ( ) ) ;
2020-04-08 02:20:01 +00:00
if ( matcher . find ( ) )
{
String rawUUID = matcher . group ( 0 ) . replaceFirst ( " ([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]+) " , " $1-$2-$3-$4-$5 " ) ;
return UUID . fromString ( rawUUID ) ;
}
}
catch ( Exception e )
{
FLog . severe ( " Failed to convert name to UUID: \ n " + e . toString ( ) ) ;
}
return null ;
}
2020-11-30 23:17:27 +00:00
public static Response sendRequest ( String endpoint , String method , List < String > headers , String body ) throws IOException
2020-04-08 02:20:01 +00:00
{
URL url = new URL ( endpoint ) ;
2020-09-12 03:27:26 +00:00
HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ( ) ;
2020-09-14 09:36:25 +00:00
2020-06-30 07:25:38 +00:00
connection . setRequestMethod ( method ) ;
2020-09-14 09:36:25 +00:00
if ( headers ! = null )
{
for ( String header : headers )
{
String [ ] kv = header . split ( " : " ) ;
connection . setRequestProperty ( kv [ 0 ] , kv [ 1 ] ) ;
}
}
FLog . info ( connection . getRequestMethod ( ) ) ;
if ( body ! = null )
2020-06-30 07:25:38 +00:00
{
2020-09-14 09:36:25 +00:00
connection . setDoOutput ( true ) ;
DataOutputStream outputStream = new DataOutputStream ( connection . getOutputStream ( ) ) ;
outputStream . writeBytes ( body ) ;
outputStream . flush ( ) ;
outputStream . close ( ) ;
2020-06-30 07:25:38 +00:00
}
2020-09-14 09:36:25 +00:00
2020-04-08 02:20:01 +00:00
BufferedReader in = new BufferedReader ( new InputStreamReader ( connection . getInputStream ( ) ) ) ;
String inputLine ;
2020-12-25 19:46:43 +00:00
StringBuilder response = new StringBuilder ( ) ;
2020-04-08 02:20:01 +00:00
while ( ( inputLine = in . readLine ( ) ) ! = null )
{
response . append ( inputLine ) ;
}
in . close ( ) ;
2020-09-14 09:36:25 +00:00
return new Response ( connection . getResponseCode ( ) , response . toString ( ) ) ;
2020-04-08 02:20:01 +00:00
}
2011-10-24 02:43:52 +00:00
public static void bcastMsg ( String message , ChatColor color )
2011-10-13 23:07:52 +00:00
{
2020-04-08 08:34:08 +00:00
bcastMsg ( message , color , true ) ;
}
public static void bcastMsg ( String message , ChatColor color , Boolean toConsole )
{
if ( toConsole )
{
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
}
}
2020-04-08 08:34:08 +00:00
public static void bcastMsg ( String message , Boolean toConsole )
{
bcastMsg ( message , null , toConsole ) ;
}
2011-10-24 02:43:52 +00:00
public static void bcastMsg ( String message )
2011-10-13 23:07:52 +00:00
{
2020-04-08 08:34:08 +00:00
FUtil . bcastMsg ( message , null , true ) ;
2011-10-13 23:07:52 +00:00
}
2013-01-21 18:58:42 +00:00
// Still in use by listeners
2012-09-14 22:49:44 +00:00
public static void playerMsg ( CommandSender sender , String message , ChatColor color )
{
sender . sendMessage ( color + message ) ;
}
2012-09-16 01:19:55 +00:00
2013-01-21 18:58:42 +00:00
// Still in use by listeners
2012-09-14 22:49:44 +00:00
public static void playerMsg ( CommandSender sender , String message )
{
2015-10-19 17:43:46 +00:00
FUtil . playerMsg ( sender , message , ChatColor . GRAY ) ;
2012-09-14 22:49:44 +00:00
}
2015-05-13 12:52:01 +00:00
public static void setFlying ( Player player , boolean flying )
{
2015-05-12 18:17:38 +00:00
player . setAllowFlight ( true ) ;
player . setFlying ( flying ) ;
}
2020-12-04 00:28:53 +00:00
public static void adminAction ( String adminName , String action , boolean isRed )
2012-09-14 22:49:44 +00:00
{
2020-12-04 00:28:53 +00:00
FUtil . bcastMsg ( adminName + " - " + action , ( isRed ? ChatColor . RED : ChatColor . AQUA ) ) ;
2015-02-16 16:00:38 +00:00
}
2014-04-21 15:45:32 +00:00
2013-08-25 16:32:01 +00:00
public static String formatLocation ( Location location )
2011-10-13 23:07:52 +00:00
{
return String . format ( " %s: (%d, %d, %d) " ,
2020-12-25 19:46:43 +00:00
Objects . requireNonNull ( location . getWorld ( ) ) . getName ( ) ,
2013-08-25 16:32:01 +00:00
Math . round ( location . getX ( ) ) ,
Math . round ( location . getY ( ) ) ,
Math . round ( location . getZ ( ) ) ) ;
2011-10-13 23:07:52 +00:00
}
2018-01-01 03:43:10 +00:00
public static boolean deleteFolder ( 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
2014-05-04 22:01:57 +00:00
public static void deleteCoreDumps ( )
{
2020-12-25 19:46:43 +00:00
final File [ ] coreDumps = new File ( " . " ) . listFiles ( file - > file . getName ( ) . startsWith ( " java.core " ) ) ;
2014-05-04 22:01:57 +00:00
for ( File dump : coreDumps )
{
2015-10-19 17:43:46 +00:00
FLog . info ( " Removing core dump file: " + dump . getName ( ) ) ;
2014-05-04 22:01:57 +00:00
dump . delete ( ) ;
}
}
2021-02-26 21:26:13 +00:00
private static final List < String > regxList = new ArrayList < String > ( ) { {
add ( " y " ) ;
add ( " mo " ) ;
add ( " w " ) ;
add ( " d " ) ;
add ( " h " ) ;
add ( " m " ) ;
add ( " s " ) ;
} } ;
private static long a ( String parse ) {
StringBuilder sb = new StringBuilder ( ) ;
regxList . forEach ( obj - > {
if ( parse . endsWith ( obj ) ) {
sb . append ( parse . split ( obj ) [ 0 ] ) ;
2012-09-16 21:18:30 +00:00
}
2021-02-26 21:26:13 +00:00
} ) ;
return Long . parseLong ( sb . toString ( ) ) ;
}
private static TimeUnit verify ( String arg ) {
TimeUnit unit = null ;
for ( String c : regxList )
{
if ( arg . endsWith ( c ) )
2012-09-16 21:18:30 +00:00
{
2021-02-26 21:26:13 +00:00
switch ( c )
2012-09-16 21:18:30 +00:00
{
2021-02-26 21:26:13 +00:00
case " y " :
unit = ( TimeUnit . YEAR ) ;
break ;
case " mo " :
unit = ( TimeUnit . MONTH ) ;
break ;
case " w " :
unit = ( TimeUnit . WEEK ) ;
break ;
case " d " :
unit = ( TimeUnit . DAY ) ;
break ;
case " h " :
unit = ( TimeUnit . HOUR ) ;
break ;
case " m " :
unit = ( TimeUnit . MINUTE ) ;
break ;
case " s " :
unit = ( TimeUnit . SECOND ) ;
break ;
2012-09-16 21:18:30 +00:00
}
break ;
}
}
2021-02-26 21:26:13 +00:00
return ( unit ! = null ) ? unit : TimeUnit . DAY ;
}
2012-09-16 21:18:30 +00:00
2021-02-26 21:26:13 +00:00
public static Date parseDateOffset ( String . . . time )
{
Instant instant = Instant . now ( ) ;
for ( String arg : time )
2012-09-16 21:18:30 +00:00
{
2021-02-26 21:26:13 +00:00
instant = instant . plusSeconds ( verify ( arg ) . get ( ) * a ( arg ) ) ;
2012-09-16 21:18:30 +00:00
}
2021-02-26 21:26:13 +00:00
return Date . from ( instant ) ;
2012-09-16 21:18:30 +00:00
}
2012-09-17 23:46:59 +00:00
2012-09-18 00:13:13 +00:00
public static String playerListToNames ( Set < OfflinePlayer > players )
2012-09-17 16:29:43 +00:00
{
2016-03-02 19:28:01 +00:00
List < String > names = new ArrayList < > ( ) ;
2013-08-14 14:01:42 +00:00
for ( OfflinePlayer player : players )
2012-09-17 16:29:43 +00:00
{
2013-08-25 16:32:01 +00:00
names . add ( player . getName ( ) ) ;
2012-09-17 16:29:43 +00:00
}
2013-08-25 16:32:01 +00:00
return StringUtils . join ( names , " , " ) ;
2012-09-17 16:29:43 +00:00
}
2012-11-05 03:44:24 +00:00
2012-11-13 01:42:30 +00:00
public static String dateToString ( Date date )
{
return new SimpleDateFormat ( DATE_STORAGE_FORMAT , Locale . ENGLISH ) . format ( date ) ;
}
2013-08-25 16:32:01 +00:00
public static Date stringToDate ( String dateString )
2012-11-13 01:42:30 +00:00
{
try
{
2013-08-25 16:32:01 +00:00
return new SimpleDateFormat ( DATE_STORAGE_FORMAT , Locale . ENGLISH ) . parse ( dateString ) ;
2012-11-13 01:42:30 +00:00
}
2013-08-25 16:32:01 +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
2013-08-25 16:32:01 +00:00
public static boolean isFromHostConsole ( String senderName )
2012-11-18 03:57:24 +00:00
{
2016-05-12 19:40:39 +00:00
return ConfigEntry . HOST_SENDER_NAMES . getList ( ) . contains ( senderName . toLowerCase ( ) ) ;
2012-11-23 02:32:55 +00:00
}
2013-08-25 16:32:01 +00:00
public static boolean fuzzyIpMatch ( String a , String b , int octets )
2012-11-23 02:32:55 +00:00
{
2013-08-25 16:32:01 +00:00
boolean match = true ;
2012-11-23 02:32:55 +00:00
2013-08-25 16:32:01 +00:00
String [ ] aParts = a . split ( " \\ . " ) ;
String [ ] bParts = b . split ( " \\ . " ) ;
2012-11-23 02:32:55 +00:00
2013-08-25 16:32:01 +00:00
if ( aParts . length ! = 4 | | bParts . length ! = 4 )
2012-11-23 02:32:55 +00:00
{
return false ;
}
2013-08-25 16:32:01 +00:00
if ( octets > 4 )
2012-11-23 02:32:55 +00:00
{
2013-08-25 16:32:01 +00:00
octets = 4 ;
2012-11-23 02:32:55 +00:00
}
2013-08-25 16:32:01 +00:00
else if ( octets < 1 )
2012-11-23 02:32:55 +00:00
{
2013-08-25 16:32:01 +00:00
octets = 1 ;
2012-11-23 02:32:55 +00:00
}
2020-12-25 19:46:43 +00:00
for ( int i = 0 ; i < octets ; i + + )
2012-11-23 02:32:55 +00:00
{
2013-08-25 16:32:01 +00:00
if ( aParts [ i ] . equals ( " * " ) | | bParts [ i ] . equals ( " * " ) )
2012-11-23 02:32:55 +00:00
{
continue ;
}
2013-08-25 16:32:01 +00:00
if ( ! aParts [ i ] . equals ( bParts [ i ] ) )
2012-11-23 02:32:55 +00:00
{
2013-08-25 16:32:01 +00:00
match = false ;
2012-11-23 02:32:55 +00:00
break ;
}
}
2013-08-25 16:32:01 +00:00
return match ;
2012-11-21 01:29:57 +00:00
}
2012-12-01 20:11:00 +00:00
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 ;
}
2013-04-10 02:05:24 +00:00
//getField: Borrowed from WorldEdit
@SuppressWarnings ( " unchecked " )
2017-11-27 07:04:53 +00:00
public static < T > T getField ( Object from , String name )
2013-04-10 02:05:24 +00:00
{
Class < ? > checkClass = from . getClass ( ) ;
do
{
try
{
Field field = checkClass . getDeclaredField ( name ) ;
field . setAccessible ( true ) ;
2018-07-31 07:01:29 +00:00
return ( T ) field . get ( from ) ;
2014-05-11 16:41:58 +00:00
2017-11-27 07:04:53 +00:00
}
2020-12-25 19:46:43 +00:00
catch ( NoSuchFieldException | IllegalAccessException ignored )
2013-04-10 02:05:24 +00:00
{
}
2018-07-31 06:41:56 +00:00
}
while ( checkClass . getSuperclass ( ) ! = Object . class
2017-11-27 07:04:53 +00:00
& & ( ( checkClass = checkClass . getSuperclass ( ) ) ! = null ) ) ;
2014-05-11 16:41:58 +00:00
2013-04-10 02:05:24 +00:00
return null ;
}
2013-07-11 01:46:29 +00:00
2017-11-27 07:04:53 +00:00
public static ChatColor randomChatColor ( )
{
return CHAT_COLOR_POOL . get ( RANDOM . nextInt ( CHAT_COLOR_POOL . size ( ) ) ) ;
2017-10-13 18:38:05 +00:00
}
2017-12-23 04:07:36 +00:00
2017-11-27 07:04:53 +00:00
public static String rainbowify ( String string )
{
2020-12-25 19:46:43 +00:00
Iterator < ChatColor > CHAT_COLOR_ITERATOR = CHAT_COLOR_POOL . iterator ( ) ;
2017-12-23 04:07:36 +00:00
2018-01-01 03:43:10 +00:00
StringBuilder newString = new StringBuilder ( ) ;
char [ ] chars = string . toCharArray ( ) ;
2017-12-23 04:07:36 +00:00
2017-11-27 07:04:53 +00:00
for ( char c : chars )
{
if ( ! CHAT_COLOR_ITERATOR . hasNext ( ) )
{
CHAT_COLOR_ITERATOR = CHAT_COLOR_POOL . iterator ( ) ; //Restart from first colour if there are no more colours in iterator.
}
newString . append ( CHAT_COLOR_ITERATOR . next ( ) ) . append ( c ) ;
2017-10-13 18:38:05 +00:00
}
2017-12-23 04:07:36 +00:00
2017-11-27 07:04:53 +00:00
return newString . toString ( ) ;
2013-07-04 16:10:08 +00:00
}
2013-08-12 10:26:49 +00:00
2013-09-21 17:58:16 +00:00
public static String colorize ( String string )
2013-08-12 10:26:49 +00:00
{
2020-07-18 23:51:17 +00:00
if ( string ! = null )
2020-06-30 07:25:38 +00:00
{
2020-07-18 23:51:17 +00:00
Matcher matcher = Pattern . compile ( " &#[a-f0-9A-F]{6} " ) . matcher ( string ) ;
while ( matcher . find ( ) )
{
String code = matcher . group ( ) . replace ( " & " , " " ) ;
string = string . replace ( " & " + code , net . md_5 . bungee . api . ChatColor . of ( code ) + " " ) ;
}
2020-06-30 07:25:38 +00:00
2020-07-18 23:51:17 +00:00
string = ChatColor . translateAlternateColorCodes ( '&' , string ) ;
}
2020-06-30 07:25:38 +00:00
return string ;
2013-08-12 10:26:49 +00:00
}
2013-09-24 12:05:48 +00:00
2015-11-22 18:26:47 +00:00
public static Date getUnixDate ( long unix )
2014-04-04 14:48:39 +00:00
{
2015-11-22 18:26:47 +00:00
return new Date ( unix * 1000 ) ;
2014-04-04 14:48:39 +00:00
}
2015-11-22 18:26:47 +00:00
public static long getUnixTime ( )
2014-04-14 19:11:41 +00:00
{
2015-11-22 18:26:47 +00:00
return System . currentTimeMillis ( ) / 1000L ;
2014-04-14 19:11:41 +00:00
}
public static long getUnixTime ( Date date )
{
if ( date = = null )
{
return 0 ;
}
return date . getTime ( ) / 1000L ;
}
2017-12-28 05:50:39 +00:00
public static String getNMSVersion ( )
2014-05-04 22:01:57 +00:00
{
2020-08-18 02:21:13 +00:00
String packageName = getServer ( ) . getClass ( ) . getPackage ( ) . getName ( ) ;
2014-05-04 22:01:57 +00:00
return packageName . substring ( packageName . lastIndexOf ( '.' ) + 1 ) ;
2014-07-30 02:38:08 +00:00
}
2018-01-14 21:53:05 +00:00
2020-04-08 08:34:08 +00:00
public static int randomInteger ( int min , int max )
2018-01-14 21:53:05 +00:00
{
int range = max - min + 1 ;
2020-12-25 19:46:43 +00:00
return ( int ) ( Math . random ( ) * range ) + min ;
2018-01-14 21:53:05 +00:00
}
2019-12-11 01:13:47 +00:00
2020-04-08 08:34:08 +00:00
public static String randomString ( int length )
{
2020-08-08 05:51:09 +00:00
String characters = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789-_=+[]{};:,.<>~ " ;
2020-12-25 19:46:43 +00:00
StringBuilder randomString = new StringBuilder ( ) ;
2020-04-08 08:34:08 +00:00
for ( int i = 0 ; i < length ; i + + )
{
2020-08-08 05:51:09 +00:00
int selectedCharacter = randomInteger ( 1 , characters . length ( ) ) - 1 ;
2020-12-25 19:46:43 +00:00
randomString . append ( characters . charAt ( selectedCharacter ) ) ;
2020-08-08 05:51:09 +00:00
}
2020-12-25 19:46:43 +00:00
return randomString . toString ( ) ;
2020-04-08 08:34:08 +00:00
2020-08-08 05:51:09 +00:00
}
public static String randomAlphanumericString ( int length )
{
String characters = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789 " ;
2020-12-25 19:46:43 +00:00
StringBuilder randomString = new StringBuilder ( ) ;
2020-08-08 05:51:09 +00:00
for ( int i = 0 ; i < length ; i + + )
{
2020-04-08 08:34:08 +00:00
int selectedCharacter = randomInteger ( 1 , characters . length ( ) ) - 1 ;
2020-12-25 19:46:43 +00:00
randomString . append ( characters . charAt ( selectedCharacter ) ) ;
2020-04-08 08:34:08 +00:00
}
2020-12-25 19:46:43 +00:00
return randomString . toString ( ) ;
2020-04-08 08:34:08 +00:00
}
2019-12-11 01:13:47 +00:00
public static boolean isPaper ( )
{
try
{
Class . forName ( " com.destroystokyo.paper.PaperConfig " ) ;
return true ;
}
2021-02-26 21:26:13 +00:00
catch ( ClassNotFoundException ignored )
2019-12-11 01:13:47 +00:00
{
return false ;
}
}
2019-12-14 03:17:34 +00:00
public static void fixCommandVoid ( Player player )
{
for ( Player p : Bukkit . getOnlinePlayers ( ) )
{
for ( Entity passengerEntity : p . getPassengers ( ) )
{
if ( passengerEntity = = player )
{
p . removePassenger ( passengerEntity ) ;
}
}
}
}
2020-01-07 20:13:59 +00:00
public static char getRandomCharacter ( )
{
2020-12-25 20:57:10 +00:00
return CHARACTER_STRING . charAt ( new SplittableRandom ( ) . nextInt ( CHARACTER_STRING . length ( ) ) ) ;
2020-01-07 20:13:59 +00:00
}
2020-01-08 23:00:15 +00:00
public static void give ( Player player , Material material , String coloredName , int amount , String . . . lore )
{
ItemStack stack = new ItemStack ( material , amount ) ;
ItemMeta meta = stack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert meta ! = null ;
2020-01-08 23:00:15 +00:00
meta . setDisplayName ( FUtil . colorize ( coloredName ) ) ;
List < String > loreList = new ArrayList < > ( ) ;
for ( String entry : lore )
{
loreList . add ( FUtil . colorize ( entry ) ) ;
}
meta . setLore ( loreList ) ;
stack . setItemMeta ( meta ) ;
player . getInventory ( ) . setItem ( player . getInventory ( ) . firstEmpty ( ) , stack ) ;
}
public static Player getRandomPlayer ( )
{
List < Player > players = new ArrayList < > ( Bukkit . getOnlinePlayers ( ) ) ;
2020-04-08 08:34:08 +00:00
return players . get ( randomInteger ( 0 , players . size ( ) - 1 ) ) ;
2020-01-08 23:00:15 +00:00
}
2020-01-12 14:51:29 +00:00
// convert the current time
public static int getTimeInTicks ( int tz )
{
if ( timeZoneOutOfBounds ( tz ) )
{
return - 1 ;
}
Calendar date = Calendar . getInstance ( TimeZone . getTimeZone ( TIMEZONE_LOOKUP . get ( tz ) ) ) ;
int res = 0 ;
for ( int i = 0 ; i < date . get ( Calendar . HOUR_OF_DAY ) - 6 ; i + + ) // oh yeah i don't know why this is 6 hours ahead
{
res + = 1000 ;
}
int addExtra = 0 ; // we're adding extra to account for repeating decimals
for ( int i = 0 ; i < date . get ( Calendar . MINUTE ) ; i + + )
{
res + = 16 ;
addExtra + + ;
if ( addExtra = = 3 )
{
res + = 1 ;
addExtra = 0 ;
}
}
// this is the best it can be. trust me.
return res ;
}
public static boolean timeZoneOutOfBounds ( int tz )
{
return tz < - 12 | | tz > 12 ;
}
2020-04-08 02:20:01 +00:00
2020-07-01 01:51:06 +00:00
public static String getIp ( Player player )
{
2020-12-25 19:46:43 +00:00
return Objects . requireNonNull ( player . getAddress ( ) ) . getAddress ( ) . getHostAddress ( ) . trim ( ) ;
2020-07-01 01:51:06 +00:00
}
public static String getIp ( PlayerLoginEvent event )
{
return event . getAddress ( ) . getHostAddress ( ) . trim ( ) ;
}
2020-07-14 00:41:44 +00:00
private static Color interpolateColor ( Color c1 , Color c2 , double factor )
{
long [ ] c1values = { c1 . getRed ( ) , c1 . getGreen ( ) , c1 . getBlue ( ) } ;
long [ ] c2values = { c2 . getRed ( ) , c2 . getGreen ( ) , c2 . getBlue ( ) } ;
for ( int i = 0 ; i < 3 ; i + + )
{
c1values [ i ] = Math . round ( c1values [ i ] + factor * ( c2values [ i ] - c1values [ i ] ) ) ;
}
2020-11-30 23:17:27 +00:00
return Color . fromRGB ( ( int ) c1values [ 0 ] , ( int ) c1values [ 1 ] , ( int ) c1values [ 2 ] ) ;
2020-07-14 00:41:44 +00:00
}
2020-08-01 04:10:44 +00:00
public static boolean isValidIPv4 ( String ip )
{
2020-12-25 19:46:43 +00:00
return ! ip . matches ( " ^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])) \\ .([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])) \\ .([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])) \\ .([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5]))$ " )
& & ! ip . matches ( " ^([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])) \\ .([0-9]|[1-9][0-9]|1([0-9][0-9])|2([0-4][0-9]|5[0-5])) \\ .([*]) \\ .([*])$ " ) ;
2020-08-01 04:10:44 +00:00
}
2020-07-14 00:41:44 +00:00
public static List < Color > createColorGradient ( Color c1 , Color c2 , int steps )
{
double factor = 1 . 0 / ( steps - 1 . 0 ) ;
List < Color > colors = new ArrayList < > ( ) ;
for ( int i = 0 ; i < steps ; i + + )
{
colors . add ( interpolateColor ( c1 , c2 , factor * i ) ) ;
}
return colors ;
}
2020-07-31 19:46:44 +00:00
public static boolean colorClose ( Color first , Color second , int tresHold )
{
int redDelta = Math . abs ( first . getRed ( ) - second . getRed ( ) ) ;
int greenDelta = Math . abs ( first . getGreen ( ) - second . getGreen ( ) ) ;
int blueDelta = Math . abs ( first . getBlue ( ) - second . getBlue ( ) ) ;
return ( redDelta + greenDelta + blueDelta ) < tresHold ;
}
2020-07-14 00:41:44 +00:00
public static Color fromAWT ( java . awt . Color color )
{
return Color . fromRGB ( color . getRed ( ) , color . getGreen ( ) , color . getBlue ( ) ) ;
}
public static java . awt . Color toAWT ( Color color )
{
return new java . awt . Color ( color . getRed ( ) , color . getGreen ( ) , color . getBlue ( ) ) ;
}
2020-07-15 04:23:26 +00:00
public static java . awt . Color getRandomAWTColor ( )
{
return new java . awt . Color ( randomInteger ( 0 , 255 ) , randomInteger ( 0 , 255 ) , randomInteger ( 0 , 255 ) ) ;
}
public static String getHexStringOfAWTColor ( java . awt . Color color )
{
String hex = Integer . toHexString ( color . getRGB ( ) & 0xFFFFFF ) ;
if ( hex . length ( ) < 6 )
{
hex = " 0 " + hex ;
}
return " # " + hex ;
}
2020-07-01 03:38:29 +00:00
public static void createExplosionOnDelay ( Location location , float power , int delay )
{
new BukkitRunnable ( )
{
@Override
public void run ( )
{
2020-12-25 19:46:43 +00:00
Objects . requireNonNull ( location . getWorld ( ) ) . createExplosion ( location , power ) ;
2020-07-01 03:38:29 +00:00
}
} . runTaskLater ( TotalFreedomMod . getPlugin ( ) , delay ) ;
}
2020-11-30 23:17:27 +00:00
public static class PaginationList < T > extends ArrayList < T >
{
private final int epp ;
public PaginationList ( int epp )
{
super ( ) ;
this . epp = epp ;
}
@SafeVarargs
public PaginationList ( int epp , T . . . elements )
{
super ( Arrays . asList ( elements ) ) ;
this . epp = epp ;
}
public int getPageCount ( )
{
return ( int ) Math . ceil ( ( double ) size ( ) / ( double ) epp ) ;
}
public List < T > getPage ( int page )
{
if ( page < 1 | | page > getPageCount ( ) )
{
return null ;
}
int startIndex = ( page - 1 ) * epp ;
int endIndex = Math . min ( startIndex + ( epp - 1 ) , this . size ( ) - 1 ) ;
return subList ( startIndex , endIndex + 1 ) ;
}
}
2020-12-15 20:53:07 +00:00
}