Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions common/src/main/java/taboolib/common/OpenResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tabooproject.reflex.Reflex;

import java.lang.reflect.Field;

public class OpenResult {

Expand Down Expand Up @@ -77,8 +78,30 @@ public static OpenResult failed() {
* 从其他插件的 OpenResult 转换为当前插件的 OpenResult
*/
public static OpenResult cast(Object source) {
Object successful = Reflex.Companion.getLocalProperty(source, "successful");
Object value = Reflex.Companion.getLocalProperty(source, "value");
if (source == null) {
return failed();
}
if (source instanceof OpenResult) {
return (OpenResult) source;
}
Object successful = readField(source, "successful");
Object value = readField(source, "value");
return new OpenResult(Boolean.TRUE.equals(successful), value);
}
}

private static Object readField(Object source, String fieldName) {
Class<?> type = source.getClass();
while (type != null) {
try {
Field field = type.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(source);
} catch (NoSuchFieldException ignored) {
type = type.getSuperclass();
} catch (IllegalAccessException ex) {
throw new IllegalStateException("Cannot access field '" + fieldName + "' in " + source.getClass().getName(), ex);
}
}
throw new IllegalArgumentException("Field '" + fieldName + "' not found in " + source.getClass().getName());
}
}
Loading