2021-06-06 12:18:29 -06:00
package net.brysonsteck.Resurrection.commands ;
2022-01-04 00:36:47 -07:00
import net.brysonsteck.Resurrection.Resurrection ;
2021-06-16 18:02:05 -06:00
import net.brysonsteck.Resurrection.player.PlayerData ;
2022-01-04 00:36:47 -07:00
import java.util.logging.Logger ;
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 ;
2022-01-04 00:36:47 -07:00
import org.bukkit.plugin.java.JavaPlugin ;
2021-06-06 12:18:29 -06:00
import org.bukkit.potion.PotionEffect ;
public class CommandResurrect implements CommandExecutor {
2021-09-13 17:14:31 -06:00
boolean DEBUG ;
2021-09-13 15:31:29 -06:00
public CommandResurrect ( String debug ) {
2021-09-13 17:14:31 -06:00
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 ) {
2022-01-04 00:36:47 -07:00
Logger log = JavaPlugin . getProvidingPlugin ( Resurrection . class ) . getLogger ( ) ;
2021-09-13 17:14:31 -06:00
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-09-13 17:14:31 -06:00
2021-06-06 12:18:29 -06:00
if ( commandSender instanceof Player ) {
2021-09-13 17:14:31 -06:00
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 ) {
2021-09-13 17:14:31 -06:00
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:23:03 -06:00
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 ;
}
2021-09-13 17:14:31 -06:00
if ( DEBUG ) {
Bukkit . broadcastMessage ( ChatColor . YELLOW + " " + ChatColor . BOLD + " [Res. DEBUG]: Specified player \" " + resurrectPlayer . getDisplayName ( ) + " \" exists " ) ;
}
2021-06-15 18:49:36 -06:00
if ( resurrectPlayer . getGameMode ( ) = = GameMode . SPECTATOR ) {
2021-09-13 17:14:31 -06:00
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 ( ) ) {
2022-01-05 14:17:01 -07:00
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 ) ;
}
2021-06-15 20:23:28 -06:00
}
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 ) {
2021-09-13 17:14:31 -06:00
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 {
2021-08-25 00:23:03 -06:00
p . sendMessage ( ChatColor . RED + " ERROR: " + strings [ 0 ] + " is not dead! Failed to resurrect. " ) ;
2021-06-06 12:18:29 -06:00
return false ;
}
} else {
2022-01-04 00:36:47 -07:00
p . sendMessage ( ChatColor . RED + " ERROR: Too few arguments! " ) ;
2021-06-06 12:18:29 -06:00
return false ;
}
} else {
2021-09-13 17:14:31 -06:00
if ( DEBUG ) {
Bukkit . broadcastMessage ( ChatColor . YELLOW + " " + ChatColor . BOLD + " [Res. DEBUG]: CommandSender is console. " ) ;
}
2021-06-06 12:18:29 -06:00
if ( valid ) {
2021-09-13 17:14:31 -06:00
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 ) {
2022-01-04 00:36:47 -07:00
log . warning ( " ERROR: That player is not online/doesn't exist! Failed to resurrect. " ) ;
2021-06-06 12:18:29 -06:00
return false ;
}
2021-09-13 17:14:31 -06:00
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 ) {
2021-09-13 17:14:31 -06:00
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 ( ) ) {
2022-01-05 14:17:01 -07:00
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 ) ;
}
2021-06-15 20:23:28 -06:00
}
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 ) {
2021-09-13 17:14:31 -06:00
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 {
2022-01-04 00:36:47 -07:00
log . warning ( " ERROR: " + strings [ 0 ] + " is not dead! Failed to resurrect. " ) ;
2021-06-06 12:18:29 -06:00
return false ;
}
} else {
2022-01-04 00:36:47 -07:00
log . warning ( " 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 ( ) ) ) {
2021-09-13 17:14:31 -06:00
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
}