Skip to content

Utils ​

com.jsmacrosce.jsmacros.api.library.FUtils

Since1.8.4

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 ​

decode(String message): StringSince1.8.4#
Base64-decodes the string and converts the decoded bytes to text using the platform's default charset.

Parameters:

  • message: the message to decode
Returns: the decoded message.

encode ​

encode(String message): StringSince1.8.4#
Base64-encodes the string's bytes, using the platform's default charset to convert the string to bytes and the encoded bytes back to text.

Parameters:

  • message: the message to encode
Returns: the encoded message.

guessName ​

guessName(String text): StringSince1.8.4#
Guesses the sender name from a chat message; the heuristic may not fit every server's format.

Parameters:

  • text: the text to check
Returns: the name of the sender or null if it couldn't be guessed.

guessNameAndRoles ​

guessNameAndRoles(String text): List<String>Since1.8.4#
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 ​

hashString(String message): StringSince1.8.4#
Hashes the message with SHA-256 and returns lowercase hexadecimal.

Parameters:

  • message: the message to hash
Returns: the hashed message.
hashString(String message, String algorithm): StringSince1.8.4#
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 hash
  • algorithm: sha1 | sha256 | sha384 | sha512 | md2 | md5
Returns: the digest as lowercase hexadecimal, or the original message if the algorithm is unrecognized
hashString(String message, String algorithm, Boolean base64): StringSince1.9.1#
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 hash
  • algorithm: sha1 | sha256 | sha384 | sha512 | md2 | md5
  • base64: encode the result in base64
Returns: the digest in the selected encoding, or the original message if the algorithm is unrecognized

requireNonNull ​

requireNonNull(T obj): TSince1.9.1#
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 null
requireNonNull(T obj, String message): TSince1.9.1#
Returns obj if it is non-null; otherwise throws a NullPointerException with message as its detail message.

Parameters:

  • obj: the object reference to check for nullity
  • message: detail message for the NullPointerException if obj is null

Type Parameters:

  • T: the reference type
Returns: obj if not null