From 42fb1d1fef47ae7cc55794764aa4057364348616 Mon Sep 17 00:00:00 2001 From: "Tim (robske_110)" Date: Wed, 19 Jul 2017 16:22:04 +0200 Subject: [PATCH] Improve usability of Sign API (#1202) * Improve usability of Sign API * PHP * Throw exceptions if out of range * Fix phpdocs * Formatting, additional checks in the SignChangeEvent. * Blame php storm * require line count to always be 4 * Adjust exception message --- .../event/block/SignChangeEvent.php | 16 +++++++++++ src/pocketmine/tile/Sign.php | 27 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/pocketmine/event/block/SignChangeEvent.php b/src/pocketmine/event/block/SignChangeEvent.php index af8186621..85616fb06 100644 --- a/src/pocketmine/event/block/SignChangeEvent.php +++ b/src/pocketmine/event/block/SignChangeEvent.php @@ -69,14 +69,30 @@ class SignChangeEvent extends BlockEvent implements Cancellable{ * @return string */ public function getLine($index){ + if($index < 0 or $index > 3){ + throw new \InvalidArgumentException("Index must be in the range 0-3!"); + } return $this->lines[$index]; } + /** + * @param string[] $lines + */ + public function setLines(array $lines){ + if(count($lines) !== 4){ + throw new \InvalidArgumentException("Array size must be 4!"); + } + $this->lines = $lines; + } + /** * @param int $index 0-3 * @param string $line */ public function setLine($index, $line){ + if($index < 0 or $index > 3){ + throw new \InvalidArgumentException("Index must be in the range 0-3!"); + } $this->lines[$index] = $line; } } \ No newline at end of file diff --git a/src/pocketmine/tile/Sign.php b/src/pocketmine/tile/Sign.php index 4aeaec6f1..c573cc1a6 100644 --- a/src/pocketmine/tile/Sign.php +++ b/src/pocketmine/tile/Sign.php @@ -64,6 +64,33 @@ class Sign extends Spawnable{ return true; } + + /** + * @param int $index 0-3 + * @param string $line + * @param bool $update + */ + public function setLine(int $index, string $line, bool $update = true){ + if($index < 0 or $index > 3){ + throw new \InvalidArgumentException("Index must be in the range 0-3!"); + } + $this->namedtag["Text" . ($index + 1)] = $line; + if($update){ + $this->onChanged(); + } + } + + /** + * @param int $index 0-3 + * + * @return string + */ + public function getLine(int $index) : string{ + if($index < 0 or $index > 3){ + throw new \InvalidArgumentException("Index must be in the range 0-3!"); + } + return (string) $this->namedtag["Text" . ($index + 1)]; + } public function getText(){ return [