Skip to content

Commit

Permalink
Support Pistons
Browse files Browse the repository at this point in the history
  • Loading branch information
tedo0627 committed Mar 29, 2019
1 parent bffb2d6 commit cd389f2
Show file tree
Hide file tree
Showing 13 changed files with 703 additions and 47 deletions.
44 changes: 24 additions & 20 deletions RedstoneCircuit/src/redstone/Main.php
Expand Up @@ -20,6 +20,7 @@
use redstone\blockEntities\BlockEntityDispenser;
use redstone\blockEntities\BlockEntityDropper;
use redstone\blockEntities\BlockEntityHopper;
use redstone\blockEntities\BlockEntityMovingBlock;
use redstone\blockEntities\BlockEntityNoteBlock;
use redstone\blockEntities\BlockEntityObserver;
use redstone\blockEntities\BlockEntityPistonArm;
Expand All @@ -39,9 +40,12 @@
use redstone\blocks\BlockIronDoor;
use redstone\blocks\BlockIronTrapdoor;
use redstone\blocks\BlockLever;
use redstone\blocks\BlockMoving;
use redstone\blocks\BlockNote;
use redstone\blocks\BlockObserver;
use redstone\blocks\BlockPiston;
use redstone\blocks\BlockPistonarmcollision;
use redstone\blocks\BlockPistonSticky;
use redstone\blocks\BlockPressurePlateStone;
use redstone\blocks\BlockPressurePlateWooden;
use redstone\blocks\BlockRedstone;
Expand All @@ -54,6 +58,7 @@
use redstone\blocks\BlockRedstoneTorch;
use redstone\blocks\BlockRedstoneTorchUnlit;
use redstone\blocks\BlockRedstoneWire;
use redstone\blocks\BlockSlime;
use redstone\blocks\BlockTNT;
use redstone\blocks\BlockTrapdoor;
use redstone\blocks\BlockTrappedChest;
Expand All @@ -67,6 +72,7 @@
use redstone\listeners\ScheduledBlockUpdateListener;

use redstone\utils\CustomConfig;
use redstone\utils\GlobalBlockPalette;
use redstone\utils\ScheduledBlockUpdateLoader;

class Main extends PluginBase {
Expand All @@ -81,18 +87,21 @@ public static function getInstance() : Main {

private $scheduledBlockUpdateLoader;

private $palette;

public function onEnable() {
Main::$instance = $this;

$this->config = new CustomConfig();

$this->scheduledBlockUpdateLoader = new ScheduledBlockUpdateLoader();

$this->palette = new GlobalBlockPalette();

$this->getServer()->getPluginManager()->registerEvents(new EventListener(), $this);

$this->initBlocks();
$this->initBlockEntities();
$this->initItems();
$this->initCreativeItem();
}

Expand All @@ -114,6 +123,10 @@ public function getScheduledBlockUpdateLoader() : ScheduledBlockUpdateLoader {
return $this->scheduledBlockUpdateLoader;
}

public function getGlobalBlockPalette() : GlobalBlockPalette {
return $this->palette;
}

private function initBlocks() : void {
BlockFactory::registerBlock(new BlockRedstoneWire(), true);

Expand Down Expand Up @@ -160,7 +173,10 @@ private function initBlocks() : void {

BlockFactory::registerBlock(new BlockHopper(), true);

//BlockFactory::registerBlock(new BlockPiston(), true);
BlockFactory::registerBlock(new BlockPiston(), true);
BlockFactory::registerBlock(new BlockPistonarmcollision(), true);
BlockFactory::registerBlock(new BlockPistonSticky(), true);
BlockFactory::registerBlock(new BlockMoving(), true);

BlockFactory::registerBlock(new BlockCommand(), true);
BlockFactory::registerBlock(new BlockCommandRepeating(), true);
Expand All @@ -186,6 +202,8 @@ private function initBlocks() : void {
BlockFactory::registerBlock(new BlockFenceGate(Block::JUNGLE_FENCE_GATE, 0, "Jungle Fence Gate"), true);
BlockFactory::registerBlock(new BlockFenceGate(Block::DARK_OAK_FENCE_GATE, 0, "Dark Oak Fence Gate"), true);
BlockFactory::registerBlock(new BlockFenceGate(Block::ACACIA_FENCE_GATE, 0, "Acacia Fence Gate"), true);

BlockFactory::registerBlock(new BlockSlime(), true);
}

private function initBlockEntities() : void {
Expand All @@ -195,30 +213,16 @@ private function initBlockEntities() : void {
Tile::registerTile(BlockEntityDropper::class, ["Dropper", "minecraft:dropper"]);
Tile::registerTile(BlockEntityDispenser::class, ["Dispenser", "minecraft:dispenser"]);
Tile::registerTile(BlockEntityHopper::class, ["Hopper", "minecraft:hopper"]);
Tile::registerTile(BlockEntityMovingBlock::class, ["Movingblock", "minecraft:movingblock"]);
Tile::registerTile(BlockEntityNoteBlock::class, ["NoteBlock", "minecraft:note_block"]);
Tile::registerTile(BlockEntityObserver::class, ["Observer", "minecraft:observer"]);
Tile::registerTile(BlockEntityPistonArm::class, ["PistonArm", "minecraft:piston_arm"]);
Tile::registerTile(BlockEntityRedstoneComparator::class, ["Comparator", "minecraft:comparator"]);
}

private function initItems() : void {
ItemFactory::registerItem(new ItemBlock(Block::UNPOWERED_REPEATER, 0, Item::REPEATER), true);
ItemFactory::registerItem(new ItemBlock(Block::UNPOWERED_COMPARATOR, 0, Item::COMPARATOR), true);
ItemFactory::registerItem(new ItemBlock(Block::COMMAND_BLOCK, 0, Item::COMMAND_BLOCK), true);
ItemFactory::registerItem(new ItemBlock(Block::DROPPER, 0, Item::DROPPER), true);
ItemFactory::registerItem(new ItemBlock(Block::DISPENSER, 0, Item::DISPENSER), true);
ItemFactory::registerItem(new ItemBlock(Block::OBSERVER, 0, Item::OBSERVER), true);
ItemFactory::registerItem(new ItemBlock(Block::PISTON, 0, Item::PISTON), true);
}

private function initCreativeItem() : void {
Item::addCreativeItem(Item::get(Item::OBSERVER));
Item::addCreativeItem(Item::get(Item::REPEATER));
Item::addCreativeItem(Item::get(Item::COMPARATOR));
Item::addCreativeItem(Item::get(Item::HOPPER));
Item::addCreativeItem(Item::get(Item::COMMAND_BLOCK));
Item::addCreativeItem(Item::get(Item::DROPPER));
Item::addCreativeItem(Item::get(Item::DISPENSER));
//Item::addCreativeItem(Item::get(Item::PISTON));
Item::initCreativeItems();
Item::addCreativeItem(Item::get(Item::PISTON));
Item::addCreativeItem(Item::get(Item::STICKY_PISTON));
}
}
120 changes: 120 additions & 0 deletions RedstoneCircuit/src/redstone/blockEntities/BlockEntityMovingBlock.php
@@ -0,0 +1,120 @@
<?php

namespace redstone\blockEntities;

use pocketmine\block\Block;

use pocketmine\math\Vector3;

use pocketmine\nbt\tag\CompoundTag;

use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;


use redstone\Main;

class BlockEntityMovingBlock extends Spawnable {

private $piston;

private $movingBlock;
private $movingBlockExtra;

private $movingEntity;

protected function readSaveData(CompoundTag $nbt) : void {
$this->piston = new Vector3(0, -1, 0);
if ($nbt->hasTag("pistonPosX")) {
$this->piston->x = $nbt->getInt("pistonPosX");
}
if ($nbt->hasTag("pistonPosY")) {
$this->piston->x = $nbt->getInt("pistonPosY");
}
if ($nbt->hasTag("pistonPosZ")) {
$this->piston->x = $nbt->getInt("pistonPosZ");
}

if ($nbt->hasTag("movingBlock")) {
$this->movingBlock = $nbt->getTag("movingBlock");
} else {
$tag = new CompoundTag("movingBlock");
$tag->setShort("val", 0);
$tag->setString("name", "minecraft:air");
$this->movingBlock = $tag;
}
if ($nbt->hasTag("movingBlockExtra")) {
$this->movingBlockExtra = $nbt->getTag("movingBlockExtra");
} else {
$tag = new CompoundTag("movingBlockExtra");
$tag->setShort("val", 0);
$tag->setString("name", "minecraft:air");
$this->movingBlockExtra = $tag;
}
if ($nbt->hasTag("movingEntity")) {
$this->movingEntity = $nbt->getCompoundTag("movingEntity");
}
}

protected function writeSaveData(CompoundTag $nbt) : void {
$nbt->setInt("pistonPosX", $this->piston->x);
$nbt->setInt("pistonPosY", $this->piston->y);
$nbt->setInt("pistonPosZ", $this->piston->z);

$nbt->setTag($this->movingBlock);
$nbt->setTag($this->movingBlockExtra);
}

public function getName() : string{
return "movingBlock";
}

protected function addAdditionalSpawnData(CompoundTag $nbt) : void {
$nbt->setInt("pistonPosX", $this->piston->x);
$nbt->setInt("pistonPosY", $this->piston->y);
$nbt->setInt("pistonPosZ", $this->piston->z);

$nbt->setTag($this->movingBlock);
$nbt->setTag($this->movingBlockExtra);

if ($this->movingEntity != null) {
$nbt->setTag($this->movingEntity);
}
}

public function setData(Block $piston, Block $sourceBlock, ?Tile $tile) : void {
$this->piston->x = $piston->x;
$this->piston->y = $piston->y;
$this->piston->z = $piston->z;

$tag = new CompoundTag("movingBlock");
$tag->setShort("val", $sourceBlock->getDamage());
$tag->setString("name", Main::getInstance()->getGlobalBlockPalette()->getNameAt($sourceBlock));
$this->movingBlock = $tag;

if ($tile != null) {
$tag = $tile->saveNBT();
$tag->setName("movingEntity");
$tag->setInt("x", $this->x);
$tag->setInt("y", $this->y);
$tag->setInt("z", $this->z);
$this->movingEntity = $tag;
}

$this->onChanged();
}

public function setBlock() : void {
$level = $this->getLevel();
if ($this->movingEntity != null) {
$tag = $this->movingEntity;
Tile::createTile($tag->getString("id"), $level, $tag);
}

$tag = $this->movingBlock;
$name = $tag->getString("name");
$damage = $tag->getShort("val");
$block = Main::getInstance()->getGlobalBlockPalette()->getBlock($name, $damage);
$level->setBlock($this, $block);
}
}
Expand Up @@ -9,7 +9,7 @@
class BlockEntityObserver extends Tile {

private $runtimeId;

protected function readSaveData(CompoundTag $nbt) : void {
if ($nbt->hasTag("runtimeId")) {
$this->runtimeId = $nbt->getInt("runtimeId");
Expand Down

0 comments on commit cd389f2

Please sign in to comment.