#John's black box cast issue
1 messages Β· Page 1 of 1 (latest)
Johnbork is experiencing a ClassCastException issue in his code. The error occurs when trying to set a value in a PersistentDataContainer using a StorageKey.
The StorageKey is defined as follows:
@RequiredArgsConstructor
@Getter
public enum StorageKey implements IPDCKey {
DATA("data", DataType.asEnumMap(DataTypeImpl.class, DataType.FLOAT));
private final @NotNull String key;
private final @NotNull PersistentDataType<?, ?> type;
private final @NotNull RPComputer3 instance = RPComputer3.instance;
}
The DataType.asEnumMap(DataTypeImpl.class, DataType.FLOAT) call returns a MapDataType<EnumMap<DataTypeImpl, Float>, DataTypeImpl, Float>.
The issue arises when trying to set a value using this StorageKey in the following method:
@Override
public @NotNull ItemStack getItemStack() {
ItemStack itemStack = super.getItemStack();
itemStack.editMeta(meta -> StorageKey.DATA.set(meta, this.data.toEnumMap()));
return itemStack;
}
set definition:
default <T> void set(@Nullable PersistentDataHolder holder, T value) {
if (holder != null) {
PersistentDataType<Object, T> type = this.getType();
holder.getPersistentDataContainer().set(this.getNamespace(), type, value);
}
}
The this.data.toEnumMap() call returns an EnumMap<DataTypeImpl, AbstractDataType>. The ClassCastException occurs because the types do not match.
Johnbork mentions that the code used to work when the enum was named StorageDataType and the PersistentDataType was PersistentDataType.FLOAT. The enum was later renamed to DataTypeImpl and the PersistentDataType was changed to DataType.FLOAT, which is a constant that points to PersistentDataType.FLOAT.
The issue seems to be related to the type mismatch between the EnumMap<DataTypeImpl, Float> expected by the StorageKey and the EnumMap<DataTypeImpl, AbstractDataType> returned by this.data.toEnumMap().
Stack trace: https://paste.md-5.net/eqegarivaf.rb
thank you javier π
so question is, if they are using Alex's lib, what is the reason for changing it?
are they wanting to add their own datatype?
im not entirely sure what you mean
ah btw John
you can change toEnumMap to return the float enummap again btw, since that wasn't useful at all
package index
if you need the javadocs
I am not sure why the name needed to be changed though, or what exactly needs to be done
i dont think thats the problem ngl
but persistentdatatype relates to bukkits api just fyi
alright I'm done cooking, let's go to vc I guess
probably going to end up talking to you here since I don't know if you'll understand my shitty english pronounciation lol
kk give me 2 mins
but if you are wanting to create your own generic type, his lib allows you to do that
aight
I guess now I am just curious in what it is you are trying to accomplish π€
im in
Let's go to IJ
or well, first reproduce the issue
when you click what did the error pop up
@brave topaz @sly jolt while we don't know why, the issue was in a completely different place than the stack trace was pointing at
it was in PCItem#createFromItem, which rebuilds the item kinda I guess
it was casting to AbstractDataType there, but well it got solved now
Yeah itβs funny because if the stacktrace mentioned it, it wouldve been fixed in 5 seconds π€¦ββοΈ
Huge shout out to Javier tho, bros a legend π
so it was the itemstack
which, the stacktrace kinda did point to it
huh, where
Oh yeah it did tbf