Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
declare(strict_types=1);
/**
* @name MyPlotPlotHomes
* @main jasonwynn10\MyPlotPlotHomes\Main
* @version 0.1.0
* @api 3.0.0
* @description A plugin script which allows players to set the block of the plot border via form
* @author jasonwynn10
* @depend MyPlot
*/
namespace jasonwynn10\MyPlotPlotHomes {
use MyPlot\Commands;
use MyPlot\MyPlot;
use MyPlot\Plot;
use MyPlot\subcommand\HomeSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\OfflinePlayer;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;
class Main extends PluginBase {
/** @var self|null $instance */
private static $instance = null;
/**
* @return self|null
*/
public static function getInstance() : ?self {
return self::$instance;
}
public function onEnable() {
self::$instance = $this;
$command = new class(MyPlot::getInstance(), "home") extends HomeSubCommand {
public function getUsage() : string {
return "/p home [player: string] [number: int] [world: string]";
}
/**
* @param Player $sender
* @param array $args
*
* @return bool
*/
public function execute(CommandSender $sender, array $args) : bool {
$selected = null;
if(empty($args)) {
$selected = $sender;
$selectedName = $sender->getName();
$plotNumber = 1;
}elseif(isset($args[0])) {
$selected = $this->getPlugin()->getServer()->getPlayer($args[0]) ?? $this->getPlugin()->getServer()->getOfflinePlayer($args[0]);
$selectedName = $selected instanceof Player ? $selected->getName() : $args[0];
$plotNumber = (int) ($args[1] ?? 1);
}else{
return false;
}
if($selected instanceof OfflinePlayer and !isset($args[2])) {
$sender->sendMessage(TextFormat::RED . $this->translateString("home.error"));
return true;
}
$levelName = $args[2] ?? $selected->getLevel()->getFolderName();
$plots = $this->getPlugin()->getPlotsOfPlayer($selectedName, $levelName);
if(empty($plots)) {
$sender->sendMessage(TextFormat::RED . "No plots found in world");
return true;
}
if(!isset($plots[$plotNumber - 1])) {
$sender->sendMessage(TextFormat::RED . $this->translateString("home.notexist", [$plotNumber]));
return true;
}
usort($plots, function(Plot $plot1, Plot $plot2) {
if($plot1->levelName == $plot2->levelName) {
return 0;
}
return ($plot1->levelName < $plot2->levelName) ? -1 : 1;
});
$plot = $plots[$plotNumber - 1];
if($this->getPlugin()->teleportPlayerToPlot($sender, $plot)) {
$sender->sendMessage($this->translateString("home.success", [$plot, $plot->levelName]));
}else{
$sender->sendMessage(TextFormat::RED . $this->translateString("home.error"));
}
return true;
}
};
/** @var Commands $commands */
$commands = $this->getServer()->getCommandMap()->getCommand("plot");
$commands->unloadSubCommand("home");
$commands->loadSubCommand($command);
$this->getLogger()->debug("SubCommand loaded");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.