mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-06-28 03:34:43 +02:00
Optimized our String/number conversions a bit. Also moved all
String-related util functions from Misc.java to StringUtils.java
This commit is contained in:
@ -100,87 +100,6 @@ public final class Misc {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a capitalized version of the target string.
|
||||
*
|
||||
* @param target String to capitalize
|
||||
* @return the capitalized string
|
||||
*/
|
||||
public static String getCapitalized(String target) {
|
||||
String firstLetter = target.substring(0,1);
|
||||
String remainder = target.substring(1);
|
||||
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
|
||||
|
||||
return capitalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a nicely formatted string version of an item name from a given item ID.
|
||||
*
|
||||
* @param itemID The ID of the item to convert to string.
|
||||
* @return the nicely formatting string
|
||||
*/
|
||||
public static String prettyItemString(int itemID) {
|
||||
String baseString = Material.getMaterial(itemID).toString();
|
||||
String[] substrings = baseString.split("_");
|
||||
String prettyString = "";
|
||||
int size = 1;
|
||||
|
||||
for (String s : substrings) {
|
||||
prettyString = prettyString.concat(Misc.getCapitalized(s));
|
||||
|
||||
if (size < substrings.length) {
|
||||
prettyString = prettyString.concat(" ");
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
return prettyString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the int represented by this string
|
||||
*/
|
||||
public static int getInt(String string) {
|
||||
if (isInt(string)) {
|
||||
return Integer.parseInt(string);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the long represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the long represented by this string
|
||||
*/
|
||||
public static long getLong(String string) {
|
||||
if (isLong(string)) {
|
||||
return Long.parseLong(string);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the long represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the long represented by this string
|
||||
*/
|
||||
public static double getDouble(String string) {
|
||||
if (isDouble(string)) {
|
||||
return Double.parseDouble(string);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if an entity is currently invincible.
|
||||
*
|
||||
@ -283,54 +202,6 @@ public final class Misc {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents an Integer
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is an Integer, false otherwise
|
||||
*/
|
||||
public static boolean isInt(String string) {
|
||||
try {
|
||||
Integer.parseInt(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents a Long
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is a Long, false otherwise
|
||||
*/
|
||||
public static boolean isLong(String string) {
|
||||
try {
|
||||
Long.parseLong(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents a Double
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is a Double, false otherwise
|
||||
*/
|
||||
public static boolean isDouble(String string) {
|
||||
try {
|
||||
Double.parseDouble(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop items at a given location.
|
||||
*
|
||||
|
124
src/main/java/com/gmail/nossr50/util/StringUtils.java
Normal file
124
src/main/java/com/gmail/nossr50/util/StringUtils.java
Normal file
@ -0,0 +1,124 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
* Gets a capitalized version of the target string.
|
||||
*
|
||||
* @param target String to capitalize
|
||||
* @return the capitalized string
|
||||
*/
|
||||
public static String getCapitalized(String target) {
|
||||
String firstLetter = target.substring(0,1);
|
||||
String remainder = target.substring(1);
|
||||
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
|
||||
|
||||
return capitalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a nicely formatted string version of an item name from a given item ID.
|
||||
*
|
||||
* @param itemID The ID of the item to convert to string.
|
||||
* @return the nicely formatting string
|
||||
*/
|
||||
public static String getPrettyItemString(int itemID) {
|
||||
String baseString = Material.getMaterial(itemID).toString();
|
||||
String[] substrings = baseString.split("_");
|
||||
String prettyString = "";
|
||||
int size = 1;
|
||||
|
||||
for (String s : substrings) {
|
||||
prettyString = prettyString.concat(getCapitalized(s));
|
||||
|
||||
if (size < substrings.length) {
|
||||
prettyString = prettyString.concat(" ");
|
||||
}
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
return prettyString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the int represented by this string
|
||||
*/
|
||||
public static int getInt(String string) {
|
||||
try {
|
||||
return Integer.parseInt(string);
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the long represented by this string.
|
||||
*
|
||||
* @param string The string to parse
|
||||
* @return the long represented by this string
|
||||
*/
|
||||
public static long getLong(String string) {
|
||||
try {
|
||||
return Long.parseLong(string);
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents an Integer
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is an Integer, false otherwise
|
||||
*/
|
||||
public static boolean isInt(String string) {
|
||||
try {
|
||||
Integer.parseInt(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents a Long
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is a Long, false otherwise
|
||||
*/
|
||||
public static boolean isLong(String string) {
|
||||
try {
|
||||
Long.parseLong(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a string represents a Double
|
||||
*
|
||||
* @param string String to check
|
||||
* @return true if the string is a Double, false otherwise
|
||||
*/
|
||||
public static boolean isDouble(String string) {
|
||||
try {
|
||||
Double.parseDouble(string);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nFE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user