Basically I'm working on a custom enchantment plugin and I'm wondering which would be better to use, NBT or setCustomName()? Any suggestions?
They're effectively the same but I would use setCustomName(). It would be more obvious what you were trying to do if you ever had to read back your code.
K follow up question, is it possible to change the stacking size of said "item" that has a custom name?
There is a Item::getMaxStackSize() method but it doesn't return an arbitrary value. So I don't think you can.
I was looking at how EssentialsPE changed it with this $item->setCount($sender->hasPermission("essentials.oversizedstacks") ? $this->getPlugin()->getConfig()->get("oversized-stacks") : $item->getMaxStackSize()); So maybe I can cancel the event if they to try to go over the size?
It seems on further examination that Item::setCount() doesn't care if the number of items goes above max stack size. You may be able to take advantage of that.
you can create new class for this item and extend certain item class and in your class create method getMaxStackSize() that overrides pocketmine one, and the register this new item in Item class. It shouldn't broke anything.
Exactly. And there is nothing right now apart from reimplementing item stacking, which is still nothing. This will break everything as long as more than one plugin is trying to extend item class. More importantly, if your items that have changed stack size are not predefined, you can't use this method because you can't (without a lot of trouble) create dynamic classes.
IDK, its hard, i think i have a solution PHP: public function addNonStackableItem(Player $player,Item $item){$count[$item] = 0;for($slot = 0;$slot < $player->getInventory()->getSize();$slot++){ if($player->getInventory()->getItem($slot)->getId() === $item->getId() && $player->getInventory()->getItem($slot)->getName() === $item->getName()){//$item is your specific item you want to disable stacking on $count[$item]++; $name = $player->getInventory()->getItem($slot)->getName(); }}if($count[$item] > 0){ if($item->hasCustomName()){ $item->clearCustomName(); } $item->setCustomName($name.str_repeat("\", $count[$item])); // im sorry about that}$player->getInventory()->addItem($item);return true;} then the item wont stack and "\" doesnt show on item's name the item wont stack because his name is diffrent from others This method is horrible, and it is very poorly coded, i know, i just cant find a way to do it, use it if you really have no choice left
Why $count[$item]? Not just $count? And \ is the string literal escape character. You need to escape it as "\\".