Skip to content

Instantly share code, notes, and snippets.

@PEMapModder
Last active September 8, 2015 10:14
Show Gist options
  • Select an option

  • Save PEMapModder/b10798d5c53b1f95e400 to your computer and use it in GitHub Desktop.

Select an option

Save PEMapModder/b10798d5c53b1f95e400 to your computer and use it in GitHub Desktop.
<?php
use pocketmine\Player;
use pocketmine\entity\Arrow;
use pocketmine\item\Item as StructItem;
use pocketmine\level\Position;
use pocketmine\nbt\tag;
use pocketmine\network\protocol;
class DamagingItem extends Arrow{
const NETWORK_ID = 64;
public function initEntity(){
$this->setMaxHealth(5);
$this->setHealth($this->namedtag["Health"]);
if(isset($this->namedtag->Age)){
$this->age = $this->namedtag["Age"];
}
if(isset($this->namedtag->PickupDelay)){
$this->pickupDelay = $this->namedtag["PickupDelay"];
}
$this->item = StructItem::get($this->namedtag->Item["id"], $this->namedtag->Item["Damage"], $this->namedtag->Item["Count"]);
}
public function saveNBT(){
parent::saveNBT();
$this->namedtag->Item = new tag\Compound("Item", [
"id" => new tag\Short("id", $this->item->getId()),
"Damage" => new tag\Short("Damage", $this->item->getDamage()),
"Count" => new tag\Byte("Count", $this->item->getCount())
]);
$this->namedtag->Health = new tag\Short("Health", $this->getHealth());
$this->namedtag->PickupDelay = new tag\Short("PickupDelay", $this->pickupDelay);
}
public function spawnTo(Player $player){
$pk = new protocol\AddItemEntityPacket;
$pk->eid = $this->getId();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->speedX = $this->motionX;
$pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ;
$pk->item = $this->getItem();
$player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
$this->sendData($player);
parent::spawnTo($player);
}
public static function createNew(Position $pos, StructItem $item){
$nbt = new tag\Compound("", [
"Pos" => new tag\Enum("Pos", [
new tag\Double("", $this->x),
new tag\Double("", $this->y + $this->getEyeHeight()),
new tag\Double("", $this->z)
]),
"Motion" => new tag\Enum("Motion", [
new tag\Double("", $aimPos->x),
new tag\Double("", $aimPos->y),
new tag\Double("", $aimPos->z)
]),
"Rotation" => new tag\Enum("Rotation", [
new tag\Float("", $this->yaw),
new tag\Float("", $this->pitch)
]),
"PickupDelay" => new tag\Short("PickupDelay", 32767),
"Health" => new tag\Short("Health", 1),
"Item" => new tag\Compound("Item", [
"id" => new tag\Short("id", $item->getId()),
"Damage" => new tag\Short("Damage", $item->getDamage()),
"Count" => new tag\Byte("Count", $item->getCount()))
],
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment