Fixing bugs + VacuumTile !

This commit is contained in:
Ad5001 2017-02-17 18:49:00 +01:00
parent 96456e69b4
commit 9094478518
6 changed files with 101 additions and 84 deletions

View file

@ -86,6 +86,7 @@ class FallableTile extends CustomBlockData {
"TileID" => new IntTag("TileID", $this->getBlock()->getId()),
"Data" => new ByteTag("Data", $this->getBlock()->getDamage()),
]))->spawnToAll();
$this->level->removeTile($this);
// $this->getLevel()->setBlock($this, Block::get(0, 0));
}
}

View file

@ -1,66 +0,0 @@
<?php
# ____ _ _ ____ _ _
# | __ ) ___ | |_ | |_ ___ _ __ | __ ) | | ___ ___ | | __ ___
# | _ \ / _ \ | __| | __| / _ \ | '__| | _ \ | | / _ \ / __| | |/ / / __|
# | |_) | | __/ | |_ | |_ | __/ | | | |_) | | | | (_) | | (__ | < \__ \
# |____/ \___| \__| \__| \___| |_| |____/ |_| \___/ \___| |_|\_\ |___/
#
# Extends your Minecraft PE blocks palette ! For PocketMine.
namespace Ad5001\BetterBlocks\CustomBlockData;
use pocketmine\Server;
use pocketmine\Player;
use pocketmine\level\format\Chunk;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\IntTag;
use Ad5001\BetterBlocks\CustomBlockData;
class VacuumTile extends CustomBlockData {
public function __construct(Chunk $chunk, CompoundTag $nbt){
if(!isset($nbt->AttractRadius)) $nbt->AttractRadius = new IntTag("AttractRadius", 5);
parent::__construct($chunk, $nbt);
}
/*
Sets the attracting radius of the vacuum hopper
@param $radius int
*/
public function setAttractRadius(int $radius) {
$this->namedtag->AttractRadius->setValue($radius);
return $this;
}
/*
Get the radius of attractivity of the Vacuum Hopper.
@return int
*/
public function getAttractRadius() : int {
return (int) $this->namedtag->AttractRadius->getValue();
}
/*
Attracts all blocks in the defined radius.
*/
public function attract() {
foreach ($this->getLevel()->getEntities() as $et) {
if($et->x < $this->x + $this->namedtag->AttractRadius->getValue() && $et->x > $this->x - $this->namedtag->AttractRadius->getValue() &&
$et->y < $this->y + $this->namedtag->AttractRadius->getValue() && $et->y > $this->y - $this->namedtag->AttractRadius->getValue() &&
$et->z < $this->z + $this->namedtag->AttractRadius->getValue() && $et->z > $this->z - $this->namedtag->AttractRadius->getValue()) {
$et->teleport(new Vector3($this->x, $this->y + 1, $this->z));
}
}
}
}

View file

@ -28,11 +28,11 @@ use Ad5001\BetterBlocks\CustomBlockData\GraveTile;
use Ad5001\BetterBlocks\CustomBlockData\RedstonePoweringTile;
use Ad5001\BetterBlocks\CustomBlockData\SoundHolderTile;
use Ad5001\BetterBlocks\CustomBlockData\StickTile;
use Ad5001\BetterBlocks\CustomBlockData\VacuumTile;
use Ad5001\BetterBlocks\tasks\AttractTask;
use Ad5001\BetterBlocks\tasks\BlockRegenerateTask;
use Ad5001\BetterBlocks\tasks\Drop2CraftTask;
use Ad5001\BetterBlocks\tasks\SetVacuumTask;
class Main extends PluginBase implements Listener {
@ -51,7 +51,7 @@ class Main extends PluginBase implements Listener {
$this->getServer()->getCraftingManager()->registerRecipe((new ShapedRecipe($ssb, 3, 3))->addIngredient(1, 1, $ep)->addIngredient(1, 2, Item::get(165, 0)));
// Vacuum Hopper
$vh = Item::get(410, 0);
$vh->setNamedTag(NBT::parseJSON('{"isVacuum":"true"}'));
$vh->setNamedTag(NBT::parseJSON('{"isVacuum":"true"'));
$vh->setCustomName("§rVacuum Item Hopper");
$this->getServer()->getCraftingManager()->registerRecipe((new ShapedRecipe($vh, 3, 3))->addIngredient(1, 0, $ep)->addIngredient(0, 0, Item::get(410, 0))->addIngredient(0, 1, Item::get(49, 0)));
// Trappers (used to create trap blocks)
@ -93,7 +93,10 @@ class Main extends PluginBase implements Listener {
Tile::registerTile(RedstonePoweringTile::class);
Tile::registerTile(SoundHolderTile::class);
Tile::registerTile(StickTile::class);
Tile::registerTile(VacuumTile::class);
// Launch tasks
$this->getServer()->getScheduler()->scheduleRepeatingTask(new AttractTask($this), 5);
$this->getServer()->getScheduler()->scheduleRepeatingTask(new Drop2CraftTask($this), 5);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->saveDefaultConfig();
@ -110,11 +113,12 @@ class Main extends PluginBase implements Listener {
if(isset($event->getItem()->getNamedTag()->isStickable) && $event->getItem()->getNamedTag()->isStickable->getValue() == "true") {
Tile::createTile("StickTile", $event->getBlock()->getLevel()->getChunk($event->getBlock()->x >> 4, $event->getBlock()->z >> 4), NBT::parseJSON(json_encode([$event->getBlock()->x, $event->getBlock()->y, $event->getBlock()->z], JSON_FORCE_OBJECT)));
} elseif(isset($event->getItem()->getNamedTag()->isVacuum) && $event->getItem()->getNamedTag()->isVacuum->getValue() == "true") {
Tile::createTile("VacuumTile", $event->getBlock()->getLevel()->getChunk($event->getBlock()->x >> 4, $event->getBlock()->z >> 4), NBT::parseJSON(json_encode([$event->getBlock()->x, $event->getBlock()->y, $event->getBlock()->z], JSON_FORCE_OBJECT)));
$this->getServer()->getScheduler()->scheduleRepeatingTask(new SetVacuumTask($this, $event->getBlock()), 1); // Tile gets created after the event so delaying it one 1 tick.
} elseif(isset($event->getItem()->getNamedTag()->isTrapper) && $event->getItem()->getNamedTag()->isTrapper->getValue() == "true") {
Tile::createTile("TrapTile", $event->getBlock()->getLevel()->getChunk($event->getBlock()->x >> 4, $event->getBlock()->z >> 4), NBT::parseJSON(json_encode([$event->getBlock()->x, $event->getBlock()->y - 1, $event->getBlock()->z], JSON_FORCE_OBJECT)));
$this->getServer()->getScheduler()->scheduleDelayedTask(new BlockRegenerateTask($this, Block::get(0, 0), $event->getBlock()->getLevel()), 30); // Clears the lever
}
$this->getLogger()->debug("Created tile " . json_encode($event->getBlock()->getLevel()->getTile($event->getBlock())));
}
@ -125,32 +129,41 @@ class Main extends PluginBase implements Listener {
public function onBlockBreak(BlockBreakEvent $event) {
//1
$block = $event->getBlock();
if($event->getBlock()->getLevel()->getTile($block) instanceof CustomBlockData && !$event->isCancelled()) {
switch(substr(get_class($event->getBlock()->getLevel()->getTile($block)), 36)) {
case "GraveTile":
$event->getBlock()->getLevel()->getTile($block)->drop();
if($event->getBlock()->getLevel()->getTile($event->getBlock()) instanceof CustomBlockData && !$event->isCancelled()) {
switch(get_class($event->getBlock()->getLevel()->getTile($event->getBlock()))) {
case "Ad5001\\BetterBlocks\\CustomBlockData\\GraveTile":
$event->getBlock()->getLevel()->getTile($event->getBlock())->drop();
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "RedstonePoweringTile":
case "Ad5001\\BetterBlocks\\CustomBlockData\\RedstonePoweringTile":
$redstoneblock = Item::get(152, 0);
$event->setDrops(array_merge([$redstoneblock], $event->getDrops()));
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "SoundHolderTile":
case "Ad5001\\BetterBlocks\\CustomBlockData\\SoundHolderTile":
$noteblock = Item::get(25, 0);
$event->setDrops(array_merge([$noteblock], $event->getDrops()));
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "StickTile":
case "Ad5001\\BetterBlocks\\CustomBlockData\\StickTile":
$ssb = Item::get(165, 0);
$ssb->setCustomName("§rSticky Slime Block");
$ssb->setNamedTag(NBT::parseJSON('{"isStickable":"true"}'));
$event->setDrops([$ssb]);
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "TrapTile":
case "Ad5001\\BetterBlocks\\CustomBlockData\\TrapTile":
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "VacuumTile":
case "Ad5001\\BetterBlocks\\CustomBlockData\\VacuumTile":
$vh = Item::get(410, 0);
$vh->setNamedTag(NBT::parseJSON('{"isVacuum":"true"}'));
$vh->setCustomName("§rVacuum Item Hopper");
$event->setDrops([$vh]);
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
case "Ad5001\\BetterBlocks\\CustomBlockData\\FallableTile":
$event->getBlock()->getLevel()->removeTile($event->getBlock()->getLevel()->getTile($event->getBlock()));
break;
}
}
@ -163,6 +176,7 @@ class Main extends PluginBase implements Listener {
@param $event \pocketmine\event\player\PlayerInteractEvent
*/
public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {
$this->getLogger()->info(get_class($event->getBlock()->getLevel()->getTile($event->getBlock())));
if(isset($event->getItem()->getNamedTag()->isHammer) && $event->getItem()->getNamedTag()->isHammer == "true" && !($event->getBlock() instanceof \pocketmine\block\Fallable) && !($event->getBlock()->getLevel()->getTile($event->getBlock()) instanceof Tile)) {
Tile::createTile("FallableTile", $event->getBlock()->getLevel()->getChunk($event->getBlock()->x >> 4, $event->getBlock()->z >> 4), NBT::parseJSON('{"x":"'.$event->getBlock()->x.'","y":"' .$event->getBlock()->y.'","z":"'. $event->getBlock()->z . '"}'));
$event->getPlayer()->sendPopup("This block seems now unstable... You shouldn't walk on it...");
@ -218,9 +232,9 @@ class Main extends PluginBase implements Listener {
// 1)
if($tileUnder instanceof TrapTile) {
$block = clone $event->getPlayer()->getLevel()->getBlock($v3Under);
$b = clone $event->getPlayer()->getLevel()->getBlock($v3Under);
$event->getPlayer()->getLevel()->setBlock($v3Under, Block::get(0, 0));
$this->getServer()->getScheduler()->scheduleDelayedTask(new BlockRegenerateTask($this, $block, $event->getPlayer()->getLevel()), 30);
$this->getServer()->getScheduler()->scheduleDelayedTask(new BlockRegenerateTask($this, $b, $event->getPlayer()->getLevel()), 30);
}
// 2)
if($tileUnder instanceof StickTile || $tileUpper instanceof StickTile) {

View file

@ -17,7 +17,7 @@ use pocketmine\Player;
use Ad5001\BetterBlocks\CustomBlockData;
use Ad5001\BetterBlocks\CustomBlockData\VacuumTile;
use Ad5001\BetterBlocks\Main;
@ -42,8 +42,16 @@ class AttractTask extends PluginTask {
public function onRun($tick) {
foreach($this->server->getLevels() as $level) {
foreach($level->getTiles() as $tile) {
if($tile instanceof VacuumTile) {
if(get_class($tile) == "pocketmine\\tile\\Hopper" && isset($tile->namedtag->isVacuum) && $tile->namedtag->isVacuum->getValue() == 1) {
for($i = 0; $i < 10; $i++) { // Particles
$level->addParticle(new \pocketmine\level\particle\PortalParticle(new \pocketmine\math\Vector3($tile->x + rand(-70, 170) / 100, $tile->y + rand(-70, 130) / 100, $tile->z + rand(-70, 170) / 100)));
}
foreach($level->getEntities() as $et) {
if($et instanceof \pocketmine\entity\Item && $et->distance($tile) < 3) {
$tile->getInventory()->addItem($et->getItem());
$et->close();
}
}
}
}
}

View file

@ -27,6 +27,8 @@ use Ad5001\BetterBlocks\CustomBlockData\FallableTile;
use Ad5001\BetterBlocks\CustomBlockData\RedstonePoweringTile;
use Ad5001\BetterBlocks\CustomBlockData\SoundHolderTile;
use Ad5001\BetterBlocks\Main;
class Drop2CraftTask extends PluginTask {

View file

@ -0,0 +1,58 @@
<?php
# ____ _ _ ____ _ _
# | __ ) ___ | |_ | |_ ___ _ __ | __ ) | | ___ ___ | | __ ___
# | _ \ / _ \ | __| | __| / _ \ | '__| | _ \ | | / _ \ / __| | |/ / / __|
# | |_) | | __/ | |_ | |_ | __/ | | | |_) | | | | (_) | | (__ | < \__ \
# |____/ \___| \__| \__| \___| |_| |____/ |_| \___/ \___| |_|\_\ |___/
#
# Extends your Minecraft PE blocks palette ! For PocketMine.
namespace Ad5001\BetterBlocks\tasks;
use pocketmine\Server;
use pocketmine\scheduler\PluginTask;
use pocketmine\Player;
use pocketmine\nbt\tag\ShortTag;
use Ad5001\BetterBlocks\Main;
use Ad5001\BetterBlocks\CustomBlockData;
class SetVacuumTask extends PluginTask {
protected $block;
protected $main;
protected $server;
public function __construct(Main $main, \pocketmine\block\Block $block) {
parent::__construct($main);
$this->main = $main;
$this->block = $block;
$this->server = $main->getServer();
}
public function onRun($tick) {
$tile = $this->block->getLevel()->getTile($this->block);
if(get_class($tile) == "pocketmine\\tile\\Hopper") { // For software like pocketmine that haven't implemented Hoppers.
$tile->namedtag->isVacuum = new ShortTag("isVacuum", 1);
}
}
}