Attachment.create triggers ClassCastException
Closed this issue ยท 2 comments
In AttachmentChange.create, first line of a method contains unchecked cast to AttachmentTypeImpl:
@SuppressWarnings("unchecked")
public static AttachmentChange create(AttachmentTargetInfo<?> targetInfo, AttachmentType<?> type, @Nullable Object value, RegistryAccess dynamicRegistryManager) {
StreamCodec<? super RegistryFriendlyByteBuf, Object> codec = (StreamCodec<? super RegistryFriendlyByteBuf, Object>) ((AttachmentTypeImpl<?>) type).packetCodec();
Objects.requireNonNull(codec, "attachment packet codec cannot be null");
Objects.requireNonNull(dynamicRegistryManager, "dynamic registry manager cannot be null");
}If we implement AttachmentType with synced set to true:
private static class DataType implements AttachmentType<T> {
private static final DataType INSTANCE = new DataType();
private DataType() {}
@Override
public ResourceLocation identifier() {
return ResourceLocation.tryBuild(ExampleMod.MOD_ID, "mod");
}
@Override
public @Nullable Codec<T> persistenceCodec() {
return CODEC;
}
@Override
public @Nullable Supplier<T> initializer() {
return T::new;
}
@Override
public boolean isSynced() {
return true;
}
@Override
public boolean copyOnDeath() {
return false;
}
}We will crash during an attempt to getOrCreate: level.getAttachedOrCreate(DataType.INSTANCE);
I understand that API is experimental and it's not encouraged to implement AttachmentType, but I wanted to point out the issue anyway.
you'll notice that AttachmentType is marked with ApiStatus.NonExtendable here. This tells consumers that the api is non-extensible, and that undefined behavior or crashes may occur if they do