Skip to content

Commit

Permalink
Merge branch 'minor-next' into major-next
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Nov 24, 2024
2 parents 3e9a96b + 30ee0aa commit e51903d
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/block/CakeWithCandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function getCandle() : Candle{
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($this->lit && $face !== Facing::UP){
return true;
}
if($this->onInteractCandle($item, $face, $clickVector, $player, $returnedItems)){
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private function register1to1ItemMappings() : void{
$this->map1to1Item(Ids::HONEY_BOTTLE, Items::HONEY_BOTTLE());
$this->map1to1Item(Ids::HONEYCOMB, Items::HONEYCOMB());
$this->map1to1Item(Ids::HOST_ARMOR_TRIM_SMITHING_TEMPLATE, Items::HOST_ARMOR_TRIM_SMITHING_TEMPLATE());
$this->map1to1Item(Ids::ICE_BOMB, Items::ICE_BOMB());
$this->map1to1Item(Ids::INK_SAC, Items::INK_SAC());
$this->map1to1Item(Ids::IRON_AXE, Items::IRON_AXE());
$this->map1to1Item(Ids::IRON_BOOTS, Items::IRON_BOOTS());
Expand Down
5 changes: 5 additions & 0 deletions src/entity/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use pocketmine\entity\projectile\Egg;
use pocketmine\entity\projectile\EnderPearl;
use pocketmine\entity\projectile\ExperienceBottle;
use pocketmine\entity\projectile\IceBomb;
use pocketmine\entity\projectile\Snowball;
use pocketmine\entity\projectile\SplashPotion;
use pocketmine\item\Item;
Expand Down Expand Up @@ -120,6 +121,10 @@ public function __construct(){
return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt);
}, ['FallingSand', 'minecraft:falling_block']);

$this->register(IceBomb::class, function(World $world, CompoundTag $nbt) : IceBomb{
return new IceBomb(Helper::parseLocation($nbt, $world), null, $nbt);
}, ['minecraft:ice_bomb']);

$this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{
$itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
if($itemTag === null){
Expand Down
86 changes: 86 additions & 0 deletions src/entity/projectile/IceBomb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\entity\projectile;

use pocketmine\block\Block;
use pocketmine\block\BlockTypeIds;
use pocketmine\block\VanillaBlocks;
use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\item\VanillaItems;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\RayTraceResult;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
use pocketmine\world\particle\ItemBreakParticle;
use pocketmine\world\sound\IceBombHitSound;

class IceBomb extends Throwable{
public static function getNetworkTypeId() : string{ return EntityIds::ICE_BOMB; }

Check failure on line 39 in src/entity/projectile/IceBomb.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Static method pocketmine\entity\projectile\IceBomb::getNetworkTypeId() overrides non-static method pocketmine\entity\Entity::getNetworkTypeId().

Check failure on line 39 in src/entity/projectile/IceBomb.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Static method pocketmine\entity\projectile\IceBomb::getNetworkTypeId() overrides non-static method pocketmine\entity\Entity::getNetworkTypeId().

Check failure on line 39 in src/entity/projectile/IceBomb.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Static method pocketmine\entity\projectile\IceBomb::getNetworkTypeId() overrides non-static method pocketmine\entity\Entity::getNetworkTypeId().

public function getResultDamage() : int{
return -1;
}

protected function calculateInterceptWithBlock(Block $block, Vector3 $start, Vector3 $end) : ?RayTraceResult{
if($block->getTypeId() === BlockTypeIds::WATER){
$pos = $block->getPosition();

return AxisAlignedBB::one()->offset($pos->x, $pos->y, $pos->z)->calculateIntercept($start, $end);
}

return parent::calculateInterceptWithBlock($block, $start, $end);
}

protected function onHit(ProjectileHitEvent $event) : void{
$world = $this->getWorld();
$pos = $this->location;

$world->addSound($pos, new IceBombHitSound());
$itemBreakParticle = new ItemBreakParticle(VanillaItems::ICE_BOMB());
for($i = 0; $i < 6; ++$i){
$world->addParticle($pos, $itemBreakParticle);
}
}

protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
parent::onHitBlock($blockHit, $hitResult);

$pos = $blockHit->getPosition();
$world = $pos->getWorld();
$posX = $pos->getFloorX();
$posY = $pos->getFloorY();
$posZ = $pos->getFloorZ();

$ice = VanillaBlocks::ICE();
for($x = $posX - 1; $x <= $posX + 1; $x++){
for($y = $posY - 1; $y <= $posY + 1; $y++){
for($z = $posZ - 1; $z <= $posZ + 1; $z++){
if($world->getBlockAt($x, $y, $z)->getTypeId() === BlockTypeIds::WATER){
$world->setBlockAt($x, $y, $z, $ice);
}
}
}
}
}
}
48 changes: 48 additions & 0 deletions src/item/IceBomb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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;

use pocketmine\entity\Location;
use pocketmine\entity\projectile\IceBomb as IceBombEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\player\Player;

class IceBomb extends ProjectileItem{

public function getMaxStackSize() : int{
return 16;
}

protected function createEntity(Location $location, Player $thrower) : Throwable{
return new IceBombEntity($location, $thrower);
}

public function getThrowForce() : float{
return 1.5;
}

public function getCooldownTicks() : int{
return 10;
}
}
3 changes: 2 additions & 1 deletion src/item/ItemTypeIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,9 @@ private function __construct(){
public const NAME_TAG = 20287;
public const GOAT_HORN = 20288;
public const END_CRYSTAL = 20289;
public const ICE_BOMB = 20290;

public const FIRST_UNUSED_ITEM_ID = 20290;
public const FIRST_UNUSED_ITEM_ID = 20291;

private static int $nextDynamicId = self::FIRST_UNUSED_ITEM_ID;

Expand Down
1 change: 1 addition & 0 deletions src/item/StringToItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ private static function registerItems(self $result) : void{
$result->register("honey_bottle", fn() => Items::HONEY_BOTTLE());
$result->register("host_armor_trim_smithing_template", fn() => Items::HOST_ARMOR_TRIM_SMITHING_TEMPLATE());
$result->register("honeycomb", fn() => Items::HONEYCOMB());
$result->register("ice_bomb", fn() => Items::ICE_BOMB());
$result->register("ink_sac", fn() => Items::INK_SAC());
$result->register("iron_axe", fn() => Items::IRON_AXE());
$result->register("iron_boots", fn() => Items::IRON_BOOTS());
Expand Down
2 changes: 2 additions & 0 deletions src/item/VanillaItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
* @method static Item HONEYCOMB()
* @method static HoneyBottle HONEY_BOTTLE()
* @method static Item HOST_ARMOR_TRIM_SMITHING_TEMPLATE()
* @method static IceBomb ICE_BOMB()
* @method static Item INK_SAC()
* @method static Axe IRON_AXE()
* @method static Armor IRON_BOOTS()
Expand Down Expand Up @@ -504,6 +505,7 @@ protected static function setup() : void{
self::register("heart_of_the_sea", fn(IID $id) => new Item($id, "Heart of the Sea"));
self::register("honey_bottle", fn(IID $id) => new HoneyBottle($id, "Honey Bottle"));
self::register("honeycomb", fn(IID $id) => new Item($id, "Honeycomb"));
self::register("ice_bomb", fn(IID $id) => new IceBomb($id, "Ice Bomb"));
self::register("ink_sac", fn(IID $id) => new Item($id, "Ink Sac"));
self::register("iron_ingot", fn(IID $id) => new Item($id, "Iron Ingot"));
self::register("iron_nugget", fn(IID $id) => new Item($id, "Iron Nugget"));
Expand Down
1 change: 0 additions & 1 deletion src/network/mcpe/InventoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ public function syncContents(Inventory $inventory) : void{
$info = $this->trackItemStack($entry, $slot, $itemStack, null);
$contents[] = new ItemStackWrapper($info->getStackId(), $itemStack);
}
$clearSlotWrapper = new ItemStackWrapper(0, ItemStack::null());
if($entry->complexSlotMap !== null){
foreach($contents as $slotId => $info){
$packetSlot = $entry->complexSlotMap->mapCoreToNet($slotId) ?? null;
Expand Down
7 changes: 7 additions & 0 deletions src/scheduler/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
use pocketmine\utils\Utils;

abstract class Task{
/** @phpstan-var TaskHandler<static>|null */
private ?TaskHandler $taskHandler = null;

/**
* @phpstan-return TaskHandler<static>|null
*/
final public function getHandler() : ?TaskHandler{
return $this->taskHandler;
}
Expand All @@ -36,6 +40,9 @@ public function getName() : string{
return Utils::getNiceClassName($this);
}

/**
* @phpstan-param TaskHandler<static>|null $taskHandler
*/
final public function setHandler(?TaskHandler $taskHandler) : void{
if($this->taskHandler === null || $taskHandler === null){
$this->taskHandler = $taskHandler;
Expand Down
9 changes: 9 additions & 0 deletions src/scheduler/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
use pocketmine\timings\Timings;
use pocketmine\timings\TimingsHandler;

/**
* @template TTask of Task
*/
class TaskHandler{
protected int $nextRun;

Expand All @@ -36,6 +39,9 @@ class TaskHandler{
private string $taskName;
private string $ownerName;

/**
* @phpstan-param TTask $task
*/
public function __construct(
protected Task $task,
protected int $delay = -1,
Expand Down Expand Up @@ -66,6 +72,9 @@ public function setNextRun(int $ticks) : void{
$this->nextRun = $ticks;
}

/**
* @phpstan-return TTask
*/
public function getTask() : Task{
return $this->task;
}
Expand Down
44 changes: 41 additions & 3 deletions src/scheduler/TaskScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
class TaskScheduler{
private bool $enabled = true;

/** @phpstan-var ReversePriorityQueue<int, TaskHandler> */
/** @phpstan-var ReversePriorityQueue<int, TaskHandler<covariant Task>> */
protected ReversePriorityQueue $queue;

/**
* @var ObjectSet|TaskHandler[]
* @phpstan-var ObjectSet<TaskHandler>
* @phpstan-var ObjectSet<TaskHandler<covariant Task>>
*/
protected ObjectSet $tasks;

Expand All @@ -51,18 +51,42 @@ public function __construct(
$this->tasks = new ObjectSet();
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TTask $task
*
* @phpstan-return TaskHandler<TTask>
*/
public function scheduleTask(Task $task) : TaskHandler{
return $this->addTask($task, -1, -1);
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TTask $task
*
* @phpstan-return TaskHandler<TTask>
*/
public function scheduleDelayedTask(Task $task, int $delay) : TaskHandler{
return $this->addTask($task, $delay, -1);
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TTask $task
*
* @phpstan-return TaskHandler<TTask>
*/
public function scheduleRepeatingTask(Task $task, int $period) : TaskHandler{
return $this->addTask($task, -1, $period);
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TTask $task
*
* @phpstan-return TaskHandler<TTask>
*/
public function scheduleDelayedRepeatingTask(Task $task, int $delay, int $period) : TaskHandler{
return $this->addTask($task, $delay, $period);
}
Expand All @@ -77,10 +101,19 @@ public function cancelAllTasks() : void{
}
}

/**
* @phpstan-param TaskHandler<covariant Task> $task
*/
public function isQueued(TaskHandler $task) : bool{
return $this->tasks->contains($task);
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TTask $task
*
* @phpstan-return TaskHandler<TTask>
*/
private function addTask(Task $task, int $delay, int $period) : TaskHandler{
if(!$this->enabled){
throw new \LogicException("Tried to schedule task to disabled scheduler");
Expand All @@ -99,6 +132,11 @@ private function addTask(Task $task, int $delay, int $period) : TaskHandler{
return $this->handle(new TaskHandler($task, $delay, $period, $this->owner));
}

/**
* @phpstan-template TTask of Task
* @phpstan-param TaskHandler<TTask> $handler
* @phpstan-return TaskHandler<TTask>
*/
private function handle(TaskHandler $handler) : TaskHandler{
if($handler->isDelayed()){
$nextRun = $this->currentTick + $handler->getDelay();
Expand Down Expand Up @@ -128,7 +166,7 @@ public function mainThreadHeartbeat(int $currentTick) : void{
}
$this->currentTick = $currentTick;
while($this->isReady($this->currentTick)){
/** @var TaskHandler $task */
/** @phpstan-var TaskHandler<covariant Task> $task */
$task = $this->queue->extract();
if($task->isCancelled()){
$this->tasks->remove($task);
Expand Down
5 changes: 5 additions & 0 deletions src/timings/Timings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use pocketmine\network\mcpe\protocol\ServerboundPacket;
use pocketmine\player\Player;
use pocketmine\scheduler\AsyncTask;
use pocketmine\scheduler\Task;
use pocketmine\scheduler\TaskHandler;
use function get_class;
use function str_starts_with;
Expand Down Expand Up @@ -192,6 +193,10 @@ public static function init() : void{

}

/**
* @template TTask of Task
* @phpstan-param TaskHandler<TTask> $task
*/
public static function getScheduledTaskTimings(TaskHandler $task, int $period) : TimingsHandler{
self::init();
$name = "Task: " . $task->getTaskName();
Expand Down
Loading

0 comments on commit e51903d

Please sign in to comment.