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

Solved Villager Trade

Discussion in 'Development' started by HeyDeniis_, May 2, 2020.

  1. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    With the help of some github codes I made a mini api of the villager's trade, it works well but I realized that the window that opens is an old trade window, by chance the game limits us to that old window or has some way to use the new?

    PHP:
    <?php

    namespace trade;

    use 
    pocketmine\Player;
    use 
    pocketmine\entity\Villager;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\network\mcpe\protocol\UpdateTradePacket;
    use 
    pocketmine\network\mcpe\protocol\AddActorPacket;
    use 
    pocketmine\network\mcpe\protocol\types\WindowTypes;
    use 
    pocketmine\nbt\NetworkLittleEndianNBTStream;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\nbt\tag\{
     
    CompoundTag,
     
    ListTag,
     
    ByteTag,
     
    IntTag
    };
    class 
    Trade{

    protected 
    $eid 0;

    protected 
    $title "";

    protected 
    $willing false;
    protected 
    $tradeTier 1;

    protected 
    $recipes = [];

    public function 
    __construct(string $title){
    $this->title $title;
    }
    public function 
    getEid() : int{
        return 
    $this->eid;
    }
    public function 
    getTitle() : string{
        return 
    $this->title;
    }
    public function 
    setTitle(string $title){
        
    $this->title $title;
    }
    public function 
    isWilling() : bool{
        return 
    $this->willing;
    }
    public function 
    setWilling(bool $v){
        
    $this->willing $v;
    }
    public function 
    getTradeTier() : int{
        return 
    $this->tradeTier;
    }
    public function 
    setTradeTier(int $v){
        
    $this->tradeTier $v;
    }
    /*
    @Param Item $buy - First Item to trade
    @Param Item $buyB - Second Item To Trade (is opcional)
    @Param Item $sell - Result Item of Trade
    @Param bool $rewardXp - xp trade
    @Param int $maxUse - Trade limit
    @Param int $defaultUses - Default trade use
    */
    public function addRecipe(Item $buy, ?Item $buyB nullItem $sellbool $rewardXp trueint $maxUse 12int $defaultUses 0){
    $arr = [
        new 
    ByteTag("rewardExp"$rewardXp),
        new 
    IntTag("maxUses"$maxUse),
        new 
    IntTag("uses"$defaultUses),
        
    $buy->nbtSerialize(-1"buyA"),
        
    $sell->nbtSerialize(-1"sell")
    ];
    if(!
    is_null($buyB)){
        
    $arr[] =  $buyB->nbtSerialize(-1"buyB");
    }
    $this->recipes[] = new CompoundTag(""$arr);
    }
    public function 
    send(Player $p, ?Villager $v null){
            
    $eid is_null($v) ? $this->getFakeVillagerId($p) : $v->getId();
            
    $this->eid $eid;
            
    $pk = new UpdateTradePacket();
            
    $pk->windowId WindowTypes::TRADING;
            
    $pk->tradeTier $this->getTradeTier();
            
    $pk->isWilling $this->isWilling();
            
    $pk->traderEid $eid;
            
    $pk->playerEid $p->getId();
            
    $pk->displayName $this->getTitle();
            
    $writer = new NetworkLittleEndianNBTStream();
            
    $tags = new CompoundTag("Offers", [
                        new 
    ListTag("Recipes"$this->recipes)
            ]);
            
    $pk->offers $writer->write($tags);
            
    $p->dataPacket($pk);
            
    Main::saveTrade($p$this);
    }
    public function 
    getFakeVillagerId(Player $p) : int{
        
    $eid Entity::$entityCount++;
        
    $pk = new AddActorPacket();
        
    $pk->entityRuntimeId $eid;
        
    $pk->type Villager::NETWORK_ID;
        
    $pk->position $p->asVector3();
        
    $pk->motion = new Vector3();
        
    $pk->yaw $pk->pitch 0;
        
    $pk->metadata = [
            
    Entity::DATA_FLAGS => [Entity::DATA_TYPE_LONG<< Entity::DATA_FLAG_IMMOBILE],
            
    Entity::DATA_SCALE => [Entity::DATA_TYPE_FLOAT0]
        ];
        
    $p->dataPacket($pk);
        return 
    $eid;
    }
    }
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    Image
     

    Attached Files:

  3. JaxkDev

    JaxkDev Silverfish

    Messages:
    15
    GitHub:
    JaxkDev
    HeyDeniis_ likes this.
  4. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
  5. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    I set it to true but it's still in the old one :/

    PHP:
    $pk = new UpdateTradePacket();
            
    $pk->windowId WindowTypes::TRADING;
            
    $pk->tradeTier $this->getTradeTier();
            
    $pk->isWilling $this->isWilling();
            
    $pk->traderEid $eid;
            
    $pk->playerEid $p->getId();
            
    $pk->isV2Trading = (bool) true;
            
    $pk->displayName $this->getTitle();
            
    $writer = new NetworkLittleEndianNBTStream();
            
    $tags = new CompoundTag("Offers", [
                        new 
    ListTag("Recipes"$this->recipes)
            ]);
            
    $pk->offers $writer->write($tags);
            
    $p->dataPacket($pk);
     
  6. JaxkDev

    JaxkDev Silverfish

    Messages:
    15
    GitHub:
    JaxkDev
    Odd, I tried it myself before writing the reply it showed me the new version.
     
  7. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    can you send me the items in your UpdateTradePacket () packet code?
     
  8. JaxkDev

    JaxkDev Silverfish

    Messages:
    15
    GitHub:
    JaxkDev
    I used your class with & without that exact line(Minus the type casting)
    Using Two items of Apple to trade for One item of bed but I doubt the data used is a problem.
     
  9. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    I understand, I wanted your code because it could be something that I put wrong, you already deleted it?
     
  10. JaxkDev

    JaxkDev Silverfish

    Messages:
    15
    GitHub:
    JaxkDev
    Yes it was only a quick piggyback on a plugin, the last thing I can suggest is making sure your server is updated to 3.12.0 previous versions miss-matched encoding that specific variable.
     
    HeyDeniis_ likes this.
  11. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    it worked, it was just an incorrect version of the server sorry for the inconvenience thank you
     
  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.