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

How to get only 1 team?

Comments in 'Plugin Development' started by korado531m7, Apr 20, 2016.

  1. korado531m7
    Offline

    korado531m7 Active Member

    Joined:
    Apr 4, 2015
    Posts:
    158
    Minecraft User:
    korado531m7
    I'm trying to make a plugin that using team.
    This is my first teaming plugin.
    I'm going to use global variable.
    Here's the code
    PHP:
    //$name is player name
    $this->team[$name] = "TEAM COLOR"//Ex, blue
    to set the team color,
    PHP:
    $this->team[$name] = "red";
    $this->team[$name] = "blue";
    $this->team[$name] = "yellow";
    $this->team[$name] = "cyan";
    $this->team[$name] = "purple";
    $this->team[$name] = "black";
    I want to make six teams.
    I can set the team color, and get these.
    But there is a problem.

    How to get if only 1 team color reaming?
    I can't type this code, so continuing battle (in other words, not finish that battle).

    how to write this? please tell me.
    Thank you for reading my thread, and answering.
  2. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    PHP:
    $array_of_colors = array (
    "blue",
    "red"
    //add all your colors.
    );
    $this->team[$name] = "red";
    unset(
    $array_of_colors[array_search($this->team[$name], $array_of_colors)]); //remove the color red from array.
    And if the array amount gets to 1 or less, you will know you ran out of colors..
    applqpak likes this.
  3. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    use an array like this:

    PHP:
    //to add a player
    $teams['red'][] = $playername;

    if(empty(
    $teams['red'])){
        
    //the array is empty, no one is in the team
        //code here
    }
  4. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    PHP:
    array_search($needle$array);
  5. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Yes. Search the color ($this->team[$name]) in the array ($array_of_colors).
  6. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    $this->team[$name] is say Team
    Like
    PHP:
    $k{"nawaf"} = "red";
    Now Say A Team Name Not A Player Name
  7. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Look at the top post.
    PHP:
    $this->team[$name] = "red";
    $this->team[$name] is equal to red, not a team name.
    This is a team color stored, and the team color is accessed using team name $team.
  8. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    I think i donot understand this thread
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    You can't create global variable in OOP only new class properties :p

    When I'm creating teaming plugin I always use sessions
    PHP:
    <?php

    class Session {

       private 
    $data = [];

       private static 
    $defaultData = [
         
    'team' => null,
         
    'god' => false,
         
    'frozen' => false,
         
    'coins' => 0,
         
    // And so on
        
    ];

       
    // I made this static because you won't have to pass it again and again
       // When creating new Session
       
    public static $plugin;

       public function 
    __construct(Player $player)
       {
         
    $this->load($player);
       }

       public function 
    load(Player $player)
       {
         
    // Depends on your plugin structure
         // But mostly you'll load player data from data provider
         // Or create new data file/row for this player
         // If he doesn't have any saves before
       
    }

       public function 
    getPlayer() : Player
       
    {
         return 
    $this->player;
       }

       public function 
    set($key$value)
       {
         
    $this->data[$key] = $value;
       }

       public function 
    getSessionData() : array
       {
         return 
    $this->data;
       }

       public function 
    close()
       {
         
    $this->save();
       }

    }

  10. korado531m7
    Offline

    korado531m7 Active Member

    Joined:
    Apr 4, 2015
    Posts:
    158
    Minecraft User:
    korado531m7
    Must I create a new class?
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    It's a good practice but if you are only beginner in plugin development then I don't suggest you to go hardcore yet. Use arrays for now
  12. korado531m7
    Offline

    korado531m7 Active Member

    Joined:
    Apr 4, 2015
    Posts:
    158
    Minecraft User:
    korado531m7
    It's my first 'team plugin'.
    So, I'll try array, and if done I use class.
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Lemme help
    PHP:
       protected $teams = [
         
    'red' => [],
         
    'blue' => [],
         
    'yellow' => [],
         
    'green' => [],
       ];

       public function 
    setTeam(Player $player$team)
       {
         if(isset(
    $this->teams[$team]){
           if(
    $this->getPlayerTeam($player) === null)
           {
             
    $this->teams[$team][$player->getName()] = $player;
             return 
    true;
           }
         }
         return 
    false;
       }

       public function 
    getPlayerTeam(Player $player)
       {
         foreach(
    $this->teams as $team)
         {
           foreach(
    $team as $member)
           {
             if(
    $player === $member) return $team;
           }
         }
         return 
    null;
       }

       public function 
    kickFromTeam(Player $player)
       {
         
    $team $this->getPlayerTeam($player);
         if(!
    $team) return false;
         unset(
    $this->teams[$team][$player->getName()];
         return 
    true;
       }
  14. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b

Share This Page

Advertisement