|
37 | 37 | import com.google.common.collect.ImmutableList; |
38 | 38 |
|
39 | 39 | import net.techcable.spawnshield.combattag.CombatTagPlugin; |
40 | | -import net.techcable.spawnshield.combattag.MultiPluginCombatTagPlugin; |
| 40 | +import net.techcable.spawnshield.combattag.legacy.CombatTagLegacySupport; |
| 41 | +import net.techcable.spawnshield.combattag.plus.CombatTagPlusSupport; |
| 42 | +import net.techcable.spawnshield.combattag.pvpmanager.PvPManagerSupport; |
41 | 43 | import net.techcable.spawnshield.compat.worldguard6.WorldGuard6Plugin; |
42 | 44 | import net.techcable.spawnshield.config.SpawnShieldConfig; |
43 | 45 | import net.techcable.spawnshield.config.SpawnShieldMessages; |
|
51 | 53 | import org.bukkit.plugin.RegisteredServiceProvider; |
52 | 54 | import org.bukkit.plugin.ServicePriority; |
53 | 55 | import org.bukkit.plugin.java.JavaPlugin; |
54 | | -import org.reflections.Reflections; |
55 | 56 |
|
56 | 57 | @Getter |
57 | 58 | public class SpawnShield extends TechPlugin<SpawnShieldPlayer> { |
@@ -111,7 +112,7 @@ public int getValue() { |
111 | 112 | } |
112 | 113 | }); |
113 | 114 | Metrics.Graph pluginGraph = metrics.createGraph("Combat Plugin"); |
114 | | - for (CombatTagPlugin plugin : getCombatTagPlugins()) { // Get all plugins, even uninstalled ones |
| 115 | + for (CombatTagPlugin plugin : getCombatTagPlugins()) { |
115 | 116 | pluginGraph.addPlotter(new Metrics.Plotter(plugin.getPlugin().getName()) { |
116 | 117 | @Override |
117 | 118 | public int getValue() { |
@@ -164,41 +165,19 @@ public int getValue() { |
164 | 165 | } |
165 | 166 |
|
166 | 167 | public CombatTagPlugin getCombatTagPlugin() { |
167 | | - ImmutableList<CombatTagPlugin> plugins = getCombatTagPlugins(); |
168 | | - switch (plugins.size()) { |
169 | | - case 0: |
170 | | - throw new IllegalStateException("No CombatTagPlugin installed!"); |
171 | | - case 1: |
172 | | - return plugins.get(0); |
173 | | - default: |
174 | | - return new MultiPluginCombatTagPlugin(plugins); |
175 | | - } |
| 168 | + return getServer().getServicesManager().getRegistrations(CombatTagPlugin.class).stream() |
| 169 | + .filter((combatService) -> combatService.getProvider().isInstalled()) // Only consider installed plugins :) |
| 170 | + .max((first, second) -> first.getPriority().compareTo(second.getPriority())) // Get the highest priortiy service |
| 171 | + .map(RegisteredServiceProvider::getProvider) |
| 172 | + .orElseThrow(() -> new IllegalStateException("No CombatTagPlugin installed!")); |
176 | 173 | } |
177 | 174 |
|
178 | 175 |
|
179 | 176 | public ImmutableList<CombatTagPlugin> getCombatTagPlugins() { |
180 | 177 | if (getServer().getServicesManager().getRegistrations(this).isEmpty()) { |
181 | | - // Search classpath for our own impls |
182 | | - String className = CombatTagPlugin.class.getName(); |
183 | | - String packageName = className.substring(0, className.lastIndexOf('.') - 1); |
184 | | - Set<Class<? extends CombatTagPlugin>> pluginTypes = new Reflections(packageName).getSubTypesOf(CombatTagPlugin.class); |
185 | | - for (Class<? extends CombatTagPlugin> pluginType : pluginTypes) { |
186 | | - if (pluginType == MultiPluginCombatTagPlugin.class) continue; |
187 | | - CombatTagPlugin plugin; |
188 | | - try { |
189 | | - Constructor<? extends CombatTagPlugin> c = pluginType.getConstructor(); |
190 | | - c.setAccessible(true); |
191 | | - plugin = c.newInstance(); |
192 | | - } catch (NoSuchMethodException | InstantiationException e) { |
193 | | - continue; |
194 | | - } catch (IllegalAccessException e) { |
195 | | - throw new RuntimeException("Unable to call setAccessible() on " + pluginType.getSimpleName()); |
196 | | - } catch (InvocationTargetException e) { |
197 | | - Throwable cause = e.getTargetException(); |
198 | | - throw new RuntimeException("new " + pluginType.getSimpleName() + "() threw an exception", cause); |
199 | | - } |
200 | | - getServer().getServicesManager().register(CombatTagPlugin.class, plugin, this, ServicePriority.Normal); |
201 | | - } |
| 178 | + getServer().getServicesManager().register(CombatTagPlugin.class, new CombatTagLegacySupport(), this, ServicePriority.Low); |
| 179 | + getServer().getServicesManager().register(CombatTagPlugin.class, new PvPManagerSupport(), this, ServicePriority.Normal); |
| 180 | + getServer().getServicesManager().register(CombatTagPlugin.class, new CombatTagPlusSupport(), this, ServicePriority.Normal); |
202 | 181 | } |
203 | 182 | return getServer().getServicesManager().getRegistrations(CombatTagPlugin.class).stream() |
204 | 183 | .map(RegisteredServiceProvider::getProvider) |
|
0 commit comments