<?
php
namespace Spawn;
use pocketmine\plugin\PluginBase;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
use pocketmine\scheduler\Task;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerMoveEvent;
class Main extends PluginBase implements Listener {
private $countdownTasks = [];
public function onEnable(): void {
$this->getLogger()->info(TextFormat::GREEN . "SpawnPlugin habilitado!");
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onDisable(): void {
$this->getLogger()->info(TextFormat::RED . "SpawnPlugin deshabilitado!");
}
public function onCommand(CommandSender $sender, Command $command, $label,
array $args): bool {
if ($command->getName() === "spawn") {
if ($sender instanceof Player) {
try {
$spawn = $this->getServer()->getDefaultLevel()-
>getSpawnLocation();
if ($spawn === null) {
$sender->sendMessage(TextFormat::RED . "No se ha encontrado
la ubicación del spawn.");
return false;
}
$this->showCountdown($sender, 10);
$this->getServer()->getScheduler()->scheduleDelayedTask(new
class($sender, $spawn) extends Task {
private $player;
private $spawn;
public function __construct(Player $player, $spawn) {
$this->player = $player;
$this->spawn = $spawn;
}
public function onRun(): void {
$this->player->teleport($this->spawn);
$this->player->sendTip(TextFormat::GREEN . "¡Te has
teletransportado al spawn!");
}
}, 200);
return true;
} catch (\Exception $e) {
$this->getLogger()->error("Error al ejecutar el comando /spawn:
" . $e->getMessage());
$sender->sendTip(TextFormat::RED . "Hubo un error al
teletransportarte. Por favor, intenta nuevamente.");
return false;
}
} else {
$sender->sendTip(TextFormat::RED . "¡Este comando solo se puede
usar en el juego!");
return false;
}
}
return false;
}
private function showCountdown(Player $player, int $seconds): void {
$server = $this->getServer();
$this->countdownTasks[$player->getName()] = new class($player, $seconds,
$this) extends Task {
private $player;
private $seconds;
private $plugin;
public function __construct(Player $player, int $seconds, $plugin) {
$this->player = $player;
$this->seconds = $seconds;
$this->plugin = $plugin;
}
public function onRun(): void {
if ($this->seconds > 0) {
$this->player->sendMessage(TextFormat::YELLOW .
"¡Teletransportándote en " . $this->seconds . " segundos!");
$this->seconds--;
} else {
$this->player->sendTip(TextFormat::GREEN . "¡Te has
teletransportado al spawn!");
unset($this->plugin->countdownTasks[$this->player->getName()]);
}
}
};
$server->getScheduler()->scheduleRepeatingTask($this-
>countdownTasks[$player->getName()], 20);
}
public function onPlayerMove(PlayerMoveEvent $event): void {
$player = $event->getPlayer();
if (isset($this->countdownTasks[$player->getName()])) {
unset($this->countdownTasks[$player->getName()]);
$player->sendTip(TextFormat::RED . "¡Has cancelado el teletransporte al
spawn al moverte!");
}
}
}