mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-14 09:49:50 +00:00
Added InventoryOpenEvent and InventoryPickupItemEvent
This commit is contained in:
parent
e1ccd7f9ea
commit
d92c5332da
@ -26,6 +26,7 @@ use pocketmine\command\CommandSender;
|
|||||||
use pocketmine\entity\DroppedItem;
|
use pocketmine\entity\DroppedItem;
|
||||||
use pocketmine\entity\Human;
|
use pocketmine\entity\Human;
|
||||||
use pocketmine\event\inventory\InventoryCloseEvent;
|
use pocketmine\event\inventory\InventoryCloseEvent;
|
||||||
|
use pocketmine\event\inventory\InventoryPickupItemEvent;
|
||||||
use pocketmine\event\player\PlayerAchievementAwardedEvent;
|
use pocketmine\event\player\PlayerAchievementAwardedEvent;
|
||||||
use pocketmine\event\player\PlayerChatEvent;
|
use pocketmine\event\player\PlayerChatEvent;
|
||||||
use pocketmine\event\player\PlayerCommandPreprocessEvent;
|
use pocketmine\event\player\PlayerCommandPreprocessEvent;
|
||||||
@ -1209,6 +1210,11 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->server->getPluginManager()->callEvent($ev = new InventoryPickupItemEvent($this->inventory, $item));
|
||||||
|
if($ev->isCancelled()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
switch($item->getID()){
|
switch($item->getID()){
|
||||||
case Item::WOOD:
|
case Item::WOOD:
|
||||||
$this->awardAchievement("mineWood");
|
$this->awardAchievement("mineWood");
|
||||||
@ -1217,6 +1223,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
$this->awardAchievement("diamond");
|
$this->awardAchievement("diamond");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$pk = new TakeItemEntityPacket;
|
$pk = new TakeItemEntityPacket;
|
||||||
$pk->eid = 0;
|
$pk->eid = 0;
|
||||||
$pk->target = $entity->getID();
|
$pk->target = $entity->getID();
|
||||||
@ -1998,7 +2005,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
break;
|
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)){
|
if($this->currentTransaction === null or $this->currentTransaction->getCreationTime() > (microtime(true) - 1)){
|
||||||
$this->currentTransaction = new SimpleTransactionGroup();
|
$this->currentTransaction = new SimpleTransactionGroup();
|
||||||
}
|
}
|
||||||
@ -2045,6 +2051,40 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
// return false;
|
// 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();
|
$craftingGroup->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2277,6 +2317,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
if($this->windows->contains($inventory)){
|
if($this->windows->contains($inventory)){
|
||||||
return $this->windows[$inventory];
|
return $this->windows[$inventory];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($forceId === 0 or $inventory->open($this)){
|
||||||
if($forceId === null){
|
if($forceId === null){
|
||||||
$this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99);
|
$this->windowCnt = $cnt = max(2, ++$this->windowCnt % 99);
|
||||||
}else{
|
}else{
|
||||||
@ -2284,17 +2326,20 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
|
|||||||
}
|
}
|
||||||
$this->windowIndex[$cnt] = $inventory;
|
$this->windowIndex[$cnt] = $inventory;
|
||||||
$this->windows->attach($inventory, $cnt);
|
$this->windows->attach($inventory, $cnt);
|
||||||
$inventory->onOpen($this);
|
|
||||||
|
|
||||||
return $cnt;
|
return $cnt;
|
||||||
|
}else{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeWindow(Inventory $inventory){
|
public function removeWindow(Inventory $inventory){
|
||||||
$inventory->onClose($this);
|
$inventory->close($this);
|
||||||
if($this->windows->contains($inventory)){
|
if($this->windows->contains($inventory)){
|
||||||
$inventory->onClose($this);
|
|
||||||
$id = $this->windows[$inventory];
|
$id = $this->windows[$inventory];
|
||||||
unset($this->windowIndex[$id]);
|
unset($this->windowIndex[$id]);
|
||||||
|
$this->windows->detach($this->windows[$inventory]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
52
src/pocketmine/event/inventory/InventoryOpenEvent.php
Normal file
52
src/pocketmine/event/inventory/InventoryOpenEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
52
src/pocketmine/event/inventory/InventoryPickupItemEvent.php
Normal file
52
src/pocketmine/event/inventory/InventoryPickupItemEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,6 +23,7 @@ namespace pocketmine\inventory;
|
|||||||
|
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
use pocketmine\event\entity\EntityInventoryChangeEvent;
|
use pocketmine\event\entity\EntityInventoryChangeEvent;
|
||||||
|
use pocketmine\event\inventory\InventoryOpenEvent;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\network\protocol\ContainerSetContentPacket;
|
use pocketmine\network\protocol\ContainerSetContentPacket;
|
||||||
use pocketmine\network\protocol\ContainerSetSlotPacket;
|
use pocketmine\network\protocol\ContainerSetSlotPacket;
|
||||||
@ -348,6 +349,19 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$this->setMaxStackSize($size);
|
$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){
|
public function onOpen(Player $who){
|
||||||
$this->viewers->attach($who);
|
$this->viewers->attach($who);
|
||||||
}
|
}
|
||||||
|
@ -194,6 +194,17 @@ interface Inventory{
|
|||||||
*/
|
*/
|
||||||
public function onOpen(Player $who);
|
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
|
* @param Player $who
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user