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

Tutorial Calculate experience

Comments in 'Resources' started by Creeperface, Dec 28, 2015.

  1. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    this is code to calculate experience and experience level in pocketmine

    (I got some information from craftbukkit)

    PHP:
    public $oldLevel = -1;
        public 
    $expLevel;
        public 
    $expTotal;
        public 
    $exp;

        public function 
    addExp($i)
        {
            
    $j 2147483647 $this->expTotal;
            if (
    $i $j) {
                
    $i $j;
            }

            
    $this->exp += (float)$i / (float)$this->getExpToLevel();

            for (
    $this->expTotal += $i$this->exp >= 1.0;
                
    $this->exp /= (float)$this->getExpToLevel()) {
                
    $this->exp = ($this->exp 1.0) * (float)$this->getExpToLevel();
                
    $this->levelDown(1);
            }

            
    $this->sendExpLevel();
            
    $this->sendExp();
        }

        public function 
    addExpLevels($amount)
        {
            
    $this->levelDown($amount);
            
    $this->sendExpLevel();
        }

        public function 
    getExp()
        {
            return 
    $this->exp;
        }

        public function 
    setExp($amount)
        {
            
    $this->exp $amount;
            
    $this->sendExp();
        }

        public function 
    setExpLevel($amount)
        {
            
    $this->expLevel $amount;
            
    $this->sendExpLevel();
        }

        public function 
    getExpLevel()
        {
            return 
    $this->expLevel;
        }

        public function 
    setTotalExp($amount)
        {
            
    $this->expTotal $amount;
        }

        public function 
    getTotalExp()
        {
            return 
    $this->expTotal;
        }

        public function 
    sendExpLevel()
        {
            if (
    $this->spawned === true) {
                
    $pk = new UpdateAttributesPacket();

                
    $pk->minValue 0;
                
    $pk->maxValue 1;
                
    $pk->value $this->exp;
                
    $pk->name UpdateAttributesPacket::EXPERIENCE;
                
    $this->dataPacket($pk);
            }
        }

        public function 
    sendExp()
        {
            if (
    $this->spawned === true) {
                
    $pk = new UpdateAttributesPacket();

                
    $pk->minValue 0;
                
    $pk->maxValue 2147483647;
                
    $pk->value $this->expLevel;
                
    $pk->name UpdateAttributesPacket::EXPERIENCE_LEVEL;
                
    $this->dataPacket($pk);
            }
        }

        public function 
    levelDown($i)
        {
            
    $this->expLevel += $i;
            if (
    $this->expLevel 0) {
                
    $this->expLevel 0;
                
    $this->exp = (float)0.0;
                
    $this->expTotal 0;
            }
        }

        public function 
    getExpToLevel()
        {
            return 
    $this->expLevel >= 30 112 + ($this->expLevel 30) * : ($this->expLevel >= 15 37 + ($this->expLevel 15) * $this->expLevel 2);
        }
    Last edited: Dec 28, 2015
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why not PR to PM?
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    I dont think they'll merge it

    And why they dont want to add new features to pocketmine? (Nether portál, hunger, XP)
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not that they don't want to, just that they are not yet reviewing them.

Share This Page

Advertisement