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

Solved Item in hand

Comments in 'Plugin Development' started by jojoe77777, Jun 20, 2015.

  1. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Hi

    I'd like to know how to get a player's currently held item, and set it on a Human entity.

    Thanks,
    jojoe77777
  2. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    Use the PlayerItemHeldEvent event.

    PHP:
    public function ItemHeld(PlayerItemHeldEvent $event) {
    $item $event->getItem();
    $id $item->getId();
    $damage $item->getDamage();
    }
    jojoe77777 likes this.
  3. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Thanks, that's the first part of my post, but how do I set it onto an entity?
  4. GmWM
    Offline

    GmWM Active Member Plugin Developer

    Joined:
    Sep 8, 2013
    Posts:
    181
    Plugins:
    3
    I don't have the best idea on how you could do that. Wait for others to reply.
    jojoe77777 likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What do you mean? Do you mean force a human entity hold it? Try using the swtItemInHand method for $human->getInventory()
    jojoe77777 likes this.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    So that a NPC you spawned holds the same thing you have?
    jojoe77777 likes this.
  7. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Yes, force a human to hold it. Ok.

    Yeah.
    Last edited: Jun 21, 2015
  8. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Do you mean setItemInHand? :D
    PEMapModder likes this.
  9. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    Ok, now I'm using getItemInHand() for getting the item and setItemInHand() for setting it, but when I use getItemInHand(), it gives me something like (when using iron sword) "Item Iron Sword (id:damage)x1", but I think I need "IRON_SWORD". Is there a way to convert "Item Iron Sword (id:damage)" into "IRON_SWORD"?
    Last edited: Jun 21, 2015
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The return value is an Item, which contains ID, damage and count. You can pass it directly into setItemInHand.
  11. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    I tried that, but it didn't work. (The human doesn't hold anything)
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then the problem is at somewhere else.
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    How are you spawning the NPCs? I am curious about the code.
  14. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    I'll put it in here soon.
  15. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    This is most of the important parts:


    $humanInv = $sender->getInventory();
    $pHand = $sender->getInventory()->getItemInHand();


    $nbt = new Compound;

    $motion = new Vector3(0,0,0);

    $nbt->Pos = new Enum("Pos", [

    new Double("", $playerX),
    new Double("", $playerY),
    new Double("", $playerZ)

    ]);

    $nbt->Motion = new Enum("Motion", [

    new Double("", $motion->x),
    new Double("", $motion->y),
    new Double("", $motion->z)

    ]);

    $nbt->Rotation = new Enum("Rotation", [

    new Float("", $playerYaw),
    new Float("", $playerPitch)

    ]);

    $nbt->Health = new Short("Health", $pHealth);

    $nbt->Inventory = new Enum("Inventory", $humanInv);

    $nbt->NameTag = new String("name",$name);

    $nbt->Invulnerable = new Byte("Invulnerable", 1);

    $nbt->Skin = new Compound("Skin", [
    "Data" => new String("Data", $senderSkin),
    "Slim" => new Byte("Slim", $IsSlim)
    ]);

    $clonedHuman = Entity::createEntity("Human", $sender->getLevel()->getChunk($playerX>>4, $playerZ>>4),$nbt);
    $Inv = $clonedHuman->getInventory();

    $sender->sendMessage(TextFormat::GREEN . "[" . TextFormat::YELLOW . "Slapper" . TextFormat::GREEN . "] ");

    $pHelm = $humanInv->getHelmet();
    $pChes = $humanInv->getChestplate();
    $pLegg = $humanInv->getLeggings();
    $pBoot = $humanInv->getBoots();

    $Inv->setHelmet($pHelm);
    $Inv->setChestplate($pChes);
    $Inv->setLeggings($pLegg);
    $Inv->setBoots($pBoot);
    $Inv->setItemInHand($pHand);
    $clonedHuman->spawnToAll();

    $sender->sendMessage(TextFormat::GREEN."[" . TextFormat::YELLOW . "Slapper" . TextFormat::GREEN . "] " . "Player spawned with name " . TextFormat::WHITE . "\"" . TextFormat::BLUE . $name . TextFormat::WHITE . "\"");
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    PHP:
    <?php
           $humanInv 
    $sender->getInventory();
           
    $pHand $sender->getInventory()->getItemInHand();
           
    $nbt = new Compound;
           
    $motion = new Vector3(0,0,0);
           
    $nbt->Pos = new Enum("Pos", [
           new 
    Double(""$playerX),
           new 
    Double(""$playerY),
           new 
    Double(""$playerZ)
           ]);
           
    $nbt->Motion = new Enum("Motion", [
           new 
    Double(""$motion->x),
           new 
    Double(""$motion->y),
           new 
    Double(""$motion->z)
           ]);
           
    $nbt->Rotation = new Enum("Rotation", [
           new 
    Float(""$playerYaw),
           new 
    Float(""$playerPitch)
           ]);
           
    $nbt->Health = new Short("Health"$pHealth);
           
    $nbt->Inventory = new Enum("Inventory"$humanInv);
           
    $nbt->NameTag = new String("name",$name);
           
    $nbt->Invulnerable = new Byte("Invulnerable"1);
           
    $nbt->Skin = new Compound("Skin", [
           
    "Data" => new String("Data"$senderSkin),
           
    "Slim" => new Byte("Slim"$IsSlim)
           ]);
           
    $clonedHuman Entity::createEntity("Human"$sender->getLevel()->getChunk($playerX>>4$playerZ>>4),$nbt);
           
    $Inv $clonedHuman->getInventory();
           
    $sender->sendMessage(TextFormat::GREEN "[" TextFormat::YELLOW "Slapper" TextFormat::GREEN "] ");
           
    $pHelm $humanInv->getHelmet();
           
    $pChes $humanInv->getChestplate();
           
    $pLegg $humanInv->getLeggings();
           
    $pBoot $humanInv->getBoots();
           
    $Inv->setHelmet($pHelm);
           
    $Inv->setChestplate($pChes);
           
    $Inv->setLeggings($pLegg);
           
    $Inv->setBoots($pBoot);
           
    $Inv->setItemInHand($pHand);
           
    $clonedHuman->spawnToAll();
           
    $sender->sendMessage(TextFormat::GREEN."[" TextFormat::YELLOW "Slapper" TextFormat::GREEN "] " "Player spawned with name " TextFormat::WHITE "\"" TextFormat::BLUE $name TextFormat::WHITE "\"");
    ?
    How do I need to define $name, $senderskin, $IsSlim, and $pHealth?
  17. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    656
    Plugins:
    1
    Minecraft User:
    jojoe77777
    $senderSkin = $sender->getSkinData();
    $IsSlim = $sender->isSkinSlim();
    $pHealth = $sender->getHealth();
    $name = trim(implode(" ", $args));
    Last edited: Feb 3, 2016
    Legoboy0215 likes this.
  18. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Help everyone...
    GmWM and jojoe77777 like this.
  19. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Lol...
  20. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Yay! I made it! Do you think I should post it here?

    BTW, how do I kill a NPC that I spawned?
    Last edited: Jun 22, 2015

Share This Page

Advertisement