RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
CombatListenerManager.java
1package com.osroyale.game.world.entity.combat.attack.listener;
2
3import com.osroyale.game.world.entity.combat.strategy.CombatStrategy;
4import com.osroyale.game.world.entity.mob.npc.Npc;
5import com.osroyale.game.world.entity.mob.player.Player;
6import io.github.classgraph.*;
7import org.apache.logging.log4j.LogManager;
8import org.apache.logging.log4j.Logger;
9
10import java.lang.annotation.Annotation;
11import java.lang.reflect.Constructor;
12import java.util.*;
13
46
47public final class CombatListenerManager {
48
49 private static final Logger logger = LogManager.getLogger(CombatListenerManager.class);
50
51 private static final Map<Integer, Set<CombatListenerSet>> ITEM_LISTENERS = new HashMap<>();
52 public static final Map<Integer, CombatListener<Npc>> NPC_LISTENERS = new HashMap<>();
53 public static final Map<Integer, CombatStrategy<Npc>> NPC_STRATEGIES = new HashMap<>();
54
55 public static void load() {
56 loadItems("com.osroyale.game.world.entity.combat.attack.listener.item");
57 loadNpcs("com.osroyale.game.world.entity.combat.attack.listener.npc");
58 }
59
60 private static void loadItems(final String pkg) {
61 try (final ScanResult scanResult =
62 new ClassGraph()
63 .enableClassInfo()
64 .enableMethodInfo()
65 .enableAnnotationInfo()
66 .acceptPackages(pkg)
67 .scan()) {
68 for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(ItemCombatListenerSignature.class)) {
69 try {
70 final MethodInfoList constructorInfos = classInfo.getDeclaredConstructorInfo();
71 final MethodInfo constructorInfo = constructorInfos.get(0);
72 final Constructor<?> constructor = constructorInfo.loadClassAndGetConstructor();
73
74 final Object instance = constructor.newInstance();
75 @SuppressWarnings("unchecked") final CombatListener<Player> listener = (CombatListener<Player>) instance;
76
77 final AnnotationInfo annotationInfo = classInfo.getAnnotationInfo(ItemCombatListenerSignature.class);
78 final Annotation annotation = annotationInfo.loadClassAndInstantiate();
80
81 for (int item : meta.items()) {
82 Set<CombatListenerSet> set = ITEM_LISTENERS.getOrDefault(item, new HashSet<>());
83 set.add(new CombatListenerSet(meta.items(), meta.requireAll(), listener));
84 ITEM_LISTENERS.put(item, set);
85 }
86 } catch (Exception ex) {
87 logger.error(String.format("Error loading item set combat listener=%s", classInfo.getSimpleName()), ex);
88 }
89 }
90 }
91 logger.info(String.format("Loaded: %d item set combat listeners.", ITEM_LISTENERS.size()));
92 }
93
94 private static void loadNpcs(final String pkg) {
95 try (final ScanResult scanResult =
96 new ClassGraph()
97 .enableClassInfo()
98 .enableMethodInfo()
99 .enableAnnotationInfo()
100 .acceptPackages(pkg)
101 .scan()) {
102 for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(NpcCombatListenerSignature.class)) {
103 try {
104 final MethodInfoList constructorInfos = classInfo.getDeclaredConstructorInfo();
105 final MethodInfo constructorInfo = constructorInfos.get(0);
106 final Constructor<?> constructor = constructorInfo.loadClassAndGetConstructor();
107
108 final Object instance = constructor.newInstance();
109 @SuppressWarnings("unchecked") final CombatListener<Npc> listener = (CombatListener<Npc>) instance;
110
111 final AnnotationInfo annotationInfo = classInfo.getAnnotationInfo(NpcCombatListenerSignature.class);
112 final Annotation annotation = annotationInfo.loadClassAndInstantiate();
114
115 for (int npc : meta.npcs()) {
116 NPC_LISTENERS.put(npc, listener);
117 }
118 } catch (Exception ex) {
119 logger.error(String.format("Error loading npc combat listener=%s", classInfo.getSimpleName()), ex);
120 }
121 }
122 }
123 logger.info(String.format("Loaded: %d npc combat listeners.", NPC_LISTENERS.size()));
124 }
125
126 public static void addListener(Player player, int id) {
127 Set<CombatListenerSet> sets = ITEM_LISTENERS.get(id);
128
129 if (sets == null) {
130 return;
131 }
132
133 for (CombatListenerSet set : sets) {
134 if (set.requireAll && !player.equipment.containsAll(set.set)) {
135 continue;
136 }
137
138 if (!set.requireAll && !player.equipment.containsAny(set.set)) {
139 continue;
140 }
141
142 player.getCombat().addListener(set.listener);
143 }
144 }
145
146 public static void removeListener(Player player, int id) {
147 Set<CombatListenerSet> sets = ITEM_LISTENERS.get(id);
148
149 if (sets == null) {
150 return;
151 }
152
153 for (CombatListenerSet set : sets) {
154 if (set.requireAll && player.equipment.containsAll(set.set)) {
155 continue;
156 }
157
158 if (!set.requireAll && player.equipment.containsAny(set.set)) {
159 continue;
160 }
161
162 player.getCombat().removeListener(set.listener);
163 }
164 }
165
166 public static final class CombatListenerSet {
167 private final int[] set;
168 private final boolean requireAll;
169 private final CombatListener<Player> listener;
170
171 CombatListenerSet(int[] set, boolean requireAll, CombatListener<Player> listener) {
172 this.set = set;
173 this.requireAll = requireAll;
174 this.listener = listener;
175 }
176
177 @Override
178 public boolean equals(Object obj) {
179 if (obj == this)
180 return true;
181 if (obj instanceof CombatListenerSet) {
182 CombatListenerSet other = (CombatListenerSet) obj;
183 return Arrays.equals(other.set, set) && other.requireAll == requireAll;
184 }
185 return false;
186 }
187
188 @Override
189 public int hashCode() {
190 return Objects.hash(set, requireAll);
191 }
192
193 @Override
194 public String toString() {
195 return "ItemSet[set=" + Arrays.toString(set) + ", requireAll=" + requireAll + "]";
196 }
197 }
198}