Package dev.majek.pc.chat
Class ChatParser
- java.lang.Object
-
- dev.majek.pc.chat.ChatParser
-
public class ChatParser extends Object
Handles parsing raw strings into rich json components with hover/click events.- Author:
- Majekdor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ChatParser.ParseException
Thrown if there is an error parsing a function.
-
Constructor Summary
Constructors Constructor Description ChatParser()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ChatParser
clearClickEvents()
Clear all click events from the functions.ChatParser
clearColors()
Clear all colors from the parsed string.ChatParser
clearHoverEvents()
Clear all hover events from the functions.net.md_5.bungee.api.chat.BaseComponent[]
getAsBaseComponent()
Get the parsed string as an array ofBaseComponent
s.net.kyori.adventure.text.Component
getAsComponent()
Get the parsed string as aComponent
.String
getAsRawString()
Get the parsed string as a raw string.ChatParser
insertItemHover(String target, String replacement, org.bukkit.inventory.ItemStack hover)
Insert a hover item on a certain bit of text in the component.ChatParser
parse(String string)
Parse the provided string starting at the beginning.ChatParser
parse(String string, int start)
Parse the provided string starting at the provided start location.ChatParser
parse(String delimiter, String... strings)
Parse the provided strings starting at the beginning.
-
-
-
Method Detail
-
parse
public ChatParser parse(String string, int start)
Parse the provided string starting at the provided start location. This will find all functions and format them intoComponent
s. If there is an error parsing the text this will throwChatParser.ParseException
.- Parameters:
string
- The string containing the functions to parse.start
- The location to start parsing in the string.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
parse
public ChatParser parse(String string)
Parse the provided string starting at the beginning. This will find all functions and format them intoComponent
s. If there is an error parsing the text this will throwChatParser.ParseException
.- Parameters:
string
- The string containing the functions to parse.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
parse
public ChatParser parse(String delimiter, String... strings)
Parse the provided strings starting at the beginning. This will find all functions and format them intoComponent
s. If there is an error parsing the text this will throwChatParser.ParseException
.- Parameters:
strings
- These strings will be joined using the provided delimiter.delimiter
- The delimiter to join the string with.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
clearClickEvents
public ChatParser clearClickEvents()
Clear all click events from the functions. If you want to clear all click events and hover events you should be usinggetAsRawString()
.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
clearHoverEvents
public ChatParser clearHoverEvents()
Clear all hover events from the functions. If you want to clear all click events and hover events you should be usinggetAsRawString()
.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
clearColors
public ChatParser clearColors()
Clear all colors from the parsed string.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
insertItemHover
public ChatParser insertItemHover(String target, String replacement, org.bukkit.inventory.ItemStack hover)
Insert a hover item on a certain bit of text in the component.- Parameters:
target
- The current text to target for the hover event.replacement
- The replacement for the current text.hover
- The ItemStack to create a hover event from.- Returns:
- Instance of itself. Use
getAsComponent()
,getAsBaseComponent()
, orgetAsRawString()
to finish the process.
-
getAsBaseComponent
public net.md_5.bungee.api.chat.BaseComponent[] getAsBaseComponent()
Get the parsed string as an array ofBaseComponent
s.- Returns:
BaseComponent
array of the parsed string.
-
getAsComponent
public net.kyori.adventure.text.Component getAsComponent()
Get the parsed string as aComponent
.- Returns:
Component
of the parsed string.
-
getAsRawString
public String getAsRawString()
Get the parsed string as a raw string. This will remove all hover and click events but retain colors. If you want to clear colors as well callclearColors()
before this.- Returns:
- Raw string of parsed string.
-
-