Hello, how can i change text in the item to go after the enchants? eg: --------------------- Diamond sword Sharpness V Unbreaking III PvPer Kit Sword --------------------- or even making custom enchants go after the vanilla enchants. if this is possible it would make my day
Use tag 'ench' for custom enchants. See \pocketmine\item\Item::addEnchantment(). As for adding a name below the enchant list, use PHP_EOL or "\n". PHP: $item = $player->getInventory()->getItemInHand();$item->setCustomName($item->getName().PHP_EOL.'PvPer Kit Sword');$playet->getInventory()->setItemInHand($item); //inventory update
Don't use PHP _EOL. It'll be \r\n on Windows, but you're sending it to the client, so there'd be an extra \r on the client which shows as a ¶ character.
It does? I use it everywhere, displays just like "\n". From what I know, it depends on the OS you are running the code on as to whether it would replace it by \n or \n\r.
I think, he meant it to auto change whenever someone enchants the sword rather than adding it manually with \n same functionality like the /enchant vanilla command
@Muqsit is right. You currently can't change the enchantment names/description with functions, so you just have to mess around with custom names and use a whole lot of line breaks and coloured texts to make it look like it.
Couldn't it use the same pathway as the vanilla command in auto changing the name whenever there's a new Enchantment?
You only want to use PHP_EOL when you are doing something system-dependent, especially when you show a message on console, etc. You don't want to use PHP_EOL when you are sending something to the client. PHP_EOL is a constant, i.e. it will immediately be replaced as \n or \r\n. It won't keep sending PHP_EOL to the client and let the client choose which EOL to use.
AFAIK, the enchant names are not handled by the server, so you'll need to create a custom addEnchantment function for now. PHP: /*** Enchant an Item $item with enchantment ID $enid* and enchant level $enlevel (default: 1)* Compatible with vanilla enchants too.*/public static function enchant($item, int $enid, int $enlevel = 1) { $ennames = [ 100 => 'Blindness', 101 => 'Confusion', ]; $level = [1 => 'I', 2 => 'II', 3 => 'III', 4 => 'IV', 5 => 'V']; $level = isset($level[$enlevel]) ? $level[$level] : $enlevel; $enchant = Enchantment::getEnchantment($enid); $enchant->setLevel($enlevel); $enchname = isset($ennames[$enid]) ? $ennames[$enid] : null; if (!is_null($enchname)) $item->setCustomName($item->getName()."\n".$enchname.' '.$level); $item->addEnchantment($enchant);}
Sometimes I just don't understand why you would want to use !is_null() instead of !== null... Calling a function is actually slightly slower, and the code is more readable using !== than !is_null()...
Yeah, I'm mainly talking about readability. But since it is your code style, never mind, although I don't see how better !is_null() is over !== null.
P.S. Just saying... Do not $item->setCustomName() after $item->addEnchantment(). Custom name it before, else the name won't display and you will have to relog back (resending inventory contents won't help). You will have to modify the source if "custom" enchantments is what you want. Or just google up PHP hacks to modify a private method (\pocketmine\item\enchantment\Enchantment::__construct() is what you want to hack into). I've had used !== null if I hadn't found is_null() function actually. Now I'm just having a mixture of opinions where I use both together. Actually not my code style. I like to be more international when it comes to styling rather than making one of my own.
PHP: self::enchant($item, 100, 5); will add 'Blindness V' string to the item's name, making 'Blindness V' look like an enchantment name. But you will need to register enchantment ID 100 as Blindness for using it in events [using it as: if ($item->hasEnchantment(100))]. To register an enchant, add a const and modify the init() in Enchantment.php Or if you don't like to be a source modifier, find a way to hack into private functions in PHP.
I actually made a full functional custom enchants system without modifying the source, i just used the custom name, but i guess some lines in the item name cant go under the vanilla enchant maybe i could set the vanilla enchants in the name first then the custom enchants, but not a bad idea. Just 1 question, is there a way to cancel the popup that is sending while the player is changing held item? EDIT: nevermind i will just create a new thread
AFAIK, there's no packet that can handle the popup sent to the player on transactions. I could be wrong. Btw, using custom names and strpos() (I guess you are using strpos or an alias) is a bad idea. What if someone renames the item? The enchants will go away. Try using NBT tags instead.
is there a way to rename an item in pocketmine yet? not too sure and i can check if theres an \n in the name, anvils do not work with \n and to rename the item i just explode all the \n from the name(the lines) and set the first line to the name the players want, i guess, there must be a PlayerAnvilUseEvent on the future of pocketmine NBT tags could be a good idea tho
PHP: /*** Item $item.* string $itemname.*/$item->setCustomName($itemname); You can check if the item has \n in it's name by... PHP: if (strpos($item->getName(), '\n') !== false) {//$item has '\n' in it's name.} else {//$item doesn't have '\n' in it's name.}//P.S. $item->getName() will return the item's custom name if it has one, so BONUS. Why explode..