aboutsummaryrefslogtreecommitdiff
path: root/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java
blob: a026d31f44cd7738e6f1bb952cd1e1fc281e4ec1 (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
160
161
162
163
164
165
166
167
168
package net.brysonsteck.Resurrection.commands;

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

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Sound;
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 org.bukkit.potion.PotionEffect;

public class CommandResurrect implements CommandExecutor {
    boolean DEBUG;

    public CommandResurrect(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 `resurrect` command was ran by " + commandSender.getName());
        }

        boolean valid = (strings.length == 1);

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

            Player p = (Player) commandSender;
            if (valid) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; an argument was specified. Assuming it as name of player to resurrect");
                }

                Player resurrectPlayer = Bukkit.getPlayer(strings[0]);
                if (resurrectPlayer == null) {
                    p.sendMessage(ChatColor.RED + "ERROR: That player is not online/doesn't exist! Failed to resurrect.");
                    return false;
                }
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player \"" + resurrectPlayer.getDisplayName() + "\" exists");
                }

                if (resurrectPlayer.getGameMode() == GameMode.SPECTATOR) {
                    if (DEBUG) {
                        Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player in spectator mode, assuming dead");
                    }
                    for (PotionEffect effect : resurrectPlayer.getActivePotionEffects())
                        resurrectPlayer.removePotionEffect(effect.getType());
                    resurrectPlayer.setGameMode(GameMode.SURVIVAL);
                    for(Player player : Bukkit.getOnlinePlayers()){
                        try {
                            player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 1, 0);
                        } catch (NoSuchFieldError e) {
                            log.warning("NoSuchFieldError encountered, playing Wither noise instead.");
                            player.playSound(player.getLocation(), Sound.ENTITY_WITHER_DEATH, 1, 0);
                        }
                    }
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + strings[0] + " has been resurrected manually by an admin!");
                    removeDeath(resurrectPlayer);
                    if (p.getBedSpawnLocation() != null) {
                        if (DEBUG) {
                            Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting");
                        }

                        p.teleport(p.getBedSpawnLocation());
                    }
                    return true;
                } else {
                    p.sendMessage(ChatColor.RED + "ERROR: " + strings[0] + " is not dead! Failed to resurrect.");
                    return false;
                }
            } else {
                p.sendMessage(ChatColor.RED + "ERROR: Too few arguments!");
                return false;
            }
        } else {
            if (DEBUG) {
                Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console.");
            }

            if (valid) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; an argument was specified. Assuming it as name of player to resurrect");
                }

                Player resurrectPlayer = Bukkit.getPlayer(strings[0]);
                if (resurrectPlayer == null) {
                    log.warning("ERROR: That player is not online/doesn't exist! Failed to resurrect.");
                    return false;
                }
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player \"" + resurrectPlayer.getDisplayName() + "\" exists");
                }

                if (resurrectPlayer.getGameMode() == GameMode.SPECTATOR) {
                    if (DEBUG) {
                        Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player in spectator mode, assuming dead");
                    }
                    for (PotionEffect effect : resurrectPlayer.getActivePotionEffects())
                        resurrectPlayer.removePotionEffect(effect.getType());
                    resurrectPlayer.setGameMode(GameMode.SURVIVAL);
                    for(Player player : Bukkit.getOnlinePlayers()){
                        try {
                            player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 1, 0);
                        } catch (NoSuchFieldError e) {
                            log.warning("NoSuchFieldError encountered, playing Wither noise instead.");
                            player.playSound(player.getLocation(), Sound.ENTITY_WITHER_DEATH, 1, 0);
                        }
                    }
                    Bukkit.broadcastMessage(strings[0] + " has been resurrected manually by an admin!");
                    removeDeath(resurrectPlayer);
                    if (resurrectPlayer.getBedSpawnLocation() != null) {
                        if (DEBUG) {
                            Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting");
                        }

                        resurrectPlayer.teleport(resurrectPlayer.getBedSpawnLocation());
                    }
                    return true;
                } else {
                    log.warning("ERROR: " + strings[0] + " is not dead! Failed to resurrect.");
                    return false;
                }
            } else {
                log.warning("ERROR: Too few arguments!");
                return false;
            }
        }
    }

    public void removeDeath(Player p) {
        PlayerData playerData = new PlayerData();
        playerData.readData();
        String rawData = playerData.getRawData();
        String[] rawPlayers = rawData.split(";");
        int index = 0;
        for (String players : rawPlayers) {
            if (players.startsWith(p.getDisplayName())) {
                if (DEBUG) {
                    Bukkit.broadcastMessage(ChatColor.YELLOW  +""+ ChatColor.BOLD + "[Res. DEBUG]: Removing the death from the resurrected player");
                }
                String[] playerSplit = players.split(",");
                playerSplit[1] = "false";
                playerSplit[2] = "0";

                // save data
                rawPlayers[index] = String.join(",", playerSplit);
                rawData = String.join(";", rawPlayers);
                playerData.saveData(rawData);
                break;
            }
            index++;
        }
    }
}