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

Rates not working.

Comments in 'Plugin Development' started by Irish, May 7, 2016.

  1. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    I have coded a simple rates plugin, and it receives no errors when blocks are broken, but it doesn't work.

    Main:
    PHP:
    <?php

    namespace Rates;

    use 
    pocketmine\event\block\BlockBreakEvent as Breaker;
    use 
    pocketmine\plugin\PluginBase as P;
    use 
    pocketmine\event\Listener as L;
    use 
    pocketmine\item\Item;

    class 
    Main extends implements {


        
    /**
         * @param Breaker $event
         */
        
    public function onBreak(Breaker $event){
            
    $block $event->getBlock()->getId();
            if(
    $block === Item::get(Item::LEAVES) or $block === Item::get(Item::LEAVES2)){
                
    $rand rand(12);
                switch(
    $rand){
                    case 
    1:
                        
    $event->setDrops([Item::get(Item::APPLE01)]);
                        break;

                }
            }
            elseif(
    $block === Item::get(Item::GRAVEL)){
                
    $rand rand(12);
                switch(
    $rand){
                    case 
    1:
                        
    $event->setDrops([Item::get(Item::FLINT01)]);
                        break;

                }
            }
        }
    }
    plugin.yml:

    Code:
    name: Rates+
    author: Irish
    version: 1.0.0
    api:
        -1.0.0
        -2.0.0
    main: Rates\main
    
  2. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Make sure you're registering the class as an event listener.
    applqpak likes this.
  3. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Hm, ok.
  4. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    It's still not working even after registering it
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    How are you registering it?
    applqpak likes this.
  6. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    Primus likes this.
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Can you show the code after adding the register?
    Primus and applqpak like this.
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Oh. Item::get returns an Item object, not the id. Simply Item::LEAVES is enough.
    Primus and applqpak like this.
  9. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Oh, ok thanks :D
  10. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Nope. still not working. hmm, this actually puzzles me.
  11. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    rand returns an integer, correct? or would I have to use rand_int?
  12. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Here is an updated version with different ways.
    PHP:
    <?php

    namespace Rates;

    use 
    pocketmine\event\block\BlockBreakEvent as Breaker;
    use 
    pocketmine\plugin\PluginBase as P;
    use 
    pocketmine\event\Listener as L;
    use 
    pocketmine\item\Item;

    class 
    Main extends implements {


        public function 
    onEnable()
        {
            
    $this->getLogger()->info('Rates+ has been enabled on the server!');
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }

        
    /**
         * @param Breaker $event
         */
        
    public function onBreak(Breaker $event){
            
    $block $event->getBlock()->getId();
            
    $random rand(12);
            switch(
    $block){
                case 
    18:
                    if(
    $random === 1) {
                        
    $event->setDrops(array(Item::get(Item::APPLE01)));
                    }
                    break;
                case 
    161:
                    if(
    $random === 1) {
                        
    $event->setDrops(array(Item::get(Item::APPLE01)));
                    }
                    break;
                case 
    13:
                    if(
    $random === 1) {
                        
    $event->setDrops(array(Item::get(Item::FLINT01)));
                    }
                    break;
            }

            switch(
    $random){
                case 
    1:
                    if(
    $block === 18){
                        
    $event->setDrops(array(Item::get(Item::APPLE01)));
                    }
                    if(
    $block === 161){
                        
    $event->setDrops(array(Item::get(Item::APPLE01)));
                    }
                    if(
    $block === 13){
                        
    $event->setDrops(array(Item::get(Item::FLINT01)));
                    }
                    break;
            }
        }
    }
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Tip:
    PHP:
    $random mt_rand(01# 0 - represents false and 1 - true

    if($random){
        
    # ON
    } else {
        
    # OFF
    }
    That's how computers are working.

    And are you sure that you want the chance of getting those items so big? Currently the chance are 50/50
    PHP:
    if(!mt_rand(010)){}
    Example above the chance is 10/100

    Make sure that event is actually fired by outputting something.
    PHP:
    public function(\pocketmine\event\block\BlockBreakEvent $e){
            echo 
    "Block {$e->getBlock()->__toString()} breaked. \n"
    }
  14. Irish
    Offline

    Irish Active Member

    Joined:
    Apr 24, 2016
    Posts:
    96
    Minecraft User:
    _Irish_
    Ok, those chances were just testing, I'll try that out. Thanks

Share This Page

Advertisement