Fix reflection in PlayerDataStorage + new wrapOptional method in reflection library
This commit is contained in:
parent
e16487431d
commit
fcac9af7d1
@ -16,13 +16,12 @@ public class PlayerDataStorage extends ReflectWrapper {
|
|||||||
public static final ReflectMethod<?> load = wrapEx(() -> REFLECT.method("load", String.class, String.class));
|
public static final ReflectMethod<?> load = wrapEx(() -> REFLECT.method("load", String.class, String.class));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param playerName the name of the player: used for loading error message and of offline UUID generation.
|
* @param playerName the name of the player: used for loading error message and for offline UUID generation.
|
||||||
* @param playerId UUID of a player as it is used to name the player data file (UUID.toString())
|
* @param playerId UUID of a player as it is used to name the player data file (UUID.toString()).
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Optional<CompoundTag> load(String playerName, String playerId) {
|
public Optional<CompoundTag> load(String playerName, String playerId) {
|
||||||
return ((Optional<Object>) wrapReflectEx(() -> load.invoke(__getRuntimeInstance(), playerId)))
|
return wrapOptional((Optional<Object>) wrapReflectEx(() -> load.invoke(__getRuntimeInstance(), playerName, playerId)), CompoundTag.class);
|
||||||
.map(o -> wrap(o, CompoundTag.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import fr.pandacube.lib.reflect.ReflectConstructor;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
|
import static fr.pandacube.lib.util.ThrowableUtil.wrapEx;
|
||||||
|
|
||||||
@ -98,7 +99,7 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
|||||||
Class<?> runtimeClass = runtimeObj.getClass();
|
Class<?> runtimeClass = runtimeObj.getClass();
|
||||||
Class<?> expectedRuntimeClass = (expectedWrapperClass == null) ? null : WrapperRegistry.getRuntimeClassOfWrapperClass(expectedWrapperClass);
|
Class<?> expectedRuntimeClass = (expectedWrapperClass == null) ? null : WrapperRegistry.getRuntimeClassOfWrapperClass(expectedWrapperClass);
|
||||||
if (expectedRuntimeClass != null && !expectedRuntimeClass.isAssignableFrom(runtimeClass)) {
|
if (expectedRuntimeClass != null && !expectedRuntimeClass.isAssignableFrom(runtimeClass)) {
|
||||||
throw new ClassCastException("Runtime class " + runtimeClass + " is not a sub-class or a sub-interface of expected runtime class " + expectedRuntimeClass + "" +
|
throw new ClassCastException("Runtime class " + runtimeClass + " is not a sub-class or a sub-interface of expected runtime class " + expectedRuntimeClass +
|
||||||
" (expected wrapper class " + expectedWrapperClass + ").");
|
" (expected wrapper class " + expectedWrapperClass + ").");
|
||||||
}
|
}
|
||||||
Class<? extends ReflectWrapperI> wrapperClass = WrapperRegistry.getWrapperOfRuntimeClass(runtimeClass);
|
Class<? extends ReflectWrapperI> wrapperClass = WrapperRegistry.getWrapperOfRuntimeClass(runtimeClass);
|
||||||
@ -139,6 +140,27 @@ public abstract class ReflectWrapper implements ReflectWrapperI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the provided {@link Optional} containing a runtime object to the reflection wrapper.
|
||||||
|
* @param runtimeOptional the object to wrap.
|
||||||
|
* @param expectedWrapperClass the reflection wrapper class expected to be returned.
|
||||||
|
* @param <W> the type of the reflection wrapper.
|
||||||
|
* @throws ClassCastException if the runtime class of the object is not handled by the expected wrapper class or its
|
||||||
|
* subclasses.
|
||||||
|
* @throws IllegalArgumentException if the runtime class of the object is not handled by any of the registered
|
||||||
|
* wrapper classes.
|
||||||
|
* @implNote Despite that the convention is to not assign null values as Optional, this method properly handles null
|
||||||
|
* value for parameter runtimeOptional.
|
||||||
|
* @return an optional of the wrapper.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("OptionalAssignedToNull")
|
||||||
|
public static <W extends ReflectWrapperI> Optional<W> wrapOptional(Optional<Object> runtimeOptional, Class<W> expectedWrapperClass) {
|
||||||
|
return runtimeOptional == null ? null : runtimeOptional.map(o -> wrap(o, expectedWrapperClass));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user