Skip to content
This repository has been archived by the owner on Oct 29, 2022. It is now read-only.

Commit

Permalink
Event changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonw4331 committed Apr 1, 2022
1 parent 924f9fc commit 575534f
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 227 deletions.
64 changes: 30 additions & 34 deletions src/MyPlot/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use MyPlot\events\MyPlotPlayerEnterPlotEvent;
use MyPlot\events\MyPlotPlayerLeavePlotEvent;
use MyPlot\events\MyPlotPvpEvent;
use MyPlot\plot\BasePlot;
use MyPlot\plot\SinglePlot;
use pocketmine\block\Liquid;
use pocketmine\block\Sapling;
use pocketmine\block\utils\TreeType;
Expand Down Expand Up @@ -38,25 +40,25 @@ public function __construct(private MyPlot $plugin, private InternalAPI $interna
* @param WorldLoadEvent $event
*/
public function onLevelLoad(WorldLoadEvent $event) : void{
if(file_exists($this->plugin->getDataFolder()."worlds".DIRECTORY_SEPARATOR.$event->getWorld()->getFolderName().".yml")) {
if(file_exists($this->plugin->getDataFolder() . "worlds" . DIRECTORY_SEPARATOR . $event->getWorld()->getFolderName() . ".yml")){
$this->plugin->getLogger()->debug("MyPlot level " . $event->getWorld()->getFolderName() . " loaded!");
$settings = $event->getWorld()->getProvider()->getWorldData()->getGeneratorOptions();
$settings = json_decode($settings, true);
if($settings === false) {
if($settings === false){
return;
}
$levelName = $event->getWorld()->getFolderName();
$default = array_filter((array) $this->plugin->getConfig()->get("DefaultWorld", []), function($key) : bool {
$default = array_filter((array) $this->plugin->getConfig()->get("DefaultWorld", []), function($key) : bool{
return !in_array($key, ["PlotSize", "GroundHeight", "RoadWidth", "RoadBlock", "WallBlock", "PlotFloorBlock", "PlotFillBlock", "BottomBlock"], true);
}, ARRAY_FILTER_USE_KEY);
$config = new Config($this->plugin->getDataFolder()."worlds".DIRECTORY_SEPARATOR.$levelName.".yml", Config::YAML, $default);
foreach(array_keys($default) as $key) {
$settings[$key] = $config->get((string)$key);
$config = new Config($this->plugin->getDataFolder() . "worlds" . DIRECTORY_SEPARATOR . $levelName . ".yml", Config::YAML, $default);
foreach(array_keys($default) as $key){
$settings[$key] = $config->get((string) $key);
}
$this->internalAPI->addLevelSettings($levelName, new PlotLevelSettings($levelName, $settings));

if($this->plugin->getConfig()->get('AllowFireTicking', false) === false) {
$ref = new \ReflectionClass($event->getWorld());
if($this->plugin->getConfig()->get('AllowFireTicking', false) === false){
$ref = new \ReflectionClass(World::class);
$prop = $ref->getProperty('randomTickBlocks');
$prop->setAccessible(true);
$randomTickBlocks = $prop->getValue($event->getWorld());
Expand All @@ -71,7 +73,7 @@ public function onLevelLoad(WorldLoadEvent $event) : void{
*
* @param WorldUnloadEvent $event
*/
public function onLevelUnload(WorldUnloadEvent $event) : void {
public function onLevelUnload(WorldUnloadEvent $event) : void{
$levelName = $event->getWorld()->getFolderName();
if($this->internalAPI->unloadLevelSettings($levelName)){
$this->plugin->getLogger()->debug("Level " . $event->getWorld()->getFolderName() . " unloaded!");
Expand All @@ -84,7 +86,7 @@ public function onLevelUnload(WorldUnloadEvent $event) : void {
*
* @param BlockPlaceEvent $event
*/
public function onBlockPlace(BlockPlaceEvent $event) : void {
public function onBlockPlace(BlockPlaceEvent $event) : void{
$this->onEventOnBlock($event);
}

Expand All @@ -94,7 +96,7 @@ public function onBlockPlace(BlockPlaceEvent $event) : void {
*
* @param BlockBreakEvent $event
*/
public function onBlockBreak(BlockBreakEvent $event) : void {
public function onBlockBreak(BlockBreakEvent $event) : void{
$this->onEventOnBlock($event);
}

Expand All @@ -104,7 +106,7 @@ public function onBlockBreak(BlockBreakEvent $event) : void {
*
* @param PlayerInteractEvent $event
*/
public function onPlayerInteract(PlayerInteractEvent $event) : void {
public function onPlayerInteract(PlayerInteractEvent $event) : void{
$this->onEventOnBlock($event);
}

Expand All @@ -114,7 +116,7 @@ public function onPlayerInteract(PlayerInteractEvent $event) : void {
*
* @param SignChangeEvent $event
*/
public function onSignChange(SignChangeEvent $event) : void {
public function onSignChange(SignChangeEvent $event) : void{
$this->onEventOnBlock($event);
}

Expand All @@ -136,8 +138,8 @@ private function onEventOnBlock(BlockPlaceEvent|SignChangeEvent|PlayerInteractEv

$ev->call();
$ev->isCancelled() ? $event->cancel() : $event->uncancel();
$plot = $this->internalAPI->getPlotFromCache($plot);
if($plot === null){
$plot = $this->internalAPI->getPlotFromCache($plot, true);
if(!$plot instanceof SinglePlot){
$event->cancel();
$this->plugin->getLogger()->debug("Cancelled block change event at $x,$pos->y,$z in [$levelName]");
return;
Expand Down Expand Up @@ -167,8 +169,8 @@ private function onEventOnBlock(BlockPlaceEvent|SignChangeEvent|PlayerInteractEv
elseif($plotLevel->editBorderBlocks){
$plot = $this->internalAPI->getPlotBorderingPosition($event->getBlock()->getPosition());
if($plot !== null){
$plot = $this->internalAPI->getPlotFromCache($plot);
if($plot === null){
$plot = $this->internalAPI->getPlotFromCache($plot, true);
if(!$plot instanceof SinglePlot){
$event->cancel();
$this->plugin->getLogger()->debug("Cancelled block change event at $x,$pos->y,$z in [$levelName]");
return;
Expand All @@ -188,7 +190,7 @@ private function onEventOnBlock(BlockPlaceEvent|SignChangeEvent|PlayerInteractEv
}
}
$event->cancel();
$this->plugin->getLogger()->debug("Block placement/break/interaction of {$event->getBlock()->getName()} was cancelled at ".$event->getBlock()->getPosition()->__toString());
$this->plugin->getLogger()->debug("Block placement/break/interaction of {$event->getBlock()->getName()} was cancelled at " . $event->getBlock()->getPosition()->__toString());
}

/**
Expand Down Expand Up @@ -230,15 +232,15 @@ public function onExplosion(EntityExplodeEvent $event) : void{
*
* @param EntityMotionEvent $event
*/
public function onEntityMotion(EntityMotionEvent $event) : void {
public function onEntityMotion(EntityMotionEvent $event) : void{
$level = $event->getEntity()->getWorld();
if(!$level instanceof World)
return;
$levelName = $level->getFolderName();
if($this->internalAPI->getLevelSettings($levelName) === null)
return;
$settings = $this->internalAPI->getLevelSettings($levelName);
if($settings->restrictEntityMovement and !($event->getEntity() instanceof Player)) {
if($settings->restrictEntityMovement and !($event->getEntity() instanceof Player)){
$event->cancel();
$this->plugin->getLogger()->debug("Cancelled entity motion on " . $levelName);
}
Expand Down Expand Up @@ -282,7 +284,7 @@ public function onBlockSpread(BlockSpreadEvent $event) : void{
*
* @param PlayerMoveEvent $event
*/
public function onPlayerMove(PlayerMoveEvent $event) : void {
public function onPlayerMove(PlayerMoveEvent $event) : void{
$this->onEventOnMove($event->getPlayer(), $event);
}

Expand All @@ -292,9 +294,9 @@ public function onPlayerMove(PlayerMoveEvent $event) : void {
*
* @param EntityTeleportEvent $event
*/
public function onPlayerTeleport(EntityTeleportEvent $event) : void {
public function onPlayerTeleport(EntityTeleportEvent $event) : void{
$entity = $event->getEntity();
if ($entity instanceof Player) {
if($entity instanceof Player){
$this->onEventOnMove($entity, $event);
}
}
Expand Down Expand Up @@ -325,24 +327,18 @@ private function onEventOnMove(Player $player, EntityTeleportEvent|PlayerMoveEve
$ev->isCancelled() ? $event->cancel() : $event->uncancel();
}

if($plot !== null){
if($plot !== null)
$plot = $this->internalAPI->getPlotFromCache($plot, true);
if($plot === null){
$event->cancel(); // This causes a stutter step where the player will be denied for a tick or longer while the cache populates
$this->plugin->getLogger()->debug("Cancelled player move on [$levelName] due to plot not cached");
return;
}
}

if($plot !== null and ($plotFrom === null or !$plot->isSame($plotFrom))){
if($plot instanceof BasePlot and ($plotFrom === null or !$plot->isSame($plotFrom))){
if(str_contains((string) $plot, "-0"))
return;
$plot = SinglePlot::fromBase($plot);

$ev = new MyPlotPlayerEnterPlotEvent($plot, $player);
$event->isCancelled() ? $ev->cancel() : $ev->uncancel();

$username = $ev->getPlayer()->getName();
$plot = $this->internalAPI->getPlotFromCache($plot, true);
if($plot->owner !== $username and
($plot->isDenied($username) or $plot->isDenied("*")) and
!$ev->getPlayer()->hasPermission("myplot.admin.denyplayer.bypass")
Expand Down Expand Up @@ -384,7 +380,7 @@ private function onEventOnMove(Player $player, EntityTeleportEvent|PlayerMoveEve
*
* @param EntityDamageByEntityEvent $event
*/
public function onEntityDamage(EntityDamageByEntityEvent $event) : void {
public function onEntityDamage(EntityDamageByEntityEvent $event) : void{
$damaged = $event->getEntity();
$damager = $event->getDamager();
if($damaged instanceof Player and $damager instanceof Player){
Expand All @@ -399,7 +395,7 @@ public function onEntityDamage(EntityDamageByEntityEvent $event) : void {
$plot = $this->internalAPI->getPlotFast($x, $z, $plotLevel);
if($plot !== null){
$plot = $this->internalAPI->getPlotFromCache($plot);
if($plot === null){
if(!$plot instanceof SinglePlot){
if($plotLevel->restrictPVP and !$damager->hasPermission("myplot.admin.pvp.bypass"))
$event->cancel();
$this->plugin->getLogger()->debug("Cancelled player damage on [$levelName] due to plot not cached");
Expand Down
18 changes: 9 additions & 9 deletions src/MyPlot/events/MyPlotBlockEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declare(strict_types=1);
namespace MyPlot\events;

use MyPlot\Plot;
use MyPlot\plot\BasePlot;
use pocketmine\block\Block;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\block\BlockPlaceEvent;
Expand All @@ -13,7 +13,7 @@
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\player\Player;

class MyPlotBlockEvent extends MyPlotPlotEvent implements Cancellable {
class MyPlotBlockEvent extends MyPlotPlotEvent implements Cancellable{
use CancellableTrait;

private Block $block;
Expand All @@ -24,30 +24,30 @@ class MyPlotBlockEvent extends MyPlotPlotEvent implements Cancellable {
/**
* MyPlotBlockEvent constructor.
*
* @param Plot $plot
* @param Block $block
* @param Player $player
* @param BasePlot $plot
* @param Block $block
* @param Player $player
* @param BlockPlaceEvent|BlockBreakEvent|PlayerInteractEvent|SignChangeEvent $event
*/
public function __construct(Plot $plot, Block $block, Player $player, Event $event) {
public function __construct(BasePlot $plot, Block $block, Player $player, Event $event){
$this->block = $block;
$this->player = $player;
$this->event = $event;
parent::__construct($plot);
}

public function getBlock() : Block {
public function getBlock() : Block{
return $this->block;
}

/**
* @return BlockPlaceEvent|BlockBreakEvent|PlayerInteractEvent|SignChangeEvent
*/
public function getEvent() : Event {
public function getEvent() : Event{
return $this->event;
}

public function getPlayer() : Player {
public function getPlayer() : Player{
return $this->player;
}
}
2 changes: 1 addition & 1 deletion src/MyPlot/events/MyPlotBorderChangeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
namespace MyPlot\events;


class MyPlotBorderChangeEvent extends MyPlotBlockEvent {
class MyPlotBorderChangeEvent extends MyPlotBlockEvent{

}
14 changes: 7 additions & 7 deletions src/MyPlot/events/MyPlotClearEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
declare(strict_types=1);
namespace MyPlot\events;

use MyPlot\Plot;
use MyPlot\plot\BasePlot;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;

class MyPlotClearEvent extends MyPlotPlotEvent implements Cancellable {
class MyPlotClearEvent extends MyPlotPlotEvent implements Cancellable{
use CancellableTrait;

private int $maxBlocksPerTick;

/**
* MyPlotClearEvent constructor.
*
* @param Plot $plot
* @param int $maxBlocksPerTick
* @param BasePlot $plot
* @param int $maxBlocksPerTick
*/
public function __construct(Plot $plot, int $maxBlocksPerTick = 256) {
public function __construct(BasePlot $plot, int $maxBlocksPerTick = 256){
$this->maxBlocksPerTick = $maxBlocksPerTick;
parent::__construct($plot);
}

public function getMaxBlocksPerTick() : int {
public function getMaxBlocksPerTick() : int{
return $this->maxBlocksPerTick;
}

public function setMaxBlocksPerTick(int $maxBlocksPerTick) : self {
public function setMaxBlocksPerTick(int $maxBlocksPerTick) : self{
$this->maxBlocksPerTick = $maxBlocksPerTick;
return $this;
}
Expand Down
18 changes: 6 additions & 12 deletions src/MyPlot/events/MyPlotCloneEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@
declare(strict_types=1);
namespace MyPlot\events;

use MyPlot\Plot;
use MyPlot\plot\BasePlot;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;

class MyPlotCloneEvent extends MyPlotPlotEvent implements Cancellable {
class MyPlotCloneEvent extends MyPlotPlotEvent implements Cancellable{
use CancellableTrait;

protected Plot $clonePlot;
protected BasePlot $clonePlot;

/**
* MyPlotCloneEvent constructor.
*
* @param Plot $originPlot
* @param Plot $clonePlot
*/
public function __construct(Plot $originPlot, Plot $clonePlot) {
public function __construct(BasePlot $originPlot, BasePlot $clonePlot){
$this->clonePlot = $clonePlot;
parent::__construct($originPlot);
}

public function setClonePlot(Plot $clonePlot) : void {
public function setClonePlot(BasePlot $clonePlot) : void{
$this->clonePlot = $clonePlot;
}

public function getClonePlot() : Plot {
public function getClonePlot() : BasePlot{
return $this->clonePlot;
}
}
Loading

0 comments on commit 575534f

Please sign in to comment.