Refactored more Gatherers

Now all "non special" gatherers have been combined
to use Gatherer interface inside ProviderValueGatherer
Two gatherers are not yet abstracted:
- BooleanProviderValueGatherer, returns Conditions
- TableProviderValueGatherer, different provider save§

This removes a lot of duplicate code that was pretty
difficult to understand due to the complexity of the
functional interfaces inside the classes
This commit is contained in:
Rsl1122 2020-01-01 22:56:07 +02:00
parent f438366adc
commit 6a0c25edd5
20 changed files with 152 additions and 779 deletions

View File

@ -128,7 +128,7 @@ public class DataProviderExtractor {
}
private <T extends Annotation> void extractProviders(PluginInfo pluginInfo, Map<Method, Tab> tabs, Map<Method, Conditional> conditions, Class<T> ofKind, DataProviderFactory<T> factory) {
String pluginName = extractor.getPluginInfo().name();
String pluginName = pluginInfo.name();
for (Map.Entry<Method, T> entry : extractor.getMethodAnnotations().getMethodAnnotations(ofKind).entrySet()) {
Method method = entry.getKey();

View File

@ -25,23 +25,39 @@ import java.lang.reflect.Method;
import java.util.UUID;
public interface Parameters {
static Parameters player(UUID playerUUID, String playerName) {
return new PlayerParameters(playerUUID, playerName);
static Parameters player(UUID serverUUID, UUID playerUUID, String playerName) {
return new PlayerParameters(serverUUID, playerUUID, playerName);
}
static Parameters server() {
return new ServerParameters();
static Parameters server(UUID serverUUID) {
return new ServerParameters(serverUUID);
}
static Parameters group(String groupName) {
return new GroupParameters(groupName);
static Parameters group(UUID serverUUID, String groupName) {
return new GroupParameters(serverUUID, groupName);
}
Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException;
MethodType getMethodType();
UUID getServerUUID();
default UUID getPlayerUUID() {
return null;
}
class ServerParameters implements Parameters {
private final UUID serverUUID;
public ServerParameters(UUID serverUUID) {
this.serverUUID = serverUUID;
}
public UUID getServerUUID() {
return serverUUID;
}
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
return method.invoke(extension);
@ -54,14 +70,24 @@ public interface Parameters {
}
class PlayerParameters implements Parameters {
private final UUID serverUUID;
private final UUID playerUUID;
private final String playerName;
public PlayerParameters(UUID playerUUID, String playerName) {
public PlayerParameters(UUID serverUUID, UUID playerUUID, String playerName) {
this.serverUUID = serverUUID;
this.playerUUID = playerUUID;
this.playerName = playerName;
}
public UUID getServerUUID() {
return serverUUID;
}
public UUID getPlayerUUID() {
return playerUUID;
}
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
Class<?> parameterType = method.getParameterTypes()[0];
@ -79,12 +105,18 @@ public interface Parameters {
}
class GroupParameters implements Parameters {
private final UUID serverUUID;
private final String groupName;
public GroupParameters(String groupName) {
public GroupParameters(UUID serverUUID, String groupName) {
this.serverUUID = serverUUID;
this.groupName = groupName;
}
public UUID getServerUUID() {
return serverUUID;
}
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
Group group = this::getGroupName;

View File

@ -30,7 +30,7 @@ import java.lang.reflect.Method;
*/
public class PercentageDataProvider extends DataProvider<Double> {
// TODO Remove need for instanceof in DoubleAndPercentageProviderGatherer
// TODO Remove need for instanceof PercentageDataProvider
private PercentageDataProvider(ProviderInformation providerInformation, MethodWrapper<Double> methodWrapper) {
super(providerInformation, methodWrapper);

View File

@ -64,19 +64,19 @@ class BooleanProviderValueGatherer {
Conditions gatherBooleanDataOfPlayer(UUID playerUUID, String playerName) {
Conditions conditions = new Conditions();
List<DataProvider<Boolean>> unsatisifiedProviders = new ArrayList<>(dataProviders.getPlayerMethodsByType(Boolean.class));
List<DataProvider<Boolean>> unsatisfiedProviders = new ArrayList<>(dataProviders.getPlayerMethodsByType(Boolean.class));
Set<DataProvider<Boolean>> satisfied;
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(serverUUID, playerUUID, playerName));
BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTrancationCreator = (method, result) -> new StorePlayerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
do {
// Loop through all unsatisfied providers to see if more conditions are satisfied
satisfied = attemptToSatisfyMoreConditionsAndStoreResults(methodCaller, storeTrancationCreator, conditions, unsatisifiedProviders);
satisfied = attemptToSatisfyMoreConditionsAndStoreResults(methodCaller, storeTrancationCreator, conditions, unsatisfiedProviders);
// Remove now satisfied Providers so that they are not called again
unsatisifiedProviders.removeAll(satisfied);
unsatisfiedProviders.removeAll(satisfied);
// If no new conditions could be satisfied, stop looping.
} while (!satisfied.isEmpty());
@ -86,19 +86,19 @@ class BooleanProviderValueGatherer {
Conditions gatherBooleanDataOfServer() {
Conditions conditions = new Conditions();
List<DataProvider<Boolean>> unsatisifiedProviders = new ArrayList<>(dataProviders.getServerMethodsByType(Boolean.class));
List<DataProvider<Boolean>> unsatisfiedProviders = new ArrayList<>(dataProviders.getServerMethodsByType(Boolean.class));
Set<DataProvider<Boolean>> satisfied;
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server(serverUUID));
BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTransactionCreator = (method, result) -> new StoreServerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
do {
// Loop through all unsatisfied providers to see if more conditions are satisfied
satisfied = attemptToSatisfyMoreConditionsAndStoreResults(methodCaller, storeTransactionCreator, conditions, unsatisifiedProviders);
satisfied = attemptToSatisfyMoreConditionsAndStoreResults(methodCaller, storeTransactionCreator, conditions, unsatisfiedProviders);
// Remove now satisfied Providers so that they are not called again
unsatisifiedProviders.removeAll(satisfied);
unsatisfiedProviders.removeAll(satisfied);
// If no new conditions could be satisfied, stop looping.
} while (!satisfied.isEmpty());
@ -108,10 +108,10 @@ class BooleanProviderValueGatherer {
private Set<DataProvider<Boolean>> attemptToSatisfyMoreConditionsAndStoreResults(
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller,
BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTransactionCreator,
Conditions conditions, List<DataProvider<Boolean>> unsatisifiedProviders
Conditions conditions, List<DataProvider<Boolean>> unsatisfiedProviders
) {
Set<DataProvider<Boolean>> satisfied = new HashSet<>();
for (DataProvider<Boolean> booleanProvider : unsatisifiedProviders) {
for (DataProvider<Boolean> booleanProvider : unsatisfiedProviders) {
ProviderInformation information = booleanProvider.getProviderInformation();
Optional<String> condition = information.getCondition();

View File

@ -1,124 +0,0 @@
/*
* 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.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.*;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerDoubleResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerPercentageResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StoreServerDoubleResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StoreServerPercentageResultTransaction;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Gathers DoubleProvider and PercentageProvider method data.
*
* @author Rsl1122
*/
class DoubleAndPercentageProviderValueGatherer {
private final String pluginName;
private final DataExtension extension;
private final UUID serverUUID;
private final Database database;
private final DataProviders dataProviders;
DoubleAndPercentageProviderValueGatherer(
String pluginName, DataExtension extension,
UUID serverUUID, Database database,
DataProviders dataProviders
) {
this.pluginName = pluginName;
this.extension = extension;
this.serverUUID = serverUUID;
this.database = database;
this.dataProviders = dataProviders;
}
void gatherDoubleDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Double>, Callable<Double>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
BiFunction<MethodWrapper<Double>, Double, Transaction> percStoreTransactionCreator = (method, result) -> new StorePlayerPercentageResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
BiFunction<MethodWrapper<Double>, Double, Transaction> doubleStoreTransactionCreator = (method, result) -> new StorePlayerDoubleResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<Double> doubleProvider : dataProviders.getPlayerMethodsByType(Double.class)) {
gatherDoubleDataOfProvider(methodCaller, percStoreTransactionCreator, doubleStoreTransactionCreator, conditions, doubleProvider);
}
}
void gatherDoubleDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Double>, Callable<Double>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
BiFunction<MethodWrapper<Double>, Double, Transaction> percStoreTransactionCreator = (method, result) -> new StoreServerPercentageResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
BiFunction<MethodWrapper<Double>, Double, Transaction> doubleStoreTransactionCreator = (method, result) -> new StoreServerDoubleResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<Double> doubleProvider : dataProviders.getServerMethodsByType(Double.class)) {
gatherDoubleDataOfProvider(methodCaller, percStoreTransactionCreator, doubleStoreTransactionCreator, conditions, doubleProvider);
}
}
private void gatherDoubleDataOfProvider(
Function<MethodWrapper<Double>, Callable<Double>> methodCaller,
BiFunction<MethodWrapper<Double>, Double, Transaction> percStoreTransactionCreator,
BiFunction<MethodWrapper<Double>, Double, Transaction> doubleStoreTransactionCreator,
Conditions conditions, DataProvider<Double> doubleProvider
) {
ProviderInformation providerInformation = doubleProvider.getProviderInformation();
Optional<String> condition = providerInformation.getCondition();
if (condition.isPresent() && conditions.isNotFulfilled(condition.get())) {
return;
}
MethodWrapper<Double> method = doubleProvider.getMethod();
Double result = getMethodResult(methodCaller.apply(method), method);
if (result == null) {
return; // Error during call
}
database.executeTransaction(new StoreIconTransaction(providerInformation.getIcon()));
database.executeTransaction(new StoreProviderTransaction(doubleProvider, serverUUID));
if (doubleProvider instanceof PercentageDataProvider) {
database.executeTransaction(percStoreTransactionCreator.apply(method, result));
} else {
database.executeTransaction(doubleStoreTransactionCreator.apply(method, result));
}
}
private <T> T getMethodResult(Callable<T> callable, MethodWrapper<T> method) {
try {
return callable.call();
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
throw new DataExtensionMethodCallException(e, pluginName, method);
}
}
}

View File

@ -1,91 +0,0 @@
/*
* 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.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerGroupsResultTransaction;
import com.djrapitops.plan.storage.database.Database;
import java.util.Optional;
import java.util.UUID;
/**
* Gathers GroupProvider method data.
*
* @author Rsl1122
*/
class GroupProviderValueGatherer {
private final String pluginName;
private final DataExtension extension;
private final UUID serverUUID;
private final Database database;
private final DataProviders dataProviders;
GroupProviderValueGatherer(
String pluginName, DataExtension extension,
UUID serverUUID, Database database,
DataProviders dataProviders
) {
this.pluginName = pluginName;
this.extension = extension;
this.serverUUID = serverUUID;
this.database = database;
this.dataProviders = dataProviders;
}
void gatherGroupDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
for (DataProvider<String[]> groupProvider : dataProviders.getPlayerMethodsByType(String[].class)) {
gatherGroupDataOfProvider(playerUUID, playerName, conditions, groupProvider);
}
}
private void gatherGroupDataOfProvider(
UUID playerUUID, String playerName,
Conditions conditions,
DataProvider<String[]> groupProvider
) {
ProviderInformation providerInformation = groupProvider.getProviderInformation();
Optional<String> condition = providerInformation.getCondition();
if (condition.isPresent() && conditions.isNotFulfilled(condition.get())) {
return;
}
MethodWrapper<String[]> method = groupProvider.getMethod();
try {
String[] result = method.callMethod(extension, Parameters.player(playerUUID, playerName));
if (result == null) {
return; // Error during call
}
database.executeTransaction(new StoreIconTransaction(providerInformation.getIcon()));
database.executeTransaction(new StoreProviderTransaction(groupProvider, serverUUID));
database.executeTransaction(new StorePlayerGroupsResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result));
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
throw new DataExtensionMethodCallException(e, pluginName, method);
}
}
}

View File

@ -1,116 +0,0 @@
/*
* 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.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerNumberResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StoreServerNumberResultTransaction;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Gathers NumberProvider method data.
*
* @author Rsl1122
*/
class NumberProviderValueGatherer {
private final String pluginName;
private final DataExtension extension;
private final UUID serverUUID;
private final Database database;
private final DataProviders dataProviders;
NumberProviderValueGatherer(
String pluginName, DataExtension extension,
UUID serverUUID, Database database,
DataProviders dataProviders
) {
this.pluginName = pluginName;
this.extension = extension;
this.serverUUID = serverUUID;
this.database = database;
this.dataProviders = dataProviders;
}
void gatherNumberDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Long>, Callable<Long>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
BiFunction<MethodWrapper<Long>, Long, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerNumberResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<Long> numberProvider : dataProviders.getPlayerMethodsByType(Long.class)) {
gatherNumberDataOfProvider(methodCaller, storeTransactionCreator, conditions, numberProvider);
}
}
void gatherNumberDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Long>, Callable<Long>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
BiFunction<MethodWrapper<Long>, Long, Transaction> storeTransactionCreator = (method, result) -> new StoreServerNumberResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<Long> numberProvider : dataProviders.getServerMethodsByType(Long.class)) {
gatherNumberDataOfProvider(methodCaller, storeTransactionCreator, conditions, numberProvider);
}
}
private void gatherNumberDataOfProvider(
Function<MethodWrapper<Long>, Callable<Long>> methodCaller,
BiFunction<MethodWrapper<Long>, Long, Transaction> storeTransactionCreator,
Conditions conditions, DataProvider<Long> numberProvider
) {
ProviderInformation providerInformation = numberProvider.getProviderInformation();
Optional<String> condition = providerInformation.getCondition();
if (condition.isPresent() && conditions.isNotFulfilled(condition.get())) {
return;
}
MethodWrapper<Long> method = numberProvider.getMethod();
Long result = getMethodResult(methodCaller.apply(method), method);
if (result == null) {
return; // Error during call
}
database.executeTransaction(new StoreIconTransaction(providerInformation.getIcon()));
database.executeTransaction(new StoreProviderTransaction(numberProvider, serverUUID));
database.executeTransaction(storeTransactionCreator.apply(method, result));
}
private <T> T getMethodResult(Callable<T> callable, MethodWrapper<T> method) {
try {
return callable.call();
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
throw new DataExtensionMethodCallException(e, pluginName, method);
}
}
}

View File

@ -30,16 +30,13 @@ import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIc
import com.djrapitops.plan.extension.implementation.storage.transactions.StorePluginTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreTabInformationTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveInvalidResultsTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerNumberResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StoreServerNumberResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.*;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import java.util.UUID;
import java.util.function.BiFunction;
/**
* Object that can be called to place data about players to the database.
@ -57,12 +54,14 @@ public class ProviderValueGatherer {
private final DataProviders dataProviders;
private final BooleanProviderValueGatherer booleanGatherer;
private final DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer;
private final StringProviderValueGatherer stringGatherer;
private final TableProviderValueGatherer tableGatherer;
private final GroupProviderValueGatherer groupGatherer;
private Gatherer<Long> serverNumberGatherer;
private Gatherer<Long> playerNumberGatherer;
private final Gatherer<Long> serverNumberGatherer;
private final Gatherer<Long> playerNumberGatherer;
private final Gatherer<Double> serverDoubleGatherer;
private final Gatherer<Double> playerDoubleGatherer;
private final Gatherer<String> serverStringGatherer;
private final Gatherer<String> playerStringGatherer;
private final Gatherer<String[]> playerGroupGatherer;
public ProviderValueGatherer(
DataExtension extension,
@ -83,25 +82,30 @@ public class ProviderValueGatherer {
booleanGatherer = new BooleanProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
NumberProviderValueGatherer numberGatherer = new NumberProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
doubleAndPercentageGatherer = new DoubleAndPercentageProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
stringGatherer = new StringProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
tableGatherer = new TableProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
groupGatherer = new GroupProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders
);
serverNumberGatherer = new Gatherer<>(Long.class,
StoreProviderTransaction::new,
(provider, result) -> new StoreServerNumberResultTransaction(provider, serverUUID, result)
serverNumberGatherer = new Gatherer<>(
Long.class, StoreServerNumberResultTransaction::new
);
serverDoubleGatherer = new Gatherer<>(
Double.class, StoreServerDoubleResultTransaction::new
);
serverStringGatherer = new Gatherer<>(
String.class, StoreServerStringResultTransaction::new
);
playerNumberGatherer = new Gatherer<>(
Long.class, StorePlayerNumberResultTransaction::new
);
playerDoubleGatherer = new Gatherer<>(
Double.class, StorePlayerDoubleResultTransaction::new
);
playerStringGatherer = new Gatherer<>(
String.class, StorePlayerStringResultTransaction::new
);
playerGroupGatherer = new Gatherer<>(
String[].class, StorePlayerGroupsResultTransaction::new
);
}
@ -145,54 +149,37 @@ public class ProviderValueGatherer {
public void updateValues(UUID playerUUID, String playerName) {
Conditions conditions = booleanGatherer.gatherBooleanDataOfPlayer(playerUUID, playerName);
Parameters params = Parameters.player(playerUUID, playerName);
UUID serverUUID = serverInfo.getServerUUID();
playerNumberGatherer = new Gatherer<>(Long.class,
StoreProviderTransaction::new,
(provider, result) -> new StorePlayerNumberResultTransaction(provider, serverUUID, playerUUID, result) // TODO Sort out this use of playerUUID
);
Parameters params = Parameters.player(serverInfo.getServerUUID(), playerUUID, playerName);
playerNumberGatherer.gather(conditions, params);
doubleAndPercentageGatherer.gatherDoubleDataOfPlayer(playerUUID, playerName, conditions);
stringGatherer.gatherStringDataOfPlayer(playerUUID, playerName, conditions);
playerDoubleGatherer.gather(conditions, params);
playerStringGatherer.gather(conditions, params);
tableGatherer.gatherTableDataOfPlayer(playerUUID, playerName, conditions);
groupGatherer.gatherGroupDataOfPlayer(playerUUID, playerName, conditions);
playerGroupGatherer.gather(conditions, params);
}
public void updateValues() {
Conditions conditions = booleanGatherer.gatherBooleanDataOfServer();
Parameters params = Parameters.server();
Parameters params = Parameters.server(serverInfo.getServerUUID());
serverNumberGatherer.gather(conditions, params);
doubleAndPercentageGatherer.gatherDoubleDataOfServer(conditions);
stringGatherer.gatherStringDataOfServer(conditions);
serverDoubleGatherer.gather(conditions, params);
serverStringGatherer.gather(conditions, params);
tableGatherer.gatherTableDataOfServer(conditions);
}
interface ProviderTransactionConstructor<T> extends BiFunction<DataProvider<T>, UUID, Transaction> {
default Transaction create(DataProvider<T> provider, UUID serverUUID) {
return apply(provider, serverUUID);
}
}
interface ResultTransactionConstructor<T> extends BiFunction<DataProvider<T>, T, Transaction> {
default Transaction create(DataProvider<T> provider, T result) {
return apply(provider, result);
}
interface ResultTransactionConstructor<T> {
Transaction create(DataProvider<T> provider, Parameters parameters, T result);
}
class Gatherer<T> {
private final Class<T> type;
private final ProviderTransactionConstructor<T> providerTransactionConstructor;
private final ResultTransactionConstructor<T> resultTransactionConstructor;
public Gatherer(
Class<T> type,
ProviderTransactionConstructor<T> providerTransactionConstructor,
ResultTransactionConstructor<T> resultTransactionConstructor
) {
this.type = type;
this.providerTransactionConstructor = providerTransactionConstructor;
this.resultTransactionConstructor = resultTransactionConstructor;
}
@ -215,8 +202,8 @@ public class ProviderValueGatherer {
Database db = dbSystem.getDatabase();
db.executeTransaction(new StoreIconTransaction(information.getIcon()));
db.executeTransaction(providerTransactionConstructor.create(provider, serverInfo.getServerUUID()));
db.executeTransaction(resultTransactionConstructor.create(provider, result));
db.executeTransaction(new StoreProviderTransaction(provider, parameters.getServerUUID()));
db.executeTransaction(resultTransactionConstructor.create(provider, parameters, result));
}
}
}

View File

@ -1,121 +0,0 @@
/*
* 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.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerStringResultTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StoreServerStringResultTransaction;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import org.apache.commons.lang3.StringUtils;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Gathers StringProvider method data.
*
* @author Rsl1122
*/
class StringProviderValueGatherer {
private final String pluginName;
private final DataExtension extension;
private final UUID serverUUID;
private final Database database;
private final DataProviders dataProviders;
StringProviderValueGatherer(
String pluginName, DataExtension extension,
UUID serverUUID, Database database,
DataProviders dataProviders
) {
this.pluginName = pluginName;
this.extension = extension;
this.serverUUID = serverUUID;
this.database = database;
this.dataProviders = dataProviders;
}
void gatherStringDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<String>, Callable<String>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
BiFunction<MethodWrapper<String>, String, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerStringResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<String> stringProvider : dataProviders.getPlayerMethodsByType(String.class)) {
gatherStringDataOfProvider(methodCaller, storeTransactionCreator, conditions, stringProvider);
}
}
void gatherStringDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<String>, Callable<String>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
BiFunction<MethodWrapper<String>, String, Transaction> storeTransactionCreator = (method, result) -> new StoreServerStringResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<String> stringProvider : dataProviders.getServerMethodsByType(String.class)) {
gatherStringDataOfProvider(methodCaller, storeTransactionCreator, conditions, stringProvider);
}
}
private void gatherStringDataOfProvider(
Function<MethodWrapper<String>, Callable<String>> methodCaller,
BiFunction<MethodWrapper<String>, String, Transaction> storeTransactionCreator,
Conditions conditions,
DataProvider<String> stringProvider
) {
ProviderInformation providerInformation = stringProvider.getProviderInformation();
Optional<String> condition = providerInformation.getCondition();
if (condition.isPresent() && conditions.isNotFulfilled(condition.get())) {
return;
}
MethodWrapper<String> method = stringProvider.getMethod();
String result = getMethodResult(methodCaller.apply(method), method);
if (result == null) {
return; // Error during call
}
result = StringUtils.truncate(result, 50);
database.executeTransaction(new StoreIconTransaction(providerInformation.getIcon()));
database.executeTransaction(new StoreProviderTransaction(stringProvider, serverUUID));
database.executeTransaction(storeTransactionCreator.apply(method, result));
}
private <T> T getMethodResult(Callable<T> callable, MethodWrapper<String> method) {
try {
return callable.call();
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
throw new DataExtensionMethodCallException(e, pluginName, method);
}
}
}

View File

@ -67,7 +67,7 @@ class TableProviderValueGatherer {
void gatherTableDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(serverUUID, playerUUID, playerName));
BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<Table> tableProvider : dataProviders.getPlayerMethodsByType(Table.class)) {
@ -78,7 +78,7 @@ class TableProviderValueGatherer {
void gatherTableDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server(serverUUID));
BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StoreServerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<Table> tableProvider : dataProviders.getServerMethodsByType(Table.class)) {

View File

@ -109,21 +109,6 @@ public class StoreProviderTransaction extends ThrowawayTransaction {
}
private Executable insertProvider() {
/* PROVIDER_NAME
PLUGIN_ID
TEXT
DESCRIPTION // Can be null
PRIORITY
GROUPABLE // default false
CONDITION // Can be null, related to @Conditional
ICON_ID
TAB_ID // Can be null, related to @Tab
SHOW_IN_PLAYERS_TABLE // default false
HIDDEN // default false, related to @BooleanProvider
PROVIDED_CONDITION // Can be null, related to @BooleanProvider
FORMAT_TYPE // Can be null, related to @NumberProvider
IS_PLAYER_NAME // default false, related to @StringProvider */
String sql = "INSERT INTO " + TABLE_NAME + '(' +
PROVIDER_NAME + ',' +
TEXT + ',' +

View File

@ -16,6 +16,9 @@
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.extension.implementation.providers.PercentageDataProvider;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -28,9 +31,13 @@ import java.util.UUID;
import static com.djrapitops.plan.storage.database.sql.building.Sql.AND;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
import static com.djrapitops.plan.storage.database.sql.tables.ExtensionPlayerValueTable.*;
import static com.djrapitops.plan.storage.database.sql.tables.ExtensionServerValueTable.DOUBLE_VALUE;
import static com.djrapitops.plan.storage.database.sql.tables.ExtensionServerValueTable.PERCENTAGE_VALUE;
/**
* Transaction to store method result of a {@link com.djrapitops.plan.extension.implementation.providers.DoubleDataProvider}.
* Transaction to store method result of.
* - {@link com.djrapitops.plan.extension.annotation.DoubleProvider}
* - {@link com.djrapitops.plan.extension.annotation.PercentageProvider}
*
* @author Rsl1122
*/
@ -42,13 +49,15 @@ public class StorePlayerDoubleResultTransaction extends ThrowawayTransaction {
private final UUID playerUUID;
private final double value;
private final boolean percentage;
public StorePlayerDoubleResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, double value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.playerUUID = playerUUID;
public StorePlayerDoubleResultTransaction(DataProvider<Double> provider, Parameters parameters, double value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.playerUUID = parameters.getPlayerUUID();
this.value = value;
this.percentage = provider instanceof PercentageDataProvider;
}
@Override
@ -68,7 +77,7 @@ public class StorePlayerDoubleResultTransaction extends ThrowawayTransaction {
private Executable updateValue() {
String sql = "UPDATE " + TABLE_NAME +
" SET " +
DOUBLE_VALUE + "=?" +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "=?" +
WHERE + USER_UUID + "=?" +
AND + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID;
@ -84,7 +93,7 @@ public class StorePlayerDoubleResultTransaction extends ThrowawayTransaction {
private Executable insertValue() {
String sql = "INSERT INTO " + TABLE_NAME + "(" +
DOUBLE_VALUE + "," +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "," +
USER_UUID + "," +
PROVIDER_ID +
") VALUES (?,?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")";

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionGroupsTable;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
@ -43,11 +45,11 @@ public class StorePlayerGroupsResultTransaction extends ThrowawayTransaction {
private final String[] value;
public StorePlayerGroupsResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, String[] value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.playerUUID = playerUUID;
public StorePlayerGroupsResultTransaction(DataProvider<String[]> provider, Parameters parameters, String[] value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.playerUUID = parameters.getPlayerUUID();
this.value = value;
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -44,18 +45,14 @@ public class StorePlayerNumberResultTransaction extends ThrowawayTransaction {
private final long value;
public StorePlayerNumberResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, long value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.playerUUID = playerUUID;
public StorePlayerNumberResultTransaction(DataProvider<Long> provider, Parameters parameters, long value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.playerUUID = parameters.getPlayerUUID();
this.value = value;
}
public StorePlayerNumberResultTransaction(DataProvider<Long> provider, UUID serverUUID, UUID playerUUID, long value) {
this(provider.getProviderInformation().getPluginName(), serverUUID, provider.getProviderInformation().getName(), playerUUID, value);
}
@Override
protected void performOperations() {
execute(storeValue());

View File

@ -1,100 +0,0 @@
/*
* 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.storage.transactions.results;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
import com.djrapitops.plan.storage.database.transactions.ThrowawayTransaction;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import static com.djrapitops.plan.storage.database.sql.building.Sql.AND;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
import static com.djrapitops.plan.storage.database.sql.tables.ExtensionPlayerValueTable.*;
/**
* Transaction to store method result of a {@link com.djrapitops.plan.extension.implementation.providers.PercentageDataProvider}.
*
* @author Rsl1122
*/
public class StorePlayerPercentageResultTransaction extends ThrowawayTransaction {
private final String pluginName;
private final UUID serverUUID;
private final String providerName;
private final UUID playerUUID;
private final double value;
public StorePlayerPercentageResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, double value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.playerUUID = playerUUID;
this.value = value;
}
@Override
protected void performOperations() {
execute(storeValue());
}
private Executable storeValue() {
return connection -> {
if (!updateValue().execute(connection)) {
return insertValue().execute(connection);
}
return false;
};
}
private Executable updateValue() {
String sql = "UPDATE " + TABLE_NAME +
" SET " +
PERCENTAGE_VALUE + "=?" +
WHERE + USER_UUID + "=?" +
AND + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID;
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
statement.setString(2, playerUUID.toString());
ExtensionProviderTable.set3PluginValuesToStatement(statement, 3, providerName, pluginName, serverUUID);
}
};
}
private Executable insertValue() {
String sql = "INSERT INTO " + TABLE_NAME + "(" +
PERCENTAGE_VALUE + "," +
USER_UUID + "," +
PROVIDER_ID +
") VALUES (?,?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")";
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
statement.setString(2, playerUUID.toString());
ExtensionProviderTable.set3PluginValuesToStatement(statement, 3, providerName, pluginName, serverUUID);
}
};
}
}

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -43,11 +45,11 @@ public class StorePlayerStringResultTransaction extends ThrowawayTransaction {
private final String value;
public StorePlayerStringResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, String value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.playerUUID = playerUUID;
public StorePlayerStringResultTransaction(DataProvider<String> provider, Parameters parameters, String value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.playerUUID = parameters.getPlayerUUID();
this.value = value;
}

View File

@ -16,6 +16,9 @@
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.extension.implementation.providers.PercentageDataProvider;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -40,12 +43,14 @@ public class StoreServerDoubleResultTransaction extends ThrowawayTransaction {
private final String providerName;
private final double value;
private final boolean percentage;
public StoreServerDoubleResultTransaction(String pluginName, UUID serverUUID, String providerName, double value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
public StoreServerDoubleResultTransaction(DataProvider<Double> provider, Parameters parameters, double value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.value = value;
this.percentage = provider instanceof PercentageDataProvider;
}
@Override
@ -65,7 +70,7 @@ public class StoreServerDoubleResultTransaction extends ThrowawayTransaction {
private Executable updateValue() {
String sql = "UPDATE " + TABLE_NAME +
" SET " +
DOUBLE_VALUE + "=?" +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "=?" +
WHERE + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID;
return new ExecStatement(sql) {
@ -79,7 +84,7 @@ public class StoreServerDoubleResultTransaction extends ThrowawayTransaction {
private Executable insertValue() {
String sql = "INSERT INTO " + TABLE_NAME + "(" +
DOUBLE_VALUE + "," +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "," +
PROVIDER_ID +
") VALUES (?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")";
return new ExecStatement(sql) {

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -42,17 +43,13 @@ public class StoreServerNumberResultTransaction extends ThrowawayTransaction {
private final long value;
public StoreServerNumberResultTransaction(String pluginName, UUID serverUUID, String providerName, long value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
public StoreServerNumberResultTransaction(DataProvider<Long> provider, Parameters parameters, long value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.value = value;
}
public StoreServerNumberResultTransaction(DataProvider<Long> provider, UUID serverUUID, long value) {
this(provider.getProviderInformation().getPluginName(), serverUUID, provider.getProviderInformation().getName(), value);
}
@Override
protected void performOperations() {
execute(storeValue());

View File

@ -1,93 +0,0 @@
/*
* 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.storage.transactions.results;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
import com.djrapitops.plan.storage.database.transactions.ThrowawayTransaction;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
import static com.djrapitops.plan.storage.database.sql.tables.ExtensionServerValueTable.*;
/**
* Transaction to store method result of a {@link com.djrapitops.plan.extension.implementation.providers.PercentageDataProvider}.
*
* @author Rsl1122
*/
public class StoreServerPercentageResultTransaction extends ThrowawayTransaction {
private final String pluginName;
private final UUID serverUUID;
private final String providerName;
private final double value;
public StoreServerPercentageResultTransaction(String pluginName, UUID serverUUID, String providerName, double value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
this.value = value;
}
@Override
protected void performOperations() {
execute(storeValue());
}
private Executable storeValue() {
return connection -> {
if (!updateValue().execute(connection)) {
return insertValue().execute(connection);
}
return false;
};
}
private Executable updateValue() {
String sql = "UPDATE " + TABLE_NAME +
" SET " +
PERCENTAGE_VALUE + "=?" +
WHERE + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID;
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
ExtensionProviderTable.set3PluginValuesToStatement(statement, 2, providerName, pluginName, serverUUID);
}
};
}
private Executable insertValue() {
String sql = "INSERT INTO " + TABLE_NAME + "(" +
PERCENTAGE_VALUE + "," +
PROVIDER_ID +
") VALUES (?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")";
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
ExtensionProviderTable.set3PluginValuesToStatement(statement, 2, providerName, pluginName, serverUUID);
}
};
}
}

View File

@ -16,6 +16,8 @@
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.storage.database.sql.tables.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
@ -41,10 +43,10 @@ public class StoreServerStringResultTransaction extends ThrowawayTransaction {
private final String value;
public StoreServerStringResultTransaction(String pluginName, UUID serverUUID, String providerName, String value) {
this.pluginName = pluginName;
this.serverUUID = serverUUID;
this.providerName = providerName;
public StoreServerStringResultTransaction(DataProvider<String> provider, Parameters parameters, String value) {
this.pluginName = provider.getProviderInformation().getPluginName();
this.providerName = provider.getProviderInformation().getName();
this.serverUUID = parameters.getServerUUID();
this.value = value;
}