From 16641995746be1b493e0653412bc444a1317ebe0 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sun, 3 Jan 2021 12:27:49 +0200 Subject: [PATCH] Extension Table now handles Optionals Updated mcMMO Extension to R1.2 Affects issues: - Fixed #1643 --- Plan/api/build.gradle | 3 +- .../plan/extension/table/Table.java | 15 ++++- .../plan/extension/table/TableTest.java | 61 +++++++++++++++++++ Plan/extensions/build.gradle | 2 +- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 Plan/api/src/test/java/com/djrapitops/plan/extension/table/TableTest.java diff --git a/Plan/api/build.gradle b/Plan/api/build.gradle index c1fd3ccef..eef225ec8 100644 --- a/Plan/api/build.gradle +++ b/Plan/api/build.gradle @@ -3,7 +3,8 @@ plugins { } dependencies { - compileOnly group: 'org.apache.commons', name: 'commons-lang3', version: '3.11' + compileOnly "org.apache.commons:commons-text:$commonsTextVersion" + testCompile "org.apache.commons:commons-text:$commonsTextVersion" compileOnly "com.google.code.gson:gson:$gsonVersion" } diff --git a/Plan/api/src/main/java/com/djrapitops/plan/extension/table/Table.java b/Plan/api/src/main/java/com/djrapitops/plan/extension/table/Table.java index c8a1a6a72..7c263cc31 100644 --- a/Plan/api/src/main/java/com/djrapitops/plan/extension/table/Table.java +++ b/Plan/api/src/main/java/com/djrapitops/plan/extension/table/Table.java @@ -22,8 +22,9 @@ import com.djrapitops.plan.extension.icon.Icon; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.Optional; /** * Object for giving Plan table data. @@ -186,7 +187,17 @@ public final class Table { return this; // Ignore row when all values are null or no values are present. } - building.rows.add(Arrays.copyOf(values, 5)); + Object[] row = new Object[5]; + + for (int i = 0; i < Math.min(values.length, 5); i++) { + Object value = values[i]; + if (value instanceof Optional) { + value = ((Optional) value).map(Objects::toString).orElse("-"); + } + row[i] = value; + } + + building.rows.add(row); return this; } diff --git a/Plan/api/src/test/java/com/djrapitops/plan/extension/table/TableTest.java b/Plan/api/src/test/java/com/djrapitops/plan/extension/table/TableTest.java new file mode 100644 index 000000000..01455d8b8 --- /dev/null +++ b/Plan/api/src/test/java/com/djrapitops/plan/extension/table/TableTest.java @@ -0,0 +1,61 @@ +/* + * 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.table; + +import com.djrapitops.plan.extension.icon.Icon; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class TableTest { + + @Test + void tableWithVaryingRowLengthsHasNoErrors() { + Table.Factory table = Table.builder() + .columnOne("", Icon.called("").build()) + .columnTwo("", Icon.called("").build()) + .columnThree("", Icon.called("").build()) + .columnFour("", Icon.called("").build()) + .columnFive("", Icon.called("").build()); + + table.addRow(); + table.addRow("a"); + table.addRow("a", "b"); + table.addRow("a"); + table.addRow("a", "b", "c", "d", "e", "f"); + table.addRow("a", "b", "c", "d"); + + List expected = Arrays.asList( + new Object[]{"a", null, null, null, null}, + new Object[]{"a", "b", null, null, null}, + new Object[]{"a", null, null, null, null}, + new Object[]{"a", "b", "c", "d", "e"}, + new Object[]{"a", "b", "c", "d", null} + ); + List result = table.build().getRows(); + + for (int i = 0; i < expected.size(); i++) { + assertArrayEquals(expected.get(i), result.get(i)); + } + assertEquals(expected.size(), result.size()); + } + +} \ No newline at end of file diff --git a/Plan/extensions/build.gradle b/Plan/extensions/build.gradle index 0bf6cc7bf..32851d1b1 100644 --- a/Plan/extensions/build.gradle +++ b/Plan/extensions/build.gradle @@ -21,7 +21,7 @@ dependencies { compile 'com.djrapitops:Extension-Jobs:4.13.1-R0.1' compile 'com.djrapitops:Extension-Litebans:0.3.2-R0.1' compile 'com.djrapitops:Extension-LuckPerms:5.0-R0.2' - compile 'com.djrapitops:Extension-McMMO:2.1.149-R1.1' + compile 'com.djrapitops:Extension-McMMO:2.1.149-R1.2' compile 'com.djrapitops:Extension-MinigamesLib:1.14.17-R0.2' compile 'com.djrapitops:Extension-Nucleus:1.14.0-R0.1' compile 'com.djrapitops:Extension-nuVotifier:2.3.4-R0.3'