PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
SignText.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block\utils;
25
28use function array_fill;
29use function array_pad;
30use function array_slice;
31use function count;
32use function explode;
33use function is_int;
34use function str_contains;
35
37 public const LINE_COUNT = 4;
38
43 private array $lines;
44 private Color $baseColor;
45 private bool $glowing;
46
55 public function __construct(?array $lines = null, ?Color $baseColor = null, bool $glowing = false){
56 $this->lines = array_fill(0, self::LINE_COUNT, "");
57 if($lines !== null){
58 if(count($lines) > self::LINE_COUNT){
59 throw new \InvalidArgumentException("Expected at most 4 lines, got " . count($lines));
60 }
61 foreach($lines as $k => $line){
62 $this->checkLineIndex($k);
63 Utils::checkUTF8($line);
64 if(str_contains($line, "\n")){
65 throw new \InvalidArgumentException("Line must not contain newlines");
66 }
67 //TODO: add length checks
68 $this->lines[$k] = $line;
69 }
70 }
71 $this->baseColor = $baseColor ?? new Color(0, 0, 0);
72 $this->glowing = $glowing;
73 }
74
81 public static function fromBlob(string $blob, ?Color $baseColor = null, bool $glowing = false) : SignText{
82 return new self(array_slice(array_pad(explode("\n", $blob), self::LINE_COUNT, ""), 0, self::LINE_COUNT), $baseColor, $glowing);
83 }
84
91 public function getLines() : array{
92 return $this->lines;
93 }
94
95 private function checkLineIndex(int|string $index) : void{
96 if(!is_int($index)){
97 throw new \InvalidArgumentException("Index must be an integer");
98 }
99 if($index < 0 || $index >= self::LINE_COUNT){
100 throw new \InvalidArgumentException("Line index is out of bounds");
101 }
102 }
103
109 public function getLine(int $index) : string{
110 $this->checkLineIndex($index);
111 return $this->lines[$index];
112 }
113
117 public function getBaseColor() : Color{
118 return $this->baseColor;
119 }
120
125 public function isGlowing() : bool{
126 return $this->glowing;
127 }
128}
__construct(?array $lines=null, ?Color $baseColor=null, bool $glowing=false)
Definition SignText.php:55
static fromBlob(string $blob, ?Color $baseColor=null, bool $glowing=false)
Definition SignText.php:81