2022-07-20 13:18:57 +02:00
package fr.pandacube.lib.chat ;
2020-11-02 23:23:41 +01:00
import java.util.Objects ;
2021-07-09 00:27:59 +02:00
import net.kyori.adventure.text.Component ;
2022-07-21 02:19:28 +02:00
import net.kyori.adventure.text.ComponentLike ;
2021-09-05 20:06:07 +02:00
import net.kyori.adventure.text.format.NamedTextColor ;
2021-07-20 00:19:17 +02:00
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer ;
2020-11-02 23:23:41 +01:00
import net.md_5.bungee.api.chat.BaseComponent ;
2022-07-20 13:18:57 +02:00
import fr.pandacube.lib.chat.Chat.FormatableChat ;
2020-11-02 23:23:41 +01:00
public abstract class ChatStatic {
2021-07-09 00:27:59 +02:00
2022-07-21 02:19:28 +02:00
private static FormatableChat chatComponent ( Component c ) {
2021-07-19 01:27:50 +02:00
return new FormatableChat ( Chat . componentToBuilder ( c ) ) ;
2020-11-02 23:23:41 +01:00
}
2021-07-09 00:27:59 +02:00
public static FormatableChat chatComponent ( BaseComponent c ) {
2021-07-19 01:27:50 +02:00
return new FormatableChat ( Chat . componentToBuilder ( Chat . toAdventure ( c ) ) ) ;
2021-07-09 00:27:59 +02:00
}
2021-08-08 12:55:51 +02:00
2022-07-21 02:19:28 +02:00
public static FormatableChat chatComponent ( ComponentLike c ) {
return chatComponent ( c . asComponent ( ) ) ;
2021-08-08 12:55:51 +02:00
}
2020-11-02 23:23:41 +01:00
public static FormatableChat chat ( ) {
2021-07-25 20:12:51 +02:00
return new FormatableChat ( Component . text ( ) ) ;
2020-11-02 23:23:41 +01:00
}
public static FormatableChat chatComponent ( BaseComponent [ ] c ) {
2021-07-09 00:27:59 +02:00
return chatComponent ( Chat . toAdventure ( c ) ) ;
2020-11-02 23:23:41 +01:00
}
2022-07-21 02:19:28 +02:00
/ * *
* Create a Chat instance with the provided plain text as its main text content .
*
* @param plainText the text to use as he content of the new Chat instance .
* @return a Chat instance with the provided text as its main text content .
2022-07-28 01:11:28 +02:00
* @throws IllegalArgumentException if the { @code plainText } parameter is instance of { @link Chat } or
2022-07-21 02:19:28 +02:00
* { @link Component } . The caller should use { @link # chatComponent ( ComponentLike ) }
* instead .
* /
2020-11-02 23:23:41 +01:00
public static FormatableChat text ( Object plainText ) {
2022-07-21 02:19:28 +02:00
if ( plainText instanceof ComponentLike ) {
throw new IllegalArgumentException ( " Expected any object except instance of " + ComponentLike . class + " . Received " + plainText + " . Please use ChatStatic.chatComponent(ComponentLike) instead. " ) ;
2021-07-25 20:12:51 +02:00
}
return new FormatableChat ( Component . text ( ) . content ( Objects . toString ( plainText ) ) ) ;
2020-11-02 23:23:41 +01:00
}
2022-07-21 02:19:28 +02:00
/ * *
* Create a Chat instance with the provided legacy text as its main text content .
*
* @param legacyText the text to use as he content of the new Chat instance .
* @return a Chat instance with the provided text as its main text content .
* @throws IllegalArgumentException If the { @code plainText } parameter is instance of { @link Chat } or
* { @link Component } . The caller should use { @link # chatComponent ( ComponentLike ) }
* instead .
* /
2020-11-02 23:23:41 +01:00
public static FormatableChat legacyText ( Object legacyText ) {
2022-07-21 02:19:28 +02:00
if ( legacyText instanceof ComponentLike ) {
throw new IllegalArgumentException ( " Expected any object except instance of " + ComponentLike . class + " . Received " + legacyText + " . Please use ChatStatic.chatComponent(ComponentLike) instead. " ) ;
2021-07-25 20:12:51 +02:00
}
2021-07-20 00:19:17 +02:00
return chatComponent ( LegacyComponentSerializer . legacySection ( ) . deserialize ( Objects . toString ( legacyText ) ) ) ;
2020-11-02 23:23:41 +01:00
}
public static FormatableChat infoText ( Object plainText ) {
return text ( plainText ) . infoColor ( ) ;
}
2022-05-25 12:56:29 +02:00
public static FormatableChat warningText ( Object plainText ) {
return text ( plainText ) . warningColor ( ) ;
}
2020-11-02 23:23:41 +01:00
public static FormatableChat dataText ( Object plainText ) {
return text ( plainText ) . dataColor ( ) ;
}
public static FormatableChat decorationText ( Object plainText ) {
return text ( plainText ) . decorationColor ( ) ;
}
public static FormatableChat successText ( Object plainText ) {
return text ( plainText ) . successColor ( ) ;
}
public static FormatableChat failureText ( Object plainText ) {
return text ( plainText ) . failureColor ( ) ;
}
public static FormatableChat playerNameText ( String legacyText ) {
2021-09-05 20:06:07 +02:00
FormatableChat fc = legacyText ( legacyText ) ;
fc . builder . colorIfAbsent ( NamedTextColor . WHITE ) ;
return fc ;
2020-11-02 23:23:41 +01:00
}
2021-07-18 22:56:21 +02:00
public static FormatableChat playerNameComponent ( Component c ) {
FormatableChat fc = chatComponent ( c ) ;
2021-09-05 20:06:07 +02:00
fc . builder . colorIfAbsent ( NamedTextColor . WHITE ) ;
2021-07-18 22:56:21 +02:00
return fc ;
}
2020-11-02 23:23:41 +01:00
public static FormatableChat translation ( String key , Object . . . with ) {
2022-07-28 01:11:28 +02:00
return new FormatableChat ( Component . translatable ( ) . key ( key ) . args ( Chat . filterObjToComponentLike ( with ) ) ) ;
2020-11-02 23:23:41 +01:00
}
public static FormatableChat keybind ( String key ) {
2022-07-28 01:11:28 +02:00
return new FormatableChat ( Component . keybind ( ) . keybind ( key ) ) ;
2020-11-02 23:23:41 +01:00
}
2021-07-09 00:27:59 +02:00
public static FormatableChat score ( String name , String objective ) {
2022-07-28 01:11:28 +02:00
return new FormatableChat ( Component . score ( ) . name ( name ) . objective ( objective ) ) ;
}
public static FormatableChat clickableURL ( Chat inner , String url , Chat hover ) {
Objects . requireNonNull ( url , " url " ) ;
if ( inner = = null )
inner = text ( url ) ;
if ( hover = = null )
hover = text ( ChatUtil . wrapInLimitedPixels ( url , 240 ) ) ;
return ( FormatableChat ) chat ( ) . clickURL ( url ) . urlColor ( ) . hover ( hover ) . then ( inner ) ;
}
public static FormatableChat clickableURL ( Chat inner , String url ) {
return clickableURL ( inner , url , null ) ;
}
public static FormatableChat clickableURL ( String url , Chat hover ) {
return clickableURL ( null , url , hover ) ;
}
public static FormatableChat clickableURL ( String url ) {
return clickableURL ( null , url , null ) ;
}
public static FormatableChat clickableCommand ( Chat inner , String commandWithSlash , Chat hover ) {
Objects . requireNonNull ( commandWithSlash , " commandWithSlash " ) ;
if ( ! commandWithSlash . startsWith ( " / " ) )
throw new IllegalArgumentException ( " commandWithSlash must start with a '/' character. " ) ;
if ( inner = = null )
inner = text ( commandWithSlash ) ;
if ( hover = = null )
hover = text ( ChatUtil . wrapInLimitedPixels ( commandWithSlash , 240 ) ) ;
return ( FormatableChat ) chat ( ) . clickCommand ( commandWithSlash ) . commandColor ( ) . hover ( hover ) . then ( inner ) ;
2020-11-02 23:23:41 +01:00
}
2022-07-21 02:19:28 +02:00
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableCommand ( Chat inner , String commandWithSlash ) {
return clickableCommand ( inner , commandWithSlash , null ) ;
}
2022-07-21 02:19:28 +02:00
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableCommand ( String commandWithSlash , Chat hover ) {
return clickableCommand ( null , commandWithSlash , hover ) ;
}
2022-07-21 02:19:28 +02:00
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableCommand ( String commandWithSlash ) {
return clickableCommand ( null , commandWithSlash , null ) ;
}
2022-07-21 02:19:28 +02:00
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableSuggest ( Chat inner , String commandWithSlash , Chat hover ) {
Objects . requireNonNull ( commandWithSlash , " commandWithSlash " ) ;
if ( ! commandWithSlash . startsWith ( " / " ) )
throw new IllegalArgumentException ( " commandWithSlash must start with a '/' character. " ) ;
if ( inner = = null )
inner = text ( commandWithSlash ) ;
if ( hover = = null )
hover = text ( ChatUtil . wrapInLimitedPixels ( commandWithSlash , 240 ) ) ;
return ( FormatableChat ) chat ( ) . clickSuggest ( commandWithSlash ) . commandColor ( ) . hover ( hover ) . then ( inner ) ;
}
2022-07-21 02:19:28 +02:00
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableSuggest ( Chat inner , String commandWithSlash ) {
return clickableSuggest ( inner , commandWithSlash , null ) ;
2022-07-21 02:19:28 +02:00
}
2022-07-28 01:11:28 +02:00
public static FormatableChat clickableSuggest ( String commandWithSlash , Chat hover ) {
return clickableSuggest ( null , commandWithSlash , hover ) ;
}
public static FormatableChat clickableSuggest ( String commandWithSlash ) {
return clickableSuggest ( null , commandWithSlash , null ) ;
}
public static Chat prefixedAndColored ( ComponentLike message ) {
2022-07-21 02:19:28 +02:00
return Chat . chat ( )
. broadcastColor ( )
. then ( Chat . getConfig ( ) . prefix . get ( ) )
. then ( message ) ;
}
2021-07-20 00:36:57 +02:00
2022-07-10 00:55:56 +02:00
2020-11-02 23:23:41 +01:00
}