From ee9a9ba7d86955611749322a6b5d49fa0166a29c Mon Sep 17 00:00:00 2001 From: Rsl1122 <24460436+Rsl1122@users.noreply.github.com> Date: Wed, 25 Dec 2019 11:11:24 +0200 Subject: [PATCH] Refactored code of DataProvider Some optional values from different providers were not included in ProviderInformation, which lead to unnecessary instanceof usage and weird static methods. Added the optional info to ProviderInformation and created a Builder, making much cleaner code and removing those odd ball methods. - Also fixed StoreProviderTransaction having insert of Icon and Tab in wrong order --- .../extractor/MethodAnnotations.java | 6 +- .../implementation/DataProviderExtractor.java | 8 +- .../implementation/ProviderInformation.java | 144 ++++++++++++++++-- .../providers/BooleanDataProvider.java | 61 +++----- .../providers/DataProvider.java | 4 +- .../providers/DoubleDataProvider.java | 31 ++-- .../providers/GroupDataProvider.java | 32 ++-- .../providers/NumberDataProvider.java | 49 +++--- .../providers/PercentageDataProvider.java | 25 ++- .../providers/StringDataProvider.java | 49 +++--- .../providers/TableDataProvider.java | 42 ++--- .../BooleanProviderValueGatherer.java | 16 +- .../gathering/TableProviderValueGatherer.java | 3 +- .../providers/StoreProviderTransaction.java | 23 ++- .../StoreTableProviderTransaction.java | 29 ++-- 15 files changed, 305 insertions(+), 217 deletions(-) diff --git a/Plan/api/src/main/java/com/djrapitops/plan/extension/extractor/MethodAnnotations.java b/Plan/api/src/main/java/com/djrapitops/plan/extension/extractor/MethodAnnotations.java index d7dea0e08..b773ac111 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/extension/extractor/MethodAnnotations.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/extension/extractor/MethodAnnotations.java @@ -30,15 +30,15 @@ import java.util.Optional; */ public class MethodAnnotations { - private final Map> byAnnotationType; + private final Map, Map> byAnnotationType; public MethodAnnotations() { byAnnotationType = new HashMap<>(); } - public static boolean hasAnyOf(Method method, Class... annotationClasses) { + public static boolean hasAnyOf(Method method, Class... annotationClasses) { for (Annotation annotation : method.getAnnotations()) { - for (Class annotationClass : annotationClasses) { + for (Class annotationClass : annotationClasses) { if (annotationClass.isAssignableFrom(annotation.getClass())) { return true; } 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 5f2f64b3e..fca24ae07 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 @@ -128,17 +128,19 @@ public class DataProviderExtractor { } private void extractProviders(PluginInfo pluginInfo, Map tabs, Map conditions, Class ofKind, DataProviderFactory factory) { + String pluginName = extractor.getPluginInfo().name(); + for (Map.Entry entry : extractor.getMethodAnnotations().getMethodAnnotations(ofKind).entrySet()) { Method method = entry.getKey(); T annotation = entry.getValue(); - Optional conditional = Optional.ofNullable(conditions.get(method)); + Conditional conditional = conditions.get(method); Optional tab = Optional.ofNullable(tabs.get(method)); factory.placeToDataProviders( providers, method, annotation, - conditional.orElse(null), + conditional, tab.map(Tab::value).orElse(null), - pluginInfo.name() + pluginName ); } } 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 index a06b625cd..e13185626 100644 --- 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 @@ -16,7 +16,9 @@ */ package com.djrapitops.plan.extension.implementation; +import com.djrapitops.plan.extension.FormatType; import com.djrapitops.plan.extension.annotation.Conditional; +import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.icon.Icon; import com.djrapitops.plan.extension.implementation.results.ExtensionDescriptive; import org.apache.commons.lang3.StringUtils; @@ -33,17 +35,29 @@ public class ProviderInformation extends ExtensionDescriptive { private final String pluginName; private final boolean showInPlayersTable; - private final String tab; // can be null - private final Conditional condition; // can be null + private final String tab; // can be null + private final Conditional condition; // can be null + private final boolean hidden; // default false, BooleanProvider + private final String providedCondition; // can be null, BooleanProvider + private final FormatType formatType; // can be null, NumberProvider + private final boolean isPlayerName; // default false, StringProvider + private final Color tableColor; // can be null, TableProvider - public ProviderInformation( - String pluginName, String name, String text, String description, Icon icon, int priority, boolean showInPlayersTable, String tab, Conditional condition - ) { - super(name, text, description, icon, priority); - this.pluginName = pluginName; - this.showInPlayersTable = showInPlayersTable; - this.tab = tab; - this.condition = condition; + private ProviderInformation(ProviderInformation.Builder builder) { + super(builder.name, builder.text, builder.description, builder.icon, builder.priority); + pluginName = builder.pluginName; + showInPlayersTable = builder.showInPlayersTable; + tab = builder.tab; + condition = builder.condition; + hidden = builder.hidden; + providedCondition = builder.providedCondition; + formatType = builder.formatType; + isPlayerName = builder.isPlayerName; + tableColor = builder.tableColor; + } + + public static ProviderInformation.Builder builder(String pluginName) { + return new ProviderInformation.Builder(pluginName); } public String getPluginName() { @@ -54,6 +68,22 @@ public class ProviderInformation extends ExtensionDescriptive { return showInPlayersTable; } + public boolean isHidden() { + return hidden; + } + + public String getProvidedCondition() { + return providedCondition; + } + + public Optional getFormatType() { + return Optional.ofNullable(formatType); + } + + public boolean isPlayerName() { + return isPlayerName; + } + public Optional getTab() { return tab == null || tab.isEmpty() ? Optional.empty() @@ -89,4 +119,98 @@ public class ProviderInformation extends ExtensionDescriptive { public int hashCode() { return Objects.hash(super.hashCode(), pluginName, tab, condition); } + + public Color getTableColor() { + return tableColor; + } + + public static class Builder { + private final String pluginName; + private String name; + private String text; + private String description; + private Icon icon; + private int priority; + private boolean showInPlayersTable = false; + private String tab; // can be null + private Conditional condition; // can be null + private boolean hidden = false; // default false, BooleanProvider + private String providedCondition; // can be null, BooleanProvider + private FormatType formatType; // can be null, NumberProvider + private boolean isPlayerName = false; // default false, StringProvider + private Color tableColor; // can be null, TableProvider + + public Builder(String pluginName) { + this.pluginName = pluginName; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setText(String text) { + this.text = text; + return this; + } + + public Builder setDescription(String description) { + this.description = description; + return this; + } + + public Builder setIcon(Icon icon) { + this.icon = icon; + return this; + } + + public Builder setPriority(int priority) { + this.priority = priority; + return this; + } + + public Builder setShowInPlayersTable(boolean showInPlayersTable) { + this.showInPlayersTable = showInPlayersTable; + return this; + } + + public Builder setTab(String tab) { + this.tab = tab; + return this; + } + + public Builder setCondition(Conditional condition) { + this.condition = condition; + return this; + } + + public Builder setHidden(boolean hidden) { + this.hidden = hidden; + return this; + } + + public Builder setProvidedCondition(String providedCondition) { + this.providedCondition = providedCondition; + return this; + } + + public Builder setFormatType(FormatType formatType) { + this.formatType = formatType; + return this; + } + + public Builder setPlayerName(boolean playerName) { + isPlayerName = playerName; + return this; + } + + public Builder setTableColor(Color tableColor) { + this.tableColor = tableColor; + return this; + } + + public ProviderInformation build() { + return new ProviderInformation(this); + } + } } \ 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 1c84849cd..a88e8ce1f 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 @@ -20,60 +20,41 @@ import com.djrapitops.plan.extension.annotation.BooleanProvider; import com.djrapitops.plan.extension.annotation.Conditional; import com.djrapitops.plan.extension.icon.Icon; import com.djrapitops.plan.extension.implementation.ProviderInformation; -import org.apache.commons.lang3.StringUtils; import java.lang.reflect.Method; -import java.util.Optional; /** - * Represents a DataExtension API method annotated with {@link BooleanProvider} annotation. - *

- * Used to obtain data to place in the database. + * Contains code that acts on {@link BooleanProvider} annotations. * * @author Rsl1122 */ -public class BooleanDataProvider extends DataProvider { +public class BooleanDataProvider { - private final String providedCondition; - private final boolean hidden; - - private BooleanDataProvider(ProviderInformation providerInformation, MethodWrapper method, String providedCondition, boolean hidden) { - super(providerInformation, method); - - this.providedCondition = providedCondition; - this.hidden = hidden; + private BooleanDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, BooleanProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setDescription(annotation.description()) + .setPriority(annotation.priority()) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.iconColor()) + ).setShowInPlayersTable(annotation.showInPlayerTable()) + .setCondition(condition) + .setTab(tab) + .setHidden(annotation.hidden()) + .setProvidedCondition(annotation.conditionName()) + .build(); + 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(), annotation.showInPlayerTable(), tab, condition - ); - - dataProviders.put(new BooleanDataProvider(providerInformation, methodWrapper, annotation.conditionName(), annotation.hidden())); - } - - public static Optional getProvidedCondition(DataProvider provider) { - if (provider instanceof BooleanDataProvider) { - return ((BooleanDataProvider) provider).getProvidedCondition(); - } - return Optional.empty(); - } - - public Optional getProvidedCondition() { - return providedCondition == null || providedCondition.isEmpty() ? Optional.empty() : Optional.of(StringUtils.truncate(providedCondition, 50)); - } - - public static boolean isHidden(DataProvider provider) { - return provider instanceof BooleanDataProvider && ((BooleanDataProvider) provider).isHidden(); - } - - public boolean isHidden() { - return hidden; + dataProviders.put(new DataProvider<>(information, methodWrapper)); } } \ No newline at end of file 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 0c9216adf..1764b0129 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 @@ -21,11 +21,11 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.util.Objects; /** - * Abstract representation of all values a Provider annotation provides. + * Representation of all values a Provider annotation provides. * * @author Rsl1122 */ -public abstract class DataProvider { +public class DataProvider { private final ProviderInformation providerInformation; private final MethodWrapper method; 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 ac467983e..a8945e9b7 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 @@ -24,29 +24,36 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; /** - * Represents a DataExtension API method annotated with {@link DoubleProvider} annotation. - *

- * Used to obtain data to place in the database. + * Contains code that acts on {@link DoubleProvider} annotations. * * @author Rsl1122 */ -public class DoubleDataProvider extends DataProvider { +public class DoubleDataProvider { - private DoubleDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { - super(providerInformation, methodWrapper); + private DoubleDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, DoubleProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setDescription(annotation.description()) + .setPriority(annotation.priority()) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.iconColor()) + ).setShowInPlayersTable(annotation.showInPlayerTable()) + .setCondition(condition) + .setTab(tab) + .build(); + 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(), annotation.showInPlayerTable(), tab, condition - ); - - dataProviders.put(new DoubleDataProvider(providerInformation, methodWrapper)); + dataProviders.put(new DataProvider<>(information, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/GroupDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/GroupDataProvider.java index 962e9d6d8..fc7246b71 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/GroupDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/GroupDataProvider.java @@ -18,36 +18,40 @@ package com.djrapitops.plan.extension.implementation.providers; import com.djrapitops.plan.extension.annotation.Conditional; import com.djrapitops.plan.extension.annotation.GroupProvider; -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; /** - * Represents a DataExtension API method annotated with {@link StringProvider} annotation. - *

- * Used to obtain data to place in the database. + * Contains code that acts on {@link GroupProvider} annotations. * * @author Rsl1122 */ -public class GroupDataProvider extends DataProvider { +public class GroupDataProvider { - private GroupDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { - super(providerInformation, methodWrapper); + private GroupDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, GroupProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setPriority(0) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.groupColor()) + ).setShowInPlayersTable(true) + .setCondition(condition) + .setTab(tab) + .build(); + MethodWrapper methodWrapper = new MethodWrapper<>(method, String[].class); - Icon providerIcon = new Icon(annotation.iconFamily(), annotation.iconName(), annotation.groupColor()); - - ProviderInformation providerInformation = new ProviderInformation( - pluginName, method.getName(), annotation.text(), null, providerIcon, 0, true, tab, condition - ); - - dataProviders.put(new GroupDataProvider(providerInformation, methodWrapper)); + dataProviders.put(new DataProvider<>(information, methodWrapper)); } } \ 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 4ba47156c..7dfc7b00e 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,53 +16,44 @@ */ package com.djrapitops.plan.extension.implementation.providers; -import com.djrapitops.plan.extension.FormatType; import com.djrapitops.plan.extension.annotation.Conditional; 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; /** - * Represents a DataExtension API method annotated with {@link NumberProvider} annotation. - *

- * Used to obtain data to place in the database. + * Contains code that acts on {@link NumberProvider} annotations. * * @author Rsl1122 */ -public class NumberDataProvider extends DataProvider { +public class NumberDataProvider { - private final FormatType formatType; - - private NumberDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper, FormatType formatType) { - super(providerInformation, methodWrapper); - this.formatType = formatType; + private NumberDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, NumberProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setDescription(annotation.description()) + .setPriority(annotation.priority()) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.iconColor()) + ).setShowInPlayersTable(annotation.showInPlayerTable()) + .setCondition(condition) + .setTab(tab) + .setFormatType(annotation.format()) + .build(); + 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(), annotation.showInPlayerTable(), tab, condition - ); - - dataProviders.put(new NumberDataProvider(providerInformation, methodWrapper, annotation.format())); - } - - public static Optional getFormatType(DataProvider provider) { - if (provider instanceof NumberDataProvider) { - return Optional.of(((NumberDataProvider) provider).getFormatType()); - } - return Optional.empty(); - } - - public FormatType getFormatType() { - return formatType; + dataProviders.put(new DataProvider<>(information, 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 6c07db992..91453e2b9 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 @@ -25,13 +25,13 @@ import java.lang.reflect.Method; /** * Represents a DataExtension API method annotated with {@link PercentageProvider} annotation. - *

- * Used to obtain data to place in the database. * * @author Rsl1122 */ public class PercentageDataProvider extends DataProvider { + // TODO Remove need for instanceof in DoubleAndPercentageProviderGatherer + private PercentageDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper) { super(providerInformation, methodWrapper); } @@ -40,13 +40,22 @@ public class PercentageDataProvider extends DataProvider { DataProviders dataProviders, Method method, PercentageProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setDescription(annotation.description()) + .setPriority(annotation.priority()) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.iconColor()) + ).setShowInPlayersTable(annotation.showInPlayerTable()) + .setCondition(condition) + .setTab(tab) + .build(); + 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(), annotation.showInPlayerTable(), tab, condition - ); - - dataProviders.put(new PercentageDataProvider(providerInformation, methodWrapper)); + dataProviders.put(new PercentageDataProvider(information, 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 bcc0bd5d7..701a9e2eb 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 @@ -24,45 +24,36 @@ import com.djrapitops.plan.extension.implementation.ProviderInformation; import java.lang.reflect.Method; /** - * Represents a DataExtension API method annotated with {@link StringProvider} annotation. - *

- * Used to obtain data to place in the database. + * Contains code that acts on {@link StringProvider} annotations. * * @author Rsl1122 */ -public class StringDataProvider extends DataProvider { +public class StringDataProvider { - private final boolean playerName; - - private StringDataProvider(ProviderInformation providerInformation, MethodWrapper methodWrapper, boolean playerName) { - super(providerInformation, methodWrapper); - this.playerName = playerName; + private StringDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, StringProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setText(annotation.text()) + .setDescription(annotation.description()) + .setPriority(annotation.priority()) + .setIcon(new Icon( + annotation.iconFamily(), + annotation.iconName(), + annotation.iconColor()) + ).setShowInPlayersTable(annotation.showInPlayerTable()) + .setCondition(condition) + .setTab(tab) + .setPlayerName(annotation.playerName()) + .build(); + 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(), annotation.showInPlayerTable(), tab, condition - ); - - boolean playerName = annotation.playerName(); - - dataProviders.put(new StringDataProvider(providerInformation, methodWrapper, playerName)); - } - - public static boolean isPlayerName(DataProvider provider) { - if (provider instanceof StringDataProvider) { - return ((StringDataProvider) provider).isPlayerName(); - } - return false; - } - - public boolean isPlayerName() { - return playerName; + dataProviders.put(new DataProvider<>(information, methodWrapper)); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/TableDataProvider.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/TableDataProvider.java index 16600ca9e..c65af9d59 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/TableDataProvider.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/TableDataProvider.java @@ -18,52 +18,38 @@ package com.djrapitops.plan.extension.implementation.providers; import com.djrapitops.plan.extension.annotation.Conditional; import com.djrapitops.plan.extension.annotation.TableProvider; -import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.implementation.ProviderInformation; import com.djrapitops.plan.extension.table.Table; import java.lang.reflect.Method; /** - * Represents a DataExtension API method annotated with {@link com.djrapitops.plan.extension.annotation.TableProvider} annotation. + * Contains code that acts on {@link TableProvider} annotations. *

- * Used to obtain data to place in the database. - *

- * Please note that not all {@link ProviderInformation} is present. + * Please note that not all {@link ProviderInformation} is present in this annotation. * * @author Rsl1122 */ -public class TableDataProvider extends DataProvider { +public class TableDataProvider { - private final Color tableColor; - - private TableDataProvider(ProviderInformation providerInformation, MethodWrapper
methodWrapper, Color tableColor) { - super(providerInformation, methodWrapper); - - this.tableColor = tableColor; + private TableDataProvider() { + // Static method class } public static void placeToDataProviders( DataProviders dataProviders, Method method, TableProvider annotation, Conditional condition, String tab, String pluginName ) { + ProviderInformation information = ProviderInformation.builder(pluginName) + .setName(method.getName()) + .setPriority(0) + .setCondition(condition) + .setTab(tab) + .setTableColor(annotation.tableColor()) + .build(); + MethodWrapper
methodWrapper = new MethodWrapper<>(method, Table.class); - - ProviderInformation providerInformation = new ProviderInformation( - pluginName, method.getName(), null, null, null, 0, false, tab, condition - ); - - dataProviders.put(new TableDataProvider(providerInformation, methodWrapper, annotation.tableColor())); + dataProviders.put(new DataProvider<>(information, methodWrapper)); } - public static Color getTableColor(DataProvider
provider) { - if (provider instanceof TableDataProvider) { - return ((TableDataProvider) provider).getTableColor(); - } - return Color.NONE; - } - - public Color getTableColor() { - return tableColor; - } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/BooleanProviderValueGatherer.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/BooleanProviderValueGatherer.java index eac422f46..bbe518f53 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/BooleanProviderValueGatherer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/BooleanProviderValueGatherer.java @@ -19,7 +19,6 @@ 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.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; @@ -112,15 +111,16 @@ class BooleanProviderValueGatherer { ) { Set> satisfied = new HashSet<>(); for (DataProvider booleanProvider : unsatisifiedProviders) { - ProviderInformation providerInformation = booleanProvider.getProviderInformation(); + ProviderInformation information = booleanProvider.getProviderInformation(); - Optional condition = providerInformation.getCondition(); + Optional condition = information.getCondition(); if (condition.isPresent() && conditions.isNotFulfilled(condition.get())) { // Condition required by the BooleanProvider is not satisfied continue; } - Optional providedCondition = BooleanDataProvider.getProvidedCondition(booleanProvider); + + String providedCondition = information.getProvidedCondition(); MethodWrapper method = booleanProvider.getMethod(); Boolean result = getMethodResult(methodCaller.apply(method), method); if (result == null) { @@ -129,18 +129,18 @@ class BooleanProviderValueGatherer { continue; } - if (providedCondition.isPresent()) { + if (providedCondition != null) { if (result) { // The condition was fulfilled (true) for this player. - conditions.conditionFulfilled(providedCondition.get()); + conditions.conditionFulfilled(providedCondition); } else { // The negated condition was fulfilled (false) for this player. - conditions.conditionFulfilled("not_" + providedCondition.get()); + conditions.conditionFulfilled("not_" + providedCondition); } } satisfied.add(booleanProvider); // Prevents further attempts to call this provider for this player. - database.executeTransaction(new StoreIconTransaction(providerInformation.getIcon())); + database.executeTransaction(new StoreIconTransaction(information.getIcon())); database.executeTransaction(new StoreProviderTransaction(booleanProvider, serverUUID)); database.executeTransaction(storeTransactionCreator.apply(method, result)); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/TableProviderValueGatherer.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/TableProviderValueGatherer.java index 32add67a5..f169a0fd8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/TableProviderValueGatherer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/providers/gathering/TableProviderValueGatherer.java @@ -23,7 +23,6 @@ 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.TableDataProvider; 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; @@ -109,7 +108,7 @@ class TableProviderValueGatherer { database.executeTransaction(new StoreIconTransaction(icon)); } } - database.executeTransaction(new StoreTableProviderTransaction(serverUUID, providerInformation, TableDataProvider.getTableColor(tableProvider), result)); + database.executeTransaction(new StoreTableProviderTransaction(serverUUID, providerInformation, result)); database.executeTransaction(storeTransactionCreator.apply(method, result)); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreProviderTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreProviderTransaction.java index ea03cadd9..041c00eed 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreProviderTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreProviderTransaction.java @@ -18,10 +18,7 @@ package com.djrapitops.plan.extension.implementation.storage.transactions.provid import com.djrapitops.plan.extension.FormatType; import com.djrapitops.plan.extension.implementation.ProviderInformation; -import com.djrapitops.plan.extension.implementation.providers.BooleanDataProvider; import com.djrapitops.plan.extension.implementation.providers.DataProvider; -import com.djrapitops.plan.extension.implementation.providers.NumberDataProvider; -import com.djrapitops.plan.extension.implementation.providers.StringDataProvider; import com.djrapitops.plan.storage.database.sql.building.Sql; import com.djrapitops.plan.storage.database.sql.tables.ExtensionIconTable; import com.djrapitops.plan.storage.database.sql.tables.ExtensionPluginTable; @@ -94,15 +91,15 @@ public class StoreProviderTransaction extends ThrowawayTransaction { Sql.setStringOrNull(statement, 2, info.getDescription().orElse(null)); statement.setInt(3, info.getPriority()); Sql.setStringOrNull(statement, 4, info.getCondition().orElse(null)); - ExtensionTabTable.set3TabValuesToStatement(statement, 5, info.getTab().orElse(null), info.getPluginName(), serverUUID); - ExtensionIconTable.set3IconValuesToStatement(statement, 8, info.getIcon()); + ExtensionIconTable.set3IconValuesToStatement(statement, 5, info.getIcon()); + ExtensionTabTable.set3TabValuesToStatement(statement, 8, info.getTab().orElse(null), info.getPluginName(), serverUUID); statement.setBoolean(11, info.isShownInPlayersTable()); // Specific provider cases - statement.setBoolean(12, BooleanDataProvider.isHidden(provider)); - Sql.setStringOrNull(statement, 13, BooleanDataProvider.getProvidedCondition(provider).orElse(null)); - Sql.setStringOrNull(statement, 14, NumberDataProvider.getFormatType(provider).map(FormatType::name).orElse(null)); - statement.setBoolean(15, StringDataProvider.isPlayerName(provider)); + statement.setBoolean(12, info.isHidden()); + Sql.setStringOrNull(statement, 13, info.getProvidedCondition()); + Sql.setStringOrNull(statement, 14, info.getFormatType().map(FormatType::name).orElse(null)); + statement.setBoolean(15, info.isPlayerName()); // Find appropriate provider ExtensionPluginTable.set2PluginValuesToStatement(statement, 16, info.getPluginName(), serverUUID); @@ -159,10 +156,10 @@ public class StoreProviderTransaction extends ThrowawayTransaction { statement.setBoolean(6, info.isShownInPlayersTable()); // Specific provider cases - statement.setBoolean(7, BooleanDataProvider.isHidden(provider)); - Sql.setStringOrNull(statement, 8, BooleanDataProvider.getProvidedCondition(provider).orElse(null)); - Sql.setStringOrNull(statement, 9, NumberDataProvider.getFormatType(provider).map(FormatType::name).orElse(null)); - statement.setBoolean(10, StringDataProvider.isPlayerName(provider)); + statement.setBoolean(7, info.isHidden()); + Sql.setStringOrNull(statement, 8, info.getProvidedCondition()); + Sql.setStringOrNull(statement, 9, info.getFormatType().map(FormatType::name).orElse(null)); + statement.setBoolean(10, info.isPlayerName()); // Found for all providers ExtensionTabTable.set3TabValuesToStatement(statement, 11, info.getTab().orElse(null), info.getPluginName(), serverUUID); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreTableProviderTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreTableProviderTransaction.java index 2e5135471..5d973fd7c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreTableProviderTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreTableProviderTransaction.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.extension.implementation.storage.transactions.providers; -import com.djrapitops.plan.extension.icon.Color; import com.djrapitops.plan.extension.icon.Icon; import com.djrapitops.plan.extension.implementation.ProviderInformation; import com.djrapitops.plan.extension.table.Table; @@ -44,13 +43,11 @@ import static com.djrapitops.plan.storage.database.sql.tables.ExtensionTableProv public class StoreTableProviderTransaction extends ThrowawayTransaction { private final UUID serverUUID; - private final ProviderInformation providerInformation; - private final Color tableColor; + private final ProviderInformation information; private final Table table; - public StoreTableProviderTransaction(UUID serverUUID, ProviderInformation providerInformation, Color tableColor, Table table) { - this.providerInformation = providerInformation; - this.tableColor = tableColor; + public StoreTableProviderTransaction(UUID serverUUID, ProviderInformation information, Table table) { + this.information = information; this.table = table; this.serverUUID = serverUUID; } @@ -93,21 +90,21 @@ public class StoreTableProviderTransaction extends ThrowawayTransaction { return new ExecStatement(sql) { @Override public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, tableColor.name()); + statement.setString(1, information.getTableColor().name()); setStringOrNull(statement, 2, columns[0]); setStringOrNull(statement, 3, columns[1]); setStringOrNull(statement, 4, columns[2]); setStringOrNull(statement, 5, columns[3]); setStringOrNull(statement, 6, columns[4]); - setStringOrNull(statement, 7, providerInformation.getCondition().orElse(null)); - ExtensionTabTable.set3TabValuesToStatement(statement, 8, providerInformation.getTab().orElse("No Tab"), providerInformation.getPluginName(), serverUUID); + setStringOrNull(statement, 7, information.getCondition().orElse(null)); + ExtensionTabTable.set3TabValuesToStatement(statement, 8, information.getTab().orElse("No Tab"), information.getPluginName(), serverUUID); ExtensionIconTable.set3IconValuesToStatement(statement, 11, icons[0]); ExtensionIconTable.set3IconValuesToStatement(statement, 14, icons[1]); ExtensionIconTable.set3IconValuesToStatement(statement, 17, icons[2]); ExtensionIconTable.set3IconValuesToStatement(statement, 20, icons[3]); ExtensionIconTable.set3IconValuesToStatement(statement, 23, icons[4]); - statement.setString(26, providerInformation.getName()); - ExtensionPluginTable.set2PluginValuesToStatement(statement, 27, providerInformation.getPluginName(), serverUUID); + statement.setString(26, information.getName()); + ExtensionPluginTable.set2PluginValuesToStatement(statement, 27, information.getPluginName(), serverUUID); } }; } @@ -144,16 +141,16 @@ public class StoreTableProviderTransaction extends ThrowawayTransaction { return new ExecStatement(sql) { @Override public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, providerInformation.getName()); - statement.setString(2, tableColor.name()); + statement.setString(1, information.getName()); + statement.setString(2, information.getTableColor().name()); setStringOrNull(statement, 3, columns[0]); setStringOrNull(statement, 4, columns[1]); setStringOrNull(statement, 5, columns[2]); setStringOrNull(statement, 6, columns[3]); setStringOrNull(statement, 7, columns[4]); - setStringOrNull(statement, 8, providerInformation.getCondition().orElse(null)); - ExtensionTabTable.set3TabValuesToStatement(statement, 9, providerInformation.getTab().orElse("No Tab"), providerInformation.getPluginName(), serverUUID); - ExtensionPluginTable.set2PluginValuesToStatement(statement, 12, providerInformation.getPluginName(), serverUUID); + setStringOrNull(statement, 8, information.getCondition().orElse(null)); + ExtensionTabTable.set3TabValuesToStatement(statement, 9, information.getTab().orElse("No Tab"), information.getPluginName(), serverUUID); + ExtensionPluginTable.set2PluginValuesToStatement(statement, 12, information.getPluginName(), serverUUID); ExtensionIconTable.set3IconValuesToStatement(statement, 14, icons[0]); ExtensionIconTable.set3IconValuesToStatement(statement, 17, icons[1]); ExtensionIconTable.set3IconValuesToStatement(statement, 20, icons[2]);