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
    (the duplicate setting i told you only happens when you set him on the red team, but blue working)
  2. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    That makes the problem easier to fix.
  3. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    i got confused.
    i deleted all plugins, except mine,
    it ALWAYS Cancel EntityDamageEvent..
    and the thing where it removes the player from the team if he's there doesn't work
  4. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Deleting plugins mostly doesn't fix it.
    please send the whole code with my "fixes" again please
    Taha_The_Hacker likes this.
  5. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    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->bluetrue)){
                    unset(
    $this->blue[$p]);
                }
                
    array_push($this->red$p);
            }
       
            if(
    strtolower($team) === "blue"){
                if(
    in_array($p$this->redtrue)){
                    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()) === true){
        
    $event->setCancelled(true);
        
    $event->getDamager()->sendMessage($event->getEntity()->getName() . " is in your team!");
      }


    }
    }

    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;
                    }
            }
        }


    /*public function onCmd(PlayerCommandPreprocessEvent $event){
            $cmd = explode(" ", $event->getMessage());
            $player = $event->getPlayer();
    foreach($cmd as $a){
            if(strpos($cmd[1], "/") === true){
              $this->getLogger()->warning($player . " has used " . $a);
            }
          }
        }
    }//Class
    */
  6. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Try this.
    PHP:
    <?php

    namespace MCrafters\TeamPvP;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat as Color;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;
    use 
    pocketmine\Player;

    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(
    $this->getTeam($p1) === $this->getTeam($p2) && $this->getTeam($p1) !== false){
                return 
    true;
            }
            else{
                return 
    false;
            }
        }
        
    // isFriend
        
    public function getTeam($p){
            if(
    in_array($p$this->red)){
                return 
    "red";
            }
            elseif(
    in_array($p$this->blue)){
                return 
    "blue";
            }
            else
                return 
    false;
        }

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

        public function 
    onInteract(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 
    onEntityDamage(EntityDamageEvent $event){
            if(
    $event instanceof EntityDamageByEntityEvent){
                if(
    $event->getEntity() instanceof Player){
                    if(
    $this->isFriend($event->getDamager()->getName(), $event->getEntity()->getName())){
                        
    $event->setCancelled(true);
                        
    $event->getDamager()->sendMessage($event->getEntity()->getName() . " is in your team!");
                    }
                    elseif(!
    $this->isFriend($event->getDamager()->getName(), $event->getEntity()->getName())){
                        
    $event->getDamager()->sendMessage("You 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":
                                {
                                    if(
    $sender instanceof Player){
                                        
    $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;
                                    }
                                    else
                                        return 
    false;
                                }
                            case 
    "blue":
                                {
                                    if(
    $sender instanceof Player){
                                        
    $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;
                                    }
                                    else
                                        return 
    false;
                                }
                            case 
    "var":
                                {
                                    
    var_dump($this->red);
                                    
    var_dump($this->blue);
                                    return 
    true;
                                }
                            default:
                                {
                                    return 
    false;
                                }
                        }
                    }
                default:
                    {
                        return 
    false;
                    }
            }
        }
       
        
    /*
         * public function onCmd(PlayerCommandPreprocessEvent $event){
         * $cmd = explode(" ", $event->getMessage());
         * $player = $event->getPlayer();
         * foreach($cmd as $a){
         * if(strpos($cmd[1], "/") === true){
         * $this->getLogger()->warning($player . " has used " . $a);
         * }
         * }
         * }
         * }//Class
         */
    }
    I use $this->getTeam() to check if the players are in the same team
    Taha_The_Hacker likes this.
  7. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Perfect, Thank you :)
    but just the thing that removed the player from the team doesn't work :/ (unset)
    thebigsmileXD likes this.
  8. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Yea, i currently experience just the same problem in my Auth system. does $this->red[$p] = null; work?
    And you're welcome :D Thanks for giving me something to do ^^
    Taha_The_Hacker likes this.

Share This Page

Advertisement