Skip to content

Commit

Permalink
add pmmp#5333
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy3282 committed Oct 1, 2024
1 parent e03b8d7 commit f93eb5f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Additions
> https://github.com/pmmp/PocketMine-MP/pull/5122
> https://github.com/pmmp/PocketMine-MP/pull/5232
> https://github.com/pmmp/PocketMine-MP/pull/5276
> https://github.com/pmmp/PocketMine-MP/pull/5333

## What is this?
Expand Down
3 changes: 3 additions & 0 deletions src/inventory/transaction/action/DropItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function validate(Player $source) : void{
if($this->targetItem->getCount() > $this->targetItem->getMaxStackSize()){
throw new TransactionValidationException("Target item exceeds item type max stack size");
}
if($this->targetItem->getLockMode() !== null){
throw new TransactionValidationException("Target item is locked in inventory or slot"); // todo I don't think this check is necessary. If the origin item is actually locked in a slot, the SlotChangeAction changing it to air will be invalid anyway.
}
}

public function onPreExecute(Player $source) : bool{
Expand Down
7 changes: 7 additions & 0 deletions src/inventory/transaction/action/SlotChangeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\inventory\transaction\TransactionValidationException;
use pocketmine\item\Item;
use pocketmine\item\ItemLockMode;
use pocketmine\player\Player;

/**
Expand Down Expand Up @@ -74,6 +75,12 @@ public function validate(Player $source) : void{
if($this->targetItem->getCount() > $this->inventory->getMaxStackSize()){
throw new TransactionValidationException("Target item exceeds inventory max stack size");
}
if($this->targetItem->getLockMode()?->equals(ItemLockMode::SLOT()) === true){
throw new TransactionValidationException("Target item is locked in slot");
}
if($this->sourceItem->getLockMode()?->equals(ItemLockMode::SLOT()) === true){
throw new TransactionValidationException("Source item is locked in slot");
}
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use pocketmine\nbt\tag\StringTag;
use pocketmine\nbt\TreeRoot;
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use function base64_decode;
Expand All @@ -66,10 +67,14 @@ class Item implements \JsonSerializable{

public const TAG_DISPLAY = "display";
public const TAG_BLOCK_ENTITY_TAG = "BlockEntityTag";
public const TAG_ITEM_LOCK = "minecraft:item_lock";

public const TAG_DISPLAY_NAME = "Name";
public const TAG_DISPLAY_LORE = "Lore";

public const VALUE_ITEM_LOCK_IN_SLOT = 1;
public const VALUE_ITEM_LOCK_IN_INVENTORY = 2;

public const TAG_KEEP_ON_DEATH = "minecraft:keep_on_death";

private const TAG_CAN_PLACE_ON = "CanPlaceOn"; //TAG_List<TAG_String>
Expand Down Expand Up @@ -98,6 +103,8 @@ class Item implements \JsonSerializable{
*/
protected array $canDestroy = [];

protected ?ItemLockMode $lockMode = null;

protected bool $keepOnDeath = false;

/**
Expand Down Expand Up @@ -227,6 +234,15 @@ public function setCanDestroy(array $canDestroy) : void{
}
}

public function getLockMode() : ?ItemLockMode{
return $this->lockMode;
}

public function setLockMode(?ItemLockMode $lockMode) : self {
$this->lockMode = $lockMode;
return $this;
}

/**
* Returns whether players will retain this item on death. If a non-player dies it will be excluded from the drops.
*/
Expand Down Expand Up @@ -336,6 +352,11 @@ protected function deserializeCompoundTag(CompoundTag $tag) : void{
$this->canDestroy[$entry->getValue()] = $entry->getValue();
}
}
$this->lockMode = match ($tag->getByte(self::TAG_ITEM_LOCK, 0)) {
self::VALUE_ITEM_LOCK_IN_SLOT => ItemLockMode::SLOT(),
self::VALUE_ITEM_LOCK_IN_INVENTORY => ItemLockMode::INVENTORY(),
default => null
};

$this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;
}
Expand Down Expand Up @@ -401,6 +422,16 @@ protected function serializeCompoundTag(CompoundTag $tag) : void{
$tag->removeTag(self::TAG_CAN_DESTROY);
}

if ($this->lockMode !== null) {
$tag->setByte(self::TAG_ITEM_LOCK, match ($this->lockMode->id()) {
ItemLockMode::SLOT()->id() => self::VALUE_ITEM_LOCK_IN_SLOT,
ItemLockMode::INVENTORY()->id() => self::VALUE_ITEM_LOCK_IN_INVENTORY,
default => throw new AssumptionFailedError("Unknown lock mode")
});
} else {
$tag->removeTag(self::TAG_ITEM_LOCK);
}

if($this->keepOnDeath){
$tag->setByte(self::TAG_KEEP_ON_DEATH, 1);
}else{
Expand Down
25 changes: 25 additions & 0 deletions src/item/ItemLockMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace pocketmine\item;

use pocketmine\utils\EnumTrait;

/**
* This doc-block is generated automatically, do not modify it manually.
* This must be regenerated whenever registry members are added, removed or changed.
* @see build/generate-registry-annotations.php
* @generate-registry-docblock
*
* @method static ItemLockMode INVENTORY()
* @method static ItemLockMode SLOT()
*/
final class ItemLockMode{
use EnumTrait;

protected static function setup() : void{
self::registerAll(
new self("slot"),
new self("inventory")
);
}
}
2 changes: 1 addition & 1 deletion src/world/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ public function getBlockAt(int $x, int $y, int $z, bool $cached = true, bool $ad
}

if($addToCache && $relativeBlockHash !== null){
$this->blockCache[$chunkHash][$relativeBlockHash] = $block;
$this->blockCache[$chunkHash][$relativeBlockHash] = $block; // todo add fix for https://github.com/pmmp/PocketMine-MP/issues/152 by adding cache size
}

return $block;
Expand Down

0 comments on commit f93eb5f

Please sign in to comment.