Added title command and API for resetting title duration

This commit is contained in:
TheDiamondYT1 2017-04-27 16:38:51 +01:00 committed by Dylan K. Taylor
parent 84ec944b6b
commit 653b6b55a9
4 changed files with 125 additions and 2 deletions

View File

@ -3439,11 +3439,20 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
public function addTitle(string $title, string $subtitle = "", int $fadeIn = -1, int $stay = -1, int $fadeOut = -1){
$this->setTitleDuration($fadeIn, $stay, $fadeOut);
if($subtitle !== ""){
$this->sendTitleText($subtitle, SetTitlePacket::TYPE_SET_SUBTITLE);
$this->addSubTitle($subtitle);
}
$this->sendTitleText($title, SetTitlePacket::TYPE_SET_TITLE);
}
/**
* Sets the subtitle message, without sending a title.
*
* @param string $subtitle
*/
public function addSubTitle(string $subtitle){
$this->sendTitleText($subtitle, SetTitlePacket::TYPE_SET_SUBTITLE);
}
/**
* Adds small text to the user's screen.
*
@ -3462,6 +3471,15 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->dataPacket($pk);
}
/**
* Resets the title duration settings.
*/
public function resetTitles(){
$pk = new SetTitlePacket();
$pk->type = SetTitlePacket::TYPE_RESET_TITLE;
$this->dataPacket($pk);
}
/**
* Sets the title duration.
*

View File

@ -57,6 +57,7 @@ use pocketmine\command\defaults\TeleportCommand;
use pocketmine\command\defaults\TellCommand;
use pocketmine\command\defaults\TimeCommand;
use pocketmine\command\defaults\TimingsCommand;
use pocketmine\command\defaults\TitleCommand;
use pocketmine\command\defaults\TransferServerCommand;
use pocketmine\command\defaults\VanillaCommand;
use pocketmine\command\defaults\VersionCommand;
@ -115,6 +116,7 @@ class SimpleCommandMap implements CommandMap{
$this->register("pocketmine", new TeleportCommand("tp"));
$this->register("pocketmine", new TimeCommand("time"));
$this->register("pocketmine", new TimingsCommand("timings"));
$this->register("pocketmine", new TitleCommand("title"));
$this->register("pocketmine", new ReloadCommand("reload"));
$this->register("pocketmine", new TransferServerCommand("transferserver"));
@ -302,4 +304,4 @@ class SimpleCommandMap implements CommandMap{
}
}
}

View File

@ -0,0 +1,102 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\command\defaults;
use pocketmine\command\CommandSender;
use pocketmine\event\TranslationContainer;
use pocketmine\plugin\Plugin;
use pocketmine\utils\TextFormat;
class TitleCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"%pocketmine.command.title.description",
"%commands.title.usage"
);
$this->setPermission("pocketmine.command.title");
}
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return true;
}
$player = $sender->getServer()->getPlayer($args[0]);
if($player === null){
$sender->sendMessage(new TranslationContainer("commands.generic.player.notFound"));
return true;
}
switch($args[1]){
case "clear":
$player->removeTitles();
break;
case "reset":
$player->resetTitles();
break;
case "title":
if(count($args) < 3){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$player->addTitle(implode(" ", array_slice($args, 2)));
break;
case "subtitle":
if(count($args) < 3){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$player->addSubTitle(implode(" ", array_slice($args, 2)));
break;
case "actionbar":
if(count($args) < 3){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$player->addActionBarMessage(implode(" ", array_slice($args, 2)));
break;
case "times":
if(count($args) < 4){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$player->setTitleDuration($this->getInteger($sender, $args[2]), $this->getInteger($sender, $args[3]), $this->getInteger($sender, $args[4]));
break;
default:
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$sender->sendMessage(new TranslationContainer("commands.title.success"));
}
}

View File

@ -124,6 +124,7 @@ abstract class DefaultPermissions{
self::registerPermission(new Permission(self::ROOT . ".command.spawnpoint", "Allows the user to change player's spawnpoint", Permission::DEFAULT_OP), $commands);
self::registerPermission(new Permission(self::ROOT . ".command.setworldspawn", "Allows the user to change the world spawn", Permission::DEFAULT_OP), $commands);
self::registerPermission(new Permission(self::ROOT . ".command.transferserver", "Allows the user to transfer self to another server", Permission::DEFAULT_OP), $commands);
self::registerPermission(new Permission(self::ROOT . ".command.title", "Allows the user to send a title to the specified player", Permission::DEFAULT_OP), $commands);
$commands->recalculatePermissibles();