Skip to content

Commit

Permalink
Don't try and 'combine' CombatTagPlugins
Browse files Browse the repository at this point in the history
Manually register default implementations
  • Loading branch information
Techcable committed May 27, 2016
1 parent 6bb5188 commit 726c378
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 142 deletions.
7 changes: 0 additions & 7 deletions base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
<version>2.3.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- Classpath Scanning -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
<scope>compile</scope>
</dependency>
<!-- Compatibility -->
<dependency>
<groupId>net.techcable.spawnshield</groupId>
Expand Down
45 changes: 12 additions & 33 deletions base/src/main/java/net/techcable/spawnshield/SpawnShield.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import com.google.common.collect.ImmutableList;

import net.techcable.spawnshield.combattag.CombatTagPlugin;
import net.techcable.spawnshield.combattag.MultiPluginCombatTagPlugin;
import net.techcable.spawnshield.combattag.legacy.CombatTagLegacySupport;
import net.techcable.spawnshield.combattag.plus.CombatTagPlusSupport;
import net.techcable.spawnshield.combattag.pvpmanager.PvPManagerSupport;
import net.techcable.spawnshield.compat.worldguard6.WorldGuard6Plugin;
import net.techcable.spawnshield.config.SpawnShieldConfig;
import net.techcable.spawnshield.config.SpawnShieldMessages;
Expand All @@ -51,7 +53,6 @@
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.reflections.Reflections;

@Getter
public class SpawnShield extends TechPlugin<SpawnShieldPlayer> {
Expand Down Expand Up @@ -111,7 +112,7 @@ public int getValue() {
}
});
Metrics.Graph pluginGraph = metrics.createGraph("Combat Plugin");
for (CombatTagPlugin plugin : getCombatTagPlugins()) { // Get all plugins, even uninstalled ones
for (CombatTagPlugin plugin : getCombatTagPlugins()) {
pluginGraph.addPlotter(new Metrics.Plotter(plugin.getPlugin().getName()) {
@Override
public int getValue() {
Expand Down Expand Up @@ -164,41 +165,19 @@ public int getValue() {
}

public CombatTagPlugin getCombatTagPlugin() {
ImmutableList<CombatTagPlugin> plugins = getCombatTagPlugins();
switch (plugins.size()) {
case 0:
throw new IllegalStateException("No CombatTagPlugin installed!");
case 1:
return plugins.get(0);
default:
return new MultiPluginCombatTagPlugin(plugins);
}
return getServer().getServicesManager().getRegistrations(CombatTagPlugin.class).stream()
.filter((combatService) -> combatService.getProvider().isInstalled()) // Only consider installed plugins :)
.max((first, second) -> first.getPriority().compareTo(second.getPriority())) // Get the highest priortiy service
.map(RegisteredServiceProvider::getProvider)
.orElseThrow(() -> new IllegalStateException("No CombatTagPlugin installed!"));
}


public ImmutableList<CombatTagPlugin> getCombatTagPlugins() {
if (getServer().getServicesManager().getRegistrations(this).isEmpty()) {
// Search classpath for our own impls
String className = CombatTagPlugin.class.getName();
String packageName = className.substring(0, className.lastIndexOf('.') - 1);
Set<Class<? extends CombatTagPlugin>> pluginTypes = new Reflections(packageName).getSubTypesOf(CombatTagPlugin.class);
for (Class<? extends CombatTagPlugin> pluginType : pluginTypes) {
if (pluginType == MultiPluginCombatTagPlugin.class) continue;
CombatTagPlugin plugin;
try {
Constructor<? extends CombatTagPlugin> c = pluginType.getConstructor();
c.setAccessible(true);
plugin = c.newInstance();
} catch (NoSuchMethodException | InstantiationException e) {
continue;
} catch (IllegalAccessException e) {
throw new RuntimeException("Unable to call setAccessible() on " + pluginType.getSimpleName());
} catch (InvocationTargetException e) {
Throwable cause = e.getTargetException();
throw new RuntimeException("new " + pluginType.getSimpleName() + "() threw an exception", cause);
}
getServer().getServicesManager().register(CombatTagPlugin.class, plugin, this, ServicePriority.Normal);
}
getServer().getServicesManager().register(CombatTagPlugin.class, new CombatTagLegacySupport(), this, ServicePriority.Low);
getServer().getServicesManager().register(CombatTagPlugin.class, new PvPManagerSupport(), this, ServicePriority.Normal);
getServer().getServicesManager().register(CombatTagPlugin.class, new CombatTagPlusSupport(), this, ServicePriority.Normal);
}
return getServer().getServicesManager().getRegistrations(CombatTagPlugin.class).stream()
.map(RegisteredServiceProvider::getProvider)
Expand Down

This file was deleted.

0 comments on commit 726c378

Please sign in to comment.