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

Solved What's Wrong in my code?

Comments in 'Plugin Development' started by Taha_The_Hacker, Nov 11, 2015.

?

Is it my fault or PM's ?

  1. PM

    0 vote(s)
    0.0%
  2. Your Fualt

    100.0%
  1. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Hi,

    umm.. it should cancel EntityDamageByEntityEvent..
    but it doesn't..
    sometimes it does in ANY team...
    sometimes it doesn't in all teams..
    doesn't work.
    what's wrong?
    PHP:
    <?php

    namespace MCrafters\TeamPvP;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as Color;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\item\item;
    use 
    pocketmine\event\Event;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\PluginCommand;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;


    class 
    TeamPvP extends PluginBase implements Listener {
       
       
    //Teams
        
    public $red = [];
        public 
    $blue = [];
        public 
    $yml;
       
       
      public function 
    onEnable(){
         
    //Initializing config files
         
          
    $this->saveResource("config.yml");
          
    $yml = new Config($this->getDataFolder() . "config.yml"Config::YAML);
          
    $this->yml $yml->getAll();
         
          
    $this->getLogger()->debug("Config files have been saved!");
         
          
    $this->getServer()->getPluginManager()->registerEvents($this$this);
          
    $this->getServer()->getLogger()->info(Color::BOLD Color::GOLD "M" Color::AQUA "TeamPvP " Color::GREEN "Enabled" Color::RED "!");
        }
       
      public function 
    isFriend($p1$p2){
     if(
    in_array($p1$this->redtrue) && in_array($p2$this->redtrue)){
     return 
    true;

     }
    elseif(
    in_array($p1$this->bluetrue) && in_array($p2$this->bluetrue)){
     return 
    true;
     } else{
     return 
    false;
     }
     }
    //isFriend

      
    public function getTeam($p){
        if(
    in_array($p$this->redtrue)){
          return 
    "red";
        } elseif(
    in_array($p$this->bluetrue)){
          return 
    "blue";
        }
      }

      public function 
    setTeam($p$team){
        
    $red  array_search($parray_keys($this->red));
        
    $blue array_search($parray_keys($this->blue));
       
            if(
    strtolower($team) === "red"){
                if(
    in_array($p$this->blue)){
                    unset(
    $this->blue[$p]);
                }
                
    array_push($this->red$p);
            }
       
            if(
    strtolower($team) === "blue"){
                if(
    in_array($p$this->red)){
                    unset(
    $this->red[$p]);
                }
                
    array_push($this->blue$p);
            }

    }
      public function 
    Interact(PlayerInteractEvent $event){
        
    $teams = array("red""blue");
        
    $b $event->getBlock();
        if(
    $b->getX() === $this->yml["sign_join_x"] && $b->getY() === $this->yml["sign_join_y"] && $b->getZ() === $this->yml["sign_join_z"]){
          if(
    count($this->red 5) && count($this->blue 5)){
        
    $this->setTeam($event->getPlayer()->getName(), array_rand($teams1));
              
    $event->getPlayer()->inGame true;
    $event->getPlayer()->teleport(new Vector3($this->yml["blue_enter_x"], $this->yml["blue_enter_y"], $this->yml["blue_enter_z"]));

          }elseif(
    count($this->red 5)){
          
    $this->setTeam($event->getPlayer()->getName(), "red");
          
    $event->getPlayer()->inGame true;
          
    $event->getPlayer()->teleport(new Vector3($this->yml["red_enter_x"], $this->yml["red_enter_y"], $this->yml["red_enter_z"]));
        } elseif(
    count($this->blue) < 5){
          
    $this->setTeam($event->getPlayer()->getName(), "blue");
          
    $event->getPlayer()->inGame true;
          
    $event->getPlayer()->teleport(new Vector3($this->yml["blue_enter_x"], $this->yml["blue_enter_y"], $this->yml["blue_enter_z"]));
        } else {
          
    $event->getPlayer()->sendMessage("Teams are full");
        }
        }
      }

    public function 
    EntityDamage(EntityDamageEvent $event){
       if(
    $event instanceof EntityDamageByEntityEvent){
      if(
    $this->isFriend($event->getDamager()->getName(), $event->getEntity()->getName())){
        
    $event->setCancelled(true);
        
    $event->getDamager()->sendMessage($event->getEntity()->getName() . " is in your team!");
      }

      if(
    $event->isCancelled()){
        
    $this->getLogger()->info($event->getDamager()->getName() . " has hit " $event->getEntity()->getName());
      }
    }
    }

    public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
        
    $teams = array("red""blue");
    switch(
    $cmd->getName()){
          case 
    "team":
          switch(
    strtolower($args[0])){
            case 
    "red":
        
    $this->setTeam($sender->getName(), "red");
          
    $sender->inGame true;
    $sender->teleport(new Vector3($this->yml["red_enter_x"], $this->yml["red_enter_y"], $this->yml["red_enter_z"]));

            case 
    "blue":
                  
    $this->setTeam($sender->getName(), "blue");
          
    $sender->inGame true;
          
    $sender->teleport(new Vector3($this->yml["blue_enter_x"], $this->yml["blue_enter_y"], $this->yml["blue_enter_z"]));

            case 
    "var":
            
    var_dump($this->red);
            
    var_dump($this->blue);
        }
    }
    }
    }
    //Class
    Thanks for your help
    Last edited: Nov 12, 2015
  2. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Nope. Oops.
  3. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Please help us locate the bug by doing var_dump() on critical values, such as $this->red, return of isFriend(), etc.
    Taha_The_Hacker and Legoboy0215 like this.
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    BTW, you don't need === true. Remember what is a if() control structure?

    Code INSIDE the brackets execute when the statement is true
  6. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    i tried removing that too.
  7. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    This part has a bug. Ony red teams can be "friends".
    In the spoiler will be the reason why, but maybe you'll figure out first ;)
    PHP:
    public function isFriend($p1$p2){
     if(
    in_array($p1$this->redtrue) && in_array($p2$this->redtrue)){//if both are in team red
     
    return true;
     
     } else{
     
     return 
    false//you return, before you even check the blue team
     
     
    }
     if(
    in_array($p1$this->bluetrue) && in_array($p2$this->bluetrue)){ // here you check the blue team. But this will never be checked.
     
    return true;
     } else{
     return 
    false;
     }
     }
    //isFriend
    The fix:
    PHP:
     public function isFriend($p1$p2){
     if(
    in_array($p1$this->redtrue) && in_array($p2$this->redtrue)){
     return 
    true;
     
     }
    elseif(
    in_array($p1$this->bluetrue) && in_array($p2$this->bluetrue)){
     return 
    true;
     } else{
     return 
    false;
     }
     }
    //isFriend
    Taha_The_Hacker likes this.
  8. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    yea!
    but..
    now it only keeps saying (He's on your team!)...
  9. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    going to use var_dump..
  10. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Now var_dump()..
    it said that the two players are in the blue array..
    so it only puts the players in "blue" array..
    let me check
  11. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    You check in isset if array_search returns it. remember that array_search returns false if not found, else an array.
    Taha_The_Hacker likes this.
  12. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    umm.. i got confused.
  13. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Have a look into setteam
    PHP:
    array_search($p$this->red);
    Returns:
    mixed the key for needle if it is found in the array, false otherwise.
    PHP:
    if(isset($this->blue[$blue])){
    change it to
    PHP:
    if($blue!==false){
    same for red.
    Taha_The_Hacker likes this.
  14. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    you're right
    but same.
  15. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    PHP:
        public function setTeam($p$team){
            if(
    strtolower($team) === "red"){
                if(
    in_array($p$this->blue)){
                    unset(
    $this->blue[$p]);
                }
                
    array_push($this->red$p);
            }
       
            if(
    strtolower($team) === "blue"){
                if(
    in_array($p$this->red)){
                    unset(
    $this->red[$p]);
                }
                
    array_push($this->blue$p);
            }
        }
    //Edit, whoops, fixed a mistake
    Taha_The_Hacker likes this.
  16. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    umm..
    Code:
    s c Tester1 /team red
    array(1) {
      [0] =>
      string(7) "Tester1"
    }
    array(1) {
      [0] =>
      string(7) "Tester1"
    }
  17. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Where do you dump it?
    Taha_The_Hacker likes this.
  18. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args){
        
    $teams = array("red""blue");
    switch(
    $cmd->getName()){
          case 
    "team":
          switch(
    strtolower($args[0])){
            case 
    "red":
        
    $this->setTeam($sender->getName(), "red");
          
    $sender->inGame true;
    $sender->teleport(new Vector3($this->yml["red_enter_x"], $this->yml["red_enter_y"], $this->yml["red_enter_z"]));

            case 
    "blue":
                  
    $this->setTeam($sender->getName(), "blue");
          
    $sender->inGame true;
          
    $sender->teleport(new Vector3($this->yml["blue_enter_x"], $this->yml["blue_enter_y"], $this->yml["blue_enter_z"]));

            case 
    "var":
            
    var_dump($this->red);
            
    var_dump($this->blue);
        }
    }
    }
  19. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    I Think this'll never get solved..
  20. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args){
            
    $teams = array("red","blue");
            switch(
    $cmd->getName()){
                case 
    "team":
                    {
                        switch(
    strtolower($args[0])){
                            case 
    "red":
                                {
                                    
    $this->setTeam($sender->getName(), "red");
                                    
    $sender->inGame true;
                                    
    $sender->teleport(new Vector3($this->yml["red_enter_x"], $this->yml["red_enter_y"], $this->yml["red_enter_z"]));
                                    return 
    true;
                                }
                            case 
    "blue":
                                {
                                    
    $this->setTeam($sender->getName(), "blue");
                                    
    $sender->inGame true;
                                    
    $sender->teleport(new Vector3($this->yml["blue_enter_x"], $this->yml["blue_enter_y"], $this->yml["blue_enter_z"]));
                                    return 
    true;
                                }
                            case 
    "var":
                                {
                                    
    var_dump($this->red);
                                    
    var_dump($this->blue);
                                    return 
    true;
                                }
                            default:
                                {
                                    return 
    false;
                                }
                        }
                    }
                default:
                    {
                        return 
    false;
                    }
            }
        }
    }
    Taha_The_Hacker likes this.

Share This Page

Advertisement