Extract mandatory parameters into constructor parameters

the goal is obviously to ditch NBT entirely here, but there's more work to be done before that becomes possible.
This commit is contained in:
Dylan K. Taylor
2020-06-19 02:49:24 +01:00
parent 3f135da704
commit 1205432c34
22 changed files with 192 additions and 180 deletions

View File

@@ -23,12 +23,13 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\Arrow as ArrowEntity;
use pocketmine\entity\projectile\Projectile;
use pocketmine\event\entity\EntityShootBowEvent;
use pocketmine\event\entity\ProjectileLaunchEvent;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
use pocketmine\world\sound\BowShootSound;
use function intdiv;
@@ -51,18 +52,18 @@ class Bow extends Tool{
}
$location = $player->getLocation();
$nbt = EntityFactory::createBaseNBT(
$player->getEyePos(),
$player->getDirectionVector(),
($location->yaw > 180 ? 360 : 0) - $location->yaw,
-$location->pitch
);
$diff = $player->getItemUseDuration();
$p = $diff / 20;
$baseForce = min((($p ** 2) + $p * 2) / 3, 1);
$entity = new ArrowEntity($location->getWorldNonNull(), $nbt, $player, $baseForce >= 1);
$entity = new ArrowEntity(Location::fromObject(
$player->getEyePos(),
$player->getWorld(),
($location->yaw > 180 ? 360 : 0) - $location->yaw,
-$location->pitch
), $player, $baseForce >= 1, new CompoundTag());
$entity->setMotion($player->getDirectionVector());
$infinity = $this->hasEnchantment(Enchantment::INFINITY());
if($infinity){

View File

@@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\Egg as EggEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
class Egg extends ProjectileItem{
@@ -36,11 +36,7 @@ class Egg extends ProjectileItem{
}
protected function createEntity(Location $location, Player $thrower) : Throwable{
return new EggEntity(
$location->getWorldNonNull(),
EntityFactory::createBaseNBT($location, null, $location->yaw, $location->pitch),
$thrower
);
return new EggEntity($location, $thrower, new CompoundTag());
}
public function getThrowForce() : float{

View File

@@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\EnderPearl as EnderPearlEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
class EnderPearl extends ProjectileItem{
@@ -36,11 +36,7 @@ class EnderPearl extends ProjectileItem{
}
protected function createEntity(Location $location, Player $thrower) : Throwable{
return new EnderPearlEntity(
$location->getWorldNonNull(),
EntityFactory::createBaseNBT($location, null, $location->yaw, $location->pitch),
$thrower
);
return new EnderPearlEntity($location, $thrower, new CompoundTag());
}
public function getThrowForce() : float{

View File

@@ -23,20 +23,16 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\ExperienceBottle as ExperienceBottleEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
class ExperienceBottle extends ProjectileItem{
protected function createEntity(Location $location, Player $thrower) : Throwable{
return new ExperienceBottleEntity(
$location->getWorldNonNull(),
EntityFactory::createBaseNBT($location, null, $location->yaw, $location->pitch),
$thrower
);
return new ExperienceBottleEntity($location, $thrower, new CompoundTag());
}
public function getThrowForce() : float{

View File

@@ -30,7 +30,7 @@ use pocketmine\block\utils\SkullType;
use pocketmine\block\utils\TreeType;
use pocketmine\block\VanillaBlocks;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\Squid;
use pocketmine\entity\Villager;
use pocketmine\entity\Zombie;
@@ -319,17 +319,17 @@ class ItemFactory{
//TODO: the meta values should probably be hardcoded; they won't change, but the EntityLegacyIds might
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::ZOMBIE, "Zombie Spawn Egg") extends SpawnEgg{
protected function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Zombie($world, EntityFactory::createBaseNBT($pos, null, $yaw, $pitch));
return new Zombie(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag());
}
});
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::SQUID, "Squid Spawn Egg") extends SpawnEgg{
public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Squid($world, EntityFactory::createBaseNBT($pos, null, $yaw, $pitch));
return new Squid(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag());
}
});
$this->register(new class(ItemIds::SPAWN_EGG, EntityLegacyIds::VILLAGER, "Villager Spawn Egg") extends SpawnEgg{
public function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity{
return new Villager($world, EntityFactory::createBaseNBT($pos, null, $yaw, $pitch));
return new Villager(Location::fromObject($pos, $world, $yaw, $pitch), new CompoundTag());
}
});
}

View File

@@ -24,11 +24,12 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\object\Painting;
use pocketmine\entity\object\PaintingMotive;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
use pocketmine\world\sound\PaintingPlaceSound;
use function array_rand;
@@ -72,28 +73,10 @@ class PaintingItem extends Item{
/** @var PaintingMotive $motive */
$motive = $motives[array_rand($motives)];
static $directions = [
Facing::SOUTH => 0,
Facing::WEST => 1,
Facing::NORTH => 2,
Facing::EAST => 3
];
$direction = $directions[$face] ?? -1;
if($direction === -1){
return ItemUseResult::NONE();
}
$replacePos = $blockReplace->getPos();
$clickedPos = $blockClicked->getPos();
$nbt = EntityFactory::createBaseNBT($replacePos, null, $direction * 90, 0);
$nbt->setByte("Direction", $direction);
$nbt->setString("Motive", $motive->getName());
$nbt->setInt("TileX", $clickedPos->getFloorX());
$nbt->setInt("TileY", $clickedPos->getFloorY());
$nbt->setInt("TileZ", $clickedPos->getFloorZ());
$entity = new Painting($replacePos->getWorldNonNull(), $nbt);
$entity = new Painting(Location::fromObject($replacePos, $replacePos->getWorldNonNull()), $clickedPos, $face, $motive, new CompoundTag());
$this->pop();
$entity->spawnToAll();

View File

@@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\Snowball as SnowballEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
class Snowball extends ProjectileItem{
@@ -36,11 +36,7 @@ class Snowball extends ProjectileItem{
}
protected function createEntity(Location $location, Player $thrower) : Throwable{
return new SnowballEntity(
$location->getWorldNonNull(),
EntityFactory::createBaseNBT($location, null, $location->yaw, $location->pitch),
$thrower
);
return new SnowballEntity($location, $thrower, new CompoundTag());
}
public function getThrowForce() : float{

View File

@@ -25,10 +25,8 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\entity\EntityFactory;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\utils\Utils;
use pocketmine\world\World;
use function lcg_value;

View File

@@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\entity\EntityFactory;
use pocketmine\entity\Location;
use pocketmine\entity\projectile\SplashPotion as SplashPotionEntity;
use pocketmine\entity\projectile\Throwable;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\player\Player;
class SplashPotion extends ProjectileItem{
@@ -36,12 +36,7 @@ class SplashPotion extends ProjectileItem{
}
protected function createEntity(Location $location, Player $thrower) : Throwable{
$projectile = new SplashPotionEntity(
$location->getWorldNonNull(),
EntityFactory::createBaseNBT($location, null, $location->yaw, $location->pitch),
$thrower
);
$projectile = new SplashPotionEntity($location, $thrower, new CompoundTag());
$projectile->setPotionId($this->meta);
return $projectile;
}