summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/Validate.java
blob: 3b1f7d4e3f8ceb2cd6ab9c7cdddf6d1950b6e678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package io.dico.dicore.command;

import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import java.util.Optional;

public class Validate {

    private Validate() {

    }

    //@Contract("false, _ -> fail")
    public static void isTrue(boolean expression, String failMessage) throws CommandException {
        if (!expression) {
            throw new CommandException(failMessage);
        }
    }

    //@Contract("null, _ -> fail")
    public static void notNull(Object obj, String failMessage) throws CommandException {
        Validate.isTrue(obj != null, failMessage);
    }

    public static void isAuthorized(CommandSender sender, String permission, String failMessage) throws CommandException {
        Validate.isTrue(sender.hasPermission(permission), failMessage);
    }

    public static void isAuthorized(CommandSender sender, String permission) throws CommandException {
        Validate.isAuthorized(sender, permission, "You do not have permission to use that command");
    }

    //@Contract("null -> fail")
    public static void isPlayer(CommandSender sender) throws CommandException {
        isTrue(sender instanceof Player, "That command can only be used by players");
    }

    //@Contract("null -> fail")
    public static void isConsole(CommandSender sender) throws CommandException {
        isTrue(sender instanceof ConsoleCommandSender, "That command can only be used by the console");
    }

    public static <T> T returnIfPresent(Optional<T> maybe, String failMessage) throws CommandException {
        if (!maybe.isPresent()) {
            throw new CommandException(failMessage);
        }
        return maybe.get();
    }

}