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

Compound tags, Lore and Custom Names

Discussion in 'Development' started by TBNRShadowDev, Sep 28, 2017.

  1. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Hi great devs of pmmp.
    I have a few questions.
    How would I utilize one of the three in the title to make an item identifiable?
    i.e. I'm making a Notes plugin (Dollar Bills)
    PHP:
    public function onPlayerInteract(PlayerInteractEvent $event){
            
    $player $event->getPlayer();
            
    $note $player->getInventory()->getItemInHand()->getID();
            
    $verify $player->getInventory()->getItemInHand()->getCustomName();
            
    $money $player->getInventory()->getItemInHand()->getCompoundTag();
            if(
    $player instanceof Player){
                if(
    $note === Item::PAPER and $verify === "§aOfficial Note"){
                    
    $player->sendMessage(TF::GREEN."You have successfully saved into your account!");
                    
    EconomyAPI::getInstance()->addMoney($player$money);
                }
            }
        }
    Which is in my Main class (All commands and Events are registered, don't worry)
    and this
    PHP:
    $sender->getInventory()->setItemInHand(Item::get(3390$args[1]));
                        
    sleep(1);
                        
    $sender->getInventory()->getItemInHand()->setCustomName("§aOfficial Note");
                        
    $sender->getInventory()->getItemInHand()->setCompoundTag($args[0]);
                        
    $sender->sendMessage(TF::GREEN "Your notes has been issued successfully!");
                        return 
    true;
    is how I designed to issue notes. What am I doing wrong? Custom name is not appearing and the listener in main class fails to identify that it's a note, not a normal paper item upon Interaction Event.

    All help's appreciated!
    Shadow
     
  2. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Wait, isn't it this ?
    PHP:
     $nbt $item->getNamedTag(); 
    also never use sleep(Int $number);
    it will basically freeze your whole server for $number seconds ..

    also after setting everything, you have to do
    PHP:
     $player->setItemInHand($item); 
    in order to show the item's new properties like custom names, lores and other things
     
  3. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Yeah, realized that now :facepalm:
    Oh, need to try that.
    The help was greatly appreciated.
    NOT marking resolved for now.
     
    Az928 likes this.
  4. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    @Az928 BTW I do not quite understand the $item part.
    What would that be defined as? $player->getInventory()->getItemInHand? Or something else?
    All helps are appreciated!
     
  5. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    In player item held event, simply use $item = $event->getItem();
     
  6. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    But I'm calling it in a execute function though...
    BTW It's bed time for me. Will be back tomorrow.
     
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    You shouldnt store it in item name
    instated, you should store it in NBT data, storing if it's valid AND the amount in NBT tags
    (search this forum to find out how
     
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    HOLD UP. Is that a sleep() I see in your second bit of code? Do NOT use sleep() in the main thread! That stops the whole server for as long as that sleep is set to which could cause players to be disconnected. Use a delayed task instead if you want something to happen after an amount of time
     
    Muqsit, NidPlays and sharletsings123 like this.
  9. TBNRShadowDev

    TBNRShadowDev Spider Jockey

    Messages:
    25
    GitHub:
    tbnrshadowdev
    Never mind. Console returned new error
    "Trying to get Property 'isNote' of non-object"
    My code to read the tag is this
    PHP:
    public function onPlayerInteract(PlayerInteractEvent $event){
            
    $player $event->getPlayer();
            
    $note $player->getInventory()->getItemInHand();
            
    $verify $note->getNamedTag()->isNote;
            
    $money $note->getNamedTag()->noteValue;
            if(
    $player instanceof Player){
                if(
    $note === Item::PAPER and $verify === "true"){
                    
    $player->sendMessage(TF::GREEN."You have successfully saved into your account!");
                    
    EconomyAPI::getInstance()->addMoney($player$money);
                }
            }
        }
    And i use this code to set the note.
    PHP:
    $sender->getInventory()->setItemInHand(Item::get(3390$args[1]));
                        
    $note $sender->getInventory()->getItemInHand();
                        
    $nbt $note->getNamedTag() ?? new CompoundTag("", []);
                        
    $nbt->isNote = new StringTag("isNote""true");
                        
    $note->setNamedTag($nbt);
                        
    $sender->getInventory()->setItemInHand($note);
                        
    $nbt $note->getNamedTag() ?? new CompoundTag("", []);
                        
    $nbt->noteValue = new StringTag("noteValue"$args[0]);
                        
    $note->setNamedTag($nbt);
                        
    $sender->getInventory()->setItemInHand($note);

                        
    $sender->sendMessage(TF::GREEN "Your notes has been issued successfully!");
    which is inside a execute() function. It is properly configured.
    What am I doing wrong here?
    All help's greatly appreciated.
     
  10. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    why StringTag? use IntTag!
     
  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.