Проблема в том что мне нужен спавн текста только в одном мире, и чтобы в другом мире он не появлялся, но FloatingText не привязывается к мирам, и я пытался удалять и создавать текста при телепортациях по мирам, даже использовал ГПТ, но не получилось ничего. Текст то появляется при переходе в мир “spawn” но когда телепортируюсь обратно в мир “world” то текст никуда не исчезает.
Код
<?php
namespace ItemText;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\Player;
use pocketmine\utils\Config;
use pocketmine\math\Vector3;
use pocketmine\level\particle\FloatingTextParticle;
use pocketmine\network\mcpe\protocol\AddItemActorPacket;
use pocketmine\network\mcpe\protocol\RemoveActorPacket;
use pocketmine\entity\Entity;
use pocketmine\item\Item;
use pocketmine\scheduler\Task;
class Main extends PluginBase implements Listener {
public Config $config;
public array $itemEntities = [];
public function onEnable(): void {
if (!$this->getServer()->isLevelLoaded("spawn")) {
$this->getServer()->loadLevel("spawn");
}
@mkdir($this->getDataFolder());
$this->config = new Config(
$this->getDataFolder() . "config.yml",
Config::YAML,
[
"Texts" => [
[
"Coordinates" => "0 65 0",
"Text" => "Привет\n{player}",
"ItemId" => 266
]
]
]
);
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
/* ================= ПОКАЗ ================= */
public function show(Player $player): void {
$spawn = $this->getServer()->getLevelByName("spawn");
if ($spawn === null || $player->getLevel() !== $spawn) {
return;
}
$this->hideFromSpawn($player);
foreach ($this->config->get("Texts") as $data) {
if (!isset($data["Coordinates"], $data["Text"])) {
continue;
}
$coords = explode(" ", $data["Coordinates"]);
if (count($coords) !== 3) {
continue;
}
$x = (float) $coords[0];
$y = (float) $coords[1];
$z = (float) $coords[2];
$yBase = $y + 1.25;
$lines = preg_split(
"/\\n/",
str_replace('\n', "\n", (string) $data["Text"])
);
$lineCount = count($lines);
$lineHeight = 0.27;
foreach ($lines as $index => $line) {
$line = str_replace("{player}", $player->getName(), $line);
$yPos = $yBase + ($lineCount - $index - 1) * $lineHeight;
$particle = new FloatingTextParticle(
new Vector3($x, $yPos, $z),
"",
$line
);
$spawn->addParticle($particle, [$player]);
}
$runtimeId = Entity::$entityCount++;
$this->itemEntities[$player->getId()][] = [
"id" => $runtimeId
];
$itemId = (int) ($data["ItemId"] ?? 266);
$pk = new AddItemActorPacket();
$pk->entityRuntimeId = $runtimeId;
$pk->entityUniqueId = $runtimeId;
$pk->item = Item::get($itemId, 0, 1);
$pk->position = new Vector3($x, $y - 1, $z);
$pk->motion = new Vector3(0, 0, 0);
$player->dataPacket($pk);
}
}
public function hideFromSpawn(Player $player): void {
$spawn = $this->getServer()->getLevelByName("spawn");
if ($spawn === null) {
return;
}
foreach ($this->config->get("Texts") as $data) {
if (!isset($data["Coordinates"], $data["Text"])) {
continue;
}
$coords = explode(" ", $data["Coordinates"]);
if (count($coords) !== 3) {
continue;
}
$x = (float) $coords[0];
$y = (float) $coords[1];
$z = (float) $coords[2];
$yBase = $y + 1.25;
$lines = preg_split(
"/\\n/",
str_replace('\n', "\n", (string) $data["Text"])
);
$count = count($lines);
$height = 0.27;
for ($i = 0; $i < $count; $i++) {
$yPos = $yBase + ($count - $i - 1) * $height;
$spawn->addParticle(
new FloatingTextParticle(
new Vector3($x, $yPos, $z),
"",
""
),
[$player]
);
}
}
foreach ($this->itemEntities[$player->getId()] ?? [] as $data) {
$pk = new RemoveActorPacket();
$pk->entityUniqueId = $data["id"];
$player->dataPacket($pk);
}
unset($this->itemEntities[$player->getId()]);
}
public function onJoin(PlayerJoinEvent $event): void {
$player = $event->getPlayer();
$this->getScheduler()->scheduleDelayedTask(
new class($this, $player) extends Task {
public Main $plugin;
public Player $player;
public function __construct(Main $plugin, Player $player) {
$this->plugin = $plugin;
$this->player = $player;
}
public function onRun(int $currentTick): void {
if ($this->player->isOnline()) {
$this->plugin->show($this->player);
}
}
},
40
);
}
public function onQuit(PlayerQuitEvent $event): void {
$this->hideFromSpawn($event->getPlayer());
}
public function onTeleport(EntityTeleportEvent $event): void {
$player = $event->getEntity();
if (!$player instanceof Player) {
return;
}
$spawn = $this->getServer()->getLevelByName("spawn");
if ($spawn === null) {
return;
}
if ($event->getFrom()->getLevel() === $spawn) {
$this->getScheduler()->scheduleDelayedTask(
new class($this, $player) extends Task {
private Main $plugin;
private Player $player;
public function __construct(Main $plugin, Player $player) {
$this->plugin = $plugin;
$this->player = $player;
}
public function onRun(int $tick): void {
if ($this->player->isOnline()) {
$this->plugin->hideFromSpawn($this->player);
}
}
},
1
);
}
$this->getScheduler()->scheduleDelayedTask(
new class($this, $player) extends Task {
private Main $plugin;
private Player $player;
public function __construct(Main $plugin, Player $player) {
$this->plugin = $plugin;
$this->player = $player;
}
public function onRun(int $tick): void {
if (!$this->player->isOnline()) {
return;
}
$spawn = $this->plugin->getServer()->getLevelByName("spawn");
if ($spawn !== null && $this->player->getLevel() === $spawn) {
$this->plugin->show($this->player);
}
}
},
1
);
}
}
