From f10c2352dfc5d0fc4e932f347078bcf253e0e541 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 17 Mar 2019 17:52:20 +0200 Subject: [PATCH] Simplified Provider method calling: - Added an enum for each method kind - Moved parameter resolution to the end of the chain, right before invoking the method. - The enum is used for storage in DataProviders for easier access. - ProviderInformation created as early as possible --- .../plan/db/sql/tables/ExtensionTabTable.java | 16 ++++ .../implementation/DataProviderExtractor.java | 10 +-- .../extension/implementation/MethodType.java | 58 +++++++++++++ .../implementation/ProviderInformation.java | 81 +++++++++++++++++++ .../implementation/ProviderValueGatherer.java | 47 +++++++++-- .../providers/BooleanDataProvider.java | 73 +++-------------- .../providers/DataProvider.java | 71 +++------------- .../providers/DataProviders.java | 56 ++++++++++--- .../providers/DoubleDataProvider.java | 70 +++------------- .../providers/MethodWrapper.java | 59 +++++++++++--- .../providers/NumberDataProvider.java | 70 +++------------- .../providers/PercentageDataProvider.java | 70 +++------------- .../providers/StringDataProvider.java | 70 +++------------- .../StoreBooleanProviderTransaction.java | 48 +++++++++++ .../StorePluginTabTransaction.java | 16 ++++ .../StorePlayerBooleanResultTransaction.java | 49 +++++++++++ 16 files changed, 479 insertions(+), 385 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/MethodType.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderInformation.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StoreBooleanProviderTransaction.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionTabTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionTabTable.java index 080daa448..a42c8ac5c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionTabTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionTabTable.java @@ -1,3 +1,19 @@ +/* + * 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 . + */ package com.djrapitops.plan.db.sql.tables; /** diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java index 195027738..509db2877 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/DataProviderExtractor.java @@ -112,11 +112,6 @@ public class DataProviderExtractor { return dataProviders; } - private Optional extractParameterClass(Method method) { - Class[] parameterTypes = method.getParameterTypes(); - return parameterTypes.length == 1 ? Optional.of(parameterTypes[0]) : Optional.empty(); - } - private void extractAllDataProviders() { PluginInfo pluginInfo = extensionExtractor.getPluginInfo(); MethodAnnotations methodAnnotations = extensionExtractor.getMethodAnnotations(); @@ -136,13 +131,12 @@ public class DataProviderExtractor { T annotation = entry.getValue(); Optional conditional = Optional.ofNullable(conditions.get(method)); Optional tab = Optional.ofNullable(tabs.get(method)); - Optional parameterClass = extractParameterClass(method); factory.placeToDataProviders( dataProviders, method, annotation, conditional.map(Conditional::value).orElse(null), tab.map(Tab::value).orElse(null), - pluginInfo.name(), parameterClass + pluginInfo.name() ); } } @@ -155,7 +149,7 @@ public class DataProviderExtractor { interface DataProviderFactory { void placeToDataProviders( DataProviders dataProviders, - Method method, T annotation, String condition, String tab, String pluginName, Optional parameter + Method method, T annotation, String condition, String tab, String pluginName ); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/MethodType.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/MethodType.java new file mode 100644 index 000000000..2faef55b6 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/MethodType.java @@ -0,0 +1,58 @@ +/* + * 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 . + */ +package com.djrapitops.plan.extension.implementation; + +import com.djrapitops.plan.extension.Group; + +import java.lang.reflect.Method; +import java.util.UUID; + +/** + * Utility enum for determining what kind of parameters a provider method used. + *

+ * This also allows figuring out where to save method results. + * + * @author Rsl1122 + */ +public enum MethodType { + + PLAYER_UUID, + PLAYER_NAME, + GROUP, + SERVER; + + public static MethodType forMethod(Method method) { + int parameterCount = method.getParameterCount(); + if (parameterCount == 0) { + return SERVER; + } + + 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; + } else if (Group.class.equals(firstParameter)) { + return GROUP; + } + + throw new IllegalArgumentException(method.getDeclaringClass() + " method " + method.getName() + " had invalid parameters."); + } + +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderInformation.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderInformation.java new file mode 100644 index 000000000..4f46b5b51 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderInformation.java @@ -0,0 +1,81 @@ +/* + * 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 . + */ +package com.djrapitops.plan.extension.implementation; + +import com.djrapitops.plan.extension.icon.Icon; + +/** + * Represents the annotation information provided on a method. + * + * @author Rsl1122 + */ +public class ProviderInformation { + + private final String pluginName; + private final String name; + private final String text; + private final String description; + private final Icon icon; + private final int priority; + private final String tab; // can be null + private final String condition; // can be null + + public ProviderInformation( + String pluginName, String name, String text, String description, Icon icon, int priority, String tab, String condition + ) { + this.pluginName = pluginName; + this.name = name; + this.text = text; + this.description = description; + this.icon = icon; + this.priority = priority; + this.tab = tab; + this.condition = condition; + } + + public String getPluginName() { + return pluginName; + } + + public String getName() { + return name; + } + + public String getText() { + return text; + } + + public String getDescription() { + return description; + } + + public Icon getIcon() { + return icon; + } + + public int getPriority() { + return priority; + } + + public String getTab() { + return tab; + } + + public String getCondition() { + return condition; + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderValueGatherer.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderValueGatherer.java index 434cb3c58..3926f0c8d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderValueGatherer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/ProviderValueGatherer.java @@ -19,14 +19,19 @@ package com.djrapitops.plan.extension.implementation; import com.djrapitops.plan.db.Database; import com.djrapitops.plan.extension.DataExtension; import com.djrapitops.plan.extension.icon.Icon; -import com.djrapitops.plan.extension.implementation.storage.transactions.RemoveInvalidResultsTransaction; -import com.djrapitops.plan.extension.implementation.storage.transactions.StoreIconTransaction; -import com.djrapitops.plan.extension.implementation.storage.transactions.StorePluginTabTransaction; -import com.djrapitops.plan.extension.implementation.storage.transactions.StorePluginTransaction; +import com.djrapitops.plan.extension.implementation.providers.BooleanDataProvider; +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.storage.transactions.*; +import com.djrapitops.plan.extension.implementation.storage.transactions.results.StorePlayerBooleanResultTransaction; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plugin.logging.console.PluginLogger; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; import java.util.UUID; /** @@ -75,7 +80,39 @@ public class ProviderValueGatherer { database.executeTransaction(new RemoveInvalidResultsTransaction(pluginName, serverUUID, extractor.getInvalidatedMethods())); } - public void updateValues(UUID playerUUID) { + public void updateValues(UUID playerUUID, String playerName) { + gatherBooleanData(playerUUID, playerName); + } + private void gatherBooleanData(UUID playerUUID, String playerName) { + String pluginName = extractor.getPluginName(); + UUID serverUUID = serverInfo.getServerUUID(); + Database database = dbSystem.getDatabase(); + DataProviders dataProviders = extractor.getDataProviders(); + Set providedConditions = new HashSet<>(); + + // TODO parse condition tree and traverse based on that. + for (DataProvider booleanProvider : dataProviders.getPlayerMethodsByType(Boolean.class)) { + Optional providedCondition = Optional.empty(); + if (booleanProvider instanceof BooleanDataProvider) { + providedCondition = ((BooleanDataProvider) booleanProvider).getProvidedCondition(); + } + MethodWrapper method = booleanProvider.getMethod(); + + boolean result; + try { + result = method.callMethod(extension, playerUUID, playerName); + } catch (Exception | NoSuchFieldError | NoSuchMethodError e) { + logger.warn(pluginName + " has invalid implementation, method " + method.getMethodName() + " threw exception: " + e.toString()); + continue; + } + + if (result && providedCondition.isPresent()) { + providedConditions.add(providedCondition.get()); + } + + database.executeTransaction(new StoreBooleanProviderTransaction(booleanProvider, providedCondition.orElse(null), serverInfo.getServerUUID())); + database.executeTransaction(new StorePlayerBooleanResultTransaction(pluginName, serverUUID, method.getMethodName(), playerUUID, result)); + } } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/BooleanDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/BooleanDataProvider.java index 5372848a2..234dc6c39 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/BooleanDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/BooleanDataProvider.java @@ -16,13 +16,12 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.Group; import com.djrapitops.plan.extension.annotation.BooleanProvider; import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; import java.util.Optional; -import java.util.UUID; /** * Represents a DataExtension API method annotated with {@link BooleanProvider} annotation. @@ -31,74 +30,28 @@ import java.util.UUID; * * @author Rsl1122 */ -public class BooleanDataProvider extends DataProvider { +public class BooleanDataProvider extends DataProvider { private final String providedCondition; - private BooleanDataProvider( - String plugin, String condition, String tab, int priority, Icon icon, - String text, String description, MethodWrapper method, String providedCondition - ) { - super(plugin, condition, tab, priority, icon, text, description, method); + private BooleanDataProvider(ProviderInformation providerInformation, MethodWrapper method, String providedCondition) { + super(providerInformation, method); this.providedCondition = providedCondition; } public static void placeToDataProviders( DataProviders dataProviders, Method method, BooleanProvider annotation, - String condition, String tab, String pluginName, Optional parameterClass + String condition, String tab, String pluginName ) { - if (parameterClass.isPresent()) { - if (UUID.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - UUID.class, Boolean.class, - new BooleanDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, UUID.class, Boolean.class), - annotation.conditionName() - ) - ); - } else if (String.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - String.class, Boolean.class, - new BooleanDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, String.class, Boolean.class), - annotation.conditionName() - ) - ); - } else if (Group.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - Group.class, Boolean.class, - new BooleanDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, Group.class, Boolean.class), - annotation.conditionName() - ) - ); - } - } else { - dataProviders.put( - null, Boolean.class, - new BooleanDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, null, Boolean.class), - annotation.conditionName() - ) - ); - } + MethodWrapper methodWrapper = new MethodWrapper<>(method, Boolean.class); + Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()); + + ProviderInformation providerInformation = new ProviderInformation( + pluginName, method.getName(), annotation.text(), annotation.description(), providerIcon, annotation.priority(), tab, condition + ); + + dataProviders.put(new BooleanDataProvider(providerInformation, methodWrapper, annotation.conditionName())); } public Optional getProvidedCondition() { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java index 115c92e42..8e3bf1b62 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProvider.java @@ -16,77 +16,28 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.icon.Icon; - -import java.util.Optional; +import com.djrapitops.plan.extension.implementation.ProviderInformation; /** * Abstract representation of all values a Provider annotation provides. * * @author Rsl1122 */ -public abstract class DataProvider { +public abstract class DataProvider { - private final String plugin; - private final String condition; // can be null - private final String tab; // can be null - private final int priority; + private final ProviderInformation providerInformation; + private final MethodWrapper method; - private final Icon icon; - private final String text; - private final String description; - - private final MethodWrapper method; - - public DataProvider( - String plugin, - String condition, - String tab, - int priority, - Icon icon, - String text, - String description, - MethodWrapper method - ) { - this.plugin = plugin; - this.condition = condition; - this.tab = tab; - this.priority = priority; - this.icon = icon; - this.text = text; - this.description = description; + public DataProvider(ProviderInformation providerInformation, MethodWrapper method) { + this.providerInformation = providerInformation; this.method = method; } - public String getPlugin() { - return plugin; - } - - public int getPriority() { - return priority; - } - - public Icon getIcon() { - return icon; - } - - public String getText() { - return text; - } - - public String getDescription() { - return description; - } - - public Optional getCondition() { - return Optional.ofNullable(condition); - } - - public Optional getTab() { - return Optional.ofNullable(tab); - } - - public MethodWrapper getMethod() { + public MethodWrapper getMethod() { return method; } + + public ProviderInformation getProviderInformation() { + return providerInformation; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java index 098f36cf3..99ab3cf4b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DataProviders.java @@ -16,10 +16,9 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.djrapitops.plan.extension.implementation.MethodType; + +import java.util.*; /** * Group class for handling multiple different types of {@link DataProvider}s. @@ -28,23 +27,54 @@ import java.util.Map; */ public class DataProviders { - private Map>> byReturnType; + private Map>> byMethodType; public DataProviders() { - byReturnType = new HashMap<>(); + byMethodType = new EnumMap<>(MethodType.class); } - public void put(Class parameterType, Class returnType, DataProvider provider) { - Map> byParameterType = byReturnType.getOrDefault(returnType, new HashMap<>()); - List dataProviders = byParameterType.getOrDefault(parameterType, new ArrayList<>()); + public void put(DataProvider provider) { + MethodWrapper method = provider.getMethod(); + + MethodType methodType = method.getMethodType(); + Class resultType = method.getResultType(); + + Map> byParameterType = byMethodType.getOrDefault(methodType, new HashMap<>()); + List dataProviders = byParameterType.getOrDefault(resultType, new ArrayList<>()); dataProviders.add(provider); - byParameterType.put(parameterType, dataProviders); - byReturnType.put(returnType, byParameterType); + byParameterType.put(resultType, dataProviders); + byMethodType.put(methodType, byParameterType); } - public List get(Class parameterType, Class returnType) { - return byReturnType.getOrDefault(returnType, new HashMap<>()).getOrDefault(parameterType, new ArrayList<>()); + public List> getPlayerMethodsByType(Class returnType) { + Map> providersAcceptingUUID = byMethodType.getOrDefault(MethodType.PLAYER_UUID, new HashMap<>()); + Map> providersAcceptingName = byMethodType.getOrDefault(MethodType.PLAYER_NAME, new HashMap<>()); + + List> byReturnType = new ArrayList<>(); + for (DataProvider dataProvider : providersAcceptingUUID.getOrDefault(returnType, Collections.emptyList())) { + byReturnType.add((DataProvider) dataProvider); + } + for (DataProvider dataProvider : providersAcceptingName.getOrDefault(returnType, Collections.emptyList())) { + byReturnType.add((DataProvider) dataProvider); + } + return byReturnType; + } + + public List> getServerMethodsByType(Class returnType) { + List> byReturnType = new ArrayList<>(); + for (DataProvider dataProvider : byMethodType.getOrDefault(MethodType.SERVER, new HashMap<>()).getOrDefault(returnType, Collections.emptyList())) { + byReturnType.add((DataProvider) dataProvider); + } + return byReturnType; + } + + public List> getGroupMethodsByType(Class returnType) { + List> byReturnType = new ArrayList<>(); + for (DataProvider dataProvider : byMethodType.getOrDefault(MethodType.GROUP, new HashMap<>()).getOrDefault(returnType, Collections.emptyList())) { + byReturnType.add((DataProvider) dataProvider); + } + return byReturnType; } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DoubleDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DoubleDataProvider.java index dbeb87d34..91c6a5d60 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DoubleDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/DoubleDataProvider.java @@ -16,13 +16,11 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.Group; import com.djrapitops.plan.extension.annotation.DoubleProvider; import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; -import java.util.Optional; -import java.util.UUID; /** * Represents a DataExtension API method annotated with {@link DoubleProvider} annotation. @@ -31,65 +29,23 @@ import java.util.UUID; * * @author Rsl1122 */ -public class DoubleDataProvider extends DataProvider { +public class DoubleDataProvider extends DataProvider { - private DoubleDataProvider( - String plugin, String condition, String tab, int priority, Icon icon, - String text, String description, MethodWrapper method - ) { - super(plugin, condition, tab, priority, icon, text, description, method); + private DoubleDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { + super(providerInformation, methodWrapper); } public static void placeToDataProviders( DataProviders dataProviders, Method method, DoubleProvider annotation, - String condition, String tab, String pluginName, Optional parameterClass + String condition, String tab, String pluginName ) { - if (parameterClass.isPresent()) { - if (UUID.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - UUID.class, Double.class, - new DoubleDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, UUID.class, Double.class) - ) - ); - } else if (String.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - String.class, Double.class, - new DoubleDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, String.class, Double.class) - ) - ); - } else if (Group.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - Group.class, Double.class, - new DoubleDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, Group.class, Double.class) - ) - ); - } - } else { - dataProviders.put( - null, Double.class, - new DoubleDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, null, Double.class) - ) - ); - } + MethodWrapper methodWrapper = new MethodWrapper<>(method, Double.class); + Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()); + + ProviderInformation providerInformation = new ProviderInformation( + pluginName, method.getName(), annotation.text(), annotation.description(), providerIcon, annotation.priority(), tab, condition + ); + + dataProviders.put(new DoubleDataProvider(providerInformation, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java index 3999111f9..4889e80b4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/MethodWrapper.java @@ -17,38 +17,75 @@ 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; /** * Wrap a Method so that it is easier to call. * * @author Rsl1122 */ -public class MethodWrapper { +public class MethodWrapper { private final Method method; - private final Class parameterType; - private final Class resultType; + private final Class resultType; + private MethodType methodType; - public MethodWrapper(Method method, Class parameterType, Class resultType) { + public MethodWrapper(Method method, Class resultType) { this.method = method; - this.parameterType = parameterType; this.resultType = resultType; + methodType = MethodType.forMethod(this.method); } - public K callMethod(DataExtension extension, T parameter) throws InvocationTargetException, IllegalAccessException { - if (parameterType == null) { - return resultType.cast(method.invoke(extension)); + public T callMethod(DataExtension extension, UUID playerUUID, String playerName) throws InvocationTargetException, IllegalAccessException { + if (methodType != MethodType.PLAYER_NAME && methodType != MethodType.PLAYER_UUID) { + throw new IllegalStateException(method.getDeclaringClass() + " method " + method.getName() + " is not GROUP method."); } - if (parameterType.isInstance(parameter)) { - return resultType.cast(method.invoke(extension, parameter)); + return callMethod(extension, playerUUID, playerName, null); + } + + public T callMethod(DataExtension extension, Group group) throws InvocationTargetException, IllegalAccessException { + 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) throws InvocationTargetException, IllegalAccessException { + 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) throws InvocationTargetException, IllegalAccessException { + switch (methodType) { + case SERVER: + return resultType.cast(method.invoke(extension)); + case PLAYER_UUID: + return resultType.cast(method.invoke(extension, playerUUID)); + case PLAYER_NAME: + return resultType.cast(method.invoke(extension, playerName)); + case GROUP: + return resultType.cast(method.invoke(extension, group)); + default: + throw new IllegalArgumentException(method.getDeclaringClass() + " method " + method.getName() + " had invalid parameters."); } - throw new IllegalArgumentException(parameter.getClass().getName() + " can not be accepted as parameter when type is " + parameterType.getName()); } public String getMethodName() { return method.getName(); } + + public MethodType getMethodType() { + return methodType; + } + + public Class getResultType() { + return resultType; + } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/NumberDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/NumberDataProvider.java index f285663fd..f12c83d1a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/NumberDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/NumberDataProvider.java @@ -16,13 +16,11 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.Group; import com.djrapitops.plan.extension.annotation.NumberProvider; import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; -import java.util.Optional; -import java.util.UUID; /** * Represents a DataExtension API method annotated with {@link NumberProvider} annotation. @@ -31,65 +29,23 @@ import java.util.UUID; * * @author Rsl1122 */ -public class NumberDataProvider extends DataProvider { +public class NumberDataProvider extends DataProvider { - private NumberDataProvider( - String plugin, String condition, String tab, int priority, Icon icon, - String text, String description, MethodWrapper method - ) { - super(plugin, condition, tab, priority, icon, text, description, method); + private NumberDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { + super(providerInformation, methodWrapper); } public static void placeToDataProviders( DataProviders dataProviders, Method method, NumberProvider annotation, - String condition, String tab, String pluginName, Optional parameterClass + String condition, String tab, String pluginName ) { - if (parameterClass.isPresent()) { - if (UUID.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - UUID.class, Long.class, - new NumberDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, UUID.class, Long.class) - ) - ); - } else if (String.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - String.class, Long.class, - new NumberDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, String.class, Long.class) - ) - ); - } else if (Group.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - Group.class, Long.class, - new NumberDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, Group.class, Long.class) - ) - ); - } - } else { - dataProviders.put( - null, Long.class, - new NumberDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, null, Long.class) - ) - ); - } + MethodWrapper methodWrapper = new MethodWrapper<>(method, Long.class); + Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()); + + ProviderInformation providerInformation = new ProviderInformation( + pluginName, method.getName(), annotation.text(), annotation.description(), providerIcon, annotation.priority(), tab, condition + ); + + dataProviders.put(new NumberDataProvider(providerInformation, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/PercentageDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/PercentageDataProvider.java index 27823d2df..01c235d8d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/PercentageDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/PercentageDataProvider.java @@ -16,13 +16,11 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.Group; import com.djrapitops.plan.extension.annotation.PercentageProvider; import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; -import java.util.Optional; -import java.util.UUID; /** * Represents a DataExtension API method annotated with {@link PercentageProvider} annotation. @@ -31,65 +29,23 @@ import java.util.UUID; * * @author Rsl1122 */ -public class PercentageDataProvider extends DataProvider { +public class PercentageDataProvider extends DataProvider { - private PercentageDataProvider( - String plugin, String condition, String tab, int priority, Icon icon, - String text, String description, MethodWrapper method - ) { - super(plugin, condition, tab, priority, icon, text, description, method); + private PercentageDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { + super(providerInformation, methodWrapper); } public static void placeToDataProviders( DataProviders dataProviders, Method method, PercentageProvider annotation, - String condition, String tab, String pluginName, Optional parameterClass + String condition, String tab, String pluginName ) { - if (parameterClass.isPresent()) { - if (UUID.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - UUID.class, Double.class, - new PercentageDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, UUID.class, Double.class) - ) - ); - } else if (String.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - String.class, Double.class, - new PercentageDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, String.class, Double.class) - ) - ); - } else if (Group.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - Group.class, Double.class, - new PercentageDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, Group.class, Double.class) - ) - ); - } - } else { - dataProviders.put( - null, Double.class, - new PercentageDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, null, Double.class) - ) - ); - } + MethodWrapper methodWrapper = new MethodWrapper<>(method, Double.class); + Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()); + + ProviderInformation providerInformation = new ProviderInformation( + pluginName, method.getName(), annotation.text(), annotation.description(), providerIcon, annotation.priority(), tab, condition + ); + + dataProviders.put(new PercentageDataProvider(providerInformation, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/StringDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/StringDataProvider.java index e668c619f..b46a774af 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/StringDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/StringDataProvider.java @@ -16,13 +16,11 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.Group; import com.djrapitops.plan.extension.annotation.StringProvider; import com.djrapitops.plan.extension.icon.Icon; +import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; -import java.util.Optional; -import java.util.UUID; /** * Represents a DataExtension API method annotated with {@link StringProvider} annotation. @@ -31,65 +29,23 @@ import java.util.UUID; * * @author Rsl1122 */ -public class StringDataProvider extends DataProvider { +public class StringDataProvider extends DataProvider { - private StringDataProvider( - String plugin, String condition, String tab, int priority, Icon icon, - String text, String description, MethodWrapper method - ) { - super(plugin, condition, tab, priority, icon, text, description, method); + private StringDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { + super(providerInformation, methodWrapper); } public static void placeToDataProviders( DataProviders dataProviders, Method method, StringProvider annotation, - String condition, String tab, String pluginName, Optional parameterClass + String condition, String tab, String pluginName ) { - if (parameterClass.isPresent()) { - if (UUID.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - UUID.class, String.class, - new StringDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, UUID.class, String.class) - ) - ); - } else if (String.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - String.class, String.class, - new StringDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, String.class, String.class) - ) - ); - } else if (Group.class.isAssignableFrom(parameterClass.get())) { - dataProviders.put( - Group.class, String.class, - new StringDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, Group.class, String.class) - ) - ); - } - } else { - dataProviders.put( - null, String.class, - new StringDataProvider<>( - pluginName, condition, tab, - annotation.priority(), - new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()), - annotation.text(), annotation.description(), - new MethodWrapper<>(method, null, String.class) - ) - ); - } + MethodWrapper methodWrapper = new MethodWrapper<>(method, String.class); + Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.iconColor()); + + ProviderInformation providerInformation = new ProviderInformation( + pluginName, method.getName(), annotation.text(), annotation.description(), providerIcon, annotation.priority(), tab, condition + ); + + dataProviders.put(new StringDataProvider(providerInformation, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StoreBooleanProviderTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StoreBooleanProviderTransaction.java new file mode 100644 index 000000000..a6619bf2c --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StoreBooleanProviderTransaction.java @@ -0,0 +1,48 @@ +/* + * 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 . + */ +package com.djrapitops.plan.extension.implementation.storage.transactions; + +import com.djrapitops.plan.db.access.transactions.Transaction; +import com.djrapitops.plan.extension.implementation.ProviderInformation; +import com.djrapitops.plan.extension.implementation.providers.DataProvider; + +import java.util.UUID; + +/** + * Transaction to store information about a {@link com.djrapitops.plan.extension.implementation.providers.BooleanDataProvider}. + * + * @author Rsl1122 + */ +public class StoreBooleanProviderTransaction extends Transaction { + + private final DataProvider booleanProvider; + private final String providedCondition; + private final UUID serverUUID; + + public StoreBooleanProviderTransaction(DataProvider booleanProvider, String providedCondition, UUID serverUUID) { + this.booleanProvider = booleanProvider; + this.providedCondition = providedCondition; + this.serverUUID = serverUUID; + } + + @Override + protected void performOperations() { + ProviderInformation providerInformation = booleanProvider.getProviderInformation(); + + // TODO Store provider in a table + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StorePluginTabTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StorePluginTabTransaction.java index f36aed117..a3290f450 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StorePluginTabTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/StorePluginTabTransaction.java @@ -1,3 +1,19 @@ +/* + * 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 . + */ package com.djrapitops.plan.extension.implementation.storage.transactions; import com.djrapitops.plan.db.access.ExecStatement; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java new file mode 100644 index 000000000..71b77534c --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java @@ -0,0 +1,49 @@ +/* + * 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 . + */ +package com.djrapitops.plan.extension.implementation.storage.transactions.results; + +import com.djrapitops.plan.db.access.transactions.Transaction; + +import java.util.UUID; + +/** + * Transaction to store + * + * @author Rsl1122 + */ +public class StorePlayerBooleanResultTransaction extends Transaction { + + private final String pluginName; + private final UUID serverUUID; + private final String methodName; + private final UUID playerUUID; + + private final boolean value; + + public StorePlayerBooleanResultTransaction(String pluginName, UUID serverUUID, String methodName, UUID playerUUID, boolean value) { + this.pluginName = pluginName; + this.serverUUID = serverUUID; + this.methodName = methodName; + this.playerUUID = playerUUID; + this.value = value; + } + + @Override + protected void performOperations() { + // TODO Store data in a table + } +} \ No newline at end of file