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

Same Team Damage Cancel

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

  1. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    I have no idea why players on the same team can still damage each other, can you see anything wrong in the code. There were no console errors just this one bug.

    PHP:
    <?php

    namespace Team\Teams;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    //use pocketmine\event\entity\EntityDamageByChildEntityEvent;
    use pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\inventory\InventoryPickupItemEvent;
    use 
    pocketmine\event\player\PlayerDropItemEvent;
    use 
    pocketmine\level\sound\ClickSound;
    use 
    pocketmine\level\sound\AnvilFallSound;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Level;
    use 
    pocketmine\entity\Effect;

    class 
    TeamMain extends PluginBase implements Listener{
       
        public 
    $economyAPI null;
        public 
    $messages$cf;
       
        
    //private $TeamHistoryTracker;
       
        
    public function onEnable(){
            
    $this->saveDefaultConfig();
            
    //$this->TeamHistoryTracker = new TeamHistoryTracker($this);
            
    @mkdir($this->getDataFolder());
            @
    mkdir($this->getDataFolder()."Players/");
            
    $this->getLogger()->info(TextFormat::DARK_AQUA "has been successfully enabled");
            
    $this->getServer()->getPluginManager()->registerEvents($this ,$this);
            
    $this->config2 = new Config($this->getDataFolder() . "/Team.yml"Config::YAML);
            
    $this->cf = (new Config($this->getDataFolder() . "/TeamKillMoney.yml"Config::YAML, ["Amount" => 100]))->getAll();
            
    $this->config2->save();
            if(
    $this->getServer()->getPluginManager()->getPlugin("EconomyAPI") != null){
                
    $this->economyAPI = \onebone\economyapi\EconomyAPI::getInstance();
            }else{
                
    $this->getLogger()->error(TextFormat::RED ."The plugin EconomyAPi by onebone is missing!");
                
    $this->getServer()->getPluginManager()->disablePlugin($this);
            }
        }
       
        
    /*public function getTeamHistoryTracker(){
            return $this->TeamHistoryTracker;
        }*/
       
        
    public function onPickup(InventoryPickupItemEvent $event){
            
    $inv $event->getInventory();
            if(!(
    $inv instanceof PlayerInventory)){
                return;
            }
            
    $player $inv->getHolder();
            if(!(
    $player instanceof Player)){
                return;
            }
            
    $p $event->getPlayer();
            
    $item $event->getItem();
            
    $fizz = new AnvilFallSound($p);
            
    $p->getLevel()->addSound($fizz);
            
    $amount $this->cf["MoneyAmount"];
            
    $this->economyAPI->addMoney($player$amount);
            
    $this->PlayerFile->set("EXP",$amount);
            
    $this->PlayerFile->save();
        }
       
        public function 
    onDrop(PlayerDropItemEvent $event){
                
    $player $event->getPlayer();
                if(
    $player->getInventory()->getItemInHand()->getId() === Item::EMERALD){
                    
    $event->setCancelled(true);
                }
            }
       
        public function 
    onCommand(CommandSender $playerCommand $cmd$label, array $args){
            switch(
    $cmd->getName()){
                case 
    "tm":
                    
    //if($player->hasPermission("tm.use")){
                        
    if(!empty($args[0])){
                            if(
    $args[0]=="red"){
                                
    $team "§4[Red]";
                            }else
                                if(
    $args[0]=="blue"){
                                    
    $team "§9[Blue]";
                                }else
                                    if(
    $args[0]=="green"){
                                        
    $team "§a[Green]";
                                    }else
                                        if(
    $args[0]=="yellow"){
                                            
    $team "§e[Yellow]";
                                        }
                            
    $config = new Config($this->getDataFolder() . "/team.yml"Config::YAML);
                            
    $name $player->getName();
                            
    $player->setNameTag($team $name);
                           
    $config->set($name,$team);
                           
    $config->save();
                            
    $player->getInventory()->clearAll();
                            
    $player->getInventory()->addItem(Item::get(32201));
                            
    $player->getInventory()->addItem(Item::get(438215));
                            
    $player->getInventory()->addItem(Item::get(27601));
                            
    $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);
                            
    $effect1 Effect::getEffect (1);
                           
    $effect1->setDuration (999 999);
                           
    $effect1->setAmplifier (2);
                           
    $effect2 Effect::getEffect (5);
                           
    $effect2->setDuration (999 999);
                           
    $effect2->setAmplifier (1);
                           
    $effect3 Effect::getEffect (10);
                           
    $effect3->setDuration (999 999);
                           
    $effect3->setAmplifier (5);
                            
    $player->addEffect($effect1);
                            
    $player->addEffect($effect2);
                            
    $player->addEffect($effect3);
                           
    $player->sendMessage("§l§7§f»§r "TextFormat::AQUA ."You entered to the " $team " §fteam");
                            }else{
                                
    $player->sendMessage(TextFormat::AQUA ."Teams:");
                                
    $player->sendMessage("§l§7§f»§r "TextFormat::RED ."Red");
                                
    $player->sendMessage("§l§7§f»§r "TextFormat::BLUE ."Blue");
                                
    $player->sendmessage("§l§7§f»§r "TextFormat::GREEN ."Green");
                                
    $player->sendMessage("§l§7§f»§r "TextFormat::YELLOW ."Yellow");
                            }
                            return 
    true;
                        }
                    }
           
            
    /*public function onDeath(PlayerDeathEvent $event){
                $ign = $player->getName();
                $this->PlayerFile = new Config($this->getDataFolder()."Players/". $ign .".yml", Config::YAML);
                $event->setDrops(array(Item::EMERALD));
                $inv = $event->getPlayerInventory();
                $player = $event->getPlayer();
                $m = $this->PlayerFile->get("EXP");
                $a = $m - $amount;
                $this->PlayerFile->set("EXP", $a);
               $this->PlayerFile->save();
                if($event instanceof InventoryPickupItemEvent){
                    if($player->getInventory()->getItem()->getId() === Item::EMERALD){
                        $fizz = new AnvilFallSound($player);
                        $player->getLevel()->addSound($fizz);
                        $amount = $this->cf["MoneyAmount"];
                        $this->economyAPI->addMoney($player, $amount);
                        $this->PlayerFile->set("EXP",$amount);
                        $this->PlayerFile->save();
                    }
                }
            }*/
        
    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->save();
                
    $fizz = new AnvilFallSound($player);
                
    $player->getLevel()->addSound($fizz);
            }
        }

    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();
                if((
    strpos($playern"§c[Red]") !== false) && (strpos($damagern"§c[Red]") !== false)){
                    
    $event->setCancelled(true);
                }else if((
    strpos($playern"§9[Blue]") !== false) && (strpos($damagern"§9[Blue]") !== false)){
                    
    $event->setCancelled(true);
                }else if((
    strpos($playern"§a[Green]") !== false) && (strpos($damagern"§a[Green]") !== false)){
                    
    $event->setCancelled(true);
                }else if((
    strpos($playern"§e[Yellow]") !== false) && (strpos($damagern"§e[Yellow]") !== false)){
                    
    $event->setCancelled(true);
                }
            }
        }
    }
    }
    Thanks :)
  2. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Make a function that checks if they're on the same team, and if so cancel the event.
  3. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Irrelevant: but you know you can just do
    PHP:
    $config = new Config($this->getDataFolder() . "config.yml"Config::YAML, array("option1" => "value1"));
    Instead of
    PHP:
    $config = new Config($this->getDataFolder() . "config.yml"Config::YAML);
    $config->set("option1""value1");
    $config->save();
    .... Right?
    AndrewBit and Skullex like this.
  4. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    Thanks :p
    applqpak likes this.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Why even use new Config()? PocketMine already has a config built in, $this->getConfig()
    Skullex, AndrewBit and applqpak like this.
  6. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    newConfig is better to understand this.
    And then they (mostly) don't come up with "I WANT TWO CONFIGS!"
  7. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    WOW thats so important
  8. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Huh?
  9. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    !== false??? What is that? It's != first of all, or why not just true..? Jeez
    Jankirby and ImagicalGamer like this.
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Wrong. Don't post anything you don't really know about. First, !== and != is not the same. Do you know the difference between === and ==? Same difference on both. Also, strpos() does not always return true. So his code is perfectly correct. 'Jeez'
    andre_the_gamer and applqpak like this.

Share This Page

Advertisement