Added more new Events

This commit is contained in:
Shoghi Cervantes 2014-03-23 13:41:45 +01:00
parent fd0fcecb46
commit ee265d44bd
9 changed files with 366 additions and 31 deletions

View File

@ -24,7 +24,6 @@
*/ */
namespace PocketMine\Entity; namespace PocketMine\Entity;
use PocketMine;
use PocketMine\Event\Entity\EntityLevelChangeEvent; use PocketMine\Event\Entity\EntityLevelChangeEvent;
use PocketMine\Event\Entity\EntityMotionEvent; use PocketMine\Event\Entity\EntityMotionEvent;
use PocketMine\Event\Entity\EntityMoveEvent; use PocketMine\Event\Entity\EntityMoveEvent;
@ -35,14 +34,14 @@ use PocketMine\Level\Position;
use PocketMine\Math\AxisAlignedBB; use PocketMine\Math\AxisAlignedBB;
use PocketMine\Math\Vector3 as Vector3; use PocketMine\Math\Vector3 as Vector3;
use PocketMine\NBT\Tag\Compound; use PocketMine\NBT\Tag\Compound;
use PocketMine\Network;
use PocketMine\Network\Protocol\MoveEntityPacket_PosRot; use PocketMine\Network\Protocol\MoveEntityPacket_PosRot;
use PocketMine\Network\Protocol\MovePlayerPacket; use PocketMine\Network\Protocol\MovePlayerPacket;
use PocketMine\Network\Protocol\RemoveEntityPacket; use PocketMine\Network\Protocol\RemoveEntityPacket;
use PocketMine\Network\Protocol\SetEntityMotionPacket; use PocketMine\Network\Protocol\SetEntityMotionPacket;
use PocketMine\Network;
use PocketMine\Player; use PocketMine\Player;
use PocketMine\PMF\LevelFormat; use PocketMine\PMF\LevelFormat;
use PocketMine\ServerAPI; use PocketMine;
abstract class Entity extends Position{ abstract class Entity extends Position{
public static $entityCount = 1; public static $entityCount = 1;
@ -130,7 +129,7 @@ abstract class Entity extends Position{
$this->level->chunkEntities[$this->chunkIndex][$this->id] = $this; $this->level->chunkEntities[$this->chunkIndex][$this->id] = $this;
$this->lastUpdate = microtime(true); $this->lastUpdate = microtime(true);
$this->initEntity(); $this->initEntity();
ServerAPI::request()->api->dhandle("entity.add", $this); EventHandler::callEvent(new PocketMine\Event\Entity\EntitySpawnEvent($this));
} }
public function saveNBT(){ public function saveNBT(){
@ -527,7 +526,7 @@ abstract class Entity extends Position{
unset($this->level->chunkEntities[$this->chunkIndex][$this->id]); unset($this->level->chunkEntities[$this->chunkIndex][$this->id]);
unset(Entity::$list[$this->id]); unset(Entity::$list[$this->id]);
$this->despawnFromAll(); $this->despawnFromAll();
$this->server->api->dhandle("entity.remove", $this); EventHandler::callEvent(new PocketMine\Event\Entity\EntityDespawnEvent($this));
} }
} }

View File

@ -0,0 +1,84 @@
<?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\Event\Entity;
use PocketMine;
use PocketMine\Entity\Entity;
/**
* Called when a entity is despawned
*/
class EntityDespawnEvent extends EntityEvent{
public static $handlers;
public static $handlerPriority;
/**
* @param Entity $entity
*/
public function __construct(Entity $entity){
$this->entity = $entity;
}
/**
* @return int
*/
public function getType(){
//TODO: implement Entity types
return -1;
}
/**
* @return bool
*/
public function isCreature(){
return $this->entity instanceof PocketMine\Entity\Creature;
}
/**
* @return bool
*/
public function isHuman(){
return $this->entity instanceof PocketMine\Entity\Human;
}
/**
* @return bool
*/
public function isProjectile(){
return $this->entity instanceof PocketMine\Entity\Projectile;
}
/**
* @return bool
*/
public function isVehicle(){
return $this->entity instanceof PocketMine\Entity\Vehicle;
}
/**
* @return bool
*/
public function isItem(){
return $this->entity instanceof PocketMine\Entity\DroppedItem;
}
}

View File

@ -0,0 +1,100 @@
<?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\Event\Entity;
use PocketMine;
use PocketMine\Block\Block;
use PocketMine\Entity\Entity;
use PocketMine\Event\CancellableEvent;
use PocketMine\Level\Position;
/**
* Called when a entity explodes
*/
class EntityExplodeEvent extends EntityEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
/**
* @var Position
*/
protected $position;
/**
* @var Block[]
*/
protected $blocks;
/**
* @var float
*/
protected $yield;
/**
* @param Entity $entity
* @param Position $position
* @param Block[] $blocks
* @param float $yield
*/
public function __construct(Entity $entity, Position $position, array $blocks, $yield){
$this->entity = $entity;
$this->position = $position;
$this->blocks = $blocks;
$this->yield = $yield;
}
/**
* @return Position
*/
public function getPosition(){
return $this->position;
}
/**
* @return Block[]
*/
public function getBlockList(){
return $this->blocks;
}
/**
* @param Block[] $blocks
*/
public function setBlockList(array $blocks){
$this->blocks = $blocks;
}
/**
* @return float
*/
public function getYield(){
return $this->yield;
}
/**
* @param float $yield
*/
public function setYield($yield){
$this->yield = $yield;
}
}

View File

@ -22,15 +22,18 @@
namespace PocketMine\Event\Entity; namespace PocketMine\Event\Entity;
use PocketMine\Entity\Entity; use PocketMine\Entity\Entity;
use PocketMine;
use PocketMine\Event\CancellableEvent; use PocketMine\Event\CancellableEvent;
use PocketMine\Event; use PocketMine\Event;
use PocketMine;
use PocketMine\Math\Vector3 as Vector3; use PocketMine\Math\Vector3 as Vector3;
class EntityMoveEvent extends EntityEvent implements CancellableEvent{ class EntityMoveEvent extends EntityEvent implements CancellableEvent{
public static $handlers; public static $handlers;
public static $handlerPriority; public static $handlerPriority;
/**
* @var \PocketMine\Math\Vector3
*/
private $pos; private $pos;
public function __construct(Entity $entity, Vector3 $pos){ public function __construct(Entity $entity, Vector3 $pos){

View File

@ -0,0 +1,91 @@
<?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\Event\Entity;
use PocketMine;
use PocketMine\Entity\Entity;
/**
* Called when a entity is spawned
*/
class EntitySpawnEvent extends EntityEvent{
public static $handlers;
public static $handlerPriority;
/**
* @param Entity $entity
*/
public function __construct(Entity $entity){
$this->entity = $entity;
}
/**
* @return PocketMine\Level\Position
*/
public function getPosition(){
return $this->entity->getPosition();
}
/**
* @return int
*/
public function getType(){
//TODO: implement Entity types
return -1;
}
/**
* @return bool
*/
public function isCreature(){
return $this->entity instanceof PocketMine\Entity\Creature;
}
/**
* @return bool
*/
public function isHuman(){
return $this->entity instanceof PocketMine\Entity\Human;
}
/**
* @return bool
*/
public function isProjectile(){
return $this->entity instanceof PocketMine\Entity\Projectile;
}
/**
* @return bool
*/
public function isVehicle(){
return $this->entity instanceof PocketMine\Entity\Vehicle;
}
/**
* @return bool
*/
public function isItem(){
return $this->entity instanceof PocketMine\Entity\DroppedItem;
}
}

View File

@ -0,0 +1,52 @@
<?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\Event\Player;
use PocketMine\Event\CancellableEvent;
use PocketMine\Player;
use PocketMine;
/**
* Called when a player is awarded an achievement
*/
class PlayerAchievementAwardedEvent extends PlayerEvent implements CancellableEvent{
public static $handlers;
public static $handlerPriority;
/**
* @var string
*/
protected $achievement;
/**
* @param Player $player
* @param string $achievementId
*/
public function __construct(Player $player, $achievementId){
$this->player = $player;
$this->achievement = $achievementId;
}
public function getAchievement(){
return $this->achievement;
}
}

View File

@ -22,13 +22,13 @@
namespace PocketMine\Level; namespace PocketMine\Level;
use PocketMine\Block\Block; use PocketMine\Block\Block;
use PocketMine;
use PocketMine\Block\TNT; use PocketMine\Block\TNT;
use PocketMine\Entity\Entity;
use PocketMine\Item\Item; use PocketMine\Item\Item;
use PocketMine\Math\Vector3 as Vector3; use PocketMine\Math\Vector3 as Vector3;
use PocketMine\Network\Protocol\ExplodePacket; use PocketMine\Network\Protocol\ExplodePacket;
use PocketMine;
use PocketMine\Player; use PocketMine\Player;
use PocketMine\ServerAPI;
class Explosion{ class Explosion{
public static $specialDrops = array( public static $specialDrops = array(
@ -42,23 +42,22 @@ class Explosion{
public $level; public $level;
public $source; public $source;
public $size; public $size;
/**
* @var Block[]
*/
public $affectedBlocks = array(); public $affectedBlocks = array();
public $stepLen = 0.3; public $stepLen = 0.3;
private $what;
public function __construct(Position $center, $size){ public function __construct(Position $center, $size, $what = null){
$this->level = $center->level; $this->level = $center->level;
$this->source = $center; $this->source = $center;
$this->size = max($size, 0); $this->size = max($size, 0);
$this->what = $what;
} }
public function explode(){ public function explode(){
$server = ServerAPI::request(); if($this->size < 0.1){
if($this->size < 0.1 or $server->api->dhandle("entity.explosion", array(
"level" => $this->level,
"source" => $this->source,
"size" => $this->size
)) === false
){
return false; return false;
} }
@ -98,15 +97,26 @@ class Explosion{
$send = array(); $send = array();
$source = $this->source->floor(); $source = $this->source->floor();
$radius = 2 * $this->size; $radius = 2 * $this->size;
$yield = (1 / $this->size) * 100;
if($this->what instanceof Entity){
if(PocketMine\Event\EventHandler::callEvent($ev = new PocketMine\Event\Entity\EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield)) === PocketMine\Event\Event::DENY){
return;
}else{
$yield = $ev->getYield();
$this->affectedBlocks = $ev->getBlockList();
}
}
//TODO //TODO
foreach($server->api->entity->getRadius($this->source, $radius) as $entity){ /*foreach($server->api->entity->getRadius($this->source, $radius) as $entity){
$impact = (1 - $this->source->distance($entity) / $radius) * 0.5; //placeholder, 0.7 should be exposure $impact = (1 - $this->source->distance($entity) / $radius) * 0.5; //placeholder, 0.7 should be exposure
$damage = (int) (($impact * $impact + $impact) * 8 * $this->size + 1); $damage = (int) (($impact * $impact + $impact) * 8 * $this->size + 1);
$entity->harm($damage, "explosion"); $entity->harm($damage, "explosion");
} }*/
foreach($this->affectedBlocks as $block){ foreach($this->affectedBlocks as $block){
if($block instanceof TNT){ if($block instanceof TNT){
$data = array( $data = array(
"x" => $block->x + 0.5, "x" => $block->x + 0.5,
@ -118,7 +128,7 @@ class Explosion{
//TODO //TODO
//$e = $server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data); //$e = $server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data);
//$e->spawnToAll(); //$e->spawnToAll();
}elseif(mt_rand(0, 10000) < ((1 / $this->size) * 10000)){ }elseif(mt_rand(0, 100) < $yield){
if(isset(self::$specialDrops[$block->getID()])){ if(isset(self::$specialDrops[$block->getID()])){
//TODO //TODO
//$server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), Item::get(self::$specialDrops[$block->getID()], 0)); //$server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), Item::get(self::$specialDrops[$block->getID()], 0));

View File

@ -24,6 +24,7 @@
*/ */
namespace PocketMine\Level; namespace PocketMine\Level;
use PocketMine;
use PocketMine\Block\Air; use PocketMine\Block\Air;
use PocketMine\Block\Block; use PocketMine\Block\Block;
use PocketMine\Item\Item; use PocketMine\Item\Item;
@ -51,7 +52,6 @@ use PocketMine\Utils\Cache;
use PocketMine\Utils\Config; use PocketMine\Utils\Config;
use PocketMine\Utils\Random; use PocketMine\Utils\Random;
use PocketMine\Utils\Utils; use PocketMine\Utils\Utils;
use PocketMine;
/** /**
* Class Level * Class Level
@ -421,14 +421,12 @@ class Level{
}else{ }else{
$time = $this->startTime + ($now - $this->startCheck) * 20; $time = $this->startTime + ($now - $this->startCheck) * 20;
} }
if($this->server->api->dhandle("time.change", array("level" => $this, "time" => $time)) !== false){
$this->time = $time;
$this->time = $time;
$pk = new SetTimePacket; $pk = new SetTimePacket;
$pk->time = (int) $this->time; $pk->time = (int) $this->time;
$pk->started = $this->stopTime == false; $pk->started = $this->stopTime == false;
Player::broadcastPacket($this->players, $pk); Player::broadcastPacket($this->players, $pk);
}
return; return;
} }

View File

@ -25,12 +25,12 @@
*/ */
namespace PocketMine\Tile; namespace PocketMine\Tile;
use PocketMine;
use PocketMine\Level\Level; use PocketMine\Level\Level;
use PocketMine\Level\Position; use PocketMine\Level\Position;
use PocketMine\NBT\Tag\Compound; use PocketMine\NBT\Tag\Compound;
use PocketMine\PMF\LevelFormat; use PocketMine\PMF\LevelFormat;
use PocketMine\ServerAPI; use PocketMine\ServerAPI;
use PocketMine;
abstract class Tile extends Position{ abstract class Tile extends Position{
const SIGN = "Sign"; const SIGN = "Sign";
@ -84,7 +84,6 @@ abstract class Tile extends Position{
$this->chunkIndex = $index; $this->chunkIndex = $index;
$this->level->tiles[$this->id] = $this; $this->level->tiles[$this->id] = $this;
$this->level->chunkTiles[$this->chunkIndex][$this->id] = $this; $this->level->chunkTiles[$this->chunkIndex][$this->id] = $this;
$this->server->api->dhandle("tile.add", $this);
} }
public function onUpdate(){ public function onUpdate(){
@ -102,7 +101,6 @@ abstract class Tile extends Position{
unset($this->level->tiles[$this->id]); unset($this->level->tiles[$this->id]);
unset($this->level->chunkTiles[$this->chunkIndex][$this->id]); unset($this->level->chunkTiles[$this->chunkIndex][$this->id]);
unset(Tile::$list[$this->id]); unset(Tile::$list[$this->id]);
$this->server->api->dhandle("tile.remove", $this);
} }
} }