From fca5da42f4afd42847ac2e9f4c21c052e07b0f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerber=20L=C3=B3r=C3=A1nt=20Viktor?= <17655680+glorantq@users.noreply.github.com> Date: Tue, 19 Mar 2024 17:22:11 +0100 Subject: [PATCH] Change mapping type of BlockEntity lookups This commit changes the type stored for BlockEntity mappings from a class of the type associated with the ID to a method reference to its constructor. --- .../world/block/entity/BannerBlockEntity.java | 2 +- .../core/world/block/entity/BlockEntity.java | 19 +++++++------------ .../world/block/entity/SignBlockEntity.java | 2 +- .../world/block/entity/SkullBlockEntity.java | 2 +- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BannerBlockEntity.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BannerBlockEntity.java index 2c49d634..380d35ef 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BannerBlockEntity.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BannerBlockEntity.java @@ -7,7 +7,7 @@ import java.util.Map; public class BannerBlockEntity extends BlockEntity { private final List patterns = new ArrayList<>(); - private BannerBlockEntity(Map data) { + protected BannerBlockEntity(Map data) { super(data); @SuppressWarnings("unchecked") diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BlockEntity.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BlockEntity.java index c6b8fa9c..baf80be2 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BlockEntity.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/BlockEntity.java @@ -13,15 +13,16 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.StringJoiner; +import java.util.function.Function; import java.util.stream.Collectors; @NBTDeserializer(BlockEntity.BlockEntityDeserializer.class) public class BlockEntity { private static final BlueNBT BLUENBT = new BlueNBT(); - private static final Map> ID_MAPPING = Map.of( - "minecraft:sign", SignBlockEntity.class, - "minecraft:skull", SkullBlockEntity.class, - "minecraft:banner", BannerBlockEntity.class + private static final Map, ? extends BlockEntity>> ID_MAPPING = Map.of( + "minecraft:sign", SignBlockEntity::new, + "minecraft:skull", SkullBlockEntity::new, + "minecraft:banner", BannerBlockEntity::new ); protected final String id; @@ -91,16 +92,10 @@ public class BlockEntity { return null; } - Class dataClass = ID_MAPPING.getOrDefault(id, BlockEntity.class); + Function, ? extends BlockEntity> instance = ID_MAPPING.getOrDefault(id, BlockEntity::new); try { - Constructor constructor = dataClass.getDeclaredConstructor(Map.class); - if (constructor.trySetAccessible()) { - return constructor.newInstance(data); - } - } catch (NoSuchMethodException e) { - Logger.global.logError( - String.format("No constructor in %s that takes a Map!", dataClass.getCanonicalName()), e); + return instance.apply(data); } catch (Exception e) { Logger.global.logError("Failed to instantiate BlockEntity instance!", e); } diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SignBlockEntity.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SignBlockEntity.java index e974abad..fc73cd02 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SignBlockEntity.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SignBlockEntity.java @@ -8,7 +8,7 @@ public class SignBlockEntity extends BlockEntity { private final TextData backText; @SuppressWarnings("unchecked") - private SignBlockEntity(Map data) { + protected SignBlockEntity(Map data) { super(data); // Versions before 1.20 used a different format diff --git a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SkullBlockEntity.java b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SkullBlockEntity.java index 54e75796..7566fd57 100644 --- a/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SkullBlockEntity.java +++ b/BlueMapCore/src/main/java/de/bluecolored/bluemap/core/world/block/entity/SkullBlockEntity.java @@ -12,7 +12,7 @@ public class SkullBlockEntity extends BlockEntity { private final @Nullable String extraType; private final @Nullable SkullOwner skullOwner; - private SkullBlockEntity(Map data) { + protected SkullBlockEntity(Map data) { super(data); this.noteBlockSound = (String) data.get("note_block_sound");