2022-04-24 13:51:12 +00:00
package dev.plex.command.impl ;
import dev.plex.command.SunburstCommand ;
import dev.plex.command.util.CommandInfo ;
import dev.plex.command.util.CommandPerms ;
import dev.plex.player.ISunburstPlayer ;
import dev.plex.util.ComponentUtil ;
2022-04-24 23:34:42 +00:00
import dev.plex.util.MojangUtils ;
2022-04-24 13:51:12 +00:00
import net.kyori.adventure.text.Component ;
import net.kyori.adventure.text.minimessage.MiniMessage ;
2022-04-24 23:34:42 +00:00
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer ;
import org.apache.commons.lang.StringUtils ;
2022-04-24 13:51:12 +00:00
import org.bukkit.Bukkit ;
import org.bukkit.command.CommandSender ;
import org.bukkit.command.ConsoleCommandSender ;
import org.bukkit.entity.Player ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
@CommandInfo ( name = " nickname " , description = " Manages your chat display name / nick name " , usage = " /<command> [player] [new nickname | off] " , aliases = { " nick " } )
@CommandPerms ( permission = " sunburst.command.nickname " )
public class NicknameCMD extends SunburstCommand
{
@Override
public Component execute ( @NotNull CommandSender sender , @Nullable Player player , String [ ] args )
{
if ( args . length = = 0 )
{
if ( sender instanceof ConsoleCommandSender )
{
return confMsg ( " commandCheckConsole " , this . getUsage ( ) ) ;
}
2022-04-24 14:23:59 +00:00
ISunburstPlayer sunburstPlayer = plugin . getObjectHolder ( ) . getStorageSystem ( ) . getPlayer ( player . getUniqueId ( ) ) ;
2022-04-24 13:51:12 +00:00
if ( sunburstPlayer . displayName ( ) = = null )
{
return confMsg ( " nicknameNotFound " ) ;
}
2022-04-24 23:34:42 +00:00
return confMsg ( " nickname " , ComponentUtil . REGULAR_TAGS , ComponentUtil . mmCustom ( sunburstPlayer . displayName ( ) , ComponentUtil . REGULAR_TAGS ) ) ;
2022-04-24 13:51:12 +00:00
}
if ( Bukkit . getOnlinePlayers ( ) . stream ( ) . anyMatch ( p - > p . getName ( ) . equalsIgnoreCase ( args [ 0 ] ) ) )
{
2022-04-25 03:02:39 +00:00
if ( ! sender . hasPermission ( getPermission ( ) + " .other " ) )
{
return confMsg ( " noPermission " , getPermission ( ) + " .other " ) ;
}
2022-04-24 13:51:12 +00:00
if ( args . length < 2 )
{
return usage ( ) ;
}
2022-04-25 03:02:39 +00:00
Player target = getNonNullPlayer ( args [ 0 ] ) ;
ISunburstPlayer sunburstPlayer = plugin . getObjectHolder ( ) . getStorageSystem ( ) . getPlayer ( target . getUniqueId ( ) ) ;
if ( args [ 1 ] . equalsIgnoreCase ( " off " ) | | args [ 1 ] . equalsIgnoreCase ( " clear " ) )
2022-04-24 13:51:12 +00:00
{
sunburstPlayer . displayName ( null ) ;
target . sendMessage ( confMsg ( " nicknameRemoved " ) ) ;
return confMsg ( " nicknameRemovedOther " , target . getName ( ) ) ;
}
2022-04-24 23:34:42 +00:00
String nickname = StringUtils . join ( args , " " , 1 , args . length ) ;
2022-04-24 13:51:12 +00:00
nickname = nickname . replace ( " <newline> " , " " ) . replace ( " <br> " , " " ) ;
Component newNickname = MiniMessage . miniMessage ( ) . deserialize ( nickname , ComponentUtil . REGULAR_TAGS ) ;
2022-04-24 23:34:42 +00:00
String rawNick = PlainTextComponentSerializer . plainText ( ) . serialize ( newNickname ) ;
if ( ! rawNick . equalsIgnoreCase ( target . getName ( ) ) & & Bukkit . getOfflinePlayer ( rawNick ) . hasPlayedBefore ( ) )
{
if ( Bukkit . getOfflinePlayer ( nickname ) . hasPlayedBefore ( ) )
{
return confMsg ( " nicknameMatchesPlayer " ) ;
}
}
2022-04-24 13:51:12 +00:00
sunburstPlayer . displayName ( newNickname ) ;
2022-04-24 23:34:42 +00:00
target . sendMessage ( confMsg ( " nicknameSet " , ComponentUtil . REGULAR_TAGS , ComponentUtil . mmCustom ( newNickname , ComponentUtil . REGULAR_TAGS ) ) ) ;
return confMsg ( " nicknameSetOther " , ComponentUtil . REGULAR_TAGS , target . getName ( ) , ComponentUtil . mmCustom ( newNickname , ComponentUtil . REGULAR_TAGS ) ) ;
2022-04-24 13:51:12 +00:00
}
2022-04-25 03:02:39 +00:00
if ( args [ 0 ] . equalsIgnoreCase ( " off " ) | | args [ 0 ] . equalsIgnoreCase ( " clear " ) )
2022-04-24 13:51:12 +00:00
{
if ( sender instanceof ConsoleCommandSender )
{
return confMsg ( " commandCheckConsole " , this . getUsage ( ) ) ;
}
2022-04-24 14:23:59 +00:00
ISunburstPlayer sunburstPlayer = plugin . getObjectHolder ( ) . getStorageSystem ( ) . getPlayer ( player . getUniqueId ( ) ) ;
2022-04-24 13:51:12 +00:00
sunburstPlayer . displayName ( null ) ;
return confMsg ( " nicknameRemoved " ) ;
}
if ( sender instanceof ConsoleCommandSender )
{
return confMsg ( " commandCheckConsole " , this . getUsage ( ) ) ;
}
2022-04-24 14:23:59 +00:00
ISunburstPlayer sunburstPlayer = plugin . getObjectHolder ( ) . getStorageSystem ( ) . getPlayer ( player . getUniqueId ( ) ) ;
2022-04-24 23:34:42 +00:00
String nickname = StringUtils . join ( args , " " ) ;
2022-04-24 13:51:12 +00:00
nickname = nickname . replace ( " <newline> " , " " ) . replace ( " <br> " , " " ) ;
Component newNickname = MiniMessage . miniMessage ( ) . deserialize ( nickname , ComponentUtil . REGULAR_TAGS ) ;
2022-04-24 23:34:42 +00:00
String rawNick = PlainTextComponentSerializer . plainText ( ) . serialize ( newNickname ) ;
if ( ! rawNick . equalsIgnoreCase ( player . getName ( ) ) & & MojangUtils . getInfo ( rawNick ) ! = null )
{
return confMsg ( " nicknameMatchesPlayer " ) ;
}
2022-04-24 13:51:12 +00:00
sunburstPlayer . displayName ( newNickname ) ;
2022-04-24 23:34:42 +00:00
return confMsg ( " nicknameSet " , ComponentUtil . REGULAR_TAGS , ComponentUtil . mmCustom ( newNickname , ComponentUtil . REGULAR_TAGS ) ) ;
2022-04-24 13:51:12 +00:00
}
}