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

In_Array

Comments in 'Plugin Development' started by xFlare, Feb 22, 2015.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I'm sort of using a slow method of saving usernames, Rather then putting them in an "in_array" I put them in a text file. How can I add things to an "in_array", I only know how to set\check things in them.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Example please?
  3. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Lets say I am making a plugin to mute players, Normally I would add the muted player in a text file, then an if statement will check if the player is in the file or not. This method is slow.

    The new method I want to learn is to put the player in an "in_array" and then check if he is in the array, to be able to chat or not. I think this method will greatly improve speed.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    in_array() is a function. You can't put something into a function.
    iJoshuaHD likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Anyway, this may be what you want:
    PHP:
    public function onEnable(){
      
    // TODO: registerEvents
      // load the mutes from mutes.txt
      
    $this->mutes array_filter(explode(PHP_EOLfile_get_contents($this->getDataFolder() . "mutes.txt"))); // array_filter will filter out the empty lines
    }
    public function 
    onChat(PlayerChatEvent $e){
      if(
    $this->isMuted($e->getPlayer()->getName())){
        
    $e->setCancelled();
      }
    }
    // API functions: call them from commands and handlers
    public function mute($name){
      if(
    $this->isMuted($name)){
        return 
    false// player is already muted!
      
    }
      
    $this->mutes[] = strtolower($name);
      return 
    true;
    }
    public function 
    unmute($name){
      if(!
    $this->isMuted($name$key)){ // $key is passed as a reference parameter (&$key below). It doesn't need to be initialized. It will be changed after the function call ends.
        
    return false;
      }
      unset(
    $this->mutes[$key]); // $key is the key of the name in the array.
    }
    public function 
    isMuted($name, &$key null){ // &$key is a reference parameter, which means it will change after it is passed to this function. This is to avoid searching the array twice as in unmute()
      
    return ($key array_search(strtolower($name), $this->mutes)) !== false;
    }
    public function 
    onDisable(){
      
    file_put_contents($this->getDataFolder() . "mutes.txt"implode(PHP_EOL$this->mutes));
    }
    iJoshuaHD and xFlare like this.
  6. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    xFlare likes this.
  7. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
  8. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    To remove value in an array:

    foreach(array_keys($array,$value_you_want_to_delete) as $value){
    unset($array[$value]);}
    xFlare likes this.
  9. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I suppose the players ID will have to be saved in the array.
  10. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Huh
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What! Then why don't you just do $array = [];!
    Just use $array[] = new_value; no need for array_push().
    64FF00 likes this.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, the ID is dynamic, it is changed when the player rejoins.
  13. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    There are various ways to do things. It doesnt matter as long as it works.
  14. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    But, @PEMapModder knows the fastest ways xD
    PEMapModder likes this.
  15. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    In that case youre telling us that bad practices are fine because they work. :p
  16. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    its not a bad practice :eek: its just a different method
  17. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    http://php.net/manual/en/function.array-push.php

    Then in the comments there is a speed comparison:

    PEMapModder likes this.
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And then you always complain to me how your server lags.

    As I said (and @PocketMine retweeted :p), it is not hard to learn plugin development. It is to optimize the efficiency, quality and readability of your code that tests your real ability.
    64FF00 likes this.
  19. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    the only lag so far im concerned is the mysql query method -,-
    64FF00 likes this.
  20. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00

Share This Page

Advertisement