Skip to content

Commit

Permalink
add more command conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mudkipdev authored and iam4722202468 committed May 30, 2024
1 parent 5ac26b1 commit 406c686
Showing 1 changed file with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.minestom.server.command.builder.condition;


import net.kyori.adventure.text.Component;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.ConsoleSender;
import net.minestom.server.entity.Player;
Expand All @@ -10,19 +8,47 @@
* Common command conditions
*/
public class Conditions {
public static boolean playerOnly(CommandSender sender, String commandString) {
if (!(sender instanceof Player)) {
sender.sendMessage(Component.text("The command is only available for players"));
/**
* Will only execute if all command conditions succeed.
*/
public static CommandCondition all(CommandCondition... conditions) {
return (sender, commandString) -> {
for (CommandCondition condition : conditions) {
if (!condition.canUse(sender, commandString)) {
return false;
}
}

return true;
};
}

/**
* Will execute if one or more command conditions succeed.
*/
public static CommandCondition any(CommandCondition... conditions) {
return (sender, commandString) -> {
for (CommandCondition condition : conditions) {
if (condition.canUse(sender, commandString)) {
return true;
}
}

return false;
}
return true;
};
}

/**
* Will succeed if the command sender is a player.
*/
public static boolean playerOnly(CommandSender sender, String commandString) {
return sender instanceof Player;
}

/**
* Will succeed if the command sender is the server console.
*/
public static boolean consoleOnly(CommandSender sender, String commandString) {
if (!(sender instanceof ConsoleSender)) {
sender.sendMessage(Component.text("The command is only available from the console"));
return false;
}
return true;
return sender instanceof ConsoleSender;
}
}

0 comments on commit 406c686

Please sign in to comment.