From 19e1dd2937113bc3cb05b3dbd1c66914aad08f96 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 19 Mar 2023 19:38:08 +0100 Subject: [PATCH] Add pagination precondition --- .../toolbox/pagination/api/PaginationApi.java | 2 +- .../pagination/api/PaginationApiImpl.java | 15 ++++-- .../precondition/ContentCondition.java | 15 ++++++ .../precondition/HeaderCondition.java | 14 ++++++ .../precondition/NullPlayerCondition.java | 15 ++++++ Tasks/build.gradle | 21 --------- build.gradle | 47 +++++++++++++++++-- settings.gradle | 2 +- 8 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/ContentCondition.java create mode 100644 Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/HeaderCondition.java create mode 100644 Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/NullPlayerCondition.java diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java index 3adb373..3b23724 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApi.java @@ -16,7 +16,7 @@ public interface PaginationApi { *
  • empty String if you want just the header to be filled only with "="
  • * @throws IllegalArgumentException if one of its conditions is met:
      *
    1. header length is more than 50 characters
    2. - *
    3. content is empty
    4. + *
    5. content is empty/blank
    6. *
    7. playerEntity or content are null
    8. *
    */ diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java index 2d0477d..2e27090 100644 --- a/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/api/PaginationApiImpl.java @@ -1,18 +1,27 @@ package fr.altarik.toolbox.pagination.api; import fr.altarik.toolbox.pagination.PaginatedContent; +import fr.altarik.toolbox.pagination.precondition.ContentCondition; +import fr.altarik.toolbox.pagination.precondition.HeaderCondition; +import fr.altarik.toolbox.pagination.precondition.NullPlayerCondition; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Pair; import java.util.HashMap; import java.util.Map; +import java.util.function.Predicate; public class PaginationApiImpl implements PaginationApi { - - Map> paginatedContent = new HashMap<>(); - + private final Map> paginatedContent = new HashMap<>(); + private final Predicate playerCondition = new NullPlayerCondition().negate(); + private final Predicate headerCondition = new HeaderCondition().negate(); + private final Predicate contentCondition = new ContentCondition().negate(); @Override public void createTable(ServerPlayerEntity playerEntity, String content, String header) { + if(playerCondition.test(playerEntity) || headerCondition.test(header) || contentCondition.test(content)) { + throw new IllegalArgumentException("Preconditions aren't satisfied"); + } + // TODO: 01/03/2023 } } diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/ContentCondition.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/ContentCondition.java new file mode 100644 index 0000000..a5d2542 --- /dev/null +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/ContentCondition.java @@ -0,0 +1,15 @@ +package fr.altarik.toolbox.pagination.precondition; + +import java.util.function.Predicate; + +/** + * This predicate returns true if the String is not null or + * if its content is not blank (empty or only contains whitespaces) + */ +public class ContentCondition implements Predicate { + @Override + public boolean test(String s) { + return s != null && !s.isBlank(); + } + +} diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/HeaderCondition.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/HeaderCondition.java new file mode 100644 index 0000000..d39cde5 --- /dev/null +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/HeaderCondition.java @@ -0,0 +1,14 @@ +package fr.altarik.toolbox.pagination.precondition; + +import java.util.function.Predicate; + +/** + * This predicate returns true if header is not null, not blank (not empty excluding whitespaces) + * and if its length doesn't exceed 50 characters. + */ +public class HeaderCondition implements Predicate { + @Override + public boolean test(String header) { + return header != null && !header.isBlank() && header.length() <= 50; + } +} diff --git a/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/NullPlayerCondition.java b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/NullPlayerCondition.java new file mode 100644 index 0000000..16e8dfb --- /dev/null +++ b/Pagination/src/main/java/fr/altarik/toolbox/pagination/precondition/NullPlayerCondition.java @@ -0,0 +1,15 @@ +package fr.altarik.toolbox.pagination.precondition; + +import net.minecraft.server.network.ServerPlayerEntity; + +import java.util.function.Predicate; + +/** + * This predicate returns true if the player isn't null, false otherwise + */ +public class NullPlayerCondition implements Predicate { + @Override + public boolean test(ServerPlayerEntity player) { + return player != null; + } +} diff --git a/Tasks/build.gradle b/Tasks/build.gradle index 77c4a5a..2d673a0 100644 --- a/Tasks/build.gradle +++ b/Tasks/build.gradle @@ -1,24 +1,3 @@ -plugins { - id 'fabric-loom' - -} - -dependencies { - minecraft "com.mojang:minecraft:${project.minecraft_version}" - mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" - modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" - - modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" -} - -processResources { - inputs.property "version", project.version - - filesMatching("fabric.mod.json") { - expand "version": project.version - } -} - tasks.withType(JavaCompile).configureEach { // ensure that the encoding is set to UTF-8, no matter what the system default is // this fixes some edge cases with special characters not displaying correctly diff --git a/build.gradle b/build.gradle index 7ab39e6..a33d141 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,12 @@ plugins { id 'java' - id 'fabric-loom' version '1.1-SNAPSHOT' apply false + id 'fabric-loom' version '1.1-SNAPSHOT' } allprojects { apply plugin: 'java' apply plugin: 'maven-publish' + apply plugin: 'fabric-loom' group = project.maven_group version = project.maven_version @@ -46,13 +47,45 @@ allprojects { } } } + + processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } + } + + tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too + // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used. + // We'll use that if it's available, but otherwise we'll use the older option. + def targetVersion = 17 + if (JavaVersion.current().isJava9Compatible()) { + it.options.release = targetVersion + } + } + + dependencies { + testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}" + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + } + } subprojects { dependencies { - testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}" + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" } java { @@ -66,11 +99,15 @@ subprojects { } -jar { +dependencies { + include allprojects.collect { project -> project } +} + +/*jar { dependsOn subprojects.jar subprojects.each { project -> from(project.jar) { into("META-INF/jars/") } } -} +}*/ diff --git a/settings.gradle b/settings.gradle index 16f164b..3f9b534 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,4 +10,4 @@ pluginManagement { } rootProject.name = 'Toolbox' -include(':Tasks', ':Database', /* ':Pagination' */) +include(':Tasks', ':Database', ':Pagination')