From 00498f9898883fb3d6e624c51a3301ca3cd4b814 Mon Sep 17 00:00:00 2001 From: Fulminazzo Date: Tue, 5 Mar 2024 13:51:12 +0100 Subject: [PATCH] Added `.item:serializer` module to automatically load and save item objects. Added parsers: - [EnchantmentParser](item/serializer/src/main/java/it/angrybear/serializers/EnchantmentParser.java); - [ItemParser](item/serializer/src/main/java/it/angrybear/serializers/ItemParser.java); - [ShapeParser](item/serializer/src/main/java/it/angrybear/serializers/ShapeParser.java); - [ShapedRecipeParser](item/serializer/src/main/java/it/angrybear/serializers/ShapedRecipeParser.java); - [ShapelessRecipeParser](item/serializer/src/main/java/it/angrybear/serializers/ShapelessRecipeParser.java); - [FurnaceRecipeParser](item/serializer/src/main/java/it/angrybear/serializers/FurnaceRecipeParser.java). Added [YAGLParser](item/serializer/src/main/java/it/angrybear/serializers/YAGLParser.java) to automatically load parsers. Moved everything to package `it.angrybear.yagl`. Reworked `Item#copy()` method: it will try to get a default empty constructor from the current class. If it fails, it defaults to `ItemImpl`. Fixed `Item#copy(Class)` with automatic resolution of interfaces: passing `Item` or `BukkitItem` will convert them into `ItemImpl` and `BukkitItemImpl`. Fixed various bugs in **ShapedRecipe**. --- .../java/it/angrybear/yagl/items/BukkitItemImpl.java | 2 +- item/src/main/java/it/angrybear/yagl/items/Item.java | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/item/bukkit/src/main/java/it/angrybear/yagl/items/BukkitItemImpl.java b/item/bukkit/src/main/java/it/angrybear/yagl/items/BukkitItemImpl.java index b9d47e71..c5f28ba0 100644 --- a/item/bukkit/src/main/java/it/angrybear/yagl/items/BukkitItemImpl.java +++ b/item/bukkit/src/main/java/it/angrybear/yagl/items/BukkitItemImpl.java @@ -69,6 +69,6 @@ public BukkitItem setUnbreakable(final boolean unbreakable) { @Override public BukkitItem copy() { - return super.copy(BukkitItemImpl.class); + return (BukkitItem) super.copy(); } } diff --git a/item/src/main/java/it/angrybear/yagl/items/Item.java b/item/src/main/java/it/angrybear/yagl/items/Item.java index c4caf11b..4086b01f 100644 --- a/item/src/main/java/it/angrybear/yagl/items/Item.java +++ b/item/src/main/java/it/angrybear/yagl/items/Item.java @@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; @@ -397,7 +398,13 @@ default Item removeItemFlags(final @NotNull Collection itemFlags) { * @return the item */ default Item copy() { - return copy(ItemImpl.class); + Class clazz = this.getClass(); + try { + Constructor constructor = ReflectionUtils.getConstructor(clazz); + } catch (Exception e) { + clazz = ItemImpl.class; + } + return copy(clazz); } /**