Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

getCustomName not working

Comments in 'Plugin Development' started by JonsMC, Aug 21, 2016.

  1. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    hi, im trying to get an Item's custom name that i add to player's inventory using this code
    PHP:
    $item Item::get(258,0,1);
                                              
    $item->setCustomName("§bLife §cStealer");
                                              
    $s->getPlayer()->getInventory()->addItem($item);
    But when i trying to get the item's custom name using this, it won't work, no error is ouput on console
    PHP:
    public function onHeld(PlayerItemHeldEvent $ev) {
                                 if(
    $ev->getPlayer()->getInventory()->getItemInHand()->getCustomName() == "§bLife §cStealer");
                                 
    $this->getServer()->broadcastMessage("OMG, "$ev->getPlayer()->getName() ." have a §cLife §bStealer §f sword!");
                                 }
    is getCustomName not working? Or it isn't implement yet?
  2. Survingo
    Offline

    Survingo Active Member

    Joined:
    Dec 6, 2015
    Posts:
    221
    Your if() is wrong.;)
    PHP:
    public function onHeld(PlayerItemHeldEvent $event){
      if(
    $event->getItem()->getCustomName() === "§bLife §cStealer"){
        
    $this->getServer()->broadcastMessage($event->getPlayer()->getName() . " has just an #awsme item");
      }
    }
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You can also just use getName(), it will return the custom name for you as well if it's set.
  4. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Nope, nothing happend, i notice that when i use setCustomName, whether i dont have Italics or not, it still add italic to the item's name
  5. Revanex
    Offline

    Revanex New Member

    Joined:
    Jul 5, 2016
    Posts:
    4
    Minecraft User:
    Revanex
    dont forget to translate the color code, because it isnt automatically translate!
  6. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    From what I see, this thread doesn't involve any colour codes other than the § family color codes and no color codes. What needs to be translated?
  7. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Did you register events?
    PHP:
    $this->getServer()->getPluginManager()->registerEvents($this$this);
  8. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Why would i forgot it....im forgeting everything!!!
  9. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Basic debugging skills: Whenever you see that certain lines of code do not behave correctly, use var_dump() on critical variables (don't dump god objects like Server or Player! they will probably overflow your screen!), and add lines of `echo __FILE__ . "#" . __LINE__ . "\n";` to make sure that that line of code is run.

Share This Page

Advertisement