summaryrefslogtreewikicommitdiff
path: root/src/main/java/cat/freya/khs/command/Confirm.java
blob: 34b145f6caab6691d91351b87aa3a2fae6ef46a9 (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
53
54
55
56
57
58
59
60
61
62
package cat.freya.khs.command;

import cat.freya.khs.command.util.ICommand;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Consumer;

import static cat.freya.khs.configuration.Config.errorPrefix;
import static cat.freya.khs.configuration.Localization.message;

public class Confirm implements ICommand {

    public static final Map<UUID, Confirmation> confirmations = new HashMap<>();

    public void execute(Player sender, String[] args) {
        Confirmation confirmation = confirmations.get(sender.getUniqueId());
        confirmations.remove(sender.getUniqueId());
        if(confirmation == null) {
            sender.sendMessage(errorPrefix + message("NO_CONFIRMATION"));
        } else {
            long now = System.currentTimeMillis();
            float secs = (now - confirmation.start) / 1000F;
            if(secs > 10) {
                sender.sendMessage(errorPrefix + message("CONFIRMATION_TIMED_OUT"));
                return;
            }
            confirmation.callback.accept(confirmation.data);
        }
    }

    public String getLabel() {
        return "confirm";
    }

    public String getUsage() {
        return "";
    }

    public String getDescription() {
        return "Confirm another command if required";
    }

    public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) {
        return null;
    }

    public static class Confirmation {
        public final Consumer<String> callback;
        public final String data;
        public final long start;

        public Confirmation(String data, Consumer<String> callback) {
            this.callback = callback;
            this.data = data;
            this.start = System.currentTimeMillis();
        }

    }

}