Implemented Caller and CallEvents filtering

This commit is contained in:
Rsl1122 2019-03-31 12:26:22 +03:00
parent bb2be54604
commit 58e7534501
8 changed files with 133 additions and 46 deletions

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.events.*; import com.djrapitops.plan.db.access.transactions.events.*;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.ExtensionServiceImplementation; import com.djrapitops.plan.extension.ExtensionServiceImplementation;
import com.djrapitops.plan.system.cache.GeolocationCache; import com.djrapitops.plan.system.cache.GeolocationCache;
import com.djrapitops.plan.system.cache.NicknameCache; import com.djrapitops.plan.system.cache.NicknameCache;
@ -177,7 +178,7 @@ public class PlayerOnlineListener implements Listener {
)); ));
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(playerUUID)); processing.submitNonCritical(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName)); processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, CallEvents.PLAYER_JOIN));
} }

View File

@ -105,7 +105,7 @@ public class PlayerOnlineListener implements Listener {
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName)); database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID)); processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName)); processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, com.djrapitops.plan.extension.CallEvents.PLAYER_JOIN));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID())); ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.extension; package com.djrapitops.plan.extension;
import com.djrapitops.plan.data.plugin.PluginsConfigSection; import com.djrapitops.plan.data.plugin.PluginsConfigSection;
import com.djrapitops.plan.extension.implementation.CallerImplementation;
import com.djrapitops.plan.extension.implementation.DataProviderExtractor; import com.djrapitops.plan.extension.implementation.DataProviderExtractor;
import com.djrapitops.plan.extension.implementation.ExtensionRegister; import com.djrapitops.plan.extension.implementation.ExtensionRegister;
import com.djrapitops.plan.extension.implementation.providers.gathering.ProviderValueGatherer; import com.djrapitops.plan.extension.implementation.providers.gathering.ProviderValueGatherer;
@ -33,6 +34,7 @@ import javax.inject.Singleton;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
@ -78,11 +80,11 @@ public class ExtensionServiceImplementation implements ExtensionService {
} }
@Override @Override
public void register(DataExtension extension) { public Optional<Caller> register(DataExtension extension) {
DataProviderExtractor extractor = new DataProviderExtractor(extension); DataProviderExtractor extractor = new DataProviderExtractor(extension);
String pluginName = extractor.getPluginName(); String pluginName = extractor.getPluginName();
if (shouldNotAllowRegistration(pluginName)) return; if (shouldNotAllowRegistration(pluginName)) return Optional.empty();
for (String warning : extractor.getWarnings()) { for (String warning : extractor.getWarnings()) {
logger.warn("DataExtension API implementation mistake for " + pluginName + ": " + warning); logger.warn("DataExtension API implementation mistake for " + pluginName + ": " + warning);
@ -93,6 +95,7 @@ public class ExtensionServiceImplementation implements ExtensionService {
extensionGatherers.put(pluginName, gatherer); extensionGatherers.put(pluginName, gatherer);
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, pluginName + " extension registered."); logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, pluginName + " extension registered.");
return Optional.of(new CallerImplementation(gatherer, this));
} }
@Override @Override
@ -125,41 +128,55 @@ public class ExtensionServiceImplementation implements ExtensionService {
return false; // Should register. return false; // Should register.
} }
public void updatePlayerValues(UUID playerUUID, String playerName) { public void updatePlayerValues(UUID playerUUID, String playerName, CallEvents event) {
for (Map.Entry<String, ProviderValueGatherer> gatherer : extensionGatherers.entrySet()) { for (ProviderValueGatherer gatherer : extensionGatherers.values()) {
try { updatePlayerValues(gatherer, playerUUID, playerName, event);
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering values for: " + playerName);
gatherer.getValue().updateValues(playerUUID, playerName);
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering completed: " + playerName);
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
logger.warn(gatherer.getKey() + " ran into (but failed safely) " + e.getClass().getSimpleName() +
" when updating value for '" + playerName +
"', (You can disable integration with setting 'Plugins." + gatherer.getKey() + ".Enabled')" +
" reason: '" + e.getMessage() +
"', stack trace to follow:");
errorHandler.log(L.WARN, gatherer.getValue().getClass(), e);
}
} }
} }
public void updateServerValues() { public void updatePlayerValues(ProviderValueGatherer gatherer, UUID playerUUID, String playerName, CallEvents event) {
for (Map.Entry<String, ProviderValueGatherer> gatherer : extensionGatherers.entrySet()) { if (!gatherer.canCallEvent(event)) {
try { return;
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering values for server"); }
try {
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering values for: " + playerName);
gatherer.getValue().updateValues(); gatherer.updateValues(playerUUID, playerName);
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering completed for server"); logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering completed: " + playerName);
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) { } catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
logger.warn(gatherer.getKey() + " ran into (but failed safely) " + e.getClass().getSimpleName() + logger.warn(gatherer.getPluginName() + " ran into (but failed safely) " + e.getClass().getSimpleName() +
" when updating value for server" + " when updating value for '" + playerName +
", (You can disable integration with setting 'Plugins." + gatherer.getKey() + ".Enabled')" + "', (You can disable integration with setting 'Plugins." + gatherer.getPluginName() + ".Enabled')" +
" reason: '" + e.getMessage() + " reason: '" + e.getMessage() +
"', stack trace to follow:"); "', stack trace to follow:");
errorHandler.log(L.WARN, gatherer.getValue().getClass(), e); errorHandler.log(L.WARN, gatherer.getClass(), e);
} }
}
public void updateServerValues(CallEvents event) {
for (ProviderValueGatherer gatherer : extensionGatherers.values()) {
updateServerValues(gatherer, event);
}
}
public void updateServerValues(ProviderValueGatherer gatherer, CallEvents event) {
if (!gatherer.canCallEvent(event)) {
return;
}
try {
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering values for server");
gatherer.updateValues();
logger.getDebugLogger().logOn(DebugChannels.DATA_EXTENSIONS, "Gathering completed for server");
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
logger.warn(gatherer.getPluginName() + " ran into (but failed safely) " + e.getClass().getSimpleName() +
" when updating value for server" +
", (You can disable integration with setting 'Plugins." + gatherer.getPluginName() + ".Enabled')" +
" reason: '" + e.getMessage() +
"', stack trace to follow:");
errorHandler.log(L.WARN, gatherer.getClass(), e);
} }
} }
} }

View File

@ -0,0 +1,53 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.Caller;
import com.djrapitops.plan.extension.ExtensionServiceImplementation;
import com.djrapitops.plan.extension.implementation.providers.gathering.ProviderValueGatherer;
import com.djrapitops.plugin.utilities.Verify;
import java.util.UUID;
/**
* Implementation for {@link Caller} interface.
*
* @author Rsl1122
*/
public class CallerImplementation implements Caller {
private final ProviderValueGatherer gatherer;
private final ExtensionServiceImplementation extensionServiceImplementation;
public CallerImplementation(ProviderValueGatherer gatherer, ExtensionServiceImplementation extensionServiceImplementation) {
this.gatherer = gatherer;
this.extensionServiceImplementation = extensionServiceImplementation;
}
@Override
public void updatePlayerData(UUID playerUUID, String playerName) throws IllegalArgumentException {
Verify.nullCheck(playerUUID, () -> new IllegalArgumentException("'playerUUID' can not be null!"));
Verify.nullCheck(playerName, () -> new IllegalArgumentException("'playerName' can not be null!"));
extensionServiceImplementation.updatePlayerValues(gatherer, playerUUID, playerName, CallEvents.MANUAL);
}
@Override
public void updateServerData() {
extensionServiceImplementation.updateServerValues(gatherer, CallEvents.MANUAL);
}
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.extension.implementation.providers.gathering; package com.djrapitops.plan.extension.implementation.providers.gathering;
import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.icon.Icon; import com.djrapitops.plan.extension.icon.Icon;
import com.djrapitops.plan.extension.implementation.DataProviderExtractor; import com.djrapitops.plan.extension.implementation.DataProviderExtractor;
@ -38,11 +39,10 @@ import java.util.UUID;
*/ */
public class ProviderValueGatherer { public class ProviderValueGatherer {
private final DataExtension extension; private final CallEvents[] callEvents;
private final DataProviderExtractor extractor; private final DataProviderExtractor extractor;
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
private final PluginLogger logger;
private BooleanProviderValueGatherer booleanGatherer; private BooleanProviderValueGatherer booleanGatherer;
private NumberProviderValueGatherer numberGatherer; private NumberProviderValueGatherer numberGatherer;
private DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer; private DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer;
@ -55,11 +55,10 @@ public class ProviderValueGatherer {
ServerInfo serverInfo, ServerInfo serverInfo,
PluginLogger logger PluginLogger logger
) { ) {
this.extension = extension; this.callEvents = extension.callExtensionMethodsOn();
this.extractor = extractor; this.extractor = extractor;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.serverInfo = serverInfo; this.serverInfo = serverInfo;
this.logger = logger;
booleanGatherer = new BooleanProviderValueGatherer( booleanGatherer = new BooleanProviderValueGatherer(
extractor.getPluginName(), extension, extractor.getPluginName(), extension,
@ -83,6 +82,22 @@ public class ProviderValueGatherer {
); );
} }
public boolean canCallEvent(CallEvents event) {
if (event == CallEvents.MANUAL) {
return true;
}
for (CallEvents accepted : callEvents) {
if (event == accepted) {
return true;
}
}
return false;
}
public String getPluginName() {
return extractor.getPluginName();
}
public void storeExtensionInformation() { public void storeExtensionInformation() {
String pluginName = extractor.getPluginName(); String pluginName = extractor.getPluginName();
Icon pluginIcon = extractor.getPluginIcon(); Icon pluginIcon = extractor.getPluginIcon();

View File

@ -44,6 +44,7 @@ import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction; import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction; import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction;
import com.djrapitops.plan.db.patches.Patch; import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.extension.CallEvents;
import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.ExtensionServiceImplementation; import com.djrapitops.plan.extension.ExtensionServiceImplementation;
import com.djrapitops.plan.extension.annotation.*; import com.djrapitops.plan.extension.annotation.*;
@ -1040,7 +1041,7 @@ public abstract class CommonDBTest {
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService(); ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
extensionService.register(new PlayerExtension()); extensionService.register(new PlayerExtension());
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME); extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME, CallEvents.PLAYER_JOIN);
Map<UUID, List<ExtensionPlayerData>> playerDataByServerUUID = db.query(new ExtensionPlayerDataQuery(playerUUID)); Map<UUID, List<ExtensionPlayerData>> playerDataByServerUUID = db.query(new ExtensionPlayerDataQuery(playerUUID));
List<ExtensionPlayerData> ofServer = playerDataByServerUUID.get(serverUUID); List<ExtensionPlayerData> ofServer = playerDataByServerUUID.get(serverUUID);
@ -1064,7 +1065,7 @@ public abstract class CommonDBTest {
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService(); ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
extensionService.register(new ServerExtension()); extensionService.register(new ServerExtension());
extensionService.updateServerValues(); extensionService.updateServerValues(CallEvents.SERVER_EXTENSION_REGISTER);
List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID)); List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID));
assertFalse(ofServer.isEmpty()); assertFalse(ofServer.isEmpty());
@ -1086,7 +1087,7 @@ public abstract class CommonDBTest {
ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService(); ExtensionServiceImplementation extensionService = (ExtensionServiceImplementation) system.getExtensionService();
extensionService.register(new PlayerExtension()); extensionService.register(new PlayerExtension());
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME); extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME, CallEvents.PLAYER_JOIN);
List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID)); List<ExtensionServerData> ofServer = db.query(new ExtensionServerDataQuery(serverUUID));
assertFalse(ofServer.isEmpty()); assertFalse(ofServer.isEmpty());
@ -1113,14 +1114,14 @@ public abstract class CommonDBTest {
extensionService.register(new ConditionalExtension()); extensionService.register(new ConditionalExtension());
ConditionalExtension.condition = true; ConditionalExtension.condition = true;
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME); extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME, CallEvents.PLAYER_JOIN);
// Check that the wanted data exists // Check that the wanted data exists
checkThatDataExists(ConditionalExtension.condition); checkThatDataExists(ConditionalExtension.condition);
// Reverse condition // Reverse condition
ConditionalExtension.condition = false; ConditionalExtension.condition = false;
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME); extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME, CallEvents.PLAYER_JOIN);
db.executeTransaction(new RemoveUnsatisfiedConditionalResultsTransaction()); db.executeTransaction(new RemoveUnsatisfiedConditionalResultsTransaction());
@ -1129,7 +1130,7 @@ public abstract class CommonDBTest {
// Reverse condition // Reverse condition
ConditionalExtension.condition = false; ConditionalExtension.condition = false;
extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME); extensionService.updatePlayerValues(playerUUID, TestConstants.PLAYER_ONE_NAME, CallEvents.PLAYER_JOIN);
db.executeTransaction(new RemoveUnsatisfiedConditionalResultsTransaction()); db.executeTransaction(new RemoveUnsatisfiedConditionalResultsTransaction());

View File

@ -181,7 +181,7 @@ public class PlayerOnlineListener {
)); ));
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(playerUUID)); processing.submitNonCritical(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName)); processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, com.djrapitops.plan.extension.CallEvents.PLAYER_JOIN));
} }
@Listener(order = Order.POST) @Listener(order = Order.POST)

View File

@ -109,7 +109,7 @@ public class PlayerOnlineListener {
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName)); database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, playerName));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID)); processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName)); processing.submitNonCritical(() -> extensionService.updatePlayerValues(playerUUID, playerName, com.djrapitops.plan.extension.CallEvents.PLAYER_JOIN));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID())); ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) { } catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e); errorHandler.log(L.WARN, this.getClass(), e);