Javadoc a bit of Geyser (#392)

* A bunch of javadoc comments

* Cleaned up javadocs
This commit is contained in:
rtm516 2020-04-21 06:28:44 +01:00 committed by GitHub
parent 4ee95f585d
commit 1b15f3058f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 280 additions and 12 deletions

View file

@ -14,6 +14,13 @@ public enum AuthType {
return id < VALUES.length ? VALUES[id] : OFFLINE; return id < VALUES.length ? VALUES[id] : OFFLINE;
} }
/**
* Convert the AuthType string (from config) to the enum, OFFLINE on fail
*
* @param name AuthType string
*
* @return The converted AuthType
*/
public static AuthType getByName(String name) { public static AuthType getByName(String name) {
String upperCase = name.toUpperCase(); String upperCase = name.toUpperCase();
for (AuthType type : VALUES) { for (AuthType type : VALUES) {

View file

@ -51,6 +51,13 @@ public class ChatColor {
public static final String ITALIC = ESCAPE + "o"; public static final String ITALIC = ESCAPE + "o";
public static final String RESET = ESCAPE + "r"; public static final String RESET = ESCAPE + "r";
/**
* Convert chat colour codes to terminal colours
*
* @param string The text to replace colours for
*
* @return A string ready for terminal printing
*/
public static String toANSI(String string) { public static String toANSI(String string) {
string = string.replace(BOLD, (char) 0x1b + "[1m"); string = string.replace(BOLD, (char) 0x1b + "[1m");
string = string.replace(OBFUSCATED, (char) 0x1b + "[5m"); string = string.replace(OBFUSCATED, (char) 0x1b + "[5m");
@ -81,6 +88,13 @@ public class ChatColor {
return message.replace(color, ESCAPE); return message.replace(color, ESCAPE);
} }
/**
* Remove all colour formatting tags from a message
*
* @param message Message to remove colour tags from
*
* @return The sanitised message
*/
public static String stripColors(String message) { public static String stripColors(String message) {
return message = message.replaceAll("(&([a-fk-or0-9]))","").replaceAll("(§([a-fk-or0-9]))","").replaceAll("s/\\x1b\\[[0-9;]*[a-zA-Z]//g",""); return message = message.replaceAll("(&([a-fk-or0-9]))","").replaceAll("(§([a-fk-or0-9]))","").replaceAll("s/\\x1b\\[[0-9;]*[a-zA-Z]//g","");
} }

View file

@ -31,13 +31,34 @@ import org.geysermc.common.logger.IGeyserLogger;
public interface IGeyserBootstrap { public interface IGeyserBootstrap {
/**
* Called when the GeyserBootstrap is enabled
*/
void onEnable(); void onEnable();
/**
* Called when the GeyserBootstrap is disabled
*/
void onDisable(); void onDisable();
/**
* Returns the current GeyserConfig
*
* @return The current GeyserConfig
*/
IGeyserConfiguration getGeyserConfig(); IGeyserConfiguration getGeyserConfig();
/**
* Returns the current GeyserLogger
*
* @return The current GeyserLogger
*/
IGeyserLogger getGeyserLogger(); IGeyserLogger getGeyserLogger();
/**
* Returns the current GeyserCommandManager
*
* @return The current GeyserCommandManager
*/
ICommandManager getGeyserCommandManager(); ICommandManager getGeyserCommandManager();
} }

View file

@ -27,5 +27,12 @@ package org.geysermc.common.command;
public interface ICommandManager { public interface ICommandManager {
/**
* Returns the description of the given command
*
* @param command Command to get the description for
*
* @return Command description
*/
String getDescription(String command); String getDescription(String command);
} }

View file

@ -44,8 +44,8 @@ public class TropicalFishEntity extends AbstractFishEntity {
@Override @Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) { public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 16) { if (entityMetadata.getId() == 16) {
// Still not always the right model and colour is broken
TropicalFishVariant variant = TropicalFishVariant.fromVariantNumber((int) entityMetadata.getValue()); TropicalFishVariant variant = TropicalFishVariant.fromVariantNumber((int) entityMetadata.getValue());
metadata.put(EntityData.VARIANT, variant.getShape()); // Shape 0-1 metadata.put(EntityData.VARIANT, variant.getShape()); // Shape 0-1
metadata.put(EntityData.MARK_VARIANT, variant.getPattern()); // Pattern 0-5 metadata.put(EntityData.MARK_VARIANT, variant.getPattern()); // Pattern 0-5
metadata.put(EntityData.COLOR, variant.getBaseColor()); // Base color 0-15 metadata.put(EntityData.COLOR, variant.getBaseColor()); // Base color 0-15
@ -80,6 +80,13 @@ public class TropicalFishEntity extends AbstractFishEntity {
private byte baseColor; private byte baseColor;
private byte patternColor; private byte patternColor;
/**
* Convert the variant number from Java into separate values
*
* @param varNumber Variant number from Java edition
*
* @return The variant converted into TropicalFishVariant
*/
public static TropicalFishVariant fromVariantNumber(int varNumber) { public static TropicalFishVariant fromVariantNumber(int varNumber) {
return new TropicalFishVariant((varNumber & 0xFF), ((varNumber >> 8) & 0xFF), (byte) ((varNumber >> 16) & 0xFF), (byte) ((varNumber >> 24) & 0xFF)); return new TropicalFishVariant((varNumber & 0xFF), ((varNumber >> 8) & 0xFF), (byte) ((varNumber >> 16) & 0xFF), (byte) ((varNumber >> 24) & 0xFF));
} }

View file

@ -55,7 +55,7 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
for (int nodeIndex : rootNode.getChildIndices()) { for (int nodeIndex : rootNode.getChildIndices()) {
CommandNode node = packet.getNodes()[nodeIndex]; CommandNode node = packet.getNodes()[nodeIndex];
// Make sure we dont have duplicated commands (happens if there is more than 1 root node) // Make sure we don't have duplicated commands (happens if there is more than 1 root node)
if (commands.containsKey(nodeIndex)) { continue; } if (commands.containsKey(nodeIndex)) { continue; }
// Get and update the commandArgs list with the found arguments // Get and update the commandArgs list with the found arguments
@ -81,7 +81,7 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
CommandEnumData aliases = new CommandEnumData( commandName + "Aliases", new String[] { commandName.toLowerCase() }, false); CommandEnumData aliases = new CommandEnumData( commandName + "Aliases", new String[] { commandName.toLowerCase() }, false);
// Get and parse all params // Get and parse all params
CommandParamData[][] params = getParams(commandID, packet.getNodes()[commandID], packet.getNodes()); CommandParamData[][] params = getParams(packet.getNodes()[commandID], packet.getNodes());
// Build the completed command and add it to the final list // Build the completed command and add it to the final list
CommandData data = new CommandData(commandName, session.getConnector().getCommandManager().getDescription(commandName), flags, (byte) 0, aliases, params); CommandData data = new CommandData(commandName, session.getConnector().getCommandManager().getDescription(commandName), flags, (byte) 0, aliases, params);
@ -100,7 +100,15 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
session.getUpstream().sendPacket(availableCommandsPacket); session.getUpstream().sendPacket(availableCommandsPacket);
} }
private CommandParamData[][] getParams(int commandID, CommandNode commandNode, CommandNode[] allNodes) { /**
* Build the command parameter array for the given command
*
* @param commandNode The command to build the parameters for
* @param allNodes Every command node
*
* @return An array of parameter option arrays
*/
private CommandParamData[][] getParams(CommandNode commandNode, CommandNode[] allNodes) {
// Check if the command is an alias and redirect it // Check if the command is an alias and redirect it
if (commandNode.getRedirectIndex() != -1) { if (commandNode.getRedirectIndex() != -1) {
GeyserConnector.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[commandNode.getRedirectIndex()].getName()); GeyserConnector.getInstance().getLogger().debug("Redirecting command " + commandNode.getName() + " to " + allNodes[commandNode.getRedirectIndex()].getName());
@ -128,6 +136,13 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
return new CommandParamData[0][0]; return new CommandParamData[0][0];
} }
/**
* Convert Java edition command types to Bedrock edition
*
* @param parser Command type to convert
*
* @return Bedrock parameter data type
*/
private CommandParamData.Type mapCommandType(CommandParser parser) { private CommandParamData.Type mapCommandType(CommandParser parser) {
if (parser == null) { return CommandParamData.Type.STRING; } if (parser == null) { return CommandParamData.Type.STRING; }
@ -204,12 +219,23 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
private CommandParamData paramData; private CommandParamData paramData;
private List<ParamInfo> children; private List<ParamInfo> children;
/**
* Create a new parameter info object
*
* @param paramNode CommandNode the parameter is for
* @param paramData The existing parameters for the command
*/
public ParamInfo(CommandNode paramNode, CommandParamData paramData) { public ParamInfo(CommandNode paramNode, CommandParamData paramData) {
this.paramNode = paramNode; this.paramNode = paramNode;
this.paramData = paramData; this.paramData = paramData;
this.children = new ArrayList<>(); this.children = new ArrayList<>();
} }
/**
* Build the array of all the child parameters (recursive)
*
* @param allNodes Every command node
*/
public void buildChildren(CommandNode[] allNodes) { public void buildChildren(CommandNode[] allNodes) {
int enumIndex = -1; int enumIndex = -1;
@ -247,6 +273,11 @@ public class JavaServerDeclareCommandsTranslator extends PacketTranslator<Server
} }
} }
/**
* Get the tree of every parameter node (recursive)
*
* @return List of parameter options arrays for the command
*/
public List<CommandParamData[]> getTree() { public List<CommandParamData[]> getTree() {
List<CommandParamData[]> treeParamData = new ArrayList<>(); List<CommandParamData[]> treeParamData = new ArrayList<>();

View file

@ -61,6 +61,12 @@ public class DimensionUtils {
session.getUpstream().sendPacket(stopSoundPacket); session.getUpstream().sendPacket(stopSoundPacket);
} }
/**
* Map the Java edition dimension IDs to Bedrock edition
*
* @param javaDimension Dimension ID to convert
* @return Converted Bedrock edition dimension ID
*/
public static int javaToBedrock(int javaDimension) { public static int javaToBedrock(int javaDimension) {
switch (javaDimension) { switch (javaDimension) {
case -1: case -1:

View file

@ -32,6 +32,12 @@ import org.geysermc.connector.entity.type.EntityType;
public class EntityUtils { public class EntityUtils {
/**
* Convert Java edition effect IDs to Bedrock edition
*
* @param effect Effect to convert
* @return The numeric ID for the Bedrock edition effect
*/
public static int toBedrockEffectId(Effect effect) { public static int toBedrockEffectId(Effect effect) {
switch (effect) { switch (effect) {
case GLOWING: case GLOWING:
@ -51,6 +57,13 @@ public class EntityUtils {
return effect.ordinal() + 1; return effect.ordinal() + 1;
} }
} }
/**
* Converts a MobType to a Bedrock edition EntityType, returns null if the EntityType is not found
*
* @param type The MobType to convert
* @return Converted EntityType
*/
public static EntityType toBedrockEntity(MobType type) { public static EntityType toBedrockEntity(MobType type) {
try { try {
return EntityType.valueOf(type.name()); return EntityType.valueOf(type.name());
@ -59,6 +72,12 @@ public class EntityUtils {
} }
} }
/**
* Converts a ObjectType to a Bedrock edition EntityType, returns null if the EntityType is not found
*
* @param type The ObjectType to convert
* @return Converted EntityType
*/
public static EntityType toBedrockEntity(ObjectType type) { public static EntityType toBedrockEntity(ObjectType type) {
try { try {
return EntityType.valueOf(type.name()); return EntityType.valueOf(type.name());

View file

@ -27,24 +27,51 @@ package org.geysermc.connector.utils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import java.io.*; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.function.Function; import java.util.function.Function;
public class FileUtils { public class FileUtils {
/**
* Load the given YAML file into the given class
*
* @param src File to load
* @param valueType Class to load file into
* @return The data as the given class
* @throws IOException
*/
public static <T> T loadConfig(File src, Class<T> valueType) throws IOException { public static <T> T loadConfig(File src, Class<T> valueType) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
return objectMapper.readValue(src, valueType); return objectMapper.readValue(src, valueType);
} }
public static File fileOrCopiedFromResource(String name, Function<String, String> s) throws IOException { /**
return fileOrCopiedFromResource(new File(name), name, s); * Open the specified file or copy if from resources
*
* @param name File and resource name
* @param fallback Formatting callback
* @return File handle of the specified file
* @throws IOException
*/
public static File fileOrCopiedFromResource(String name, Function<String, String> fallback) throws IOException {
return fileOrCopiedFromResource(new File(name), name, fallback);
} }
public static File fileOrCopiedFromResource(File file, String name, Function<String, String> s) throws IOException { /**
* Open the specified file or copy if from resources
*
* @param file File to open
* @param name Name of the resource get if needed
* @param format Formatting callback
* @return File handle of the specified file
* @throws IOException
*/
public static File fileOrCopiedFromResource(File file, String name, Function<String, String> format) throws IOException {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream(file);
@ -54,7 +81,7 @@ public class FileUtils {
input.read(bytes); input.read(bytes);
for(char c : s.apply(new String(bytes)).toCharArray()) { for(char c : format.apply(new String(bytes)).toCharArray()) {
fos.write(c); fos.write(c);
} }
@ -66,6 +93,13 @@ public class FileUtils {
return file; return file;
} }
/**
* Writes the given data to the specified file on disk
*
* @param file File to write to
* @param data Data to write to the file
* @throws IOException
*/
public static void writeFile(File file, char[] data) throws IOException { public static void writeFile(File file, char[] data) throws IOException {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
@ -81,6 +115,13 @@ public class FileUtils {
fos.close(); fos.close();
} }
/**
* Writes the given data to the specified file on disk
*
* @param name File path to write to
* @param data Data to write to the file
* @throws IOException
*/
public static void writeFile(String name, char[] data) throws IOException { public static void writeFile(String name, char[] data) throws IOException {
writeFile(new File(name), data); writeFile(new File(name), data);
} }

View file

@ -57,9 +57,15 @@ public class LocaleUtils {
downloadAndLoadLocale(DEFAULT_LOCALE); downloadAndLoadLocale(DEFAULT_LOCALE);
} }
/**
* Fetch the latest versions asset cache from Mojang so we can grab the locale files later
*/
private static void generateAssetCache() { private static void generateAssetCache() {
try { try {
// Get the version manifest from Mojang
VersionManifest versionManifest = Toolbox.JSON_MAPPER.readValue(WebUtils.getBody("https://launchermeta.mojang.com/mc/game/version_manifest.json"), VersionManifest.class); VersionManifest versionManifest = Toolbox.JSON_MAPPER.readValue(WebUtils.getBody("https://launchermeta.mojang.com/mc/game/version_manifest.json"), VersionManifest.class);
// Get the url for the latest version of the games manifest
String latestInfoURL = ""; String latestInfoURL = "";
for (Version version : versionManifest.getVersions()) { for (Version version : versionManifest.getVersions()) {
if (version.getId().equals(versionManifest.getLatestVersion().getRelease())) { if (version.getId().equals(versionManifest.getLatestVersion().getRelease())) {
@ -68,12 +74,15 @@ public class LocaleUtils {
} }
} }
// Make sure we definitely got a version
if (latestInfoURL.isEmpty()) { if (latestInfoURL.isEmpty()) {
throw new Exception("Unable to get latest Minecraft version"); throw new Exception("Unable to get latest Minecraft version");
} }
// Get the individual version manifest
VersionInfo versionInfo = Toolbox.JSON_MAPPER.readValue(WebUtils.getBody(latestInfoURL), VersionInfo.class); VersionInfo versionInfo = Toolbox.JSON_MAPPER.readValue(WebUtils.getBody(latestInfoURL), VersionInfo.class);
// Get the smallest jar for use when downloading the en_us locale, will be either the server or client
int currentSize = Integer.MAX_VALUE; int currentSize = Integer.MAX_VALUE;
for (VersionDownload download : versionInfo.getDownloads().values()) { for (VersionDownload download : versionInfo.getDownloads().values()) {
if (download.getUrl().endsWith(".jar") && download.getSize() < currentSize) { if (download.getUrl().endsWith(".jar") && download.getSize() < currentSize) {
@ -82,8 +91,10 @@ public class LocaleUtils {
} }
} }
// Get the assets list
JsonNode assets = Toolbox.JSON_MAPPER.readTree(WebUtils.getBody(versionInfo.getAssetIndex().getUrl())).get("objects"); JsonNode assets = Toolbox.JSON_MAPPER.readTree(WebUtils.getBody(versionInfo.getAssetIndex().getUrl())).get("objects");
// Put each asset into an array for use later
Iterator<Map.Entry<String, JsonNode>> assetIterator = assets.fields(); Iterator<Map.Entry<String, JsonNode>> assetIterator = assets.fields();
while (assetIterator.hasNext()) { while (assetIterator.hasNext()) {
Map.Entry<String, JsonNode> entry = assetIterator.next(); Map.Entry<String, JsonNode> entry = assetIterator.next();
@ -95,8 +106,15 @@ public class LocaleUtils {
} }
} }
/**
* Downloads a locale from Mojang if its not already loaded
*
* @param locale Locale to download and load
*/
public static void downloadAndLoadLocale(String locale) { public static void downloadAndLoadLocale(String locale) {
locale = locale.toLowerCase(); locale = locale.toLowerCase();
// Check the locale isn't already loaded
if (!ASSET_MAP.containsKey("minecraft/lang/" + locale + ".json") && !locale.equals("en_us")) { if (!ASSET_MAP.containsKey("minecraft/lang/" + locale + ".json") && !locale.equals("en_us")) {
GeyserConnector.getInstance().getLogger().warning("Invalid locale requested to download and load: " + locale); GeyserConnector.getInstance().getLogger().warning("Invalid locale requested to download and load: " + locale);
return; return;
@ -108,9 +126,15 @@ public class LocaleUtils {
loadLocale(locale); loadLocale(locale);
} }
/**
* Downloads the specified locale if its not already downloaded
*
* @param locale Locale to download
*/
private static void downloadLocale(String locale) { private static void downloadLocale(String locale) {
File localeFile = new File("locales/" + locale + ".json"); File localeFile = new File("locales/" + locale + ".json");
// Check if we have already downloaded the locale file
if (localeFile.exists()) { if (localeFile.exists()) {
GeyserConnector.getInstance().getLogger().debug("Locale already downloaded: " + locale); GeyserConnector.getInstance().getLogger().debug("Locale already downloaded: " + locale);
return; return;
@ -128,6 +152,11 @@ public class LocaleUtils {
WebUtils.downloadFile("http://resources.download.minecraft.net/" + hash.substring(0, 2) + "/" + hash, "locales/" + locale + ".json"); WebUtils.downloadFile("http://resources.download.minecraft.net/" + hash.substring(0, 2) + "/" + hash, "locales/" + locale + ".json");
} }
/**
* Loads a locale already downloaded, if the file doesn't exist it just logs a warning
*
* @param locale Locale to load
*/
private static void loadLocale(String locale) { private static void loadLocale(String locale) {
File localeFile = new File("locales/" + locale + ".json"); File localeFile = new File("locales/" + locale + ".json");
@ -146,7 +175,7 @@ public class LocaleUtils {
try { try {
localeObj = Toolbox.JSON_MAPPER.readTree(localeStream); localeObj = Toolbox.JSON_MAPPER.readTree(localeStream);
} catch (Exception e) { } catch (Exception e) {
throw new AssertionError("Unable to load Java lang map for " + locale, e); throw new AssertionError("Unable to load Java edition lang map for " + locale, e);
} }
// Parse all the locale fields // Parse all the locale fields
@ -164,6 +193,11 @@ public class LocaleUtils {
} }
} }
/**
* Download then en_us locale by downloading the server jar and extracting it from there.
*
* @param localeFile File to save the locale to
*/
private static void downloadEN_US(File localeFile) { private static void downloadEN_US(File localeFile) {
try { try {
// Let the user know we are downloading the JAR // Let the user know we are downloading the JAR
@ -199,6 +233,13 @@ public class LocaleUtils {
} }
} }
/**
* Translate the given language string into the given locale, or falls back to the default locale
*
* @param messageText Language string to translate
* @param locale Locale to translate to
* @return Translated string or the original message if it was not found in the given locale
*/
public static String getLocaleString(String messageText, String locale) { public static String getLocaleString(String messageText, String locale) {
Map<String, String> localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase()); Map<String, String> localeStrings = LocaleUtils.LOCALE_MAPPINGS.get(locale.toLowerCase());
if (localeStrings == null) if (localeStrings == null)

View file

@ -27,6 +27,12 @@ package org.geysermc.connector.utils;
public class MathUtils { public class MathUtils {
/**
* Round the given float to the next whole number
*
* @param floatNumber Float to round
* @return Rounded number
*/
public static int ceil(float floatNumber) { public static int ceil(float floatNumber) {
int truncated = (int) floatNumber; int truncated = (int) floatNumber;
return floatNumber > truncated ? truncated + 1 : truncated; return floatNumber > truncated ? truncated + 1 : truncated;

View file

@ -121,6 +121,13 @@ public class MessageUtils {
return getTranslatedBedrockMessage(message, null, false); return getTranslatedBedrockMessage(message, null, false);
} }
/**
* Inserts the given parameters into the given message both in sequence and as requested
*
* @param message Message containing possible parameter replacement strings
* @param params A list of parameter strings
* @return Parsed message with all params inserted as needed
*/
public static String insertParams(String message, List<String> params) { public static String insertParams(String message, List<String> params) {
String newMessage = message; String newMessage = message;
@ -130,7 +137,7 @@ public class MessageUtils {
try { try {
newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s" , params.get(Integer.parseInt(m.group(1)) - 1)); newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s" , params.get(Integer.parseInt(m.group(1)) - 1));
} catch (Exception e) { } catch (Exception e) {
// Couldnt find the param to replace // Couldn't find the param to replace
} }
} }
@ -141,6 +148,12 @@ public class MessageUtils {
return newMessage; return newMessage;
} }
/**
* Gets the colour for the message style or fetches it from the parent (recursive)
*
* @param style The style to get the colour from
* @return Colour string to be used
*/
private static String getColorOrParent(MessageStyle style) { private static String getColorOrParent(MessageStyle style) {
ChatColor chatColor = style.getColor(); ChatColor chatColor = style.getColor();
@ -151,6 +164,12 @@ public class MessageUtils {
return getColor(chatColor); return getColor(chatColor);
} }
/**
* Convert a ChatColor into a string for inserting into messages
*
* @param color ChatColor to convert
* @return The converted color string
*/
private static String getColor(ChatColor color) { private static String getColor(ChatColor color) {
String base = "\u00a7"; String base = "\u00a7";
switch (color) { switch (color) {
@ -213,6 +232,12 @@ public class MessageUtils {
return base; return base;
} }
/**
* Convert a list of ChatFormats into a string for inserting into messages
*
* @param formats ChatFormats to convert
* @return The converted chat formatting string
*/
private static String getFormat(List<ChatFormat> formats) { private static String getFormat(List<ChatFormat> formats) {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
for (ChatFormat cf : formats) { for (ChatFormat cf : formats) {
@ -243,6 +268,12 @@ public class MessageUtils {
return str.toString(); return str.toString();
} }
/**
* Checks if the given text string is a json message
*
* @param text String to test
* @return True if its a valid message json string, false if not
*/
public static boolean isMessage(String text) { public static boolean isMessage(String text) {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
try { try {
@ -296,6 +327,13 @@ public class MessageUtils {
return ""; return "";
} }
/**
* Checks if the given message is over 256 characters (Java edition server chat limit) and sends a message to the user if it is
*
* @param message Message to check
* @param session GeyserSession for the user
* @return True if the message is too long, false if not
*/
public static boolean isTooLong(String message, GeyserSession session) { public static boolean isTooLong(String message, GeyserSession session) {
if (message.length() > 256) { if (message.length() > 256) {
// TODO: Add Geyser localization and translate this based on language // TODO: Add Geyser localization and translate this based on language

View file

@ -105,6 +105,12 @@ public class SkinUtils {
private String capeUrl; private String capeUrl;
private boolean alex; private boolean alex;
/**
* Generate the GameProfileData from the given GameProfile
*
* @param profile GameProfile to build the GameProfileData from
* @return The built GameProfileData
*/
public static GameProfileData from(GameProfile profile) { public static GameProfileData from(GameProfile profile) {
try { try {
GameProfile.Property skinProperty = profile.getProperty("textures"); GameProfile.Property skinProperty = profile.getProperty("textures");
@ -189,6 +195,12 @@ public class SkinUtils {
}); });
} }
/**
* Create a basic geometry json for the given name
*
* @param geometryName Geometry name to use
* @return Geometry data as a json string
*/
private static String getLegacySkinGeometry(String geometryName) { private static String getLegacySkinGeometry(String geometryName) {
return "{\"geometry\" :{\"default\" :\"" + geometryName + "\"}}"; return "{\"geometry\" :{\"default\" :\"" + geometryName + "\"}}";
} }

View file

@ -110,6 +110,12 @@ public class Toolbox {
LocaleUtils.init(); LocaleUtils.init();
} }
/**
* Get an InputStream for the given resource path, throws AssertionError if resource is not found
*
* @param resource Resource to get
* @return InputStream of the given resource
*/
public static InputStream getResource(String resource) { public static InputStream getResource(String resource) {
InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream(resource); InputStream stream = Toolbox.class.getClassLoader().getResourceAsStream(resource);
if (stream == null) { if (stream == null) {

View file

@ -36,6 +36,12 @@ import java.nio.file.StandardCopyOption;
public class WebUtils { public class WebUtils {
/**
* Makes a web request to the given URL and returns the body as a string
*
* @param reqURL URL to fetch
* @return Body contents or error message if the request fails
*/
public static String getBody(String reqURL) { public static String getBody(String reqURL) {
URL url = null; URL url = null;
try { try {
@ -61,6 +67,12 @@ public class WebUtils {
} }
} }
/**
* Downloads a file from the given URL and saves it to disk
*
* @param reqURL File to fetch
* @param fileLocation Location to save on disk
*/
public static void downloadFile(String reqURL, String fileLocation) { public static void downloadFile(String reqURL, String fileLocation) {
try { try {
InputStream in = new URL(reqURL).openStream(); InputStream in = new URL(reqURL).openStream();