Added InventoryOpenEvent and InventoryPickupItemEvent

This commit is contained in:
Shoghi Cervantes 2014-05-27 01:44:56 +02:00
parent e1ccd7f9ea
commit d92c5332da
5 changed files with 186 additions and 12 deletions

View File

@ -26,6 +26,7 @@ use pocketmine\command\CommandSender;
use pocketmine\entity\DroppedItem;
use pocketmine\entity\Human;
use pocketmine\event\inventory\InventoryCloseEvent;
use pocketmine\event\inventory\InventoryPickupItemEvent;
use pocketmine\event\player\PlayerAchievementAwardedEvent;
use pocketmine\event\player\PlayerChatEvent;
use pocketmine\event\player\PlayerCommandPreprocessEvent;
@ -1209,6 +1210,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
continue;
}
$this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $item));
if($ev->isCancelled()){
continue;
}
switch($item->getID()){
case Item::WOOD:
$this->awardAchievement("mineWood");
@ -1217,6 +1223,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->awardAchievement("diamond");
break;
}
$pk = new TakeItemEntityPacket;
$pk->eid = 0;
$pk->target = $entity->getID();
@ -1998,7 +2005,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break;
}
console("[TRANSACTION] {$this->username} {$packet->windowid}[ ".$packet->item->getID().":".$packet->item->getDamage()."(".$packet->item->getCount().") -> #{$packet->slot} ]");
if($this->currentTransaction === null or $this->currentTransaction->getCreationTime() > (microtime(true) - 1)){
$this->currentTransaction = new SimpleTransactionGroup();
}
@ -2045,6 +2051,40 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
// return false;
//}
/*switch($item->getID()){
case Item::WORKBENCH:
$this->awardAchievement("buildWorkBench");
break;
case Item::WOODEN_PICKAXE:
$this->awardAchievement("buildPickaxe");
break;
case Item::FURNACE:
$this->awardAchievement("buildFurnace");
break;
case Item::WOODEN_HOE:
$this->awardAchievement("buildHoe");
break;
case Item::BREAD:
$this->awardAchievement("makeBread");
break;
case Item::CAKE:
$this->awardAchievement("bakeCake");
$this->inventory->addItem(Item::get(Item::BUCKET, 0, 3));
break;
case Item::STONE_PICKAXE:
case Item::GOLD_PICKAXE:
case Item::IRON_PICKAXE:
case Item::DIAMOND_PICKAXE:
$this->awardAchievement("buildBetterPickaxe");
break;
case Item::WOODEN_SWORD:
$this->awardAchievement("buildSword");
break;
case Item::DIAMOND:
$this->awardAchievement("diamond");
break;
}*/
$craftingGroup->execute();
}
@ -2277,24 +2317,29 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->windows->contains($inventory)){
return $this->windows[$inventory];
}
if($forceId === null){
$this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99);
}else{
$cnt = (int) $forceId;
}
$this->windowIndex[$cnt] = $inventory;
$this->windows->attach($inventory, $cnt);
$inventory->onOpen($this);
return $cnt;
if($forceId === 0 or $inventory->open($this)){
if($forceId === null){
$this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99);
}else{
$cnt = (int) $forceId;
}
$this->windowIndex[$cnt] = $inventory;
$this->windows->attach($inventory, $cnt);
return $cnt;
}else{
return -1;
}
}
public function removeWindow(Inventory $inventory){
$inventory->onClose($this);
$inventory->close($this);
if($this->windows->contains($inventory)){
$inventory->onClose($this);
$id = $this->windows[$inventory];
unset($this->windowIndex[$id]);
$this->windows->detach($this->windows[$inventory]);
}
}

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\inventory;
use pocketmine\event\Cancellable;
use pocketmine\inventory\Inventory;
use pocketmine\Player;
/**
* Called when a entity is despawned
*/
class InventoryOpenEvent extends InventoryEvent implements Cancellable{
public static $handlerList = null;
/** @var Player */
private $who;
/**
* @param Inventory $inventory
* @param Player $who
*/
public function __construct(Inventory $inventory, Player $who){
$this->who = $who;
parent::__construct($inventory);
}
/**
* @return Player
*/
public function getPlayer(){
return $this->who;
}
}

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\inventory;
use pocketmine\event\Cancellable;
use pocketmine\inventory\Inventory;
use pocketmine\item\Item;
/**
* Called when a entity is despawned
*/
class InventoryPickupItemEvent extends InventoryEvent implements Cancellable{
public static $handlerList = null;
/** @var Item */
private $item;
/**
* @param Inventory $inventory
* @param Item $item
*/
public function __construct(Inventory $inventory, Item $item){
$this->item = $item;
parent::__construct($inventory);
}
/**
* @return Item
*/
public function getItem(){
return $this->item;
}
}

View File

@ -23,6 +23,7 @@ namespace pocketmine\inventory;
use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\item\Item;
use pocketmine\network\protocol\ContainerSetContentPacket;
use pocketmine\network\protocol\ContainerSetSlotPacket;
@ -348,6 +349,19 @@ abstract class BaseInventory implements Inventory{
$this->setMaxStackSize($size);
}
public function open(Player $who){
$who->getServer()->getPluginManager()->callEvent($ev = new InventoryOpenEvent($this, $who));
if($ev->isCancelled()){
return false;
}
$this->onOpen($who);
return true;
}
public function close(Player $who){
$this->onClose($who);
}
public function onOpen(Player $who){
$this->viewers->attach($who);
}

View File

@ -194,6 +194,17 @@ interface Inventory{
*/
public function onOpen(Player $who);
/**
* Tries to open the inventory to a player
*
* @param Player $who
*
* @return bool
*/
public function open(Player $who);
public function close(Player $who);
/**
* @param Player $who
*/