wrote notification kotlin extensions

This commit is contained in:
Samuel Pizette
2022-12-04 19:35:54 -05:00
parent 74c428b90d
commit 85f02c5ca2
2 changed files with 54 additions and 1 deletions

View File

@@ -125,7 +125,6 @@ public interface CommandBase {
* @param key key of notification message in langYml
* @param <T> the generic type of object
* @return Returns the object given or throws an exception
* @throws NotificationException
*/
default @NotNull <T> T notifyFalse(@NotNull T obj,
@NotNull Predicate<T> 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

View File

@@ -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> 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> T.notifyFalse(predicate: Predicate<T>, 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)
}