From 85f02c5ca2cee02aadcb461793466638cf4a08d2 Mon Sep 17 00:00:00 2001 From: Samuel Pizette Date: Sun, 4 Dec 2022 19:35:54 -0500 Subject: [PATCH] wrote notification kotlin extensions --- .../willfp/eco/core/command/CommandBase.java | 2 +- .../eco/core/commands/CommandHelpers.kt | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java b/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java index 3ff21d91..ec94f015 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java +++ b/eco-api/src/main/java/com/willfp/eco/core/command/CommandBase.java @@ -125,7 +125,6 @@ public interface CommandBase { * @param key key of notification message in langYml * @param the generic type of object * @return Returns the object given or throws an exception - * @throws NotificationException */ default @NotNull T notifyFalse(@NotNull T obj, @NotNull Predicate predicate, @NotNull String key) @@ -135,6 +134,7 @@ public interface CommandBase { } /** + * Throws an exception if condition is false. * @param condition the condition, throws exception if false * @param key value in the langYml * @return Returns the condition given or throws an exception diff --git a/eco-api/src/main/kotlin/com/willfp/eco/core/commands/CommandHelpers.kt b/eco-api/src/main/kotlin/com/willfp/eco/core/commands/CommandHelpers.kt index 0e3ab96d..d45593f3 100644 --- a/eco-api/src/main/kotlin/com/willfp/eco/core/commands/CommandHelpers.kt +++ b/eco-api/src/main/kotlin/com/willfp/eco/core/commands/CommandHelpers.kt @@ -4,9 +4,13 @@ package com.willfp.eco.core.commands import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.command.CommandBase +import com.willfp.eco.core.command.NotificationException import com.willfp.eco.core.command.impl.PluginCommand import com.willfp.eco.core.command.impl.Subcommand +import org.bukkit.Bukkit import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import java.util.function.Predicate /** * Helper class for creating commands with builders. @@ -141,3 +145,52 @@ fun CommandBase.addSubcommand( return command } +/** + * Notify the player if an object is null. + * + * @param key The key of the message to send to the player if obj is null + * @return The object provided originally, for more null behavior + * @throws NotificationException + */ +fun T.notifyNull(key: String): T { + if(this == null) { + throw NotificationException(key) + } + return this +} + +/** + * Throws an exception if predicate tests false. + * + * @param predicate predicate to test + * @param key key of notification message in langYml + * @return Returns the object given or throws an exception + * @throws NotificationException + */ +fun T.notifyFalse(predicate: Predicate, key: String): T { + predicate.test(this).notifyFalse(key) + return this +} + +/** + * Throws an exception if boolean is false. + * @param key value in the langYml + * @return Returns the condition given or throws an exception + * @throws NotificationException exception thrown when false + */ +fun Boolean.notifyFalse(key: String): Boolean { + return if(this) true else throw NotificationException(key) +} + +/** + * Throws an exception and sends a lang message if Bukkit.getPlayer(receiver) is null + * + * @param key value in the langYml + * @return Returns the player + * @throws NotificationException exception thrown when invalid playerName + */ +fun String.notifyPlayerRequired(key: String): Player { + return Bukkit.getPlayer(this) ?: throw NotificationException(key) +} + +