Skip to content

Commit 9236245

Browse files
committed
WindLeaf来力
1 parent 1c72418 commit 9236245

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/main/java/top/mpt/xzystudio/flywars/game/items/ArrowEntry.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
import org.bukkit.entity.Arrow;
44
import org.bukkit.entity.Player;
5+
import top.mpt.xzystudio.flywars.utils.ClassUtils;
56
import top.mpt.xzystudio.flywars.utils.ConfigUtils;
67

78
public abstract class ArrowEntry {
8-
public String path;
9-
10-
public void ArrowEntry(String path){
11-
this.path = path;
12-
}
13-
149
/**
1510
* 开始处理特殊各种箭
1611
* @param shooter 射击的玩家

src/main/java/top/mpt/xzystudio/flywars/utils/ClassUtils.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,37 @@ public static <T> ArrayList<Class<T>> getSubClasses(Class<T> clazz, String packa
2929
.distinct().collect(Collectors.toCollection(ArrayList::new));
3030
}
3131

32-
public static <T> T newInstance(Class<T> clazz) {
32+
/**
33+
* 对象类指定根据初始化对象实例
34+
* @param clazz 类对象
35+
* @param args 参数,可为空
36+
* @return 初始化后的实例对象
37+
* @param <T> 泛♂型
38+
*/
39+
public static <T> T newInstance(Class<T> clazz, Object... args) {
3340
try {
34-
return clazz.newInstance();
35-
} catch (InstantiationException | IllegalAccessException e) {
41+
if (args.length == 0) return clazz.newInstance();
42+
else {
43+
Constructor<T> constructor = clazz.getDeclaredConstructor(
44+
(Class<?>[]) Arrays.stream(args).map(o -> (Class<?>) o.getClass()).toArray()
45+
);
46+
return constructor.newInstance(args);
47+
}
48+
} catch (Exception e) {
3649
LoggerUtils.warning("#RED#创建对象失败:%s", e.getMessage());
3750
}
3851
return null;
3952
}
53+
54+
/**
55+
* 根据注解类获取指定类的注解对象
56+
* @param clazz 类指定
57+
* @param annotationClass 类注解
58+
* @return 获取到的注解对象,如果不存在则为 `null`
59+
* @param <T> 注解泛型
60+
*/
61+
public static <T> T getAnnotation(Class<?> clazz, Class<T> annotationClass) {
62+
Annotation result = clazz.getAnnotation((Class<Annotation>) annotationClass);
63+
return result == null ? null : (T) result;
64+
}
4065
}

0 commit comments

Comments
 (0)