2016-03-02 19:28:01 +00:00
package me.totalfreedom.totalfreedommod.command ;
2014-08-14 23:07:52 +00:00
2020-07-31 19:46:44 +00:00
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
2014-08-14 23:07:52 +00:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2017-12-23 04:07:36 +00:00
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
2016-03-06 15:56:15 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
2019-07-28 06:04:16 +00:00
import org.apache.commons.lang.StringUtils ;
2014-08-14 23:07:52 +00:00
import org.bukkit.ChatColor ;
2020-07-31 19:46:44 +00:00
import org.bukkit.Color ;
2014-08-14 23:07:52 +00:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2020-09-28 03:17:01 +00:00
@CommandPermissions ( level = Rank . ADMIN , source = SourceType . BOTH )
2020-06-30 07:25:38 +00:00
@CommandParameters ( description = " Essentials Interface Command - Remove illegal chatcodes from nicknames of one or all players on server. " , usage = " /<command> [player] " , aliases = " nc " )
2015-10-19 17:43:46 +00:00
public class Command_nickclean extends FreedomCommand
2014-08-14 23:07:52 +00:00
{
2020-07-31 19:48:22 +00:00
private Map < String , Color > colorCodes = new HashMap < String , Color > ( )
{ {
2020-07-31 19:46:44 +00:00
put ( " &0 " , Color . BLACK ) ;
put ( " &1 " , Color . BLUE ) ;
put ( " &2 " , Color . GREEN ) ;
put ( " &3 " , Color . TEAL ) ;
put ( " &4 " , Color . MAROON ) ;
put ( " &5 " , Color . FUCHSIA ) ;
put ( " &6 " , Color . OLIVE ) ;
put ( " &7 " , Color . SILVER ) ;
put ( " &8 " , Color . GRAY ) ;
put ( " &9 " , Color . NAVY ) ;
put ( " &a " , Color . LIME ) ;
put ( " &b " , Color . AQUA ) ;
put ( " &c " , Color . RED ) ;
put ( " &d " , Color . PURPLE ) ;
put ( " &e " , Color . YELLOW ) ;
put ( " &f " , Color . WHITE ) ;
} } ;
2015-11-22 18:26:47 +00:00
2014-08-14 23:07:52 +00:00
@Override
2015-11-22 18:26:47 +00:00
public boolean run ( CommandSender sender , Player playerSender , Command cmd , String commandLabel , String [ ] args , boolean senderIsConsole )
2014-08-14 23:07:52 +00:00
{
2020-06-30 07:25:38 +00:00
if ( args . length > 1 )
2014-08-14 23:07:52 +00:00
{
2020-06-30 07:25:38 +00:00
Player player = getPlayer ( args [ 0 ] ) ;
if ( player = = null )
2014-08-14 23:07:52 +00:00
{
2020-06-30 07:25:38 +00:00
msg ( PLAYER_NOT_FOUND ) ;
return true ;
2014-08-14 23:07:52 +00:00
}
2020-06-30 07:25:38 +00:00
2020-12-04 00:28:53 +00:00
FUtil . adminAction ( sender . getName ( ) , " Cleaning " + player . getName ( ) + " 's nickname " , false ) ;
2020-06-30 07:25:38 +00:00
cleanNickname ( player ) ;
return true ;
}
2020-12-04 00:28:53 +00:00
FUtil . adminAction ( sender . getName ( ) , " Cleaning all nicknames " , false ) ;
2020-06-30 07:25:38 +00:00
for ( final Player player : server . getOnlinePlayers ( ) )
{
cleanNickname ( player ) ;
2014-08-14 23:07:52 +00:00
}
return true ;
}
2020-06-30 07:25:38 +00:00
public void cleanNickname ( Player player )
{
final String playerName = player . getName ( ) ;
final String nickName = plugin . esb . getNickname ( playerName ) ;
2020-07-31 19:46:44 +00:00
StringBuilder newNick = new StringBuilder ( ) ;
boolean nickChanged = false ;
2020-11-24 16:53:37 +00:00
if ( nickName ! = null )
2020-06-30 07:25:38 +00:00
{
2020-11-24 16:53:37 +00:00
if ( nickName . contains ( " §x " ) )
2020-06-30 07:25:38 +00:00
{
2020-11-24 16:53:37 +00:00
// Detects colors that are similar to blocked codes.
spliterator :
for ( String split : nickName . split ( " §x " ) )
2020-07-31 19:46:44 +00:00
{
2020-11-24 16:53:37 +00:00
List < Color > colors = new ArrayList < > ( ) ;
String hexColorSub = null ;
if ( split . length ( ) > = 12 & & split . contains ( " § " ) )
{
hexColorSub = split . substring ( 0 , 12 ) ;
split = String . valueOf ( split . charAt ( 12 ) ) ;
String hexColorString = " # " + hexColorSub . replaceAll ( " § " , " " ) ;
java . awt . Color hexColor = java . awt . Color . decode ( hexColorString ) ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
// Get a range of nearby colors that are alike to the color blocked.
Color colorFirst ;
Color colorSecond ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
colorFirst = Color . fromRGB ( Math . min ( hexColor . getRed ( ) + 20 , 255 ) , Math . min ( hexColor . getGreen ( ) + 20 , 255 ) , Math . min ( hexColor . getBlue ( ) + 20 , 255 ) ) ;
colorSecond = Color . fromRGB ( Math . max ( hexColor . getRed ( ) - 20 , 0 ) , Math . max ( hexColor . getGreen ( ) - 20 , 0 ) , Math . max ( hexColor . getBlue ( ) - 20 , 0 ) ) ;
colors . addAll ( FUtil . createColorGradient ( colorFirst , colorSecond , 40 ) ) ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
colorFirst = Color . fromRGB ( Math . min ( hexColor . getRed ( ) + 20 , 255 ) , Math . min ( hexColor . getGreen ( ) , 255 ) , Math . min ( hexColor . getBlue ( ) , 255 ) ) ;
colorSecond = Color . fromRGB ( Math . max ( hexColor . getRed ( ) - 20 , 0 ) , Math . max ( hexColor . getGreen ( ) , 0 ) , Math . max ( hexColor . getBlue ( ) , 0 ) ) ;
colors . addAll ( FUtil . createColorGradient ( colorFirst , colorSecond , 40 ) ) ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
colorFirst = Color . fromRGB ( Math . min ( hexColor . getRed ( ) , 255 ) , Math . min ( hexColor . getGreen ( ) + 20 , 255 ) , Math . min ( hexColor . getBlue ( ) , 255 ) ) ;
colorSecond = Color . fromRGB ( Math . max ( hexColor . getRed ( ) , 0 ) , Math . max ( hexColor . getGreen ( ) - 20 , 0 ) , Math . max ( hexColor . getBlue ( ) , 0 ) ) ;
colors . addAll ( FUtil . createColorGradient ( colorFirst , colorSecond , 40 ) ) ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
colorFirst = Color . fromRGB ( Math . min ( hexColor . getRed ( ) , 255 ) , Math . min ( hexColor . getGreen ( ) , 255 ) , Math . min ( hexColor . getBlue ( ) + 20 , 255 ) ) ;
colorSecond = Color . fromRGB ( Math . max ( hexColor . getRed ( ) , 0 ) , Math . max ( hexColor . getGreen ( ) , 0 ) , Math . max ( hexColor . getBlue ( ) - 20 , 0 ) ) ;
colors . addAll ( FUtil . createColorGradient ( colorFirst , colorSecond , 40 ) ) ;
2020-07-31 19:46:44 +00:00
2020-11-24 16:53:37 +00:00
for ( String colorCode : ConfigEntry . BLOCKED_CHATCODES . getString ( ) . split ( " , " ) )
2020-07-31 19:46:44 +00:00
{
2020-11-24 16:53:37 +00:00
// Makes sure that there's hex colors in the split.
for ( Color color : colors )
2020-07-31 19:46:44 +00:00
{
2020-11-24 16:53:37 +00:00
if ( colorCodes . get ( colorCode ) ! = null & & FUtil . colorClose ( color , colorCodes . get ( colorCode ) , 40 ) )
{
nickChanged = true ;
newNick . append ( split ) ;
continue spliterator ;
}
2020-07-31 19:46:44 +00:00
}
2020-11-24 16:53:37 +00:00
}
newNick . append ( " §x " ) . append ( hexColorSub ) . append ( split ) ;
2020-07-31 19:46:44 +00:00
}
}
2020-06-30 07:25:38 +00:00
}
2020-11-24 16:53:37 +00:00
else
2020-07-31 19:46:44 +00:00
{
2020-11-24 16:53:37 +00:00
// Falls back on old code if hex isn't used.
final Pattern REGEX = Pattern . compile ( FUtil . colorize ( ChatColor . COLOR_CHAR + " [ " + StringUtils . join ( ConfigEntry . BLOCKED_CHATCODES . getString ( ) . split ( " , " ) , " " ) + " ] " ) , Pattern . CASE_INSENSITIVE ) ;
if ( ! nickName . isEmpty ( ) & & ! nickName . equalsIgnoreCase ( playerName ) )
2020-07-31 19:46:44 +00:00
{
2020-11-24 16:53:37 +00:00
final Matcher matcher = REGEX . matcher ( nickName ) ;
if ( matcher . find ( ) )
{
nickChanged = true ;
newNick . append ( matcher . replaceAll ( " " ) ) ;
}
2020-07-31 19:46:44 +00:00
}
}
}
2020-11-24 16:53:37 +00:00
if ( nickChanged )
2020-07-31 19:46:44 +00:00
{
msg ( ChatColor . RESET + playerName + " : \" " + nickName + ChatColor . RESET + " \" -> \" " + newNick . toString ( ) + ChatColor . RESET + " \" . " ) ;
}
plugin . esb . setNickname ( playerName , newNick . toString ( ) ) ;
2020-06-30 07:25:38 +00:00
}
2020-07-23 23:05:38 +00:00
}