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

Customname() vs NBT

Discussion in 'Development' started by Junkdude, Dec 13, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Basically I'm working on a custom enchantment plugin and I'm wondering which would be better to use, NBT or setCustomName()? Any suggestions?
     
  2. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    They're effectively the same but I would use setCustomName(). It would be more obvious what you were trying to do if you ever had to read back your code.
     
    applqpak likes this.
  3. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    K follow up question, is it possible to change the stacking size of said "item" that has a custom name?
     
  4. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    There is a Item::getMaxStackSize() method but it doesn't return an arbitrary value. So I don't think you can.
     
    applqpak likes this.
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I was looking at how EssentialsPE changed it with this $item->setCount($sender->hasPermission("essentials.oversizedstacks") ? $this->getPlugin()->getConfig()->get("oversized-stacks") : $item->getMaxStackSize()); So maybe I can cancel the event if they to try to go over the size?
     
  6. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    The API sucks. We're discussing ways to improve things like this.
     
    SOFe and applqpak like this.
  7. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Well, somethings better then nothing I guess.
     
    dktapps likes this.
  8. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    It seems on further examination that Item::setCount() doesn't care if the number of items goes above max stack size. You may be able to take advantage of that.
     
    applqpak likes this.
  9. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    But how, I'm trying to set the max stack to size to 1 if it has a certian name but...well I'm lost.
     
  10. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    you can create new class for this item and extend certain item class and in your class create method getMaxStackSize() that overrides pocketmine one, and the register this new item in Item class. It shouldn't broke anything.
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Exactly. And there is nothing right now apart from reimplementing item stacking, which is still nothing.
    This will break everything as long as more than one plugin is trying to extend item class.
    More importantly, if your items that have changed stack size are not predefined, you can't use this method because you can't (without a lot of trouble) create dynamic classes.
     
  12. Dinokiller

    Dinokiller Spider Jockey

    Messages:
    33
    You could do something like this maybe...
    PHP:
    <?php
    if($item->getCount() > 1) {
        
    $item->setCount(1)
    }
     
  13. Aviv

    Aviv Baby Zombie

    Messages:
    156
    IDK, its hard, i think i have a solution
    PHP:
    public function addNonStackableItem(Player $player,Item $item){
    $count[$item] = 0;
    for(
    $slot 0;$slot $player->getInventory()->getSize();$slot++){
      if(
    $player->getInventory()->getItem($slot)->getId() === $item->getId() && $player->getInventory()->getItem($slot)->getName() === $item->getName()){//$item is your specific item you want to disable stacking on
        
    $count[$item]++;
        
    $name $player->getInventory()->getItem($slot)->getName();
      }
    }
    if(
    $count[$item] > 0){
      if(
    $item->hasCustomName()){
        
    $item->clearCustomName();
      }
      
    $item->setCustomName($name.str_repeat("\", $count[$item])); // im sorry about that
    }
    $player->getInventory()->addItem($item);
    return true;
    }
    then the item wont stack and "\" doesnt show on item's name
    the item wont stack because his name is diffrent from others
    This method is horrible, and it is very poorly coded, i know, i just cant find a way to do it, use it if you really have no choice left
     
    Last edited: Dec 14, 2016
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Why $count[$item]? Not just $count?

    And \ is the string literal escape character. You need to escape it as "\\".
     
  15. Aviv

    Aviv Baby Zombie

    Messages:
    156
    ok, thanks!
     
  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.