Utils
com.jsmacrosce.jsmacros.api.library.FUtils
Standalone utilities for hashing and Base64 conversion, guessing chat sender names, and rejecting null values.
js
// hashing, sha-256 in hex by default
Chat.log(Utils.hashString("hello"));
// the same digest in base64, which is the form a lot of APIs ask for
Chat.log(Utils.hashString("hello", "sha256", true));
// a base64 round trip
const encoded = Utils.encode("hello");
Chat.log(`${encoded} decodes back to ${Utils.decode(encoded)}`);
// pulling the sender's name back out of a chat line
const name = Utils.guessName("Notch whispers to you: meet me at spawn");
Chat.log(name === null ? "no name could be guessed" : `that was ${name}`);Accessible in scripts via the global Utils variable.
Methods
decode
encode
guessName
guessNameAndRoles
Guesses a sender name and any extracted role or prefix fragments. It examines at most the first 60 characters; whisper-form messages yield only the name. This is a heuristic, not a chat-format parser, and input the heuristic cannot recognize produces an empty list. When the returned list contains a name, it is the first element.
Parameters:
text: the text to check
Returns: a list of names, titles and roles of the sender or an empty list if it couldn't be guessed.
hashString
Hashes the message with SHA-256 and returns lowercase hexadecimal.
Parameters:
message: the message to hash
Returns: the hashed message.
Hashes with the selected algorithm and returns lowercase hexadecimal. Algorithm names are case-sensitive; an unrecognized name returns the input unchanged, including
null. Recognized algorithms do not accept a null message.Parameters:
message: the message to hashalgorithm: sha1 | sha256 | sha384 | sha512 | md2 | md5
Returns: the digest as lowercase hexadecimal, or the original message if the algorithm is unrecognized
Hashes with the selected algorithm, returning lowercase hexadecimal or Base64 of the raw digest bytes when
base64 is true. An unrecognized algorithm returns the input unchanged; recognized algorithms do not accept a null message.Parameters:
message: the message to hashalgorithm: sha1 | sha256 | sha384 | sha512 | md2 | md5base64: encode the result in base64
Returns: the digest in the selected encoding, or the original message if the algorithm is unrecognized
requireNonNull
Returns the reference if non-null; otherwise throws NullPointerException without a detail message. Use the two-argument overload to provide one.
Parameters:
obj: the object reference to check for nullity
Type Parameters:
T: the reference type
Returns:
obj if not nullReturns
obj if it is non-null; otherwise throws a NullPointerException with message as its detail message.Parameters:
obj: the object reference to check for nullitymessage: detail message for the NullPointerException ifobjisnull
Type Parameters:
T: the reference type
Returns:
obj if not null