|
1 | 1 | package one.armelin.distale.utils; |
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
3 | 4 | import com.hypixel.hytale.component.Store; |
4 | 5 | import com.hypixel.hytale.protocol.PlayerSkin; |
5 | 6 | import com.hypixel.hytale.server.core.cosmetics.CosmeticRegistry; |
|
12 | 13 |
|
13 | 14 | import javax.annotation.Nullable; |
14 | 15 | import java.awt.image.BufferedImage; |
15 | | -import java.util.Objects; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.*; |
16 | 18 | import java.util.concurrent.CompletableFuture; |
17 | 19 | import java.util.concurrent.ExecutionException; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
18 | 22 |
|
19 | 23 | import static one.armelin.distale.utils.ImageUtils.loadImageFromAsset; |
| 24 | +import static one.armelin.distale.utils.Utils.jsonToQueryString; |
20 | 25 |
|
21 | 26 | public class SkinUtils { |
22 | 27 | public static BufferedImage getSkinToneGradient(PlayerSkin playerSkin){ |
@@ -239,4 +244,139 @@ public static void generateSkin(PlayerRef ref, @Nullable World world){ |
239 | 244 | DisTale.LOGGER.atSevere().withCause(e).log("Error generating skin for player %s", playerName); |
240 | 245 | } |
241 | 246 | } |
| 247 | + |
| 248 | + private static final Pattern PATTERN = Pattern.compile("%(json|base64|query)([*-])\\(([^)]+)\\)%|%(json|base64|query)%"); |
| 249 | + |
| 250 | + private static final Set<String> ALL_FIELDS = new HashSet<>(Arrays.asList( |
| 251 | + "bodyCharacteristic", "underwear", "face", "eyes", "ears", "mouth", |
| 252 | + "facialHair", "haircut", "eyebrows", "pants", "overpants", "undertop", |
| 253 | + "overtop", "shoes", "headAccessory", "faceAccessory", "earAccessory", |
| 254 | + "skinFeature", "gloves", "cape" |
| 255 | + )); |
| 256 | + |
| 257 | + public static String buildAvatarUrl(String urlTemplate, PlayerSkin skin, UUID uuid, String username) { |
| 258 | + StringBuffer result = new StringBuffer(); |
| 259 | + Matcher matcher = PATTERN.matcher(urlTemplate); |
| 260 | + |
| 261 | + while (matcher.find()) { |
| 262 | + String type = matcher.group(1); |
| 263 | + String operator = matcher.group(2); |
| 264 | + String fieldsStr = matcher.group(3); |
| 265 | + |
| 266 | + Set<String> fields = parseFields(operator, fieldsStr); |
| 267 | + String replacement = serialize(skin, type, fields); |
| 268 | + |
| 269 | + matcher.appendReplacement(result, Matcher.quoteReplacement(replacement)); |
| 270 | + } |
| 271 | + matcher.appendTail(result); |
| 272 | + |
| 273 | + String finalUrl = result.toString(); |
| 274 | + finalUrl = finalUrl.replace("%uuid%", uuid.toString()); |
| 275 | + finalUrl = finalUrl.replace("%username%", username); |
| 276 | + |
| 277 | + return finalUrl; |
| 278 | + } |
| 279 | + |
| 280 | + private static Set<String> parseFields(String operator, String fieldsStr) { |
| 281 | + if (operator == null || fieldsStr == null) { |
| 282 | + return new HashSet<>(ALL_FIELDS); |
| 283 | + } |
| 284 | + |
| 285 | + String[] fieldArray = fieldsStr.split(","); |
| 286 | + Set<String> specifiedFields = new HashSet<>(); |
| 287 | + for (String field : fieldArray) { |
| 288 | + specifiedFields.add(field.trim()); |
| 289 | + } |
| 290 | + |
| 291 | + if ("*".equals(operator)) { |
| 292 | + return specifiedFields; |
| 293 | + } else if ("-".equals(operator)) { |
| 294 | + Set<String> result = new HashSet<>(ALL_FIELDS); |
| 295 | + result.removeAll(specifiedFields); |
| 296 | + return result; |
| 297 | + } |
| 298 | + |
| 299 | + return new HashSet<>(ALL_FIELDS); |
| 300 | + } |
| 301 | + |
| 302 | + private static String serialize(PlayerSkin skin, String type, Set<String> fields) { |
| 303 | + JsonObject json = buildJsonObject(skin, fields); |
| 304 | + String jsonString = json.toString(); |
| 305 | + |
| 306 | + return switch (type.toLowerCase()) { |
| 307 | + case "json" -> jsonString; |
| 308 | + case "base64" -> Base64.getUrlEncoder() |
| 309 | + .withoutPadding() |
| 310 | + .encodeToString(jsonString.getBytes(StandardCharsets.UTF_8)); |
| 311 | + case "query" -> jsonToQueryString(jsonString); |
| 312 | + default -> ""; |
| 313 | + }; |
| 314 | + } |
| 315 | + |
| 316 | + private static JsonObject buildJsonObject(PlayerSkin skin, Set<String> fields) { |
| 317 | + JsonObject json = new JsonObject(); |
| 318 | + |
| 319 | + if (fields.contains("bodyCharacteristic") && skin.bodyCharacteristic != null) { |
| 320 | + json.addProperty("bodyCharacteristic", skin.bodyCharacteristic); |
| 321 | + } |
| 322 | + if (fields.contains("underwear") && skin.underwear != null) { |
| 323 | + json.addProperty("underwear", skin.underwear); |
| 324 | + } |
| 325 | + if (fields.contains("face") && skin.face != null) { |
| 326 | + json.addProperty("face", skin.face); |
| 327 | + } |
| 328 | + if (fields.contains("eyes") && skin.eyes != null) { |
| 329 | + json.addProperty("eyes", skin.eyes); |
| 330 | + } |
| 331 | + if (fields.contains("ears") && skin.ears != null) { |
| 332 | + json.addProperty("ears", skin.ears); |
| 333 | + } |
| 334 | + if (fields.contains("mouth") && skin.mouth != null) { |
| 335 | + json.addProperty("mouth", skin.mouth); |
| 336 | + } |
| 337 | + if (fields.contains("facialHair") && skin.facialHair != null) { |
| 338 | + json.addProperty("facialHair", skin.facialHair); |
| 339 | + } |
| 340 | + if (fields.contains("haircut") && skin.haircut != null) { |
| 341 | + json.addProperty("haircut", skin.haircut); |
| 342 | + } |
| 343 | + if (fields.contains("eyebrows") && skin.eyebrows != null) { |
| 344 | + json.addProperty("eyebrows", skin.eyebrows); |
| 345 | + } |
| 346 | + if (fields.contains("pants") && skin.pants != null) { |
| 347 | + json.addProperty("pants", skin.pants); |
| 348 | + } |
| 349 | + if (fields.contains("overpants") && skin.overpants != null) { |
| 350 | + json.addProperty("overpants", skin.overpants); |
| 351 | + } |
| 352 | + if (fields.contains("undertop") && skin.undertop != null) { |
| 353 | + json.addProperty("undertop", skin.undertop); |
| 354 | + } |
| 355 | + if (fields.contains("overtop") && skin.overtop != null) { |
| 356 | + json.addProperty("overtop", skin.overtop); |
| 357 | + } |
| 358 | + if (fields.contains("shoes") && skin.shoes != null) { |
| 359 | + json.addProperty("shoes", skin.shoes); |
| 360 | + } |
| 361 | + if (fields.contains("headAccessory") && skin.headAccessory != null) { |
| 362 | + json.addProperty("headAccessory", skin.headAccessory); |
| 363 | + } |
| 364 | + if (fields.contains("faceAccessory") && skin.faceAccessory != null) { |
| 365 | + json.addProperty("faceAccessory", skin.faceAccessory); |
| 366 | + } |
| 367 | + if (fields.contains("earAccessory") && skin.earAccessory != null) { |
| 368 | + json.addProperty("earAccessory", skin.earAccessory); |
| 369 | + } |
| 370 | + if (fields.contains("skinFeature") && skin.skinFeature != null) { |
| 371 | + json.addProperty("skinFeature", skin.skinFeature); |
| 372 | + } |
| 373 | + if (fields.contains("gloves") && skin.gloves != null) { |
| 374 | + json.addProperty("gloves", skin.gloves); |
| 375 | + } |
| 376 | + if (fields.contains("cape") && skin.cape != null) { |
| 377 | + json.addProperty("cape", skin.cape); |
| 378 | + } |
| 379 | + |
| 380 | + return json; |
| 381 | + } |
242 | 382 | } |
0 commit comments