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

Override Item class

Discussion in 'Development' started by Daniktheboss, Mar 24, 2018.

  1. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    Hey, i have a new class named CItem.php
    And i want to extend Item.php so i have CItem.php functions

    I know you can do that with player class through $event->setPlayerClass(CPlayer::class);
    But anyway with Items?
     
  2. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    There's a large difference between PlayerCreationEvent and the ItemFactory class. As far as I know, when the server starts, the items are instantiated via the ItemFactory class at once, which means that every time that you use the function ItemFactory::get, or Item::get, an item that is already instantiated is being returned. In contrast, however, the Player class is not instantiated when the server starts, so, from my knowledge, you'd have to override that functions by the means of modifications. However, I do not know everything, so I may be wrong. Hope this helps :)
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    But why do you want to have a custom Item class?
     
  4. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    if you want to override already existing function in item.php, it's not possible because pocketmine is mostly calling all functions and constants directly from Item.php
     
  5. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    To add custom functions, such as $item->getWhateverIWant();
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It isn't possible and there probably will never be a way to register a custom item class but you can create a masking class using reflections.
    PHP:
    class ItemMask{

        public function 
    __construct(Item $item){
            
    $this->item $item;
        }

        public function 
    setId(int $id) : void{
            
    $property = new \ReflectionProperty(Item::class, "id");
            
    $property->setAccessible(true);
            
    $property->setValue($this->item$id);
        }
    }

    /** @var Item $item */
    $mask = new ItemMask($item);
    $mask->setId(Item::BEDROCK);
    var_dump($item->getId());//Item::BEDROCK
    P.S. Changing an item's ID may cause unexpected behaviour because you aren't changing the Item class, just the item "graphic". This is just a demonstration.
     
    Last edited: Mar 25, 2018
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    mind showing more usecases?
    you can say set only certain item as custom item, not all tho
    so you would need to reregister all item clases and inject the methods
     
  8. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    Well, i have a custom enchant plugin, and i want to add a function $item->CE(CE instance);
     
  9. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Anybody else sense he's trying to re-invent the wheel?
     
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    not me
    i think you should go with a mask or just dont use item class but pass that as an arg to some class
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Register a custom enchantment during startup then call Item->addEnchantment()?
     
  12. Daniktheboss

    Daniktheboss Baby Zombie

    Messages:
    144
    GitHub:
    daniktheboss
    I guess thats the only way then. I think they should add a method to override the class just like player class
     
  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.