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

Whats wrong with this code??

Comments in 'Plugin Development' started by Hittmana, Feb 14, 2016.

  1. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    You want to save stuff per player (that has requested)
    Three ways to do this:
    1. Use an config (EASY)
    2. Use compicated arrays with useless index numbers renames to avoid finding without using unset blah (I did this, i am rewriting to method 3) [STUPID, DIFFICULT]
    3. Use playername as an index in your arrays like iamanarray[Playername] = $item (Not that easy)
    Hittmana and MichaelM04 like this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Please don't try to help if you do not know what you are doing.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Saving session data can be extremely simple. I am doing this in almost every single plugin I have.
    Config is not used for saving temporary data.

    To save data for a player only for that session, create an array with keys as player entity ID.

    The following example demonstrates a simple way to find out how long the player has been online and how many chat messages and words he sent in a session:
    PHP:
    class MainClass extends PluginBase implements Listener{
      private 
    $sessions = [];
      
    // register events in onEnable
      
    public function e_join(PlayerJoinEvent $event){
        
    $this->sessions[$event->getPlayer()->getId()] = new MySession($event->getPlayer());
      }
      public function 
    e_quit(PlayerQuitEvent $event){
        if(isset(
    $this->sessions[$i $event->getPlayer()->getId()])){
          
    $this->sessions[$i]->onQuit($onlineTime$sentMessages$sentWords);
          unset(
    $this->sessions[$i]);
        }
      }
      public function 
    onChat(PlayerChatEvent $event){
        
    $session $this->sessions[$i]; // a player should have already joined when he chats, so no need to check isset
        
    $session->onChat($event->getMessage());
      }
    }

    class 
    MySession{
      private 
    $player;
      private 
    $onlineSince$add 0;
      private 
    $sentMessages$sentWords;

      public function 
    __construct(Player $player){
        
    $this->player $player;
        
    $this->onlineSince microtime(true);
      }
      public function 
    onChat($message){
        
    $this->sentMessages++;
        
    $this->sentWords += count(explode(" "$message));
      }
      public function 
    onQuit(&$onlineTime, &$sentMessages){
        
    $onlineTime $this->getOnlineTime();
        
    $sentMessages $this->sentMessages;
      }
      public function 
    getOnlineTime(){
        
    $soFar $this->onlineSince $this->onlineSince $add;
        
    $this->add $soFar;
        
    $this->onlineSince microtime(true);
        return 
    $soFar;
      }
    }
    This code snippet is simple if you understand object-oriented programming and other basic programming concepts. This is not closely related to this thread, but this may help you with your question.

    Now, to store data between two players, this is an example of basic trade requests:
    PHP:
    class TradeManager implements Listener{
      private 
    $trades = [];

      
    // register events

      /**
       * Returns a unique value that represents the combination (not permutation) of "$name1 and $name2", where $name1 and $name2 can be swapped
       * @param string $name1
       * @param string $name2
       * @return string
      public static function biNameHash($name1, $name2){
        $name1 = strtolower($name1);
        $name2 = strtolower($name2);
        return ($name1 > $name2) ? "$name1:$name2" : "$name2:$name1";
        // yes, you can use > to compare two strings. "b" > "a". It is case-sensitive, but I strtolower()'ed them above
      }

      public function tradeWith(Player $from, Player $to, Item $item){
        $hash = self::biNameHash($from->getName(), $to->getName());
        if(isset($this->trades[$hash])){
          $trade = $this->trades[$hash];
          $trade->execute();
        }else{
          $this->trades[$hash] = new Trade($from, $to, $item);
         
      }
      public function cancelTrade($hash, $reason){
        if(!isset($this->trades[$hash])) return;
        $trade = $this->trades[$hash];
        $trade->broacast("Cancelled trade: " . $reason);
        unset($this->trades[$hash]);
      }
      public function e_quit(PlayerQuitEvent $event){
        foreach($this->trades as $trade)
          if($trade->isParticipating($event->getPlayer())) $this->cancelTrade($trade);
      }
    }
    class Trade{
      /** @var Player the players who requested or got requested */
      
    private $from$to;
      
    /** @var Item the items that from and to are offering */
      
    private $fromItem$toItem;

      
    // constructor omitted

      
    public function isParticipating(Player $player){
        return 
    $player === $this->from or $player === $this->to;
      }
      public function 
    broadcast($message){
        
    $this->from->sendMessage($message);
        
    $this->to->sendMessage($message);
      }
      public function 
    validate(){
        
    // TODO: iterate through both player inventories to confirm that they have the correct items
        // also check they are both online first
      
    }
      public function 
    execute(){
        
    $this->from->getInventory()->removeItem($this->fromItem);
        
    $this->to->getInventory()->removeItem($this->toItem);
        
    $this->from->getInventory()->addItem($this->toItem);
        
    $this->to->getInventory()->removeItem($this->fromItem);
      }
    }
    And remember not to copy any of these. They are not expected to work at all. Just to show the concept.
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    I used PlayerName as index you used Entity as Index... EntityID is better, because??? I'm just curious!
    And yes, config is not inted for storing temp stuff
    But for his Trading stuff he should aave it to config, because if the server restarts the trade requests will be loaded again.
  5. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ok thanks! This should help me with a BUNCH of plugins!
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Using integers as array keys is faster. Also, it is different every session, so you would notice something strange if something went wrong.
    Tim // robske Büba likes this.
  7. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    thanks

Share This Page

Advertisement