|
53 | 53 | import org.bukkit.entity.Painting; |
54 | 54 | import org.bukkit.entity.Player; |
55 | 55 | import org.bukkit.inventory.ItemStack; |
| 56 | +import org.bukkit.inventory.meta.ColorableArmorMeta; |
56 | 57 | import org.bukkit.inventory.meta.ItemMeta; |
| 58 | +import org.bukkit.inventory.meta.trim.ArmorTrim; |
| 59 | +import org.bukkit.inventory.meta.trim.TrimMaterial; |
| 60 | +import org.bukkit.inventory.meta.trim.TrimPattern; |
57 | 61 | import org.bukkit.potion.PotionEffectType; |
58 | 62 | import org.bukkit.potion.PotionType; |
59 | 63 | import org.junit.jupiter.api.Assertions; |
@@ -130,9 +134,9 @@ private static Location getCenterOfChunk(Chunk c) { |
130 | 134 |
|
131 | 135 | private static void testRegistry() { |
132 | 136 | assertNotNull(XRegistry.registryOf(XSound.class)); |
133 | | - assertNotNull(XRegistry.unsafeRegistryOf(XSound.class)); |
| 137 | + assertNotNull(XRegistry.rawRegistryOf(XSound.class)); |
134 | 138 | assertNotNull(XRegistry.registryOf(XAttribute.class)); |
135 | | - assertNotNull(XRegistry.unsafeRegistryOf(XAttribute.class)); |
| 139 | + assertNotNull(XRegistry.rawRegistryOf(XAttribute.class)); |
136 | 140 | } |
137 | 141 |
|
138 | 142 | private static void wrapperTest() { |
@@ -390,76 +394,131 @@ private static void assertMaterial(XMaterial original, Material expect) { |
390 | 394 | Assertions.assertSame(XMaterial.matchXMaterial(selfNameMapped.get().parseItem()), original); |
391 | 395 | } |
392 | 396 |
|
| 397 | + private static final class ItemSerialDual { |
| 398 | + private ItemStack serialized, deserialized; |
| 399 | + } |
| 400 | + |
393 | 401 | private static void testXItemStack() { |
| 402 | + Map<String, ItemSerialDual> map = new HashMap<>(); |
| 403 | + YamlConfiguration serializeConfig; |
394 | 404 | log("Testing XItemStack..."); |
| 405 | + |
395 | 406 | try { |
396 | | - serializeItemStack(); |
397 | | - deserializeItemStack(); |
| 407 | + serializeConfig = serializeItemStack(map); |
| 408 | + deserializeItemStack(map); |
398 | 409 | } catch (IOException | InvalidConfigurationException e) { |
399 | 410 | throw new AssertionFailedError("Failed to serialize/deserialize items", e); |
400 | 411 | } |
| 412 | + |
| 413 | + for (Map.Entry<String, ItemSerialDual> entry : map.entrySet()) { |
| 414 | + ItemSerialDual dual = entry.getValue(); |
| 415 | + if (dual.serialized == null || dual.deserialized == null) { |
| 416 | + log("Either serialized and deserialized doesn't exist for: " + entry.getKey()); |
| 417 | + } else { |
| 418 | + assertTrue(dual.serialized.isSimilar(dual.deserialized), |
| 419 | + () -> "Items for '" + entry.getKey() + "' are not similar:\n\nSerialized: " |
| 420 | + + dual.serialized + "\n\nDeserialized: " + dual.deserialized + '\n'); |
| 421 | + |
| 422 | + ConfigurationSection serializeRedeserialized = serializeConfig.getConfigurationSection(entry.getKey()); |
| 423 | + ItemStack redeserializedItem = XItemStack.deserialize(serializeRedeserialized); |
| 424 | + |
| 425 | + assertTrue(dual.serialized.isSimilar(redeserializedItem), |
| 426 | + () -> "Items for redeserialized '" + entry.getKey() + "' are not similar:\n\nSerialized: " |
| 427 | + + dual.serialized + "\n\nDeserialized: " + redeserializedItem + '\n'); |
| 428 | + } |
| 429 | + } |
401 | 430 | } |
402 | 431 |
|
403 | | - private static void deserializeItemStack() throws IOException, InvalidConfigurationException { |
| 432 | + private static void deserializeItemStack(Map<String, ItemSerialDual> map) throws IOException, InvalidConfigurationException { |
404 | 433 | YamlConfiguration yaml = new YamlConfiguration(); |
405 | 434 | yaml.load(ResourceHelper.getResourceAsFile("itemstack.yml")); |
406 | 435 |
|
407 | 436 | for (String section : yaml.getKeys(false)) { |
408 | 437 | ConfigurationSection itemSection = yaml.getConfigurationSection(section); |
409 | 438 | ItemStack item = XItemStack.deserialize(itemSection); |
410 | | - log("[Item] " + section + ": " + item); |
| 439 | + |
| 440 | + map.compute(section, (k, v) -> { |
| 441 | + if (v == null) v = new ItemSerialDual(); |
| 442 | + v.deserialized = item; |
| 443 | + return v; |
| 444 | + }); |
| 445 | + log("[Deserialized Item] " + section + ": " + item); |
411 | 446 | } |
412 | 447 | } |
413 | 448 |
|
414 | | - private static ItemStack createItem(XMaterial material, String name, Consumer<ItemMeta> metaConsumer) { |
| 449 | + private static ItemStack createItem(XMaterial material, Consumer<ItemMeta> metaConsumer) { |
415 | 450 | ItemStack item = material.parseItem(); |
416 | 451 | ItemMeta meta = item.getItemMeta(); |
417 | | - meta.setDisplayName(name); |
418 | 452 | metaConsumer.accept(meta); |
419 | 453 | item.setItemMeta(meta); |
420 | 454 | return item; |
421 | 455 | } |
422 | 456 |
|
| 457 | + private static boolean metaExists(String className) { |
| 458 | + try { |
| 459 | + Class.forName("org.bukkit.inventory.meta." + className); |
| 460 | + return true; |
| 461 | + } catch (ClassNotFoundException ignored) { |
| 462 | + return false; |
| 463 | + } |
| 464 | + } |
| 465 | + |
423 | 466 | @SuppressWarnings("CodeBlock2Expr") |
424 | | - private static void serializeItemStack() throws IOException { |
| 467 | + private static YamlConfiguration serializeItemStack(Map<String, ItemSerialDual> map) throws IOException { |
425 | 468 | File file = new File(Bukkit.getWorldContainer(), "serialized.yml"); |
426 | 469 | if (!file.exists()) { |
427 | 470 | file.getParentFile().mkdirs(); |
428 | 471 | file.createNewFile(); |
429 | 472 | } |
430 | 473 |
|
431 | | - List<ItemStack> items = new ArrayList<>(); |
432 | | - items.add(createItem(XMaterial.DIAMOND, "Diamonds", meta -> { |
433 | | - meta.setLore(Arrays.asList("Line 1", "", "Line 2")); |
| 474 | + Map<String, ItemStack> items = new HashMap<>(); |
| 475 | + |
| 476 | + items.put("sword", createItem(XMaterial.DIAMOND_SWORD, meta -> { |
| 477 | + meta.setDisplayName("&3Yay"); |
| 478 | + meta.setLore(Arrays.asList("Line 1", "Line 2", " ", "Line 4")); |
434 | 479 | })); |
| 480 | + if (metaExists("ColorableArmorMeta")) { |
| 481 | + items.put("leather-colored-armor-trim", createItem(XMaterial.LEATHER_CHESTPLATE, meta -> { |
| 482 | + ColorableArmorMeta leather = (ColorableArmorMeta) meta; |
| 483 | + leather.setColor(Color.fromRGB(255, 155, 155)); |
| 484 | + leather.setTrim(new ArmorTrim(TrimMaterial.DIAMOND, TrimPattern.SNOUT)); |
| 485 | + })); |
| 486 | + } |
435 | 487 | if (Constants.TEST_MOJANG_API) { |
436 | | - items.add(createItem(XMaterial.PLAYER_HEAD, "head-notch", meta -> { |
| 488 | + items.put("head-notch", createItem(XMaterial.PLAYER_HEAD, meta -> { |
437 | 489 | XSkull.of(meta).profile( |
438 | 490 | Profileable.username("Notch") |
439 | 491 | .transform(ProfileTransformer.includeOriginalValue()) |
440 | 492 | ).apply(); |
441 | 493 | })); |
442 | | - items.add(createItem(XMaterial.PLAYER_HEAD, "head-uuid", meta -> { |
| 494 | + items.put("head-uuid", createItem(XMaterial.PLAYER_HEAD, meta -> { |
443 | 495 | XSkull.of(meta).profile( |
444 | 496 | Profileable.of(UUID.fromString("45d3f688-0765-4725-b5dd-dbc28fdfc9ab")) |
445 | 497 | .transform(ProfileTransformer.includeOriginalValue()) |
446 | 498 | ).apply(); |
447 | 499 | })); |
448 | | - items.add(createItem(XMaterial.PLAYER_HEAD, "head-username-no-transform", meta -> { |
| 500 | + items.put("head-username-no-transform", createItem(XMaterial.PLAYER_HEAD, meta -> { |
449 | 501 | XSkull.of(meta).profile( |
450 | 502 | Profileable.of(UUID.fromString("45d3f688-0765-4725-b5dd-dbc28fdfc9ab")) |
451 | 503 | ).apply(); |
452 | 504 | })); |
453 | 505 | } |
454 | | - items.add(createItem(XMaterial.PLAYER_HEAD, "no-op head", meta -> {})); |
| 506 | + items.put("head-no-op", createItem(XMaterial.PLAYER_HEAD, meta -> {})); |
455 | 507 |
|
456 | 508 | YamlConfiguration yaml = new YamlConfiguration(); |
457 | | - for (ItemStack item : items) { |
458 | | - ItemMeta meta = item.getItemMeta(); |
459 | | - ConfigurationSection section = yaml.createSection(meta.getDisplayName()); |
460 | | - XItemStack.serialize(item, section); |
| 509 | + for (Map.Entry<String, ItemStack> item : items.entrySet()) { |
| 510 | + String sectionName = item.getKey(); |
| 511 | + ConfigurationSection section = yaml.createSection(sectionName); |
| 512 | + |
| 513 | + XItemStack.serialize(item.getValue(), section); |
| 514 | + map.compute(sectionName, (k, v) -> { |
| 515 | + if (v == null) v = new ItemSerialDual(); |
| 516 | + v.serialized = item.getValue(); |
| 517 | + return v; |
| 518 | + }); |
461 | 519 | } |
462 | 520 | yaml.save(file); |
| 521 | + return yaml; |
463 | 522 | } |
464 | 523 |
|
465 | 524 | private static void testSkulls() { |
|
0 commit comments