mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-11 05:55:33 +00:00
Improve item enchantment API and fix some bugs (#512)
This commit is contained in:
parent
2b4e303f52
commit
fb59b57bdf
@ -504,10 +504,26 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
if(isset($tag->ench)){
|
|
||||||
$tag = $tag->ench;
|
return isset($tag->ench) and $tag->ench instanceof ListTag;
|
||||||
if($tag instanceof ListTag){
|
}
|
||||||
return true;
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param int $level
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasEnchantment(int $id, int $level = -1) : bool{
|
||||||
|
if(!$this->hasEnchantments()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->getNamedTag()->ench as $entry){
|
||||||
|
if($entry["id"] === $id){
|
||||||
|
if($level === -1 or $entry["lvl"] === $level){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -535,6 +551,35 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param int $level
|
||||||
|
*/
|
||||||
|
public function removeEnchantment(int $id, int $level = -1){
|
||||||
|
if(!$this->hasEnchantments()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tag = $this->getNamedTag();
|
||||||
|
foreach($tag->ench as $k => $entry){
|
||||||
|
if($entry["id"] === $id){
|
||||||
|
if($level === -1 or $entry["lvl"] === $level){
|
||||||
|
unset($tag->ench[$k]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->setNamedTag($tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeEnchantments(){
|
||||||
|
if($this->hasEnchantments()){
|
||||||
|
$tag = $this->getNamedTag();
|
||||||
|
unset($tag->ench);
|
||||||
|
$this->setNamedTag($tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Enchantment $ench
|
* @param Enchantment $ench
|
||||||
*/
|
*/
|
||||||
@ -545,26 +590,26 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
$tag = $this->getNamedTag();
|
$tag = $this->getNamedTag();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$found = false;
|
||||||
|
|
||||||
if(!isset($tag->ench)){
|
if(!isset($tag->ench)){
|
||||||
$tag->ench = new ListTag("ench", []);
|
$tag->ench = new ListTag("ench", []);
|
||||||
$tag->ench->setTagType(NBT::TAG_Compound);
|
$tag->ench->setTagType(NBT::TAG_Compound);
|
||||||
}
|
}else{
|
||||||
|
foreach($tag->ench as $k => $entry){
|
||||||
$found = false;
|
if($entry["id"] === $ench->getId()){
|
||||||
|
$tag->ench->{$k} = new CompoundTag("", [
|
||||||
foreach($tag->ench as $k => $entry){
|
new ShortTag("id", $ench->getId()),
|
||||||
if($entry["id"] === $ench->getId()){
|
new ShortTag("lvl", $ench->getLevel())
|
||||||
$tag->ench->{$k} = new CompoundTag("", [
|
]);
|
||||||
new ShortTag("id", $ench->getId()),
|
$found = true;
|
||||||
new ShortTag("lvl", $ench->getLevel())
|
break;
|
||||||
]);
|
}
|
||||||
$found = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$found){
|
if(!$found){
|
||||||
$tag->ench->{count($tag->ench) + 1} = new CompoundTag("", [
|
$tag->ench->{count($tag->ench)} = new CompoundTag("", [
|
||||||
new ShortTag("id", $ench->getId()),
|
new ShortTag("id", $ench->getId()),
|
||||||
new ShortTag("lvl", $ench->getLevel())
|
new ShortTag("lvl", $ench->getLevel())
|
||||||
]);
|
]);
|
||||||
@ -577,16 +622,14 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
* @return Enchantment[]
|
* @return Enchantment[]
|
||||||
*/
|
*/
|
||||||
public function getEnchantments() : array{
|
public function getEnchantments() : array{
|
||||||
if(!$this->hasEnchantments()){
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$enchantments = [];
|
$enchantments = [];
|
||||||
|
|
||||||
foreach($this->getNamedTag()->ench as $entry){
|
if($this->hasEnchantments()){
|
||||||
$e = Enchantment::getEnchantment($entry["id"]);
|
foreach($this->getNamedTag()->ench as $entry){
|
||||||
$e->setLevel($entry["lvl"]);
|
$e = Enchantment::getEnchantment($entry["id"]);
|
||||||
$enchantments[] = $e;
|
$e->setLevel($entry["lvl"]);
|
||||||
|
$enchantments[] = $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $enchantments;
|
return $enchantments;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user