I'm making a custom enchants plugin but this code doesnt set the name, or change it at all, no errors btw PHP: public function onTap(PlayerInteractEvent $event){ $p = $event->getPlayer(); $item = $p->getInventory()->getItemInHand(); $itemname1 = $item->getCustomName(); $newname = $item->setCustomName("rekt"); $name = $event->getPlayer()->getName(); if($event->getBlock()->getId() == 1){ $item->setCustomName($itemname1 . "\n" . $newName); } }
I dont think its okay to define $item as that. Use this instead: PHP: $item = $event->getItem(); You need to update the item in hand whenever you add/modify an NBT of the item. If you enchant your item with /enchant, you'll see how the item bounces when its successfully enchanted. This is due to the $item's slot updating. You can update the item by adding this at the end. PHP: $p->getInventory()->setItemInHand($item); (Line stolen from Enchantment.php) The person below me explains it better...
The $item you have got only represents the data in the item, but the item in the inventory itself. Calling setCustomName() etc. only manipulates the data in $item, but not the reference from the inventory's side. You have to setItemInHand() to apply the changes back to the inventory.
Nah still nothing PHP: public function onTap(PlayerInteractEvent $event){ $p = $event->getPlayer(); $item = $event->getItem(); $itemname1 = $item->getCustomName(); $newname = $item->setCustomName("rekt"); if($event->getBlock()->getId() == 1){ if($item->getId() == 276){ $item->setCustomName("\nTest"); $p->getInventory()->setItemInHand($item); } }}
Full code, okay nothing happens. No error, the plugin is registered, just nothing happens... PHP: <?phpnamespace Junkdude;use pocketmine\block\Block;use pocketmine\event\player\PlayerInteractEvent;use pocketmine\event\player\PlayerJoinEvent;use pocketmine\inventory\ChestInventory;use pocketmine\event\Listener;use pocketmine\math\Vector3;use pocketmine\nbt\NBT;use pocketmine\plugin\PluginBase;use SQLite3;use pocketmine\Player;use pocketmine\Server;class Main extends PluginBase implements Listener{public function onTap(PlayerInteractEvent $event){ $p = $event->getPlayer(); $p->sendmessage("lit");}}
PHP: public function onTap(PlayerInteractEvent $event){ $p = $event->getPlayer(); $item = $event->getItem(); $item->setCustomName("rekt"); if($event->getBlock()->getId() === 1){ if($item->getId() === 276){ $item->setCustomName($item->getName()."\nTest"); $p->getInventory()->setItemInHand($item); } }}
What the? Only two lines of code and you call it the whole plugin? This doesn't do anything at all... Make sure you registered events.
If i remembered correctly, it should be like this PHP: $item->getNamedTag()->display->Name = new StringTag("Name","itemName"); I might be wrong, so just try it out