MethodWrapper Parameters

Parameters for MethodWrapper were abstracted to a
visitor interface in order to call any method types
with same class structure (Gatherer)
This commit is contained in:
Rsl1122 2019-12-31 23:30:50 +02:00
parent ee9a9ba7d8
commit f438366adc
11 changed files with 163 additions and 112 deletions

View File

@ -30,8 +30,11 @@ import java.util.UUID;
*/ */
public enum MethodType { public enum MethodType {
@Deprecated
PLAYER_UUID, PLAYER_UUID,
@Deprecated
PLAYER_NAME, PLAYER_NAME,
PLAYER,
GROUP, GROUP,
SERVER; SERVER;
@ -44,10 +47,8 @@ public enum MethodType {
Class<?>[] parameterTypes = method.getParameterTypes(); Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> firstParameter = parameterTypes[0]; Class<?> firstParameter = parameterTypes[0];
if (UUID.class.equals(firstParameter)) { if (UUID.class.equals(firstParameter) || String.class.equals(firstParameter)) {
return PLAYER_UUID; return PLAYER;
} else if (String.class.equals(firstParameter)) {
return PLAYER_NAME;
} else if (Group.class.equals(firstParameter)) { } else if (Group.class.equals(firstParameter)) {
return GROUP; return GROUP;
} }

View File

@ -47,50 +47,44 @@ public class DataProviders {
return byMethodType.computeIfAbsent(methodType, Maps::create).computeIfAbsent(returnType, Lists::create); return byMethodType.computeIfAbsent(methodType, Maps::create).computeIfAbsent(returnType, Lists::create);
} }
private List<DataProvider<?>> getProvidersByTypes(MethodType methodType, Class<?> returnType) { public <T> List<DataProvider<T>> getProvidersByTypes(MethodType methodType, Class<T> returnType) {
Map<Class<?>, List<DataProvider<?>>> byReturnType = byMethodType.getOrDefault(methodType, Collections.emptyMap()); Map<Class<?>, List<DataProvider<?>>> byReturnType = byMethodType.getOrDefault(methodType, Collections.emptyMap());
return byReturnType.getOrDefault(returnType, Collections.emptyList()); List<DataProvider<?>> uncastProviders = byReturnType.get(returnType);
} if (uncastProviders == null) {
return Collections.emptyList();
public <T> List<DataProvider<T>> getPlayerMethodsByType(Class<T> returnType) {
List<DataProvider<T>> byReturnType = new ArrayList<>();
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.PLAYER_UUID, returnType)) {
byReturnType.add((DataProvider<T>) dataProvider);
} }
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.PLAYER_NAME, returnType)) { // Cast to T
byReturnType.add((DataProvider<T>) dataProvider);
}
return byReturnType;
}
public <T> List<DataProvider<T>> getServerMethodsByType(Class<T> returnType) {
List<DataProvider<T>> providers = new ArrayList<>(); List<DataProvider<T>> providers = new ArrayList<>();
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.SERVER, returnType)) { for (DataProvider<?> dataProvider : uncastProviders) {
providers.add((DataProvider<T>) dataProvider); providers.add((DataProvider<T>) dataProvider);
} }
return providers; return providers;
} }
public <T> List<DataProvider<T>> getGroupMethodsByType(Class<T> returnType) { public <T> List<DataProvider<T>> getPlayerMethodsByType(Class<T> returnType) {
List<DataProvider<T>> byReturnType = new ArrayList<>(); return getProvidersByTypes(MethodType.PLAYER, returnType);
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.GROUP, returnType)) {
byReturnType.add((DataProvider<T>) dataProvider);
}
return byReturnType;
} }
public void removeProviderWithMethod(MethodWrapper<?> toRemove) { public <T> List<DataProvider<T>> getServerMethodsByType(Class<T> returnType) {
return getProvidersByTypes(MethodType.SERVER, returnType);
}
public <T> List<DataProvider<T>> getGroupMethodsByType(Class<T> returnType) {
return getProvidersByTypes(MethodType.GROUP, returnType);
}
public <T> void removeProviderWithMethod(MethodWrapper<T> toRemove) {
MethodType methodType = toRemove.getMethodType(); MethodType methodType = toRemove.getMethodType();
Map<Class<?>, List<DataProvider<?>>> byResultType = byMethodType.getOrDefault(methodType, Collections.emptyMap()); Map<Class<?>, List<DataProvider<?>>> byResultType = byMethodType.getOrDefault(methodType, Collections.emptyMap());
if (byResultType.isEmpty()) { if (byResultType.isEmpty()) {
return; return;
} }
Class<?> returnType = toRemove.getReturnType(); Class<T> returnType = toRemove.getReturnType();
List<DataProvider<?>> providers = getProvidersByTypes(methodType, returnType); List<DataProvider<T>> providers = getProvidersByTypes(methodType, returnType);
DataProvider<?> providerToRemove = null; DataProvider<T> providerToRemove = null;
for (DataProvider<?> provider : providers) { for (DataProvider<T> provider : providers) {
if (provider.getMethod().equals(toRemove)) { if (provider.getMethod().equals(toRemove)) {
providerToRemove = provider; providerToRemove = provider;
break; break;

View File

@ -17,14 +17,12 @@
package com.djrapitops.plan.extension.implementation.providers; package com.djrapitops.plan.extension.implementation.providers;
import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.Group;
import com.djrapitops.plan.extension.NotReadyException; import com.djrapitops.plan.extension.NotReadyException;
import com.djrapitops.plan.extension.implementation.MethodType; import com.djrapitops.plan.extension.implementation.MethodType;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Objects; import java.util.Objects;
import java.util.UUID;
/** /**
* Wrap a Method so that it is easier to call. * Wrap a Method so that it is easier to call.
@ -43,41 +41,9 @@ public class MethodWrapper<T> {
methodType = MethodType.forMethod(this.method); methodType = MethodType.forMethod(this.method);
} }
public T callMethod(DataExtension extension, UUID playerUUID, String playerName) { public T callMethod(DataExtension of, Parameters with) {
if (methodType != MethodType.PLAYER_NAME && methodType != MethodType.PLAYER_UUID) {
throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not PLAYER method.");
}
return callMethod(extension, playerUUID, playerName, null);
}
public T callMethod(DataExtension extension, Group group) {
if (methodType != MethodType.GROUP) {
throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not GROUP method.");
}
return callMethod(extension, null, null, group);
}
public T callMethod(DataExtension extension) {
if (methodType != MethodType.SERVER) {
throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not SERVER method.");
}
return callMethod(extension, null, null, null);
}
public T callMethod(DataExtension extension, UUID playerUUID, String playerName, Group group) {
try { try {
switch (methodType) { return returnType.cast(with.call(of, method));
case SERVER:
return returnType.cast(method.invoke(extension));
case PLAYER_UUID:
return returnType.cast(method.invoke(extension, playerUUID));
case PLAYER_NAME:
return returnType.cast(method.invoke(extension, playerName));
case GROUP:
return returnType.cast(method.invoke(extension, group));
default:
throw new IllegalArgumentException(method.getDeclaringClass() + " method " + method.getName() + " had invalid parameters.");
}
} catch (InvocationTargetException notReadyToBeCalled) { } catch (InvocationTargetException notReadyToBeCalled) {
if (notReadyToBeCalled.getCause() instanceof NotReadyException) { if (notReadyToBeCalled.getCause() instanceof NotReadyException) {
return null; // Data or API not available to make the call. return null; // Data or API not available to make the call.

View File

@ -0,0 +1,103 @@
/*
* 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;
import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.Group;
import com.djrapitops.plan.extension.implementation.MethodType;
import java.lang.reflect.InvocationTargetException;
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 server() {
return new ServerParameters();
}
static Parameters group(String groupName) {
return new GroupParameters(groupName);
}
Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException;
MethodType getMethodType();
class ServerParameters implements Parameters {
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
return method.invoke(extension);
}
@Override
public MethodType getMethodType() {
return MethodType.SERVER;
}
}
class PlayerParameters implements Parameters {
private final UUID playerUUID;
private final String playerName;
public PlayerParameters(UUID playerUUID, String playerName) {
this.playerUUID = playerUUID;
this.playerName = playerName;
}
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
Class<?> parameterType = method.getParameterTypes()[0];
if (UUID.class.equals(parameterType)) {
return method.invoke(extension, playerUUID);
} else {
return method.invoke(extension, playerName);
}
}
@Override
public MethodType getMethodType() {
return MethodType.PLAYER;
}
}
class GroupParameters implements Parameters {
private final String groupName;
public GroupParameters(String groupName) {
this.groupName = groupName;
}
@Override
public Object call(DataExtension extension, Method method) throws InvocationTargetException, IllegalAccessException {
Group group = this::getGroupName;
return method.invoke(extension, group);
}
public String getGroupName() {
return groupName;
}
@Override
public MethodType getMethodType() {
return MethodType.GROUP;
}
}
}

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerBooleanResultTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerBooleanResultTransaction;
@ -68,7 +69,7 @@ class BooleanProviderValueGatherer {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, playerUUID, playerName); Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTrancationCreator = (method, result) -> new StorePlayerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result); BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTrancationCreator = (method, result) -> new StorePlayerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
do { do {
@ -90,7 +91,7 @@ class BooleanProviderValueGatherer {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension); Function<MethodWrapper<Boolean>, Callable<Boolean>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTransactionCreator = (method, result) -> new StoreServerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), result); BiFunction<MethodWrapper<Boolean>, Boolean, Transaction> storeTransactionCreator = (method, result) -> new StoreServerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
do { do {

View File

@ -19,10 +19,7 @@ package com.djrapitops.plan.extension.implementation.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException; import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.DataExtension;
import com.djrapitops.plan.extension.implementation.ProviderInformation; import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.*;
import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper;
import com.djrapitops.plan.extension.implementation.providers.PercentageDataProvider;
import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction; 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.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerDoubleResultTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerDoubleResultTransaction;
@ -67,7 +64,7 @@ class DoubleAndPercentageProviderValueGatherer {
void gatherDoubleDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) { void gatherDoubleDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Double>, Callable<Double>> methodCaller = method -> () -> method.callMethod(extension, playerUUID, playerName); 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> 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); BiFunction<MethodWrapper<Double>, Double, Transaction> doubleStoreTransactionCreator = (method, result) -> new StorePlayerDoubleResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
@ -79,7 +76,7 @@ class DoubleAndPercentageProviderValueGatherer {
void gatherDoubleDataOfServer(Conditions conditions) { void gatherDoubleDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Double>, Callable<Double>> methodCaller = method -> () -> method.callMethod(extension); 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> 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); BiFunction<MethodWrapper<Double>, Double, Transaction> doubleStoreTransactionCreator = (method, result) -> new StoreServerDoubleResultTransaction(pluginName, serverUUID, method.getMethodName(), result);

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerGroupsResultTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerGroupsResultTransaction;
@ -75,7 +76,7 @@ class GroupProviderValueGatherer {
MethodWrapper<String[]> method = groupProvider.getMethod(); MethodWrapper<String[]> method = groupProvider.getMethod();
try { try {
String[] result = method.callMethod(extension, playerUUID, playerName); String[] result = method.callMethod(extension, Parameters.player(playerUUID, playerName));
if (result == null) { if (result == null) {
return; // Error during call return; // Error during call
} }

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction; 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.StorePlayerNumberResultTransaction;
@ -64,7 +65,7 @@ class NumberProviderValueGatherer {
void gatherNumberDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) { void gatherNumberDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Long>, Callable<Long>> methodCaller = method -> () -> method.callMethod(extension, playerUUID, playerName); 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); BiFunction<MethodWrapper<Long>, Long, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerNumberResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<Long> numberProvider : dataProviders.getPlayerMethodsByType(Long.class)) { for (DataProvider<Long> numberProvider : dataProviders.getPlayerMethodsByType(Long.class)) {
@ -75,7 +76,7 @@ class NumberProviderValueGatherer {
void gatherNumberDataOfServer(Conditions conditions) { void gatherNumberDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Long>, Callable<Long>> methodCaller = method -> () -> method.callMethod(extension); 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); BiFunction<MethodWrapper<Long>, Long, Transaction> storeTransactionCreator = (method, result) -> new StoreServerNumberResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<Long> numberProvider : dataProviders.getServerMethodsByType(Long.class)) { for (DataProvider<Long> numberProvider : dataProviders.getServerMethodsByType(Long.class)) {

View File

@ -16,7 +16,6 @@
*/ */
package com.djrapitops.plan.extension.implementation.providers.gathering; package com.djrapitops.plan.extension.implementation.providers.gathering;
import com.djrapitops.plan.exceptions.DataExtensionMethodCallException;
import com.djrapitops.plan.extension.CallEvents; 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;
@ -26,6 +25,7 @@ import com.djrapitops.plan.extension.implementation.TabInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.StorePluginTransaction; 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.StoreTabInformationTransaction;
@ -40,7 +40,6 @@ import com.djrapitops.plan.storage.database.transactions.Transaction;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function;
/** /**
* Object that can be called to place data about players to the database. * Object that can be called to place data about players to the database.
@ -58,7 +57,6 @@ public class ProviderValueGatherer {
private final DataProviders dataProviders; private final DataProviders dataProviders;
private final BooleanProviderValueGatherer booleanGatherer; private final BooleanProviderValueGatherer booleanGatherer;
private final NumberProviderValueGatherer numberGatherer;
private final DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer; private final DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer;
private final StringProviderValueGatherer stringGatherer; private final StringProviderValueGatherer stringGatherer;
private final TableProviderValueGatherer tableGatherer; private final TableProviderValueGatherer tableGatherer;
@ -85,7 +83,7 @@ public class ProviderValueGatherer {
booleanGatherer = new BooleanProviderValueGatherer( booleanGatherer = new BooleanProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders pluginName, extension, serverUUID, database, dataProviders
); );
numberGatherer = new NumberProviderValueGatherer( NumberProviderValueGatherer numberGatherer = new NumberProviderValueGatherer(
pluginName, extension, serverUUID, database, dataProviders pluginName, extension, serverUUID, database, dataProviders
); );
doubleAndPercentageGatherer = new DoubleAndPercentageProviderValueGatherer( doubleAndPercentageGatherer = new DoubleAndPercentageProviderValueGatherer(
@ -101,9 +99,7 @@ public class ProviderValueGatherer {
pluginName, extension, serverUUID, database, dataProviders pluginName, extension, serverUUID, database, dataProviders
); );
serverNumberGatherer = new Gatherer<>( serverNumberGatherer = new Gatherer<>(Long.class,
Long.class,
method -> method.callMethod(extension),
StoreProviderTransaction::new, StoreProviderTransaction::new,
(provider, result) -> new StoreServerNumberResultTransaction(provider, serverUUID, result) (provider, result) -> new StoreServerNumberResultTransaction(provider, serverUUID, result)
); );
@ -150,13 +146,13 @@ public class ProviderValueGatherer {
public void updateValues(UUID playerUUID, String playerName) { public void updateValues(UUID playerUUID, String playerName) {
Conditions conditions = booleanGatherer.gatherBooleanDataOfPlayer(playerUUID, playerName); Conditions conditions = booleanGatherer.gatherBooleanDataOfPlayer(playerUUID, playerName);
Parameters params = Parameters.player(playerUUID, playerName);
UUID serverUUID = serverInfo.getServerUUID(); UUID serverUUID = serverInfo.getServerUUID();
playerNumberGatherer = new Gatherer<>( playerNumberGatherer = new Gatherer<>(Long.class,
Long.class, method -> method.callMethod(extension, playerUUID, playerName),
StoreProviderTransaction::new, StoreProviderTransaction::new,
(provider, result) -> new StorePlayerNumberResultTransaction(provider, serverUUID, playerUUID, result) (provider, result) -> new StorePlayerNumberResultTransaction(provider, serverUUID, playerUUID, result) // TODO Sort out this use of playerUUID
); );
playerNumberGatherer.gather(conditions); playerNumberGatherer.gather(conditions, params);
doubleAndPercentageGatherer.gatherDoubleDataOfPlayer(playerUUID, playerName, conditions); doubleAndPercentageGatherer.gatherDoubleDataOfPlayer(playerUUID, playerName, conditions);
stringGatherer.gatherStringDataOfPlayer(playerUUID, playerName, conditions); stringGatherer.gatherStringDataOfPlayer(playerUUID, playerName, conditions);
tableGatherer.gatherTableDataOfPlayer(playerUUID, playerName, conditions); tableGatherer.gatherTableDataOfPlayer(playerUUID, playerName, conditions);
@ -165,22 +161,14 @@ public class ProviderValueGatherer {
public void updateValues() { public void updateValues() {
Conditions conditions = booleanGatherer.gatherBooleanDataOfServer(); Conditions conditions = booleanGatherer.gatherBooleanDataOfServer();
numberGatherer.gatherNumberDataOfServer(conditions); Parameters params = Parameters.server();
serverNumberGatherer.gather(conditions, params);
doubleAndPercentageGatherer.gatherDoubleDataOfServer(conditions); doubleAndPercentageGatherer.gatherDoubleDataOfServer(conditions);
stringGatherer.gatherStringDataOfServer(conditions); stringGatherer.gatherStringDataOfServer(conditions);
tableGatherer.gatherTableDataOfServer(conditions); tableGatherer.gatherTableDataOfServer(conditions);
} }
interface MethodCaller<T> extends Function<MethodWrapper<T>, T> {
default T call(DataProvider<T> provider) {
try {
return apply(provider.getMethod());
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
throw new DataExtensionMethodCallException(e, provider.getProviderInformation().getPluginName(), provider.getMethod());
}
}
}
interface ProviderTransactionConstructor<T> extends BiFunction<DataProvider<T>, UUID, Transaction> { interface ProviderTransactionConstructor<T> extends BiFunction<DataProvider<T>, UUID, Transaction> {
default Transaction create(DataProvider<T> provider, UUID serverUUID) { default Transaction create(DataProvider<T> provider, UUID serverUUID) {
return apply(provider, serverUUID); return apply(provider, serverUUID);
@ -195,35 +183,32 @@ public class ProviderValueGatherer {
class Gatherer<T> { class Gatherer<T> {
private final Class<T> type; private final Class<T> type;
private final MethodCaller<T> methodCaller;
private final ProviderTransactionConstructor<T> providerTransactionConstructor; private final ProviderTransactionConstructor<T> providerTransactionConstructor;
private final ResultTransactionConstructor<T> resultTransactionConstructor; private final ResultTransactionConstructor<T> resultTransactionConstructor;
public Gatherer( public Gatherer(
Class<T> type, Class<T> type,
MethodCaller<T> methodCaller,
ProviderTransactionConstructor<T> providerTransactionConstructor, ProviderTransactionConstructor<T> providerTransactionConstructor,
ResultTransactionConstructor<T> resultTransactionConstructor ResultTransactionConstructor<T> resultTransactionConstructor
) { ) {
this.type = type; this.type = type;
this.methodCaller = methodCaller;
this.providerTransactionConstructor = providerTransactionConstructor; this.providerTransactionConstructor = providerTransactionConstructor;
this.resultTransactionConstructor = resultTransactionConstructor; this.resultTransactionConstructor = resultTransactionConstructor;
} }
public void gather(Conditions conditions) { public void gather(Conditions conditions, Parameters parameters) {
for (DataProvider<T> provider : dataProviders.getPlayerMethodsByType(type)) { // TODO work this out for (DataProvider<T> provider : dataProviders.getProvidersByTypes(parameters.getMethodType(), type)) {
gather(conditions, provider); gather(conditions, provider, parameters);
} }
} }
private void gather(Conditions conditions, DataProvider<T> provider) { private void gather(Conditions conditions, DataProvider<T> provider, Parameters parameters) {
ProviderInformation information = provider.getProviderInformation(); ProviderInformation information = provider.getProviderInformation();
if (information.getCondition().map(conditions::isNotFulfilled).orElse(false)) { if (information.getCondition().map(conditions::isNotFulfilled).orElse(false)) {
return; // Condition not fulfilled return; // Condition not fulfilled
} }
T result = methodCaller.call(provider); T result = provider.getMethod().callMethod(extension, parameters);
if (result == null) { if (result == null) {
return; // Error during method call return; // Error during method call
} }

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreProviderTransaction; 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.StorePlayerStringResultTransaction;
@ -65,7 +66,7 @@ class StringProviderValueGatherer {
void gatherStringDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) { void gatherStringDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<String>, Callable<String>> methodCaller = method -> () -> method.callMethod(extension, playerUUID, playerName); 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); BiFunction<MethodWrapper<String>, String, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerStringResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<String> stringProvider : dataProviders.getPlayerMethodsByType(String.class)) { for (DataProvider<String> stringProvider : dataProviders.getPlayerMethodsByType(String.class)) {
@ -76,7 +77,7 @@ class StringProviderValueGatherer {
void gatherStringDataOfServer(Conditions conditions) { void gatherStringDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<String>, Callable<String>> methodCaller = method -> () -> method.callMethod(extension); 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); BiFunction<MethodWrapper<String>, String, Transaction> storeTransactionCreator = (method, result) -> new StoreServerStringResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<String> stringProvider : dataProviders.getServerMethodsByType(String.class)) { for (DataProvider<String> stringProvider : dataProviders.getServerMethodsByType(String.class)) {

View File

@ -23,6 +23,7 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.DataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider;
import com.djrapitops.plan.extension.implementation.providers.DataProviders; import com.djrapitops.plan.extension.implementation.providers.DataProviders;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper; 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.StoreIconTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreTableProviderTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.providers.StoreTableProviderTransaction;
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerTableResultTransaction; import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerTableResultTransaction;
@ -66,7 +67,7 @@ class TableProviderValueGatherer {
void gatherTableDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) { void gatherTableDataOfPlayer(UUID playerUUID, String playerName, Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, playerUUID, playerName); Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.player(playerUUID, playerName));
BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result); BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StorePlayerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
for (DataProvider<Table> tableProvider : dataProviders.getPlayerMethodsByType(Table.class)) { for (DataProvider<Table> tableProvider : dataProviders.getPlayerMethodsByType(Table.class)) {
@ -77,7 +78,7 @@ class TableProviderValueGatherer {
void gatherTableDataOfServer(Conditions conditions) { void gatherTableDataOfServer(Conditions conditions) {
// Method parameters abstracted away so that same method can be used for all parameter types // Method parameters abstracted away so that same method can be used for all parameter types
// Same with Method result store transaction creation // Same with Method result store transaction creation
Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension); Function<MethodWrapper<Table>, Callable<Table>> methodCaller = method -> () -> method.callMethod(extension, Parameters.server());
BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StoreServerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), result); BiFunction<MethodWrapper<Table>, Table, Transaction> storeTransactionCreator = (method, result) -> new StoreServerTableResultTransaction(pluginName, serverUUID, method.getMethodName(), result);
for (DataProvider<Table> tableProvider : dataProviders.getServerMethodsByType(Table.class)) { for (DataProvider<Table> tableProvider : dataProviders.getServerMethodsByType(Table.class)) {