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

Solved Call to a member function setCount() on integer? Need Help. (Please lock thread now)

Comments in 'Plugin Development' started by CavinMiana, Oct 19, 2015.

  1. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    Hello,

    Im having problems when im throwing a snowball. It says the error (Its in the title). How would I be able to fix that thx!
    Here is my code:
    PHP:
     $f 1.5;
                            
    $snowball Entity::createEntity("Item"$player->getLevel()->getChunk($player->getFloorX() >> 4$player->getFloorZ() >> 4), $nbt$player);
                            
    $snowball->setMotion($snowball->getMotion()->multiply($f));
                            if(
    $player->isSurvival()){
                                
    $item->setCount($item->getCount() - 1); // Here's where the error happens.
                                
    $player->getInventory()->setItemInHand($item->getCount() > $item Item::get(Item::AIR));
                            }
                            
    $player->getLevel()->addSound(new LaunchSound($player), $player->getViewers());
                            
    $snowball->spawnToAll();
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    The var '$item' is a integer. I believe it should be a Item or Entity instance?
  3. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    Oh ok let me try
  4. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    How would you write it.
  5. DrbeastOfficial
    Offline

    DrbeastOfficial New Member

    Joined:
    Jan 4, 2015
    Posts:
    17
    Minecraft User:
    Drbeastproject
    Idk I just work here.
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Can you post the full code? Especially the part where you defined $item
    Asparanc likes this.
  7. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Can you put all the code ?
  8. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    PHP:
                            $f 1.5;
                            
    $item getItem()->getId(); // Here is where I defined $item
                            
    $snowball Entity::createEntity("Item"$player->getLevel()->getChunk($player->getFloorX() >> 4$player->getFloorZ() >> 4), $nbt$player);
                            
    $snowball->setMotion($snowball->getMotion()->multiply($f));
                            if(
    $player->isSurvival()){
                                
    $item->setCount($item->getCount() - 1); // Heres the error
                                
    $player->getInventory()->setItemInHand($item->getCount() > $item Item::get(Item::AIR));
                            }
                            
    $player->getLevel()->addSound(new LaunchSound($player), $player->getViewers());
                            
    $snowball->spawnToAll();
            }
        }
    }
    @Hotshot_9930
    @Asparanc
  9. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Your $item is defined with a null value because you defined it wrong.


    This is in an event ?
  10. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    Oh yah sorry but thats not the problem.
    PHP:
     $f 1.5;
                            
    $item $ev->getItem()->getId(); // Fixed @Asparanc and this is where id defined it
                            
    $snowball Entity::createEntity("Item"$player->getLevel()->getChunk($player->getFloorX() >> 4$player->getFloorZ() >> 4), $nbt$player);
                            
    $snowball->setMotion($snowball->getMotion()->multiply($f));
                            if(
    $player->isSurvival()){
                                
    $item->setCount($item->getCount() - 1); // Heres the error 
                                
    $player->getInventory()->setItemInHand($item->getCount() > $item Item::get(Item::AIR));
                            }
                            
    $player->getLevel()->addSound(new LaunchSound($player), $player->getViewers());
                            
    $snowball->spawnToAll();
            }
        }
    }
  11. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Change $item to $item = $ev->getItem();
    You cant get the count of an item ID.
  12. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    Ok let me try that.
  13. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Use :
    PHP:
    $item $ev->getItem();
    What you wrote have a value of the ID of an item (like 12 87 etc...) you can't use function on an number, you need to return an instance of the item.
  14. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I already said that.. or did we reply at the same time? Lol
  15. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    Ohhh wait ok i was gonna post a error until you told me that let me try it and thx
  16. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    lel. He was saying to do instanceof. ok brb let me test.
  17. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    Yes xD
  18. Asparanc
    Offline

    Asparanc Active Member

    Joined:
    Feb 20, 2015
    Posts:
    210
    Minecraft User:
    Asparanc
    getItem() return the instance of the item ;)
  19. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Um not exactly. He said u need to return an instance of an item, not an intege. getId() returns an integer.
  20. CavinMiana
    Offline

    CavinMiana Active Member

    Joined:
    Aug 26, 2015
    Posts:
    101
    Minecraft User:
    CavinMiana
    soo you do this?
    PHP:
     $f 1.5;
                            
    $item getItem() instanceof Snowball//?? lol
                            
    $snowball Entity::createEntity("Item"$player->getLevel()->getChunk($player->getFloorX() >> 4$player->getFloorZ() >> 4), $nbt$player);
                            
    $snowball->setMotion($snowball->getMotion()->multiply($f));
                            if(
    $player->isSurvival()){
                                
    $item->setCount($item->getCount() - 1);
                                
    $player->getInventory()->setItemInHand($item->getCount() > $item Item::get(Item::AIR));
                            }
                            
    $player->getLevel()->addSound(new LaunchSound($player), $player->getViewers());
                            
    $snowball->spawnToAll();
            }
        }
    }
    @Hotshot_9930
    @Asparanc

Share This Page

Advertisement