1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Item "description"

Discussion in 'Development' started by Aviv, Dec 9, 2016.

  1. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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 :)
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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
     
    applqpak and Skullex like this.
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
    applqpak likes this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    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
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    @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.
     
    Last edited: Dec 10, 2016
    applqpak and Muqsit like this.
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Couldn't it use the same pathway as the vanilla command in auto changing the name whenever there's a new Enchantment?
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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($itemint $enidint $enlevel 1) {
        
    $ennames = [
            
    100 => 'Blindness',
            
    101 => 'Confusion',
        ];
        
    $level = [=> 'I'=> 'II'=> 'III'=> 'IV'=> '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);
    }
     
    Last edited: Dec 10, 2016
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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()...
     
    applqpak likes this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Premature optimization is the...
     
    applqpak likes this.
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    applqpak likes this.
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
    applqpak likes this.
  14. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Only that line doesn't do anything right, it needs to be implemented with Events?
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    self::enchant($item1005);
    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.
     
    applqpak likes this.
  16. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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
     
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
    applqpak likes this.
  18. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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 :p
    NBT tags could be a good idea tho
     
  19. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    what about Lore tag? :D
     
  20. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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..
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.