Solved How to use setSkin() ?

Discussion in 'Development' started by Notjblus, Mar 23, 2020.

  1. Notjblus

    Notjblus Silverfish

    Messages:
    18
    GitHub:
    DevJblus
    How to use setSkin() ?
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,091
    GitHub:
    HimbeersaftLP
  3. Notjblus

    Notjblus Silverfish

    Messages:
    18
    GitHub:
    DevJblus
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,091
    GitHub:
    HimbeersaftLP
    If you wish for me to spoon feed you with code, here it is:
    PHP:
    private function setSkinForPlayerFromPng(Player $playerstring $imagePath) {
      
    $ACCEPTED_SKIN_SIZES = [
        
    64 32 4,
        
    64 64 4,
        
    128 128 4
      
    ];

      
    $SKIN_WIDTH_MAP = [
        
    64 32 => 64,
        
    64 64 => 64,
        
    128 128 => 128
      
    ];

      
    $SKIN_HEIGHT_MAP = [
        
    64 32 => 32,
        
    64 64 => 64,
        
    128 128 => 128
      
    ];

      
    $image imagecreatefrompng($imagePath);
      if (
    $image === false) {
        
    // Wasn't able to load image
        
    return;
      }

      
    $size imagesx($image) * imagesy($image) * 4;
      if(!
    in_array($size$ACCEPTED_SKIN_SIZES)) {
        
    // Image isn't skin sized
        
    return;
      }

      
    $width $SKIN_WIDTH_MAP[$size];
      
    $height $SKIN_HEIGHT_MAP[$size];

      
    $skinData "";
      for (
    $y 0$y $height$y++) {
        for (
    $x 0$x $width$x++) {
          
    // https://www.php.net/manual/en/function.imagecolorat.php
          
    $rgba imagecolorat($image$x$y);
          
    $a = (127 - (($rgba >> 24) & 0x7F)) * 2;
          
    $r = ($rgba >> 16) & 0xff;
          
    $g = ($rgba >> 8) & 0xff;
          
    $b $rgba 0xff;
          
    $skinData .= chr($r) . chr($g) . chr($b) . chr($a);
        }
      }

      
    imagedestroy($image);

      
    $player->setSkin(new Skin($player->getSkin()->getSkinId(), $skinData));
      
    $player->sendSkin();
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.