Registered advanced ban listeners

Affects issues:
- Close #1135
This commit is contained in:
Rsl1122 2019-08-12 11:04:03 +03:00
parent b87f715492
commit e100068e78
2 changed files with 70 additions and 52 deletions

View File

@ -2,7 +2,7 @@ dependencies {
compile project(path: ":api") compile project(path: ":api")
compile 'com.djrapitops:Extension-AAC:4.0.11-R0.1' compile 'com.djrapitops:Extension-AAC:4.0.11-R0.1'
compile 'com.djrapitops:Extension-AdvancedAchievements:1.1-R0.3' compile 'com.djrapitops:Extension-AdvancedAchievements:1.1-R0.3'
compile 'com.djrapitops:Extension-AdvancedBan:2.1.5-R0.6' compile 'com.djrapitops:Extension-AdvancedBan:2.1.5-R0.7'
compile 'com.djrapitops:Extension-ASkyBlock:3.0.9.4-R0.4' compile 'com.djrapitops:Extension-ASkyBlock:3.0.9.4-R0.4'
compile 'com.djrapitops:Extension-BanManager:5.15.0-R0.5' compile 'com.djrapitops:Extension-BanManager:5.15.0-R0.5'
compile 'com.djrapitops:Extension-CoreProtect:2.16.0-R0.3' compile 'com.djrapitops:Extension-CoreProtect:2.16.0-R0.3'

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.extension.implementation; package com.djrapitops.plan.extension.implementation;
import com.djrapitops.extension.*; import com.djrapitops.extension.*;
import com.djrapitops.plan.extension.Caller;
import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.ExtensionService; import com.djrapitops.plan.extension.ExtensionService;
import com.djrapitops.plan.extension.extractor.ExtensionExtractor; import com.djrapitops.plan.extension.extractor.ExtensionExtractor;
@ -25,7 +26,8 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.BiConsumer;
import java.util.function.Function;
/** /**
* In charge of registering built in {@link com.djrapitops.plan.extension.DataExtension} implementations. * In charge of registering built in {@link com.djrapitops.plan.extension.DataExtension} implementations.
@ -37,6 +39,7 @@ public class ExtensionRegister {
private IllegalStateException registerException; private IllegalStateException registerException;
private Set<String> disabledExtensions; private Set<String> disabledExtensions;
private ExtensionService extensionService;
@Inject @Inject
public ExtensionRegister() { public ExtensionRegister() {
@ -45,68 +48,83 @@ public class ExtensionRegister {
public void registerBuiltInExtensions(Set<String> disabledExtensions) { public void registerBuiltInExtensions(Set<String> disabledExtensions) {
this.disabledExtensions = disabledExtensions; this.disabledExtensions = disabledExtensions;
// No need to catch exceptions here, extensionService = ExtensionService.getInstance();
// registerBuiltInExtensions method will not be called unless Plan has enabled properly
ExtensionService extensionService = ExtensionService.getInstance();
register(new AACExtensionFactory()::createExtension, AACExtensionFactory.class); register(new AACExtensionFactory(), AACExtensionFactory::createExtension);
register(new AdvancedAchievementsExtensionFactory()::createExtension, AdvancedAchievementsExtensionFactory.class); register(new AdvancedAchievementsExtensionFactory(), AdvancedAchievementsExtensionFactory::createExtension);
register(new AdvancedBanExtensionFactory()::createExtension, AdvancedBanExtensionFactory.class); register(new AdvancedBanExtensionFactory(), AdvancedBanExtensionFactory::createExtension, AdvancedBanExtensionFactory::registerListener);
register(new ASkyBlockExtensionFactory()::createExtension, ASkyBlockExtensionFactory.class); register(new ASkyBlockExtensionFactory(), ASkyBlockExtensionFactory::createExtension);
register(new BanManagerExtensionFactory()::createExtension, BanManagerExtensionFactory.class); register(new BanManagerExtensionFactory(), BanManagerExtensionFactory::createExtension);
register(new CoreProtectExtensionFactory()::createExtension, CoreProtectExtensionFactory.class); register(new CoreProtectExtensionFactory(), CoreProtectExtensionFactory::createExtension);
register(new DiscordSRVExtensionFactory()::createExtension, DiscordSRVExtensionFactory.class); register(new DiscordSRVExtensionFactory(), DiscordSRVExtensionFactory::createExtension);
registerEssentialsExtension(extensionService); register(new EssentialsExtensionFactory(), EssentialsExtensionFactory::createExtension, EssentialsExtensionFactory::registerUpdateListeners);
register(new GriefPreventionExtensionFactory()::createExtension, GriefPreventionExtensionFactory.class); register(new GriefPreventionExtensionFactory(), GriefPreventionExtensionFactory::createExtension);
register(new GriefPreventionSpongeExtensionFactory()::createExtension, GriefPreventionSpongeExtensionFactory.class); register(new GriefPreventionSpongeExtensionFactory(), GriefPreventionSpongeExtensionFactory::createExtension);
register(new GriefPreventionPlusExtensionFactory()::createExtension, GriefPreventionPlusExtensionFactory.class); register(new GriefPreventionPlusExtensionFactory(), GriefPreventionPlusExtensionFactory::createExtension);
register(new McMMOExtensionFactory()::createExtension, McMMOExtensionFactory.class); register(new McMMOExtensionFactory(), McMMOExtensionFactory::createExtension);
registerMinigameLibExtensions(extensionService); registerMinigameLibExtensions();
register(new NucleusExtensionFactory()::createExtension, NucleusExtensionFactory.class); register(new NucleusExtensionFactory(), NucleusExtensionFactory::createExtension);
register(new NuVotifierExtensionFactory()::createExtension, NuVotifierExtensionFactory.class); register(new NuVotifierExtensionFactory(), NuVotifierExtensionFactory::createExtension);
register(new ProtocolSupportExtensionFactory()::createExtension, ProtocolSupportExtensionFactory.class); register(new ProtocolSupportExtensionFactory(), ProtocolSupportExtensionFactory::createExtension);
register(new RedProtectExtensionFactory()::createExtension, RedProtectExtensionFactory.class); register(new RedProtectExtensionFactory(), RedProtectExtensionFactory::createExtension);
register(new SpongeEconomyExtensionFactory()::createExtension, SpongeEconomyExtensionFactory.class); register(new SpongeEconomyExtensionFactory(), SpongeEconomyExtensionFactory::createExtension);
register(new SuperbVoteExtensionFactory()::createExtension, SuperbVoteExtensionFactory.class); register(new SuperbVoteExtensionFactory(), SuperbVoteExtensionFactory::createExtension);
register(new VaultExtensionFactory()::createExtension, VaultExtensionFactory.class); register(new VaultExtensionFactory(), VaultExtensionFactory::createExtension);
register(new ViaVersionExtensionFactory()::createExtension, ViaVersionExtensionFactory.class); register(new ViaVersionExtensionFactory(), ViaVersionExtensionFactory::createExtension);
if (registerException != null) throw registerException; if (registerException != null) throw registerException;
} }
private void registerEssentialsExtension(ExtensionService extensionService) { private void registerMinigameLibExtensions() {
EssentialsExtensionFactory essentials = new EssentialsExtensionFactory();
essentials.createExtension()
.flatMap(extensionService::register) // If the extension was registered this is present.
.ifPresent(essentials::registerUpdateListeners);
}
private void registerMinigameLibExtensions(ExtensionService extensionService) {
for (DataExtension minigame : new MinigameLibExtensionFactory().createExtensions()) { for (DataExtension minigame : new MinigameLibExtensionFactory().createExtensions()) {
extensionService.register(minigame); register(minigame);
} }
} }
private void register(Supplier<Optional<DataExtension>> extension, Class factory) { private void suppressException(Class factory, Throwable e) {
ExtensionService extensionService = ExtensionService.getInstance(); // Places all exceptions to one exception with plugin information so that they can be reported.
if (registerException == null) {
registerException = new IllegalStateException("One or more extensions failed to register:");
registerException.setStackTrace(new StackTraceElement[0]);
}
IllegalStateException info = new IllegalStateException(factory.getSimpleName() + " ran into exception when creating Extension", e);
info.setStackTrace(new StackTraceElement[0]);
registerException.addSuppressed(info);
}
private <T> void register(
T factory,
Function<T, Optional<DataExtension>> createExtension
) {
try { try {
Optional<DataExtension> optional = extension.get(); // Creates the extension with factory and registers it
if (!optional.isPresent()) return; createExtension.apply(factory).flatMap(this::register);
DataExtension dataExtension = optional.get();
String extensionName = ExtensionExtractor.getPluginName(dataExtension.getClass());
if (disabledExtensions.contains(extensionName)) return;
extensionService.register(dataExtension);
} catch (IllegalStateException | NoClassDefFoundError | IncompatibleClassChangeError e) { } catch (IllegalStateException | NoClassDefFoundError | IncompatibleClassChangeError e) {
// Places all exceptions to one exception with plugin information so that they can be reported. // Places all exceptions to one exception with plugin information so that they can be reported.
if (registerException == null) { suppressException(factory.getClass(), e);
registerException = new IllegalStateException("One or more extensions failed to register:");
registerException.setStackTrace(new StackTraceElement[0]);
}
IllegalStateException info = new IllegalStateException(factory.getSimpleName() + " ran into exception when creating Extension", e);
info.setStackTrace(new StackTraceElement[0]);
registerException.addSuppressed(info);
} }
} }
private <T> void register(
T factory,
Function<T, Optional<DataExtension>> createExtension,
BiConsumer<T, Caller> registerListener
) {
try {
// Creates the extension with factory and registers it, then registers listener
createExtension.apply(factory)
.flatMap(this::register)
.ifPresent(caller -> registerListener.accept(factory, caller));
} catch (IllegalStateException | NoClassDefFoundError | IncompatibleClassChangeError e) {
// Places all exceptions to one exception with plugin information so that they can be reported.
suppressException(factory.getClass(), e);
}
}
private Optional<Caller> register(DataExtension dataExtension) {
String extensionName = ExtensionExtractor.getPluginName(dataExtension.getClass());
if (disabledExtensions.contains(extensionName)) return Optional.empty();
return extensionService.register(dataExtension);
}
} }