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

Hit sheep

Comments in 'Plugin Development' started by JackboyPlay, Jul 9, 2016.

  1. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Hi how can I do an action if I hit a green, red, ... sheep?
  2. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    Use EntityDamageByEntityEvent then you check the nbt tag color of entity (instanceof Sheep)
  3. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    Anyway, in PocketMine the mobs aren't implemented yet.
  4. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Example?
  5. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    About?
  6. Survingo
    Offline

    Survingo Active Member

    Joined:
    Dec 6, 2015
    Posts:
    221
    Color:(0-15) - Specifies the color of the sheep (0=white, 15=black, etc.)



    Colors: Click
  7. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    How can I check the color from the sheep I work not often with nbt and so
  8. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    Try this (I don't know if works)
    PHP:
    public function onEntityDamageByEntityEvent(EntityDamageByEntityEvent $ev){
            
    $player $ev->getDamager();
            
    $sheep $ev->getEntity();
            if(!(
    $player instanceof Player)) return;
            if(!(
    $sheep instanceof Sheep)) return;
      
            
    $color $this->getSheepColor($sheep);
            
    $this->setSheepColor($sheep$color);
            
    $color++;

            if(
    $color 15)
                
    $color 0;
            
    $ev->setCancelled(true); //Prevent killing of sheep
    }

    private function 
    getSheepColor(Sheep $sheep){
            return 
    $sheep->namedtag["color"];
    }

    private function 
    setSheepColor(Sheep $sheepint $color){
            
    $sheep->namedtag->Color = new IntTag("color"$color);
    }
    Last edited: Jul 10, 2016
    SOFe, JackboyPlay and Survingo like this.
  9. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Thank u :D
  10. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    You have to use EntityDamageEvent.
  11. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    Yes but no, with EntityDamageEvent you can't check who is the attacker, with EntityDamageByEntityEvent you can do this.
  12. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    I recommend using DataPacketReceiveEvent with InteractPacket. You can use it to distinguish between left-clicks (click) and right-clicks (long-tap) (like shears), using the InteractPacket::$action field and comparing it against the constants in the InteractPacket class.
  13. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    What I mean is you can't directly handle EntityDamageByEntityEvent.
    Vaivez66 likes this.
  14. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    You can check the attacker with
    PHP:
    $damager $event->getEntity()->getLastDamageCause()->getDamager(); if($damager instanceof Player){
    $event->getEntity()->sendMessage("A player hits you");
    }
  15. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    PHP:
    if($event instanceof \pocketmine\event\entity\EntityDamageByEntityEvent){/*code*/}
  16. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Just use DataPacketReceiveEvent...

    PHP:
    function ev_pkRecv(\pocketmine\network\protocol\DataPacketReceiveEvent $ev){
      
    $pk $ev->getPacket();
      if(
    $pk->pid() === \pocketmine\network\protocol\Info::INTERACT_PACKET){
        
    $type $pk->action;
        if(
    $type === \pocketmine\network\protocol\InteractPacket::ACTION_LEFT_CLICK){
          
    $eid $pk->target;
          
    $entity $ev->getPlayer()->getLevel()->getEntity($eid);
          if(
    $entity instanceof \pocketmine\entity\Sheep){
            
    $color $entity->namedTag["Color"];
            if(
    $color === YOUR_TARGET_COLOR){ // ref: http://minecraft.gamepedia.com/Data_values#Wool.2C_Stained_Clay.2C_Stained_Glass_and_Carpet
              
    executeTarget();
            }
          }
        }
      }
    }
  17. Mat
    Offline

    Mat Active Member Plugin Developer

    Joined:
    Jun 15, 2014
    Posts:
    179
    Plugins:
    1
    Minecraft User:
    matcracker
    It's already instance of EntityDamageByEntityEvent :/
  18. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    We mean you cannot directly handle EntityDamageByEntityEvent.
    You have to do this:
    PHP:
    public function onDamage(EntityDamageEvent $event){
        if(
    $event instanceof EntityDamageByEntityEvent){
            
    //Stuff
        
    }
    }
    Last edited: Jul 13, 2016
    jojoe77777 likes this.
  19. jojoe77777
    Offline

    jojoe77777 Notable Member Plugin Developer

    Joined:
    May 28, 2015
    Posts:
    653
    Plugins:
    1
    Minecraft User:
    jojoe77777
    *instanceof
    Legoboy0215 likes this.
  20. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    And why not use DataPacketReceiveEvent... It is much better than trying to detect an entity harming another entity, which can be of myriad reasons... Even an arrow shooting that entity is an EntityDamageByEntityEvent... And any players who damaged another player through a plugin-defined function can make it impossible to check whether it is called by clicking a ship...

Share This Page

Advertisement