mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Implemented a proper way to handle items cooldown (#6405)
This commit is contained in:
parent
4e6b34f573
commit
f6e6f15c63
@ -88,4 +88,8 @@ class ChorusFruit extends Food{
|
||||
public function getCooldownTicks() : int{
|
||||
return 20;
|
||||
}
|
||||
|
||||
public function getCooldownTag() : ?string{
|
||||
return ItemCooldownTags::CHORUS_FRUIT;
|
||||
}
|
||||
}
|
||||
|
@ -45,4 +45,8 @@ class EnderPearl extends ProjectileItem{
|
||||
public function getCooldownTicks() : int{
|
||||
return 20;
|
||||
}
|
||||
|
||||
public function getCooldownTag() : ?string{
|
||||
return ItemCooldownTags::ENDER_PEARL;
|
||||
}
|
||||
}
|
||||
|
@ -654,6 +654,20 @@ class Item implements \JsonSerializable{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a tag that identifies a group of items that should have cooldown at the same time
|
||||
* regardless of their state or type.
|
||||
* When cooldown starts, any other items with the same cooldown tag can't be used until the cooldown expires.
|
||||
* Such behaviour can be seen in goat horns and shields.
|
||||
*
|
||||
* If tag is null, item state id will be used to store cooldown.
|
||||
*
|
||||
* @see ItemCooldownTags
|
||||
*/
|
||||
public function getCooldownTag() : ?string{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares an Item to this Item and check if they match.
|
||||
*
|
||||
|
45
src/item/ItemCooldownTags.php
Normal file
45
src/item/ItemCooldownTags.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\item;
|
||||
|
||||
/**
|
||||
* Tags used by items to determine their cooldown group.
|
||||
*
|
||||
* These tag values are not related to Minecraft internal IDs.
|
||||
* They only share a visual similarity because these are the most obvious values to use.
|
||||
* Any arbitrary string can be used.
|
||||
*
|
||||
* @see Item::getCooldownTag()
|
||||
*/
|
||||
final class ItemCooldownTags{
|
||||
|
||||
private function __construct(){
|
||||
//NOOP
|
||||
}
|
||||
|
||||
public const CHORUS_FRUIT = "chorus_fruit";
|
||||
public const ENDER_PEARL = "ender_pearl";
|
||||
public const SHIELD = "shield";
|
||||
public const GOAT_HORN = "goat_horn";
|
||||
}
|
@ -30,6 +30,7 @@ use pocketmine\event\server\DataPacketDecodeEvent;
|
||||
use pocketmine\event\server\DataPacketReceiveEvent;
|
||||
use pocketmine\event\server\DataPacketSendEvent;
|
||||
use pocketmine\form\Form;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\lang\KnownTranslationFactory;
|
||||
use pocketmine\lang\Translatable;
|
||||
use pocketmine\math\Vector3;
|
||||
@ -65,6 +66,7 @@ use pocketmine\network\mcpe\protocol\Packet;
|
||||
use pocketmine\network\mcpe\protocol\PacketDecodeException;
|
||||
use pocketmine\network\mcpe\protocol\PacketPool;
|
||||
use pocketmine\network\mcpe\protocol\PlayerListPacket;
|
||||
use pocketmine\network\mcpe\protocol\PlayerStartItemCooldownPacket;
|
||||
use pocketmine\network\mcpe\protocol\PlayStatusPacket;
|
||||
use pocketmine\network\mcpe\protocol\ProtocolInfo;
|
||||
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
|
||||
@ -111,6 +113,7 @@ use pocketmine\utils\BinaryDataException;
|
||||
use pocketmine\utils\BinaryStream;
|
||||
use pocketmine\utils\ObjectSet;
|
||||
use pocketmine\utils\TextFormat;
|
||||
use pocketmine\world\format\io\GlobalItemDataHandlers;
|
||||
use pocketmine\world\Position;
|
||||
use pocketmine\YmlServerProperties;
|
||||
use function array_map;
|
||||
@ -1289,6 +1292,13 @@ class NetworkSession{
|
||||
$this->sendDataPacket(OpenSignPacket::create(BlockPosition::fromVector3($signPosition), $frontSide));
|
||||
}
|
||||
|
||||
public function onItemCooldownChanged(Item $item, int $ticks) : void{
|
||||
$this->sendDataPacket(PlayerStartItemCooldownPacket::create(
|
||||
GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName(),
|
||||
$ticks
|
||||
));
|
||||
}
|
||||
|
||||
public function tick() : void{
|
||||
if(!$this->isConnected()){
|
||||
$this->dispose();
|
||||
|
@ -283,7 +283,11 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
protected string $locale = "en_US";
|
||||
|
||||
protected int $startAction = -1;
|
||||
/** @var int[] ID => ticks map */
|
||||
|
||||
/**
|
||||
* @phpstan-var array<int|string, int>
|
||||
* @var int[] stateId|cooldownTag => ticks map
|
||||
*/
|
||||
protected array $usedItemsCooldown = [];
|
||||
|
||||
private int $lastEmoteTick = 0;
|
||||
@ -697,7 +701,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
*/
|
||||
public function getItemCooldownExpiry(Item $item) : int{
|
||||
$this->checkItemCooldowns();
|
||||
return $this->usedItemsCooldown[$item->getStateId()] ?? 0;
|
||||
return $this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -705,7 +709,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
*/
|
||||
public function hasItemCooldown(Item $item) : bool{
|
||||
$this->checkItemCooldowns();
|
||||
return isset($this->usedItemsCooldown[$item->getStateId()]);
|
||||
return isset($this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -714,7 +718,8 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
|
||||
public function resetItemCooldown(Item $item, ?int $ticks = null) : void{
|
||||
$ticks = $ticks ?? $item->getCooldownTicks();
|
||||
if($ticks > 0){
|
||||
$this->usedItemsCooldown[$item->getStateId()] = $this->server->getTick() + $ticks;
|
||||
$this->usedItemsCooldown[$item->getCooldownTag() ?? $item->getStateId()] = $this->server->getTick() + $ticks;
|
||||
$this->getNetworkSession()->onItemCooldownChanged($item, $ticks);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user