resurrection/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java

153 lines
7 KiB
Java
Raw Normal View History

2021-06-06 12:18:29 -06:00
package net.brysonsteck.Resurrection.commands;
2021-06-16 18:02:05 -06:00
import net.brysonsteck.Resurrection.player.PlayerData;
2021-06-06 12:18:29 -06:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
2021-06-15 20:23:28 -06:00
import org.bukkit.Sound;
2021-06-06 12:18:29 -06:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
public class CommandResurrect implements CommandExecutor {
boolean DEBUG;
2021-09-13 15:31:29 -06:00
public CommandResurrect(String debug) {
this.DEBUG = Boolean.parseBoolean(debug);
2021-09-13 15:31:29 -06:00
}
2021-06-06 12:18:29 -06:00
@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `resurrect` command was ran by " + commandSender.getName());
}
2021-06-06 12:22:07 -06:00
boolean valid = (strings.length == 1);
2021-06-06 12:18:29 -06:00
if (commandSender instanceof Player) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player.");
}
2021-06-06 12:18:29 -06:00
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");
}
2021-06-06 12:18:29 -06:00
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.");
2021-06-06 12:18:29 -06:00
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");
}
2021-06-06 12:18:29 -06:00
for (PotionEffect effect : resurrectPlayer.getActivePotionEffects())
resurrectPlayer.removePotionEffect(effect.getType());
resurrectPlayer.setGameMode(GameMode.SURVIVAL);
2021-06-15 20:23:28 -06:00
for(Player player : Bukkit.getOnlinePlayers()){
player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 1, 0);
}
2021-06-15 20:15:51 -06:00
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + strings[0] + " has been resurrected manually by an admin!");
2021-06-16 18:02:05 -06:00
removeDeath(resurrectPlayer);
2021-07-21 21:48:54 -06:00
if (p.getBedSpawnLocation() != null) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting");
}
2021-07-21 21:48:54 -06:00
p.teleport(p.getBedSpawnLocation());
}
2021-06-06 12:18:29 -06:00
return true;
} else {
p.sendMessage(ChatColor.RED + "ERROR: " + strings[0] + " is not dead! Failed to resurrect.");
2021-06-06 12:18:29 -06:00
return false;
}
} else {
System.out.println(ChatColor.RED + "ERROR: Too few arguments!");
2021-06-06 12:18:29 -06:00
return false;
}
} else {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console.");
}
2021-06-06 12:18:29 -06:00
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");
}
2021-06-06 12:18:29 -06:00
Player resurrectPlayer = Bukkit.getPlayer(strings[0]);
if (resurrectPlayer == null) {
2021-08-25 00:41:04 -06:00
System.out.println("[Resurrection] ERROR: That player is not online/doesn't exist! Failed to resurrect.");
2021-06-06 12:18:29 -06:00
return false;
}
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player \"" + resurrectPlayer.getDisplayName() + "\" exists");
}
2021-06-15 18:43:25 -06:00
if (resurrectPlayer.getGameMode() == GameMode.SPECTATOR) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player in spectator mode, assuming dead");
}
2021-06-06 12:18:29 -06:00
for (PotionEffect effect : resurrectPlayer.getActivePotionEffects())
resurrectPlayer.removePotionEffect(effect.getType());
resurrectPlayer.setGameMode(GameMode.SURVIVAL);
2021-06-15 20:23:28 -06:00
for(Player player : Bukkit.getOnlinePlayers()){
player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 1, 0);
}
2021-06-06 12:18:29 -06:00
Bukkit.broadcastMessage(strings[0] + " has been resurrected manually by an admin!");
2021-06-16 18:02:05 -06:00
removeDeath(resurrectPlayer);
2021-07-21 21:48:54 -06:00
if (resurrectPlayer.getBedSpawnLocation() != null) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting");
}
2021-07-21 21:48:54 -06:00
resurrectPlayer.teleport(resurrectPlayer.getBedSpawnLocation());
}
2021-06-06 12:18:29 -06:00
return true;
} else {
2021-08-25 00:41:04 -06:00
System.out.println("[Resurrection] ERROR: " + strings[0] + " is not dead! Failed to resurrect.");
2021-06-06 12:18:29 -06:00
return false;
}
} else {
System.out.println("[Resurrection] ERROR: Too few arguments!");
2021-06-06 12:18:29 -06:00
return false;
}
}
}
2021-06-16 18:02:05 -06:00
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");
}
2021-06-16 18:02:05 -06:00
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++;
}
}
2021-06-06 12:18:29 -06:00
}