Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A temporary plugin to allow the changing of plot borders
<?php
declare(strict_types=1);
/**
* @name MyPlotBorders
* @main jasonwynn10\MyPlotBorders\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 FormAPI
* @softdepend MyPlot
*/
namespace jasonwynn10\MyPlotBorders {
use jojoe77777\FormAPI\SimpleForm;
use MyPlot\Commands;
use MyPlot\MyPlot;
use MyPlot\Plot;
use MyPlot\subcommand\SubCommand;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\command\CommandSender;
use pocketmine\math\Vector3;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\Task;
use pocketmine\utils\TextFormat;
class Main extends PluginBase {
/** @var string[] $blocks */
public static $blocks = [];
/** @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;
$blocks = ["44:0", "44:1", "44:2", "44:3", "44:4", "44:5", "44:6", "44:7", "158:0", "158:1", "158:2", "158:3", "158:4", "158:5", "0:0", "120:0", "122:0", "138:0"];
foreach($blocks as $string) {
$arr = explode(":", $string);
$id = (int) $arr[0];
$damage = (int) ($arr[1] ?? 0);
$block = BlockFactory::get($id, $damage);
self::$blocks[$string] = $block->getName();
}
/** @var Commands $commands */
$commands = $this->getServer()->getCommandMap()->getCommand("plot");
if(version_compare(MyPlot::getInstance()->getDescription()->getVersion(), "1.5.3", ">=")) {
$command = new class(MyPlot::getInstance(), "border") extends SubCommand {
public function getName() : string {
return "border";
}
public function getAlias() : string {
return "b";
}
public function canUse(CommandSender $sender) : bool {
return ($sender instanceof Player) and $sender->hasPermission("myplot.command.border");
}
/**
* @param Player $sender
* @param array $args
*
* @return bool
*/
public function execute(CommandSender $sender, array $args) : bool {
$plot = $this->getPlugin()->getPlotByPosition($sender);
if($plot === null) {
$sender->sendMessage(TextFormat::RED . $this->getPlugin()->getLanguage()->translateString("notinplot"));
return true;
}
if($plot->owner !== $sender->getName() and !$sender->hasPermission("border.command.admin")) {
$sender->sendMessage(TextFormat::RED . $this->getPlugin()->getLanguage()->translateString("notowner"));
return true;
}
$sender->sendForm(new class(function($player, $data) use ($plot) {
if($data === null)
return; // form cancelled. do nothing
$arr = explode(":", $data);
$id = (int) $arr[0];
$damage = (int) $arr[1];
$block = BlockFactory::get($id, $damage);
Main::getInstance()->setPlotBorderBlocks($plot, $block);
}) extends SimpleForm {
public function __construct(?callable $callable) {
$this->setTitle("Plot Border");
foreach(Main::$blocks as $id => $name) {
$this->addButton($name);
}
parent::__construct($callable);
}
public function processData(&$data) : void {
$i = 0;
foreach(Main::$blocks as $id => $name) {
if($i === $data) {
$data = $id;
return;
}
$i++;
}
}
});
return true;
}
};
$commands->loadSubCommand($command);
$this->getLogger()->debug("SubCommand loaded");
}else{
$command = new class(MyPlot::getInstance(), "border") extends SubCommand {
public function canUse(CommandSender $sender) : bool {
return ($sender instanceof Player) and $sender->hasPermission("myplot.command.border");
}
/**
* @param Player $sender
* @param array $args
*
* @return bool
*/
public function execute(CommandSender $sender, array $args) : bool {
$plot = $this->getPlugin()->getPlotByPosition($sender);
if($plot === null) {
$sender->sendMessage(TextFormat::RED . $this->getPlugin()->getLanguage()->translateString("notinplot"));
return true;
}
if($plot->owner !== $sender->getName() and !$sender->hasPermission("border.command.admin")) {
$sender->sendMessage(TextFormat::RED . $this->getPlugin()->getLanguage()->translateString("notowner"));
return true;
}
$sender->sendForm(new class(function($player, $data) use ($plot) {
if($data === null)
return; // form cancelled. do nothing
$arr = explode(":", $data);
$id = (int) $arr[0];
$damage = (int) $arr[1];
$block = BlockFactory::get($id, $damage);
Main::getInstance()->setPlotBorderBlocks($plot, $block);
}) extends SimpleForm {
public function __construct(?callable $callable) {
$this->setTitle("Plot Border");
foreach(Main::$blocks as $id => $name) {
$this->addButton($name);
}
parent::__construct($callable);
}
public function processData(&$data) : void {
$i = 0;
foreach(Main::$blocks as $id => $name) {
if($i === $data) {
$data = $id;
return;
}
$i++;
}
}
});
return true;
}
};
$refObject = new \ReflectionClass($commands);
$refProperty = $refObject->getProperty("subCommands");
$refProperty->setAccessible(true);
$val = $refProperty->getValue($commands);
$val["border"] = $command;
$refProperty->setValue($commands, $val);
$refObject = new \ReflectionClass($commands);
$refProperty = $refObject->getProperty("aliasSubCommands");
$refProperty->setAccessible(true);
$val = $refProperty->getValue($commands);
$val["b"] = $command;
$refProperty->setValue($commands, $val);
}
}
public function setPlotBorderBlocks(Plot $plot, Block $block) {
$this->getScheduler()->scheduleTask(new class(MyPlot::getInstance(), $plot, $block) extends Task {
private $plot, $level, $height, $plotWallBlock, $plotBeginPos, $xMax, $zMax;
public function __construct(MyPlot $plugin, Plot $plot, Block $block) {
$this->plot = $plot;
$this->plotBeginPos = $plugin->getPlotPosition($plot);
$this->level = $this->plotBeginPos->getLevel();
$this->plotBeginPos = $this->plotBeginPos->subtract(1,0,1);
$plotLevel = $plugin->getLevelSettings($plot->levelName);
$plotSize = $plotLevel->plotSize;
$this->xMax = $this->plotBeginPos->x + $plotSize + 1;
$this->zMax = $this->plotBeginPos->z + $plotSize + 1;
$this->height = $plotLevel->groundHeight;
$this->plotWallBlock = $block;
}
public function onRun(int $currentTick) : void {
for($x = $this->plotBeginPos->x; $x <= $this->xMax; $x++) {
$this->level->setBlock(new Vector3($x, $this->height + 1, $this->plotBeginPos->z), $this->plotWallBlock, false, false);
$this->level->setBlock(new Vector3($x, $this->height + 1, $this->zMax), $this->plotWallBlock, false, false);
}
for($z = $this->plotBeginPos->z; $z <= $this->zMax; $z++) {
$this->level->setBlock(new Vector3($this->plotBeginPos->x, $this->height + 1, $z), $this->plotWallBlock, false, false);
$this->level->setBlock(new Vector3($this->xMax, $this->height + 1, $z), $this->plotWallBlock, false, false);
}
}
});
}
}
}
@gitschwar

This comment has been minimized.

Copy link

gitschwar commented Apr 10, 2019

how can i get this in a .phar data

@nikipuh

This comment has been minimized.

Copy link

nikipuh commented Apr 13, 2019

How do I import that into my Server? @jasonwynn10

@Wertzui123

This comment has been minimized.

Copy link

Wertzui123 commented Apr 28, 2019

Just upload this file 😉

@LeanderYT

This comment has been minimized.

Copy link

LeanderYT commented Jul 14, 2019

How to upload this?
@jasonwynn10

@jasonwynn10

This comment has been minimized.

Copy link
Owner Author

jasonwynn10 commented Jul 14, 2019

click the "raw" button
ctrl + s to save the file

note: make sure it is a .php file. not .txt

@Chaosfelix445

This comment has been minimized.

Copy link

Chaosfelix445 commented Oct 24, 2019

Umm how i can get this in .phar because .php i cant use at minehub8

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.