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.
This commit is contained in:
parent
574bc6f9e8
commit
fca5da42f4
|
@ -7,7 +7,7 @@ import java.util.Map;
|
||||||
public class BannerBlockEntity extends BlockEntity {
|
public class BannerBlockEntity extends BlockEntity {
|
||||||
private final List<Pattern> patterns = new ArrayList<>();
|
private final List<Pattern> patterns = new ArrayList<>();
|
||||||
|
|
||||||
private BannerBlockEntity(Map<String, Object> data) {
|
protected BannerBlockEntity(Map<String, Object> data) {
|
||||||
super(data);
|
super(data);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
@ -13,15 +13,16 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@NBTDeserializer(BlockEntity.BlockEntityDeserializer.class)
|
@NBTDeserializer(BlockEntity.BlockEntityDeserializer.class)
|
||||||
public class BlockEntity {
|
public class BlockEntity {
|
||||||
private static final BlueNBT BLUENBT = new BlueNBT();
|
private static final BlueNBT BLUENBT = new BlueNBT();
|
||||||
private static final Map<String, Class<? extends BlockEntity>> ID_MAPPING = Map.of(
|
private static final Map<String, Function<Map<String, Object>, ? extends BlockEntity>> ID_MAPPING = Map.of(
|
||||||
"minecraft:sign", SignBlockEntity.class,
|
"minecraft:sign", SignBlockEntity::new,
|
||||||
"minecraft:skull", SkullBlockEntity.class,
|
"minecraft:skull", SkullBlockEntity::new,
|
||||||
"minecraft:banner", BannerBlockEntity.class
|
"minecraft:banner", BannerBlockEntity::new
|
||||||
);
|
);
|
||||||
|
|
||||||
protected final String id;
|
protected final String id;
|
||||||
|
@ -91,16 +92,10 @@ public class BlockEntity {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<? extends BlockEntity> dataClass = ID_MAPPING.getOrDefault(id, BlockEntity.class);
|
Function<Map<String, Object>, ? extends BlockEntity> instance = ID_MAPPING.getOrDefault(id, BlockEntity::new);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Constructor<? extends BlockEntity> constructor = dataClass.getDeclaredConstructor(Map.class);
|
return instance.apply(data);
|
||||||
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);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Logger.global.logError("Failed to instantiate BlockEntity instance!", e);
|
Logger.global.logError("Failed to instantiate BlockEntity instance!", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ public class SignBlockEntity extends BlockEntity {
|
||||||
private final TextData backText;
|
private final TextData backText;
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private SignBlockEntity(Map<String, Object> data) {
|
protected SignBlockEntity(Map<String, Object> data) {
|
||||||
super(data);
|
super(data);
|
||||||
|
|
||||||
// Versions before 1.20 used a different format
|
// Versions before 1.20 used a different format
|
||||||
|
|
|
@ -12,7 +12,7 @@ public class SkullBlockEntity extends BlockEntity {
|
||||||
private final @Nullable String extraType;
|
private final @Nullable String extraType;
|
||||||
private final @Nullable SkullOwner skullOwner;
|
private final @Nullable SkullOwner skullOwner;
|
||||||
|
|
||||||
private SkullBlockEntity(Map<String, Object> data) {
|
protected SkullBlockEntity(Map<String, Object> data) {
|
||||||
super(data);
|
super(data);
|
||||||
|
|
||||||
this.noteBlockSound = (String) data.get("note_block_sound");
|
this.noteBlockSound = (String) data.get("note_block_sound");
|
||||||
|
|
Loading…
Reference in New Issue