mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-02 14:37:45 +01:00
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:
parent
ee9a9ba7d8
commit
f438366adc
@ -30,8 +30,11 @@ import java.util.UUID;
|
||||
*/
|
||||
public enum MethodType {
|
||||
|
||||
@Deprecated
|
||||
PLAYER_UUID,
|
||||
@Deprecated
|
||||
PLAYER_NAME,
|
||||
PLAYER,
|
||||
GROUP,
|
||||
SERVER;
|
||||
|
||||
@ -44,10 +47,8 @@ public enum MethodType {
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
Class<?> firstParameter = parameterTypes[0];
|
||||
|
||||
if (UUID.class.equals(firstParameter)) {
|
||||
return PLAYER_UUID;
|
||||
} else if (String.class.equals(firstParameter)) {
|
||||
return PLAYER_NAME;
|
||||
if (UUID.class.equals(firstParameter) || String.class.equals(firstParameter)) {
|
||||
return PLAYER;
|
||||
} else if (Group.class.equals(firstParameter)) {
|
||||
return GROUP;
|
||||
}
|
||||
|
@ -47,50 +47,44 @@ public class DataProviders {
|
||||
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());
|
||||
return byReturnType.getOrDefault(returnType, 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);
|
||||
List<DataProvider<?>> uncastProviders = byReturnType.get(returnType);
|
||||
if (uncastProviders == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.PLAYER_NAME, returnType)) {
|
||||
byReturnType.add((DataProvider<T>) dataProvider);
|
||||
}
|
||||
return byReturnType;
|
||||
}
|
||||
|
||||
public <T> List<DataProvider<T>> getServerMethodsByType(Class<T> returnType) {
|
||||
// Cast to T
|
||||
List<DataProvider<T>> providers = new ArrayList<>();
|
||||
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.SERVER, returnType)) {
|
||||
for (DataProvider<?> dataProvider : uncastProviders) {
|
||||
providers.add((DataProvider<T>) dataProvider);
|
||||
}
|
||||
return providers;
|
||||
}
|
||||
|
||||
public <T> List<DataProvider<T>> getGroupMethodsByType(Class<T> returnType) {
|
||||
List<DataProvider<T>> byReturnType = new ArrayList<>();
|
||||
for (DataProvider<?> dataProvider : getProvidersByTypes(MethodType.GROUP, returnType)) {
|
||||
byReturnType.add((DataProvider<T>) dataProvider);
|
||||
}
|
||||
return byReturnType;
|
||||
public <T> List<DataProvider<T>> getPlayerMethodsByType(Class<T> returnType) {
|
||||
return getProvidersByTypes(MethodType.PLAYER, returnType);
|
||||
}
|
||||
|
||||
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();
|
||||
Map<Class<?>, List<DataProvider<?>>> byResultType = byMethodType.getOrDefault(methodType, Collections.emptyMap());
|
||||
if (byResultType.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<?> returnType = toRemove.getReturnType();
|
||||
List<DataProvider<?>> providers = getProvidersByTypes(methodType, returnType);
|
||||
Class<T> returnType = toRemove.getReturnType();
|
||||
List<DataProvider<T>> providers = getProvidersByTypes(methodType, returnType);
|
||||
|
||||
DataProvider<?> providerToRemove = null;
|
||||
for (DataProvider<?> provider : providers) {
|
||||
DataProvider<T> providerToRemove = null;
|
||||
for (DataProvider<T> provider : providers) {
|
||||
if (provider.getMethod().equals(toRemove)) {
|
||||
providerToRemove = provider;
|
||||
break;
|
||||
|
@ -17,14 +17,12 @@
|
||||
package com.djrapitops.plan.extension.implementation.providers;
|
||||
|
||||
import com.djrapitops.plan.extension.DataExtension;
|
||||
import com.djrapitops.plan.extension.Group;
|
||||
import com.djrapitops.plan.extension.NotReadyException;
|
||||
import com.djrapitops.plan.extension.implementation.MethodType;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Wrap a Method so that it is easier to call.
|
||||
@ -43,41 +41,9 @@ public class MethodWrapper<T> {
|
||||
methodType = MethodType.forMethod(this.method);
|
||||
}
|
||||
|
||||
public T callMethod(DataExtension extension, UUID playerUUID, String playerName) {
|
||||
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) {
|
||||
public T callMethod(DataExtension of, Parameters with) {
|
||||
try {
|
||||
switch (methodType) {
|
||||
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.");
|
||||
}
|
||||
return returnType.cast(with.call(of, method));
|
||||
} catch (InvocationTargetException notReadyToBeCalled) {
|
||||
if (notReadyToBeCalled.getCause() instanceof NotReadyException) {
|
||||
return null; // Data or API not available to make the call.
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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.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.StorePlayerBooleanResultTransaction;
|
||||
@ -68,7 +69,7 @@ class BooleanProviderValueGatherer {
|
||||
|
||||
// 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, 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);
|
||||
|
||||
do {
|
||||
@ -90,7 +91,7 @@ class BooleanProviderValueGatherer {
|
||||
|
||||
// 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);
|
||||
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);
|
||||
|
||||
do {
|
||||
|
@ -19,10 +19,7 @@ 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.PercentageDataProvider;
|
||||
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;
|
||||
@ -67,7 +64,7 @@ class DoubleAndPercentageProviderValueGatherer {
|
||||
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, 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> doubleStoreTransactionCreator = (method, result) -> new StorePlayerDoubleResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result);
|
||||
|
||||
@ -79,7 +76,7 @@ class DoubleAndPercentageProviderValueGatherer {
|
||||
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);
|
||||
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);
|
||||
|
||||
|
@ -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.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;
|
||||
@ -75,7 +76,7 @@ class GroupProviderValueGatherer {
|
||||
|
||||
MethodWrapper<String[]> method = groupProvider.getMethod();
|
||||
try {
|
||||
String[] result = method.callMethod(extension, playerUUID, playerName);
|
||||
String[] result = method.callMethod(extension, Parameters.player(playerUUID, playerName));
|
||||
if (result == null) {
|
||||
return; // Error during call
|
||||
}
|
||||
|
@ -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.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;
|
||||
@ -64,7 +65,7 @@ class NumberProviderValueGatherer {
|
||||
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, 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);
|
||||
|
||||
for (DataProvider<Long> numberProvider : dataProviders.getPlayerMethodsByType(Long.class)) {
|
||||
@ -75,7 +76,7 @@ class NumberProviderValueGatherer {
|
||||
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);
|
||||
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)) {
|
||||
|
@ -16,7 +16,6 @@
|
||||
*/
|
||||
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.DataExtension;
|
||||
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.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.StorePluginTransaction;
|
||||
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.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 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 BooleanProviderValueGatherer booleanGatherer;
|
||||
private final NumberProviderValueGatherer numberGatherer;
|
||||
private final DoubleAndPercentageProviderValueGatherer doubleAndPercentageGatherer;
|
||||
private final StringProviderValueGatherer stringGatherer;
|
||||
private final TableProviderValueGatherer tableGatherer;
|
||||
@ -85,7 +83,7 @@ public class ProviderValueGatherer {
|
||||
booleanGatherer = new BooleanProviderValueGatherer(
|
||||
pluginName, extension, serverUUID, database, dataProviders
|
||||
);
|
||||
numberGatherer = new NumberProviderValueGatherer(
|
||||
NumberProviderValueGatherer numberGatherer = new NumberProviderValueGatherer(
|
||||
pluginName, extension, serverUUID, database, dataProviders
|
||||
);
|
||||
doubleAndPercentageGatherer = new DoubleAndPercentageProviderValueGatherer(
|
||||
@ -101,9 +99,7 @@ public class ProviderValueGatherer {
|
||||
pluginName, extension, serverUUID, database, dataProviders
|
||||
);
|
||||
|
||||
serverNumberGatherer = new Gatherer<>(
|
||||
Long.class,
|
||||
method -> method.callMethod(extension),
|
||||
serverNumberGatherer = new Gatherer<>(Long.class,
|
||||
StoreProviderTransaction::new,
|
||||
(provider, result) -> new StoreServerNumberResultTransaction(provider, serverUUID, result)
|
||||
);
|
||||
@ -150,13 +146,13 @@ 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, method -> method.callMethod(extension, playerUUID, playerName),
|
||||
playerNumberGatherer = new Gatherer<>(Long.class,
|
||||
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);
|
||||
stringGatherer.gatherStringDataOfPlayer(playerUUID, playerName, conditions);
|
||||
tableGatherer.gatherTableDataOfPlayer(playerUUID, playerName, conditions);
|
||||
@ -165,22 +161,14 @@ public class ProviderValueGatherer {
|
||||
|
||||
public void updateValues() {
|
||||
Conditions conditions = booleanGatherer.gatherBooleanDataOfServer();
|
||||
numberGatherer.gatherNumberDataOfServer(conditions);
|
||||
Parameters params = Parameters.server();
|
||||
|
||||
serverNumberGatherer.gather(conditions, params);
|
||||
doubleAndPercentageGatherer.gatherDoubleDataOfServer(conditions);
|
||||
stringGatherer.gatherStringDataOfServer(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> {
|
||||
default Transaction create(DataProvider<T> provider, UUID serverUUID) {
|
||||
return apply(provider, serverUUID);
|
||||
@ -195,35 +183,32 @@ public class ProviderValueGatherer {
|
||||
|
||||
class Gatherer<T> {
|
||||
private final Class<T> type;
|
||||
private final MethodCaller<T> methodCaller;
|
||||
private final ProviderTransactionConstructor<T> providerTransactionConstructor;
|
||||
private final ResultTransactionConstructor<T> resultTransactionConstructor;
|
||||
|
||||
public Gatherer(
|
||||
Class<T> type,
|
||||
MethodCaller<T> methodCaller,
|
||||
ProviderTransactionConstructor<T> providerTransactionConstructor,
|
||||
ResultTransactionConstructor<T> resultTransactionConstructor
|
||||
) {
|
||||
this.type = type;
|
||||
this.methodCaller = methodCaller;
|
||||
this.providerTransactionConstructor = providerTransactionConstructor;
|
||||
this.resultTransactionConstructor = resultTransactionConstructor;
|
||||
}
|
||||
|
||||
public void gather(Conditions conditions) {
|
||||
for (DataProvider<T> provider : dataProviders.getPlayerMethodsByType(type)) { // TODO work this out
|
||||
gather(conditions, provider);
|
||||
public void gather(Conditions conditions, Parameters parameters) {
|
||||
for (DataProvider<T> provider : dataProviders.getProvidersByTypes(parameters.getMethodType(), type)) {
|
||||
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();
|
||||
if (information.getCondition().map(conditions::isNotFulfilled).orElse(false)) {
|
||||
return; // Condition not fulfilled
|
||||
}
|
||||
|
||||
T result = methodCaller.call(provider);
|
||||
T result = provider.getMethod().callMethod(extension, parameters);
|
||||
if (result == null) {
|
||||
return; // Error during method call
|
||||
}
|
||||
|
@ -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.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;
|
||||
@ -65,7 +66,7 @@ class StringProviderValueGatherer {
|
||||
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, 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);
|
||||
|
||||
for (DataProvider<String> stringProvider : dataProviders.getPlayerMethodsByType(String.class)) {
|
||||
@ -76,7 +77,7 @@ class StringProviderValueGatherer {
|
||||
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);
|
||||
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)) {
|
||||
|
@ -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.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.StoreTableProviderTransaction;
|
||||
import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerTableResultTransaction;
|
||||
@ -66,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, 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);
|
||||
|
||||
for (DataProvider<Table> tableProvider : dataProviders.getPlayerMethodsByType(Table.class)) {
|
||||
@ -77,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);
|
||||
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);
|
||||
|
||||
for (DataProvider<Table> tableProvider : dataProviders.getServerMethodsByType(Table.class)) {
|
||||
|
Loading…
Reference in New Issue
Block a user