From 9d4e564699d2fd51b68b47f6d91c7679464a7ecd Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Mon, 25 Jul 2016 15:20:13 +0300 Subject: [PATCH] Moving forward ! --- LICENSE | 31 ++++++ plugin.yml | 9 ++ resources/config.yml | 18 +++ src/Ad5001/SpectatorPlus/Main.php | 175 ++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 LICENSE create mode 100644 plugin.yml create mode 100644 resources/config.yml create mode 100644 src/Ad5001/SpectatorPlus/Main.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c4b668f --- /dev/null +++ b/LICENSE @@ -0,0 +1,31 @@ +BoxOfDevs General Software License 1.1 +========================================= +This license is designed to be used with any free open source softwares. It can be found @ https://products.boxofdevs.com/. +-------------------------------------------------------------------------------------------------------------------------------------- +The License: refers to the BoxOfDevs International Software License 1.0. +The Software: refers to any works licensed under The License. +The Modification: refers to any modified version of The Software. +The Redistribution: refers to any redistributions of The Software/The Modification. +The User: refers to any user of The Software licensed under The License. +The Author: refers to any developer, organisation, legal owner, or creator of The Software that has been licensed under the The License. +-------------------------------------------------------------------------------------------------------------------------------------- +Section 1 - Allowances: +1. Both The User & The Author Can: +a. Modify private copies of The Software and use in any way they wish. +b. Change which license The Software/The Modification is licensed under. +2. The User Can: +a. Request new features, support, or more rights from The Author. +b. Redistribute The Software/The Modification, ensuring The User gives all appropriate credit to The Author and alerting The Author to The Redistribution. +3. The Author Can: +a. Change or update The Software in any way they wish, ensuring they follow the other terms of The License. +b. Grant extra rights to The User who has requested them. +Section 2 - Restrictions: +1. Both The User & The Author Cannot: +a. Introduce any malicious code into The Software/The Modification. +b. Sell any part of The Software/The Modification for profit. +2. The User Cannot: +a. Hold The Author responsible for any unknown errors. +b. Claim The Software/The Modification/The Redistribution as entirely The User's product. +3. The Author Cannot: +a. Restrict The User in any way not specified by The License. +b. Distribute any form of The Software without proper documentation. \ No newline at end of file diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..9c08920 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,9 @@ +--- +name: SpectatorPlus +author: Ad5001 +version: 1.0 +api: [2.0.0] +main: Ad5001\SpectatorPlus\Main +commands: [] +permissions: [] +... \ No newline at end of file diff --git a/resources/config.yml b/resources/config.yml new file mode 100644 index 0000000..2a01f98 --- /dev/null +++ b/resources/config.yml @@ -0,0 +1,18 @@ +--- +# Welcome to SpectatorPlus config ! +# Here you can configure every name and messages that are managed by this plugin. + +# The message used when teleporting to a player in spectator mode using a compass +TeleportMessage: "{player}, you have been teleported to {to}. In-game players remaining: {count}" +# The message when you click the bed to get back to the lobby. +LobbyMessage: Welcome back to lobby {player} + +# Here is the name of the compass +TPCompassName: Find player +# Here is the name of the back-to-lobby bed. +BedBackName: Back to lobby + +# Name of the lobby world +LobbyWorld: + +... \ No newline at end of file diff --git a/src/Ad5001/SpectatorPlus/Main.php b/src/Ad5001/SpectatorPlus/Main.php new file mode 100644 index 0000000..4a1e3b6 --- /dev/null +++ b/src/Ad5001/SpectatorPlus/Main.php @@ -0,0 +1,175 @@ +reloadConfig(); + $this->getServer()->getPluginManager()->registerEvents($this, $this); + $this->getServer()->getScheduler()->scheduleRepeatingTask(new setGamemodeTask($this), 5); + $this->players = []; + $this->lastPlayer = null; + } + + + public function onLoad(){ + $this->saveDefaultConfig(); + } + + + public function test(Player $p, int $id) { + $this->getLogger()->info("Item : " . $id . ". Is spectator : " . $this->isSpectator($p)); + if($this->isSpectator($p) and $id == 345) { + // if($event->getPacket() instanceof \pocketmine\network\protocol\UseItemPacket) { + $founds = []; + foreach($p->getLevel()->getPlayers() as $player) { + if(!$this->isSpectator($player)) { + array_push($founds, $player); + if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) { + array_push($this->players[$p->getName()], $player); + $found = true; + $event->getPlayer()->teleport(new Vector3($player->x, $player->y, $player->z)); + $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage"))))); + } + } + } + if(!isset($found)) { + $this->players[$p->getName()] = []; + } + foreach($p->getLevel()->getPlayers() as $player) { + if(!$this->isSpectator($player)) { + array_push($founds, $player); + if(!in_array($player, $this->players[$p->getName()]) and !isset($found)) { + array_push($this->players[$p->getName()], $player); + $found = true; + $p->teleport(new Vector3($player->x, $player->y, $player->z)); + $event->getPlayer()->sendTip(str_ireplace("{to}", $player->getName(), str_ireplace("{player}", $p->getName(), str_ireplace("{count}", count($founds), $this->getConfig()->get("TeleportMessage"))))); + } + } + } + // } + } elseif($this->isSpectator($event->getPlayer()) and $id == 355) { + // if($event->getPacket() instanceof \pocketmine\network\protocol\UseItemPacket) { + $p->getInventory()->clearAll(); + $p->teleport($this->getServer()->getLevelByName($this->getConfig()->get("LobbyWorld"))->getDefaultSpawn()); + $p->sendTip(str_ireplace("{lobby}", $this->getConfig()->get("LobbyWorld"), str_ireplace("{player}", $p->getName(), $this->getConfig()->get("LobbyMessage")))); + // } + } + } + + public function onInteract(PlayerInteractEvent $event) { + $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId()); + } + + public function onBlockPlace(BlockPlaceEvent $event) { + $this->test($event->getPlayer(), $event->getBlock()->getId()); + } + + public function onBlockBreak(BlockBreakEvent $event) { + $this->test($event->getPlayer(), $event->getPlayer()->getInventory()->getItemInHand()->getId()); + } + + public function onEntityDamage(EntityDamageEvent $event) { + if($event->getEntity() instanceof Player) { + if($this->isSpectator($event->getEntity())) { + $event->setCancelled(); + } + } + if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent) { + if($event->getDamager() instanceof Player) { + if($this->isSpectator($event->getDamager())) { + $event->setCancelled(); + $this->test($event->getDamager(), $event->getDamager()->getInventory()->getItemInHand()->getId()); + } + } + } + } + + public function onPlayerChat(PlayerChatEvent $event) { + if($this->getConfig()->get("PrivateSpecChat") == "true") { + foreach($event->getPlayer()->getLevel()->getPlayers() as $p) { + if($this->isSpectator($p)) { + $p->sendMessage + } + } + $event->setCancelled(); + } + } + + + public function onPlayerGameModeChange(PlayerGameModeChangeEvent $event) { + if($event->getNewGamemode() == 3) { // Testing if spectator. + $player = $event->getPlayer(); + $this->players[$player->getName()] = []; + $this->lastPlayer = $player; + } elseif($this->lastPlayer !== $player and isset($this->players[$event->getPlayer()->getName()])) { + unset($this->players[$event->getPlayer()->getName()]); + $event->getPlayer()->setAllowFlight(false); + } + } + + + + public function isSpectator(Player $player) { + return isset($this->players[$player->getName()]); + } + + + + public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ + switch($cmd->getName()){ + case "default": + break; + } + return false; + } +} + + +class setGamemodeTask extends \pocketmine\scheduler\PluginTask { + + public function __construct(Main $main) { + parent::__construct($main); + $this->main = $main; + } + + + public function onRun($tick) { + foreach($this->main->players as $playername => $tped) { + $player = $this->main->getServer()->getPlayer($playername); + // $this->main->getLogger()->info($player->getGamemode() . "/" . $player->isSpectator()); + if($player->isSpectator()) { + $player->setGamemode(0); + $compass = Item::get(345, 0, 1); + $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("TPCompassName") . '"}}')); + $player->getInventory()->addItem($compass); + $compass = Item::get(Item::BED, 0, 1); + $compass->setNamedTag(NBT::parseJSON('{display:{Name:"§r' . $this->main->getConfig()->get("BedBackName") . '"}}')); + $player->getInventory()->addItem($compass); + foreach($player->getLevel()->getPlayers() as $p) { + if(!$this->main->isSpectator($p)) { + $p->hidePlayer($player); + } + } + $player->setAllowFlight(true); + } + } + } +} \ No newline at end of file