generated from srnyx/plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnnoyingUtility.java
207 lines (188 loc) · 6.63 KB
/
AnnoyingUtility.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package xyz.srnyx.annoyingapi.utility;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* General utility methods for AnnoyingAPI
*/
public class AnnoyingUtility {
/**
* Constructs a new {@link AnnoyingUtility} instance
* <p><i>Only exists to give the constructor a Javadoc</i>
*/
private AnnoyingUtility() {
// Only exists to give the constructor a Javadoc
}
/**
* Translates {@code &} color codes to {@link ChatColor}
*
* @param message the message to translate
*
* @return the translated message
*/
@NotNull
public static String color(@Nullable String message) {
if (message == null) return "null";
return ChatColor.translateAlternateColorCodes('&', message);
}
/**
* Formats a millisecond long using the given pattern
*
* @param value the milliseconds to format
* @param pattern the way in which to format the milliseconds
* @param padWithZeros whether to pad the left hand side of numbers with 0's
*
* @return the formatted milliseconds
*/
@NotNull
public static String formatMillis(long value, @Nullable String pattern, boolean padWithZeros) {
if (pattern == null) pattern = "m':'s";
return DurationFormatUtils.formatDuration(value, pattern, padWithZeros);
}
/**
* Formats a {@link Double} value using the given pattern
*
* @param value the {@link Number} to format
* @param pattern the pattern to use
*
* @return the formatted value
*/
@NotNull
public static String formatNumber(@NotNull Number value, @Nullable String pattern) {
if (pattern == null) pattern = "#,###.##";
return new DecimalFormat(pattern).format(value);
}
/**
* Gets an {@link OfflinePlayer} from the specified name
*
* @param name the name of the player
*
* @return the {@link OfflinePlayer}, or null if not found
*/
@Nullable
public static OfflinePlayer getOfflinePlayer(@NotNull String name) {
for (final OfflinePlayer offline : Bukkit.getOfflinePlayers()) {
final String offlineName = offline.getName();
if (offlineName != null && offlineName.equalsIgnoreCase(name)) return offline;
}
return null;
}
/**
* Gets a {@link Set} of all online player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getOnlinePlayerNames() {
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toSet());
}
/**
* Gets a {@link Set} of all offline player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getOfflinePlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.filter(player -> !player.isOnline())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
}
/**
* Gets a {@link Set} of all player names
*
* @return the {@link Set} of player names
*/
@NotNull
public static Set<String> getAllPlayerNames() {
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName)
.collect(Collectors.toSet());
}
/**
* Gets a {@link Set} of all world names
*
* @return the {@link Set} of world names
*/
@NotNull
public static Set<String> getWorldNames() {
return Bukkit.getWorlds().stream()
.map(org.bukkit.World::getName)
.collect(Collectors.toSet());
}
/**
* Gets a {@link Set} of all the enum's value's names
*
* @param enumClass the enum class to get the names from
*
* @return the {@link Set} of the enum's value's names
*/
@NotNull
public static Set<String> getEnumNames(@NotNull Class<? extends Enum<?>> enumClass) {
return Arrays.stream(enumClass.getEnumConstants())
.map(Enum::name)
.collect(Collectors.toSet());
}
/**
* Gets a {@link Set} of all YML file names in a folder. If the path is not a folder, an empty {@link Set} is returned
*
* @param plugin the {@link AnnoyingPlugin} to get the folder from
* @param path the path to the folder
*
* @return {@link Set} all YML file names in the folder
*/
@NotNull
public static Set<String> getFileNames(@NotNull AnnoyingPlugin plugin, @NotNull String path) {
final File[] files = new File(plugin.getDataFolder(), path).listFiles();
if (files == null) return new HashSet<>();
return Arrays.stream(files)
.map(File::getName)
.filter(name -> name.endsWith(".yml"))
.map(name -> name.substring(0, name.length() - 4))
.collect(Collectors.toSet());
}
/**
* Retrieves the {@link JsonElement} from the specified URL
*
* @param userAgent the user agent to use when retrieving the {@link JsonElement}
* @param urlString the URL to retrieve the {@link JsonElement} from
*
* @return the {@link JsonElement} retrieved from the specified URL
*/
@Nullable
public static JsonElement getJson(@NotNull String userAgent, @NotNull String urlString) {
final HttpURLConnection connection;
final JsonElement json;
try {
connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", userAgent);
if (connection.getResponseCode() == 404) return null;
json = new JsonParser().parse(new InputStreamReader(connection.getInputStream()));
} catch (final IOException e) {
e.printStackTrace();
return null;
}
connection.disconnect();
return json;
}
}