/
StringUtils.java
189 lines (154 loc) · 5.88 KB
/
StringUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.gmail.nossr50.util.text;
import com.gmail.nossr50.datatypes.party.PartyFeature;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import org.bukkit.Material;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import java.text.DecimalFormat;
import java.util.Locale;
public class StringUtils {
protected static DecimalFormat percent = new DecimalFormat("##0.00%");
protected static DecimalFormat shortDecimal = new DecimalFormat("##0.0");
/**
* Gets a capitalized version of the target string.
*
* @param target
* String to capitalize
* @return the capitalized string
*/
public static String getCapitalized(String target) {
return target.substring(0, 1).toUpperCase() + target.substring(1).toLowerCase(Locale.ENGLISH);
}
public static String ticksToSeconds(double ticks) {
return shortDecimal.format(ticks / 20);
}
public static String convertToCamelCaseString(String baseString, String splitBy) {
String[] substrings = baseString.split(splitBy);
String prettyString = "";
int size = 1;
for (String string : substrings) {
prettyString = prettyString.concat(getCapitalized(string));
if (size < substrings.length) {
prettyString = prettyString.concat("");
}
size++;
}
return prettyString;
}
public static String getPrettyCamelCaseName(Object o) {
return StringUtils.convertToCamelCaseString(o.toString(), "_");
}
public static String getPrettySuperAbilityName(SuperAbilityType superAbilityType) {
return StringUtils.getPrettySuperAbilityString(superAbilityType);
}
public static String getPrettySuperAbilityString(SuperAbilityType ability) {
return createPrettyString(ability.toString());
}
/**
* Creates a string from an array skipping the first n elements
* @param args the array to iterate over when forming the string
* @param index the amount of elements to skip over
* @return the "trimmed" string
*/
public static String buildStringAfterNthElement(@NotNull String @NotNull []args, int index) {
StringBuilder trimMessage = new StringBuilder();
for (int i = index; i < args.length; i++) {
if(i + 1 >= args.length)
trimMessage.append(args[i]);
else
trimMessage.append(args[i]).append(" ");
}
return trimMessage.toString();
}
public static String getPrettyItemString(Material material) {
return createPrettyString(material.toString());
}
public static String getPrettyEntityTypeString(EntityType entity) {
return createPrettyString(entity.toString());
}
public static String getPrettyAbilityString(SuperAbilityType ability) {
return createPrettyString(ability.toString());
}
public static String getWildcardConfigBlockDataString(BlockData data) {
return getWildcardConfigMaterialString(data.getMaterial());
}
public static String getWildcardConfigMaterialString(Material data) {
return StringUtils.getPrettyItemString(data).replace(" ", "_") + "|*";
}
public static String getFriendlyConfigBlockDataString(BlockData data) {
switch(data.getMaterial()){
case CHORUS_FLOWER:
case COCOA:
case WHEAT:
case BEETROOTS:
case CARROTS:
case POTATOES:
case NETHER_WART: {
if (data instanceof Ageable ageData) {
if (ageData.getAge() == ageData.getMaximumAge()) {
return getPrettyItemString(data.getMaterial()).replace(" ", "_") + "_Ripe";
}
}
return getPrettyItemString(data.getMaterial()).replace(" ", "_") + "_Ungrown";
}
}
return getPrettyItemString(data.getMaterial()).replace(" ", "_");
}
public static String getFriendlyConfigMaterialString(Material data) {
return getPrettyItemString(data).replace(" ", "_");
}
public static String getExplicitConfigBlockDataString(BlockData data) {
return getExplicitConfigMaterialString(data.getMaterial());
}
public static String getExplicitConfigMaterialString(Material data) {
return StringUtils.getPrettyItemString(data).replace(" ", "_");
}
public static String getPrettyPartyFeatureString(PartyFeature partyFeature) {
return createPrettyString(partyFeature.toString());
}
private static String createPrettyString(String baseString) {
String[] substrings = baseString.split("_");
String prettyString = "";
int size = 1;
for (String string : substrings) {
prettyString = prettyString.concat(getCapitalized(string));
if (size < substrings.length) {
prettyString = prettyString.concat(" ");
}
size++;
}
return prettyString;
}
/**
* 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 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;
}
}
}