Split CreativeInventoryAction into two new action types

This commit is contained in:
Dylan K. Taylor 2019-06-15 14:40:15 +01:00
parent 4bbf1d56dc
commit a94541c531
4 changed files with 99 additions and 82 deletions

View File

@ -0,0 +1,48 @@
<?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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory\transaction\action;
use pocketmine\inventory\CreativeInventory;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\Player;
/**
* This action is used by creative players to balance transactions involving the creative inventory menu.
* The source item is the item being created ("taken" from the creative menu).
*/
class CreateItemAction extends InventoryAction{
public function __construct(Item $sourceItem){
parent::__construct($sourceItem, ItemFactory::air());
}
public function isValid(Player $source) : bool{
return !$source->hasFiniteResources() and CreativeInventory::contains($this->sourceItem);
}
public function execute(Player $source) : void{
//NOOP
}
}

View File

@ -1,75 +0,0 @@
<?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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory\transaction\action;
use pocketmine\inventory\CreativeInventory;
use pocketmine\item\Item;
use pocketmine\Player;
class CreativeInventoryAction extends InventoryAction{
/**
* Player put an item into the creative window to destroy it.
*/
public const TYPE_DELETE_ITEM = 0;
/**
* Player took an item from the creative window.
*/
public const TYPE_CREATE_ITEM = 1;
protected $actionType;
public function __construct(Item $sourceItem, Item $targetItem, int $actionType){
parent::__construct($sourceItem, $targetItem);
$this->actionType = $actionType;
}
/**
* Checks that the player is in creative, and (if creating an item) that the item exists in the creative inventory.
*
* @param Player $source
*
* @return bool
*/
public function isValid(Player $source) : bool{
return !$source->hasFiniteResources() and
($this->actionType === self::TYPE_DELETE_ITEM or CreativeInventory::getItemIndex($this->sourceItem) !== -1);
}
/**
* Returns the type of the action.
*/
public function getActionType() : int{
return $this->actionType;
}
/**
* No need to do anything extra here: this type just provides a place for items to disappear or appear from.
*
* @param Player $source
*/
public function execute(Player $source) : void{
}
}

View File

@ -0,0 +1,47 @@
<?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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory\transaction\action;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\Player;
/**
* This action type shows up when a creative player puts an item into the creative inventory menu to destroy it.
* The output is the item destroyed. You can think of this action type like setting an item into /dev/null
*/
class DestroyItemAction extends InventoryAction{
public function __construct(Item $targetItem){
parent::__construct(ItemFactory::air(), $targetItem);
}
public function isValid(Player $source) : bool{
return !$source->hasFiniteResources();
}
public function execute(Player $source) : void{
//NOOP
}
}

View File

@ -23,7 +23,8 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\types;
use pocketmine\inventory\transaction\action\CreativeInventoryAction;
use pocketmine\inventory\transaction\action\CreateItemAction;
use pocketmine\inventory\transaction\action\DestroyItemAction;
use pocketmine\inventory\transaction\action\DropItemAction;
use pocketmine\inventory\transaction\action\InventoryAction;
use pocketmine\inventory\transaction\action\SlotChangeAction;
@ -183,17 +184,13 @@ class NetworkInventoryAction{
case self::SOURCE_CREATIVE:
switch($this->inventorySlot){
case self::ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM:
$type = CreativeInventoryAction::TYPE_DELETE_ITEM;
break;
return new DestroyItemAction($this->newItem);
case self::ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM:
$type = CreativeInventoryAction::TYPE_CREATE_ITEM;
break;
return new CreateItemAction($this->oldItem);
default:
throw new \UnexpectedValueException("Unexpected creative action type $this->inventorySlot");
}
return new CreativeInventoryAction($this->oldItem, $this->newItem, $type);
case self::SOURCE_CRAFTING_GRID:
case self::SOURCE_TODO:
//These types need special handling.