Skip to content

Commit

Permalink
Experimental rewrite of Clipboards - copy & paste
Browse files Browse the repository at this point in the history
Not yet tested!
  • Loading branch information
inxomnyaa committed Feb 1, 2020
1 parent 43704dc commit f9bf5c1
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 377 deletions.
17 changes: 10 additions & 7 deletions src/xenialdan/MagicWE2/API.php
Expand Up @@ -20,7 +20,7 @@
use pocketmine\utils\TextFormat as TF;
use RuntimeException;
use xenialdan\MagicWE2\clipboard\Clipboard;
use xenialdan\MagicWE2\clipboard\CopyClipboard;
use xenialdan\MagicWE2\clipboard\SingleClipboard;
use xenialdan\MagicWE2\exception\CalculationException;
use xenialdan\MagicWE2\exception\LimitExceededException;
use xenialdan\MagicWE2\selection\Selection;
Expand All @@ -30,10 +30,10 @@
use xenialdan\MagicWE2\task\action\SetBiomeAction;
use xenialdan\MagicWE2\task\action\TaskAction;
use xenialdan\MagicWE2\task\AsyncActionTask;
use xenialdan\MagicWE2\task\AsyncClipboardTask;
use xenialdan\MagicWE2\task\AsyncCopyTask;
use xenialdan\MagicWE2\task\AsyncCountTask;
use xenialdan\MagicWE2\task\AsyncFillTask;
use xenialdan\MagicWE2\task\AsyncPasteTask;
use xenialdan\MagicWE2\task\AsyncReplaceTask;
use xenialdan\MagicWE2\tool\Brush;

Expand Down Expand Up @@ -158,24 +158,27 @@ public static function copyAsync(Selection $selection, Session $session, int $fl

/**
* TODO: flag parsing, Position to paste at
* @param CopyClipboard $clipboard TODO should this be Clipboard?
* @param SingleClipboard $clipboard TODO should this be Clipboard?
* @param Session $session
* @param Position $target
* @param int $flags
* @return bool
*/
public static function pasteAsync(CopyClipboard $clipboard, Session $session, Position $target, int $flags = self::FLAG_BASE)
public static function pasteAsync(SingleClipboard $clipboard, Session $session, Position $target, int $flags = self::FLAG_BASE)
{
#return false;
try {
$limit = Loader::getInstance()->getConfig()->get("limit", -1);
if ($clipboard->getShape()->getTotalCount() > $limit && $limit !== -1) {
if ($clipboard->getTotalCount() > $limit && $limit !== -1) {
throw new LimitExceededException($session->getLanguage()->translateString('error.limitexceeded'));
}
$c = $clipboard->getCenter();
#$c = $clipboard->getCenter();
#$clipboard->setCenter($target->asVector3());//TODO check
if ($session instanceof UserSession) $session->getBossBar()->showTo([$session->getPlayer()]);
Server::getInstance()->getAsyncPool()->submitTask(new AsyncClipboardTask($session->getUUID(), $clipboard, $clipboard->getTouchedChunks($c), AsyncClipboardTask::TYPE_PASTE, $flags));
$shape = $clipboard->selection->getShape();
$shape->setPasteVector($target->asVector3());
$touchedChunks = $shape->getTouchedChunks($target->getLevel());//TODO check if this is an ugly hack
Server::getInstance()->getAsyncPool()->submitTask(new AsyncPasteTask($session->getUUID(), $clipboard->selection, $touchedChunks, $clipboard, $flags));
} catch (Exception $e) {
$session->sendMessage($e->getMessage());
Loader::getInstance()->getLogger()->logException($e);
Expand Down
2 changes: 0 additions & 2 deletions src/xenialdan/MagicWE2/clipboard/Clipboard.php
Expand Up @@ -25,8 +25,6 @@ abstract class Clipboard implements Serializable
const FLIP_NORTH = 0x03;
const FLIP_SOUTH = 0x03;

/** @var Chunk[] */
public $chunks = [];
/** @var int|null */
public $levelid;
/** @var string */
Expand Down
190 changes: 0 additions & 190 deletions src/xenialdan/MagicWE2/clipboard/CopyClipboard.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/xenialdan/MagicWE2/clipboard/RevertClipboard.php
Expand Up @@ -10,6 +10,8 @@

class RevertClipboard extends Clipboard
{
/** @var Chunk[] */
public $chunks = [];
/** @var Block[] */
public $blocksAfter;

Expand Down
75 changes: 75 additions & 0 deletions src/xenialdan/MagicWE2/clipboard/SingleClipboard.php
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace xenialdan\MagicWE2\clipboard;

use Generator;
use pocketmine\level\Level;
use xenialdan\MagicWE2\helper\BlockEntry;
use xenialdan\MagicWE2\selection\Selection;

class SingleClipboard extends Clipboard
{
/** @var BlockEntry[] */
private $entries = [];
/** @var Selection */
public $selection;

public function addEntry(int $x, int $y, int $z, BlockEntry $entry): void
{
$this->entries[Level::blockHash($x, $y, $z)] = $entry;
}

/**
* @param int $x
* @param int $y
* @param int $z
* @return Generator|BlockEntry[]
*/
public function iterateEntries(&$x, &$y, &$z): Generator
{
foreach ($this->entries as $hash => $entry) {
Level::getBlockXYZ($hash, $x, $y, $z);
yield $entry;
}
}

public function getTotalCount(): int
{
return count($this->entries);
}

/**
* String representation of object
* @link https://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1
*/
public function serialize()
{
// TODO: Implement serialize() method.
return serialize([
$this->entries,
$this->selection
]);
}

/**
* Constructs the object
* @link https://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
* @since 5.1
*/
public function unserialize($serialized)
{
// TODO: Implement unserialize() method.
[
$this->entries,
$this->selection
] = unserialize($serialized);
}
}
4 changes: 2 additions & 2 deletions src/xenialdan/MagicWE2/commands/clipboard/PasteCommand.php
Expand Up @@ -15,7 +15,7 @@
use pocketmine\Player;
use pocketmine\utils\TextFormat as TF;
use xenialdan\MagicWE2\API;
use xenialdan\MagicWE2\clipboard\CopyClipboard;
use xenialdan\MagicWE2\clipboard\SingleClipboard;
use xenialdan\MagicWE2\exception\SessionException;
use xenialdan\MagicWE2\helper\SessionHelper;
use xenialdan\MagicWE2\Loader;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo
if (is_null($clipboard)) {
throw new Exception($lang->translateString('error.noclipboard'));
}
if (!$clipboard instanceof CopyClipboard) {//TODO check if i want to pass ANY clipboard instead
if (!$clipboard instanceof SingleClipboard) {//TODO check if i want to pass ANY clipboard instead
throw new Exception($lang->translateString('error.noclipboard'));
}
/*if (!API::hasFlag(API::flagParser(explode(" ", strval($args["flags"]))), API::FLAG_POSITION_RELATIVE)) {
Expand Down

0 comments on commit f9bf5c1

Please sign in to comment.