aboutsummaryrefslogtreecommitdiff
path: root/src/net/brysonsteck/Resurrection/commands/CommandDead.java
blob: 96fe7369af0dccbb756fa51b6a84745985b4f50f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package net.brysonsteck.Resurrection.commands;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import net.brysonsteck.Resurrection.Resurrection;
import net.brysonsteck.Resurrection.player.PlayerData;
import net.brysonsteck.Resurrection.player.TimeCheck;

public class CommandDead implements CommandExecutor {
    boolean DEBUG;

    public CommandDead(String DEBUG) {
        this.DEBUG = Boolean.parseBoolean(DEBUG);
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        Logger log = JavaPlugin.getProvidingPlugin(Resurrection.class).getLogger();
        if (DEBUG) {
            Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/dead` command was ran by " + commandSender.getName());
        }

        PlayerData playerData = new PlayerData();
        playerData.readData();
        String rawData = playerData.getRawData();
        String[] rawPlayers = rawData.split(";");
        int amountDead = 0;
        for (String players : rawPlayers) {
            String[] playerSplit = players.split(",");
            if (playerSplit.length == 3) {
                boolean dead = Boolean.parseBoolean(playerSplit[1]);
                if (dead) {
                    amountDead++;
                }
            }
        }
        String[] responses = new String[amountDead];
        int index = 0;
        for (String players : rawPlayers) {
            String[] playerSplit = players.split(",");
            if (playerSplit.length == 3) {
                String playerName = playerSplit[0];
                boolean dead = Boolean.parseBoolean(playerSplit[1]);
                long timeToResurrection = Long.parseLong(playerSplit[2]);

                if (dead) {
                    TimeCheck timeCheck = new TimeCheck(timeToResurrection - System.currentTimeMillis());
                    if (System.currentTimeMillis() > timeToResurrection) {
                        responses[index] = playerName + " will resurrect when they rejoin.";
                    } else {
                        responses[index] = playerName + " will resurrect in " + timeCheck.formatTime('f');
                    }
                    index++;
                }
                
            }
        }

        if (commandSender instanceof Player) {
            Player p = (Player) commandSender;
            if (amountDead == 0) {
                p.sendMessage(ChatColor.YELLOW + "There are currently no players awaiting resurrection.");
            } else if (amountDead == 1) {
                p.sendMessage(ChatColor.YELLOW + "There is currently 1 player awaiting resurrection:");
            } else if (amountDead >= 2) {
                p.sendMessage(ChatColor.YELLOW + "There are currently " + amountDead + " players awaiting resurrection:");
            }
            if (amountDead > 0) {
                for (String response : responses) {
                    p.sendMessage(response);
                }
            }
            
        } else {
            if (amountDead == 0) {
                log.info("There are currently no players awaiting resurrection.");
            } else if (amountDead == 1) {
                log.info("There is currently 1 player awaiting resurrection:");
            } else if (amountDead >= 2) {
                log.info("There are currently " + amountDead + " players awaiting resurrection:");
            }
            if (amountDead > 0) {
                for (String response : responses) {
                    log.info(response);
                }
            }
        }

        return true;
    }
    
}