Create floating texts for your PocketMine server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.php 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. # FloatingTexts
  3. # A new production from Ad5001 generated using ImagicalPlugCreator by Ad5001 (C) 2017
  4. namespace Ad5001\FloatingTexts;
  5. use pocketmine\Server;
  6. use pocketmine\Player;
  7. use pocketmine\plugin\PluginBase;
  8. use pocketmine\entity\Entity;
  9. class Main extends PluginBase implements \pocketmine\event\Listener {
  10. /*
  11. Called when the plugin enables
  12. */
  13. public function onEnable() {
  14. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  15. $this->sessions = [];
  16. $this->getServer()->getScheduler()->scheduleRepeatingTask(new SetNameTagVisibleTask($this),10);
  17. }
  18. /*
  19. Called when one of the defined commands of the plugin has been called
  20. @param $sender \pocketmine\command\CommandSender
  21. @param $cmd \pocketmine\command\Command
  22. @param $label mixed
  23. @param $args array
  24. return bool
  25. */
  26. public function onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $cmd, string $label, array $args): bool {
  27. switch($cmd->getName()) {
  28. case "createfloat":
  29. if($sender instanceof Player) {
  30. if(isset($args[0])) {
  31. $text = implode(" ", $args);
  32. $text = str_ireplace("\\n", "\n", $text);
  33. $this->sessions[$sender->getName()] = $text;
  34. $sender->sendMessage("Tap an entity !");
  35. }
  36. }
  37. return true;
  38. break;
  39. }
  40. }
  41. /*
  42. WHen a player hits an entity with a session, set his nametag
  43. @param $event \pocketmine\event\entity\EntityDamageEvent
  44. */
  45. public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $event) {
  46. if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) {
  47. if($event->getDamager() instanceof Player &&
  48. isset($this->sessions[$event->getDamager()->getName()])) {
  49. $event->getEntity()->addEffect(\pocketmine\entity\Effect::getEffectByName("invisibility")->setAmbient(true)->setVisible(false));
  50. $event->getEntity()->setNameTag($this->sessions[$event->getDamager()->getName()]);
  51. $event->getEntity()->setNameTagAlwaysVisible(true);
  52. $event->getEntity()->setNameTagVisible(true);
  53. $event->getEntity()->setImmobile(true);
  54. $event->getEntity()->namedtag->isUsedToFloat = new \pocketmine\nbt\tag\StringTag("isUsedToFloat", "true");
  55. $event->getEntity()->setNameTag($this->sessions[$event->getDamager()->getName()]);
  56. $event->setCancelled();
  57. unset($this->sessions[$event->getDamager()->getName()]);
  58. } elseif(isset($event->getEntity()->namedtag->isUsedToFloat)) {
  59. if(!($event->getDamager() instanceof Player && $event->getDamager()->isOp())) $event->setCancelled();
  60. }
  61. } elseif(isset($event->getEntity()->namedtag->isUsedToFloat)) {
  62. $event->setCancelled();
  63. }
  64. }
  65. /*
  66. Checks when a level loads with floats to regive them the flags and effects.
  67. @param $event \pocketmine\event\level\LevelLoadEvent
  68. */
  69. public function onLevelLoad(\pocketmine\event\level\LevelLoadEvent $event) {
  70. foreach ($event->getLevel()->getEntities() as $et) {
  71. if(isset($et->namedtag->isUsedToFloat)) {
  72. $et->addEffect(\pocketmine\entity\Effect::getEffectByName("invisibility")->setDuration(99999)->setVisible(false));
  73. $et->setNameTagAlwaysVisible(true);
  74. $et->setNameTagVisible(true);
  75. $et->setImmobile(true);
  76. }
  77. }
  78. }
  79. }