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

set item wearable

Discussion in 'Development' started by Atomization, Jun 8, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Is it possible to set item wearable? i want to set itemID 20 (glass) into the players helmet slot to resemble a space helmet because my server is space themed.
    the item gets set in the slot but doesn't show. Minecraft 6_8_2018 4_49_42 PM.png
     
  2. Angel

    Angel Spider Jockey

    Messages:
    44
    GitHub:
    aimjel
    Here's a plugin made by @Muqsit which allows just what you want.
    PHP:
    $hat = new Hat($player);
    $hat->setBlock($block);
    /** @var HatPlayer $player*/
    $player->setHat($hat);
    There's more then one way to set the player's hat, am just using the player method one for an example.
     
    Atomization and Muqsit like this.
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It uses FallingSand entity to create fake blocks. It isn't that bad but it could be done better with minecarts. If you set a custom block to a minecart and set the minecart invisible, it creates the perfect hat :). Here's the result:
    [​IMG]
     
  4. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Ahh, how might I create a minecart and set it invisible and set helmet glass?
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Create a Hat entity class with entity ID of minecart.
    PHP:
    class HatEntity extends Entity{
        const 
    NETWORK_ID self::MINECART;
    }
    Register the class on plugin startup.
    PHP:
    Entity::registerEntity(HatEntity::class);
    You can add the following method in the HatEntity class.
    PHP:
    public function setHatBlock(int $blockIdint $blockMeta 0) : void{
        
    $dpm $this->getDataPropertyManager();
        
    $dpm->setInt(self::DATA_MINECART_DISPLAY_BLOCK$blockId | ($blockMeta << 16));
        
    $dpm->setByte(self::DATA_MINECART_HAS_DISPLAY1);
    }
    Now, if you'd want to set a hat onto a player, you'll need to create the HatEntity entity, call the setHatBlock() method and then set the entity invisible (use the search bar for how to set an entity invisible).
    PHP:
    /** @var Player $player */
    $hat Entity::createEntity("HatEntity"$player->getLevel(), Entity::createBaseNBT($player));
    $hat->setHatBlock(Block::GRASS);
    To make the HatEntity follow the player, you will either have to use PlayerMoveEvent and move the hat everytime the player moves OR (this may be tricky) you can have the HatEntity ride the player... (and still listen to PlayerMoveEvent to update the HatEntity's position).
     
    HyperxXxHound likes this.
  6. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    but will it go into the helmet inventory?
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    No, you'll need to code it so that happens. Check EntityArmorChangeEvent.
     
  8. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    in what file would i need to look in?
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    There's a method in the Entity class to set the entity invisible, check the Entity class.
     
  10. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    I have tried the code you gave me but i get this error:
    Code:
    Could not pass event 'pocketmine\event\player\PlayerMoveEvent' to 'hats v1.2.2': Argument 2 passed to pocketmine\entity\DataPropertyManager::setFloat() must be of the type float, null given, called in phar://C:/Users/steve/Desktop/server/hub/PocketMine-MP.phar/src/pocketmine/entity/Entity.php on line 545 on hats\Main
    
    TypeError: "Argument 2 passed to pocketmine\entity\DataPropertyManager::setFloat() must be of the type float, null given, called in phar://C:/Users/steve/Desktop/server/hub/PocketMine-MP.phar/src/pocketmine/entity/Entity.php on line 545" (EXCEPTION) in "src/pocketmine/entity/DataPropertyManager" at line 115
    
    my code:
    PHP:
    class Main extends PluginBase implements Listener{

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    Entity::registerEntity(HatEntity::class);

        }

        public function 
    onMove(PlayerMoveEvent $event){
            
    $player $event->getPlayer();

            
    /** @var Player $player */
            
    $hat Entity::createEntity("HatEntity"$player->getLevel(), Entity::createBaseNBT($player));
            
    $hat->setHatBlock(Block::GLASS);
        }
    }

    class 
    HatEntity extends Entity{
        const 
    NETWORK_ID self::MINECART;

        public function 
    setHatBlock(int $blockIdint $blockMeta 0) : void{
            
    $dpm $this->getDataPropertyManager();
            
    $dpm->setInt(self::DATA_MINECART_DISPLAY_BLOCK$blockId | ($blockMeta << 16));
            
    $dpm->setByte(self::DATA_MINECART_HAS_DISPLAY1);
        }
    }
     
  11. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    your plugin https://github.com/Muqsit/Hats/blob/master/src/muqsit/hats/HatPlayer.php when i /hat it doesn't wear the block im holding
     
  12. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i held glass it wont wear after i /hat but its in helmet slot
     
  13. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    This thread is not to fix an old plugin but rather code help on how to achieve the block helmet
     
    xXNiceAssassinlo YT likes this.
  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.