aboutsummaryrefslogtreecommitdiff
path: root/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java
blob: 32662a0a834bc483c9f564b365dfb4cb88750705 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package net.brysonsteck.Resurrection.commands;

import net.brysonsteck.Resurrection.player.PlayerData;
import net.brysonsteck.Resurrection.player.TimeCheck;
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;

public class CommandHowLong implements CommandExecutor {
    boolean DEBUG;

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

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (DEBUG) {
            Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/howlong` command was ran by " + commandSender.getName());
        }

        if (commandSender instanceof Player) {
            if (DEBUG) {
                Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player.");
            }

            boolean self = false;
            boolean valid = false;

            if (strings.length == 0) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; no arguments given. Assuming sender wants to check own time");
                }

                self = true;
                valid = true;
            } else if (strings.length == 1) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; one argument given. Assuming sender wants to check another player's time");
                }

                valid = true;
            }

            if (valid) {
                Player p;
                if (self) {
                    p = (Player) commandSender;
                } else {
                    p = Bukkit.getPlayer(strings[0]);
                    if (p == null) {
                        commandSender.sendMessage(ChatColor.RED + "ERROR: That player is not online/doesn't exist!");
                        return false;
                    }

                    if (DEBUG) {
                        Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player " + p.getDisplayName() + " exists.");
                    }
                }
                PlayerData playerData = new PlayerData();
                playerData.readData();
                String rawData = playerData.getRawData();
                String[] rawPlayers = rawData.split(";");
                for (String players : rawPlayers) {
                    if (players.startsWith(p.getDisplayName())) {
                        String[] playerSplit = players.split(",");
                        if (Boolean.parseBoolean(playerSplit[1])) {
                            if (DEBUG) {
                                Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Player is dead according to file. Calculating time until resurrection");
                            }

                            long currentTime = System.currentTimeMillis();
                            long resurrectionTime = Long.parseLong(playerSplit[2]);

                            TimeCheck timeCheck = new TimeCheck(resurrectionTime - currentTime);
                            if (self) {
                                commandSender.sendMessage(ChatColor.YELLOW + "You will respawn in " + timeCheck.formatTime('f'));
                            } else {
                                commandSender.sendMessage(ChatColor.YELLOW + p.getDisplayName() + " will respawn in " + timeCheck.formatTime('f'));
                            }
                            return true;
                        } else {
                            if (self) {
                                commandSender.sendMessage(ChatColor.RED + "ERROR: You aren't dead, dummy.");
                            } else {
                                commandSender.sendMessage(ChatColor.RED + "ERROR: " + p.getDisplayName() + " is not dead!");
                            }
                            return false;
                        }
                    }
                }
                commandSender.sendMessage(ChatColor.RED + "ERROR: An error has occurred while trying to get time information. This is a bug in the program and not your fault.");
                return false;
            }
        } else {
            if (DEBUG) {
                Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console.");
            }

            boolean valid = false;

            if (strings.length == 0) {
                System.out.println("[Resurrection] ERROR: The /howlong command requires the name of a player when ran through the console.");
                return false;
            } else if (strings.length == 1) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; console specified player to check.");
                }

                valid = true;
            }

            if (valid) {

                Player p = Bukkit.getPlayer(strings[0]);
                if (p == null) {
                    System.out.println("[Resurrection] ERROR: That player is not online/doesn't exist!");
                    return false;
                }

                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player " + p.getDisplayName() + " exists.");
                }

                PlayerData playerData = new PlayerData();
                playerData.readData();
                String rawData = playerData.getRawData();
                String[] rawPlayers = rawData.split(";");
                for (String players : rawPlayers) {
                    if (players.startsWith(p.getDisplayName())) {
                        String[] playerSplit = players.split(",");
                        if (Boolean.parseBoolean(playerSplit[1])) {
                            if (DEBUG) {
                                Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Player is dead according to file. Calculating time until resurrection");
                            }
                            long currentTime = System.currentTimeMillis();
                            long resurrectionTime = Long.parseLong(playerSplit[2]);

                            TimeCheck timeCheck = new TimeCheck(resurrectionTime - currentTime);

                            System.out.println("[Resurrection] " + p.getDisplayName() + " will respawn in " + timeCheck.formatTime('f'));

                            return true;
                        } else {
                            System.out.println("[Resurrection] ERROR: " + p.getDisplayName() + " is not dead!");
                            return false;
                        }
                    }
                }
                System.out.println("[Resurrection] ERROR: An error has occurred while trying to get time information. This is a bug in the program and not your fault.");
                return false;
            }
        }
        return false;
    }
}