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

Damage Cancel

Comments in 'Plugin Development' started by Skullex, Apr 5, 2016.

  1. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    PHP:
    public function onEntityDamage(EntityDamageEvent $event){
        if(
    $event instanceof EntityDamageByEntityEvent){
            if(
    $event->getEntity() instanceof Player && $event->getDamager() instanceof Player){
                
    $playern $event->getEntity()->getNameTag();
                
    $damagern $event->getDamager()->getNameTag();
                
    $damager $event->getDamager();
                
    $player $event->getEntity();
                if((
    strpos($playern" §c[ RED ] ") !== false) && (strpos($damagern"§c[ RED ] ") !== false)){
                        
    $event->setCancelled();
                    }else if((
    strpos($playern" §9[ BLUE ] ") !== false) && (strpos($damagern"§9[ BLUE ] ") !== false)){
                                
    $event->setCancelled();
                            }else if((
    strpos($playern" §a[ GREEN ] ") !== false) && (strpos($damagern"§a[ GREEN ] ") !== false)){
                                        
    $event->setCancelled();
                                    }else if((
    strpos($playern" §e[ YELLOW ] ") !== false) && (strpos($damagern"§e[ YELLOW ] ") !== false)){
                                                
    $event->setCancelled();
                }
            }
        }
    }
    }
    This fails to work right, it was working before but now it just stopped. What have I done wrong?? Do you need the entire code? Also there were no console errors!
  2. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Could you show us the nametag formats
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Actually, why do you use !== false? Also, what about debugging?
  4. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    On strpos you mostly have to use not false instead of is true, you can look it up on php.net
    applqpak and PEMapModder like this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    strpos() !== false is different from strpos(). strpos() can return 0, which will be casted to false if used as a boolean. You have to use !== false to make sure that it is returning an integer, not returning that the needle doesn't exist in the haystack.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP is made to create dynamic content -.-
    applqpak likes this.
  7. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    PHP:
    public function onCommand(CommandSender $playerCommand $cmd$label, array $args){
            switch(
    $cmd->getName()){
                case 
    "tm":
                    if(!empty(
    $args[0])){
                        if(
    $args[0]=="red"){
                            
    $team "§4Red ";
                        }else
                            if(
    $args[0]=="blue"){
                                
    $team "§9Blue ";
                            }else
                                if(
    $args[0]=="green"){
                                    
    $team "§aGreen ";
                                }else
                                    if(
    $args[0]=="yellow"){
                                        
    $team "§eYellow ";
                                    }
                        
    $config = new Config($this->getDataFolder() . "/team.yml"Config::YAML);
                        
    $name $player->getName();
                        
    $player->setNameTag($teamcolor $name);
                        
    $config->set($name,$team);
                        
    $config->save();
                        
    $player->getInventory()->setHelmet(Item::get(30201));
                       
    $player->getInventory()->setChestplate(Item::get(30301));
                       
    $player->getInventory()->setLeggings(Item::get(30401));
                       
    $player->getInventory()->setBoots(Item::get(30501));
                       
    $player->getInventory()->sendArmorContents($player);
                        
    $player->sendMessage("§l§7§f»§r You entered to the " $team " §fteam");
                    }else{
                        
    $player->sendMessage(TextFormat::AQUA ."Teams:");
                        
    $player->sendMessage(TextFormat::RED ."Red");
                        
    $player->sendMessage(TextFormat::BLUE ."Blue");
                        
    $player->sendmessage(TextFormat::GREEN ."Green");
                        
    $player->sendMessage(TextFormat::YELLOW ."Yellow");
                        
    $player->sendMessage(TextFormat::AQUA ."");
                    }
                    return 
    true;
            }
        }
          
        public function 
    onDeath(PlayerDeathEvent $event){
            
    $ign $event->getPlayer()->getName();
            
    $player $event->getPlayer();
            
    $file = ($this->getDataFolder()."Players/".$ign.".yml");
            if(!
    file_exists($file)){
                
    $this->PlayerFile = new Config($this->getDataFolder()."Players/".$ign.".yml"Config::YAML);
                
    $this->PlayerFile->set("Money",0);
                
    $this->PlayerFile->set("EXP",0);
                
    $this->PlayerFile->save();
                
    $fizz = new AnvilFallSound($player);
                
    $player->getLevel()->addSound($fizz);
            }
        }

    public function 
    onEntityDamage(EntityDamageEvent $event){
        if(
    $event instanceof EntityDamageByChildEntityEvent){
                
    $entity $event->getDamager();
                if(
    $entity instanceof Player){
                    
    $ses $this->main->getSessions()->getSession($entity);
                    if(
    $ses instanceof Session and $ses->inSession($this)){
                        
    $data $this->getPlayerData($ses);
                        if(
    $data->isPlaying() and $data->getArena()->isPlaying()){
                            
    $projectile $event->getChild();
                            if(
    $projectile instanceof Snowball){
                                
    $event->setCancelled(false);
                                
    $event->setKnockBack($event->getKnockBack() * 2);
                                if(
    $event instanceof EntityDamageByEntityEvent){
                                    if(
    $event->getEntity() instanceof Player && $event->getDamager() instanceof Player){
                                        
    $playern $event->getEntity()->getNameTag();
                                        
    $damagern $event->getDamager()->getNameTag();
                                        
    $damager $event->getDamager();
                                        
    $player $event->getEntity();
                                        if((
    strpos($playern"§4Red §l§f»§r ") !== false) && (strpos($damagern"§4Red §l§f»§r ") !== false)){
                                            
    $event->setCancelled();
                                        }else if((
    strpos($playern"§9Blue §l§f»§r ") !== false) && (strpos($damagern"§9Blue §l§f»§r ") !== false)){
                                            
    $event->setCancelled();
                                        }else if((
    strpos($playern"§aGreen §l§f»§r ") !== false) && (strpos($damagern"§aGreen §l§f»§r ") !== false)){
                                            
    $event->setCancelled();
                                        }else if((
    strpos($playern"§eYellow §l§f»§r ") !== false) && (strpos($damagern"§eYellow §l§f»§r ") !== false)){
                                            
    $event->setCancelled();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
        }
    }
    }
    I've added more to EntityDamage and the nametag formats are at the top
  8. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    'Based on the interests or past behavior of the viewer.'
    I quote Interest's
  9. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Try without spaces like
    PHP:
     if((strpos($playern"§c[ RED ]") !== false) && (strpos($damagern"§c[ RED ]") !== false)){ 
    Skullex likes this.
  10. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    Sure, I'll try that
  11. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    "!== false" is useless.
    If a value returns true, the function if will run the code.
    PHP:
    $boolean true;
    // use
    if($boolean);
    // and 
    if($boolean !== false);
    // are the same!
    Skullex likes this.
  12. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Read before replying?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What if the variable is not a boolean?
    PHP:
    switch($condition){
      case 
    1$i false; break;
      case 
    2$i 0; break;
      default: 
    $i 1; break;
    }
    if(
    $i){
      echo 
    '$i is a nonzero int';
    }
    if(
    $i !== false){
      echo 
    '$i is an int';
    }
    And this situation is true for strpos(). For details, read my post above. strpos() can return 0.
    Skullex likes this.

Share This Page

Advertisement