Improved trees, improved inventory transactions, improved snowball/bow usage

This commit is contained in:
Shoghi Cervantes
2015-03-28 16:59:15 +01:00
parent 47de616ac5
commit 0a85ad0d1f
22 changed files with 363 additions and 360 deletions

View File

@ -122,12 +122,12 @@ abstract class BaseInventory implements Inventory{
}
}
public function setItem($index, Item $item, $source = null){
public function setItem($index, Item $item){
$item = clone $item;
if($index < 0 or $index >= $this->size){
return false;
}elseif($item->getId() === 0 or $item->getCount() <= 0){
return $this->clear($index, $source);
return $this->clear($index);
}
$holder = $this->getHolder();
@ -142,7 +142,7 @@ abstract class BaseInventory implements Inventory{
$old = $this->getItem($index);
$this->slots[$index] = clone $item;
$this->onSlotChange($index, $old, $source);
$this->onSlotChange($index, $old);
return true;
}
@ -227,14 +227,6 @@ abstract class BaseInventory implements Inventory{
}
public function addItem(...$slots){
if(isset($slots[$end = count($slots) - 1]) and $slots[$end] instanceof Player){
/** @var Player $source */
$source = $slots[$end];
unset($slots[$end]);
}else{
$source = null;
}
/** @var Item[] $itemSlots */
/** @var Item[] $slots */
$itemSlots = [];
@ -258,7 +250,7 @@ abstract class BaseInventory implements Inventory{
if($amount > 0){
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() + $amount);
$this->setItem($i, $item, $source);
$this->setItem($i, $item);
if($slot->getCount() <= 0){
unset($itemSlots[$index]);
}
@ -273,12 +265,13 @@ abstract class BaseInventory implements Inventory{
if(count($itemSlots) > 0 and count($emptySlots) > 0){
foreach($emptySlots as $slotIndex){
//This loop only gets the first item, then goes to the next empty slot
foreach($itemSlots as $index => $slot){
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
$slot->setCount($slot->getCount() - $amount);
$item = clone $slot;
$item->setCount($amount);
$this->setItem($slotIndex, $item, $source);
$this->setItem($slotIndex, $item);
if($slot->getCount() <= 0){
unset($itemSlots[$index]);
}
@ -291,14 +284,6 @@ abstract class BaseInventory implements Inventory{
}
public function removeItem(...$slots){
if(isset($slots[$end = count($slots) - 1]) and $slots[$end] instanceof Player){
/** @var Player $source */
$source = $slots[$end];
unset($slots[$end]);
}else{
$source = null;
}
/** @var Item[] $itemSlots */
/** @var Item[] $slots */
$itemSlots = [];
@ -319,7 +304,7 @@ abstract class BaseInventory implements Inventory{
$amount = min($item->getCount(), $slot->getCount());
$slot->setCount($slot->getCount() - $amount);
$item->setCount($item->getCount() - $amount);
$this->setItem($i, $item, $source);
$this->setItem($i, $item);
if($slot->getCount() <= 0){
unset($itemSlots[$index]);
}
@ -334,7 +319,7 @@ abstract class BaseInventory implements Inventory{
return $itemSlots;
}
public function clear($index, $source = null){
public function clear($index){
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, null, 0);
$old = $this->slots[$index];
@ -353,7 +338,7 @@ abstract class BaseInventory implements Inventory{
unset($this->slots[$index]);
}
$this->onSlotChange($index, $old, $source);
$this->onSlotChange($index, $old);
}
return true;
@ -366,20 +351,10 @@ abstract class BaseInventory implements Inventory{
}
/**
* @param Player $source
*
* @return Player[]
*/
public function getViewers($source = null){
$viewers = [];
foreach($this->viewers as $viewer){
if($viewer === $source){
continue;
}
$viewers[] = $viewer;
}
return $viewers;
public function getViewers(){
return $this->viewers;
}
public function getHolder(){
@ -412,8 +387,8 @@ abstract class BaseInventory implements Inventory{
unset($this->viewers[spl_object_hash($who)]);
}
public function onSlotChange($index, $before, $source = null){
$this->sendSlot($index, $this->getViewers($source));
public function onSlotChange($index, $before){
$this->sendSlot($index, $this->getViewers());
}

View File

@ -53,12 +53,12 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->right->getSize());
}
public function setItem($index, Item $item, $source = null){
return $index < $this->left->getSize() ? $this->left->setItem($index, $item, $source) : $this->right->setItem($index - $this->right->getSize(), $item, $source);
public function setItem($index, Item $item){
return $index < $this->left->getSize() ? $this->left->setItem($index, $item) : $this->right->setItem($index - $this->right->getSize(), $item);
}
public function clear($index, $source = null){
return $index < $this->left->getSize() ? $this->left->clear($index, $source) : $this->right->clear($index - $this->right->getSize(), $source);
public function clear($index){
return $index < $this->left->getSize() ? $this->left->clear($index) : $this->right->clear($index - $this->right->getSize());
}
public function getContents(){

View File

@ -85,8 +85,8 @@ class FurnaceInventory extends ContainerInventory{
return $this->setItem(0, $item);
}
public function onSlotChange($index, $before, $source = null){
parent::onSlotChange($index, $before, $source);
public function onSlotChange($index, $before){
parent::onSlotChange($index, $before);
$this->getHolder()->scheduleUpdate();
}

View File

@ -57,17 +57,16 @@ interface Inventory{
*
* @param int $index
* @param Item $item
* @param Player $source
*
* @return bool
*/
public function setItem($index, Item $item, $source = null);
public function setItem($index, Item $item);
/**
* Stores the given Items in the inventory. This will try to fill
* existing stacks and empty slots as well as it can.
*
* Returns the Items that did not fit. A Player source can be set at the end
* Returns the Items that did not fit.
*
* @param Item ...$item
*
@ -86,7 +85,7 @@ interface Inventory{
/**
* Removes the given Item from the inventory.
* It will return the Items that couldn't be removed. A Player source can be set at the end
* It will return the Items that couldn't be removed.
*
* @param Item ...$item
*
@ -163,11 +162,10 @@ interface Inventory{
* Will clear a specific slot
*
* @param int $index
* @param Player $source
*
* @return bool
*/
public function clear($index, $source = null);
public function clear($index);
/**
* Clears all the slots
@ -176,13 +174,11 @@ interface Inventory{
/**
* Gets all the Players viewing the inventory
* Players will be viewing their inventory at all times, even when not open.
*
* @param Player $source
* Players will view their inventory at all times, even when not open.
*
* @return Player[]
*/
public function getViewers($source = null);
public function getViewers();
/**
* @return InventoryType
@ -218,7 +214,6 @@ interface Inventory{
/**
* @param int $index
* @param Item $before
* @param Player $source
*/
public function onSlotChange($index, $before, $source = null);
public function onSlotChange($index, $before);
}

View File

@ -88,12 +88,11 @@ class PlayerInventory extends BaseInventory{
/**
* @param Item $item
* @param $source
*
* @return bool
*/
public function setItemInHand(Item $item, $source = null){
return $this->setItem($this->getHeldItemSlot(), $item, $source);
public function setItemInHand(Item $item){
return $this->setItem($this->getHeldItemSlot(), $item);
}
public function getHeldItemSlot(){
@ -153,11 +152,16 @@ class PlayerInventory extends BaseInventory{
}
}
public function onSlotChange($index, $before, $source = null){
parent::onSlotChange($index, $before, $source);
public function onSlotChange($index, $before){
$holder = $this->getHolder();
if($holder instanceof Player and !$holder->spawned){
return;
}
parent::onSlotChange($index, $before);
if($index >= $this->getSize()){
$this->sendArmorSlot($index, $this->getViewers($source));
$this->sendArmorSlot($index, $this->getViewers());
$this->sendArmorSlot($index, $this->getHolder()->getViewers());
}
}
@ -170,8 +174,8 @@ class PlayerInventory extends BaseInventory{
return $this->getItem($this->getSize() + $index);
}
public function setArmorItem($index, Item $item, $source = null){
return $this->setItem($this->getSize() + $index, $item, $source);
public function setArmorItem($index, Item $item){
return $this->setItem($this->getSize() + $index, $item);
}
public function getHelmet(){
@ -206,11 +210,11 @@ class PlayerInventory extends BaseInventory{
return $this->setItem($this->getSize() + 3, $boots);
}
public function setItem($index, Item $item, $source = null){
public function setItem($index, Item $item){
if($index < 0 or $index >= $this->size){
return false;
}elseif($item->getId() === 0 or $item->getCount() <= 0){
return $this->clear($index, $source);
return $this->clear($index);
}
if($index >= $this->getSize()){ //Armor change
@ -240,12 +244,12 @@ class PlayerInventory extends BaseInventory{
$old = $this->getItem($index);
$this->slots[$index] = clone $item;
$this->onSlotChange($index, $old, $source);
$this->onSlotChange($index, $old);
return true;
}
public function clear($index, $source = null){
public function clear($index){
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, null, 0);
$old = $this->slots[$index];
@ -278,7 +282,7 @@ class PlayerInventory extends BaseInventory{
unset($this->slots[$index]);
}
$this->onSlotChange($index, $old, $source);
$this->onSlotChange($index, $old);
}
return true;

View File

@ -151,7 +151,7 @@ class SimpleTransactionGroup implements TransactionGroup{
}
foreach($this->transactions as $transaction){
$transaction->getInventory()->setItem($transaction->getSlot(), $transaction->getTargetItem(), $this->getSource());
$transaction->getInventory()->setItem($transaction->getSlot(), $transaction->getTargetItem());
}
$this->hasExecuted = true;