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

setDrops() on BlockBreakEvent?

Discussion in 'Development' started by Zuruki, Apr 9, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Okay that really made me laugh. registerEvents(listener, plugin base)
    :D:D
    Also thankyou so much @jasonwynn10 for explaining, constructive criticism is the best way to learn imo ;)
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Massive facepalms... :facepalm:

    when attempting to register a class as a Listener in PocketMine, one must always implement the Listener Class and call the registerEvents() function. The reason it didn't work in your second attempt is because you didn't use any variables when you were registering the class. $this is the variable representation of the class. what you used $this->getServer()->getPluginManager()->registerEvents(listener, plugin base); are not variables or the proper inputs for the function.

    It's good to see that you are attempting to learn. Keep it up, but I recommend posting i the facepalm section due to the nature of these small programming mistakes. ;)
     
    Primus likes this.
  3. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I think these are :facepalm: questions, but I want to learn:
    1. What is a Listener in PMMP? When do I need to use it?

    2. I don't understand what this part of the code means:
    registerEvents($this, $this)
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    instance of Listener means
    listener is just something that implements(Look up PHP manual if you dont understand this keyword) class Listener
    registerEvents(instance of Listener,instance of BasePlugin)
    calling the function means you are telling PMMP that you want to be notified when there's an event that you have a valid event observer as mostly called in different parts of PHP community but here, we call it EventListener which you PMMP will scan all of your functions automatically if your class have any public class(Look up PHP manual if you dont understand this keyword) with only one arg PMMP will try to see if the class is instance of Event/smth around like it, if it's valid it will register it

    Instance of plugin base just references to a plugin, for crash handling or so...
    it's a way for PMMP to tell which plugin Owns the listener
    because some of us uses multiple classes like separated listener class
     
    Zuruki likes this.
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    What is class Listener? Why do I need it or a plugin like this one? Does this have something to do with instanceof? If yes, what does that mean and do?

    Plus, why
    $this, $this? Why do I need two of these '$this's? What does that exactly mean? :facepalm:
     
  6. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    $this = if it's in a class, it's referred to the object that it's in
     
  7. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    A double $this refers to the Listener and the PluginBase. The registerEvents require both the Listener and the PluginBase. If the Listener is in the PluginBase, both the Listener and the PluginBase will be $this. If your Listener is in a separated class, the first $this would be replaced by an instance of the Listener you want to register. (new ListenerName($this), $this)
     
    corytortoise likes this.
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    class listener is just a dummy class indicating it to be a event listener, nothing special but just so other can check using instanceof
    for instanceof i think the menaul could explain it better
    the first $this i the listener
    thr second $this is the base/owner
     
  9. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Is this now correct? Still doesn't work! :facepalm:
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
    I tried this here too, also didn't work:
    PHP:
    <?php

    namespace pmmp\DropIt;

    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\Player;
    use 
    pocketmine\inventory\Inventory;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        
        public function 
    onBreak(BlockBreakEvent $event) {
            if(
    $event->getBlock()->getId() === 14) {
                
    $drops = array();
                
    $drops[] = Item::get(Item::IRON_INGOT01);
                
    $event->setDrops($drops);
            }
         }
    }
     
    Last edited: Apr 11, 2017
  10. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    what's your error
     
  11. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I don't know... the console moves too fast for me to see.
    But is there anything wrong in that code?
     
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you should try to run only one plugin, you can archive it by moving all other phars into a folder named anything or "unloaded"
     
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Looks like there are no errors. Is there something wrong in the code?
     
  14. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Try registering onDisable() maybe? As you're working with listeners
     
  15. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Like this?
    PHP:
    public function onDisable(CommandSender $sender) {
        
    $sender->sendMessage("DropIt turned off!");
    }
     
  16. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    No :facepalm:

    Just like onEnable(), register the events.
     
  17. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    This?
    PHP:
    public function onDisable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
     
  18. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Finally, it works...
    Question:
    Can I do it like this too? Like @jasonwynn10 said?
    PHP:
    drops[] = new Item(155// Gives you a Quartz Block as drop
    Problem is, I cannot set how many Quartz Blocks it should drop...
     
  19. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Code:
    $drops->setCount()
    /* amount of drops goes in bracket */
     
  20. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Like this?
    PHP:
    $drops->setCount(3);
    drops[] = new Item(155)
     
  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.