Skip to content

Instantly share code, notes, and snippets.

@BEcraft
Created March 8, 2019 04:42
Show Gist options
  • Save BEcraft/855c20c1b2b488e5497b44857e2e55fb to your computer and use it in GitHub Desktop.
Save BEcraft/855c20c1b2b488e5497b44857e2e55fb to your computer and use it in GitHub Desktop.
Prueba de armadura
<?php
declare(strict_types = 1);
/**
* @name Armadura
* @author BEcraft
* @api 3.0.0
* @main BEcraft\Armadura\Cargador
* @version 1.0.0
*/
namespace BEcraft\Armadura;
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\event\inventory\InventoryPickupItemEvent;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\inventory\PlayerInventory;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\item\Armor;
use pocketmine\item\Item;
use pocketmine\Player;
class Cargador extends PluginBase implements Listener {
/**
* Index de las posiciones
* correspodientes a:
*
* 0 - Cabeza
* 1 - Chaleco
* 2 - Pantalones
* 3 - Botas
*
* @var array
*/
private static $DATOS = array(
Item::LEATHER_CAP => 0,
Item::LEATHER_CHESTPLATE => 1,
Item::LEATHER_LEGGINGS => 2,
Item::LEATHER_BOOTS => 3
);
/**
* Dependiendo de este valor la función
* "ejecutar" se ejecutará cada vez que
* un jugador entre al servidor, valores:
*
* true - Se ejecutará
* false - No se ejecutará
*
* @var boolean
*/
private static $ENTRAR = true;
/**
* Esta función se ejecuta al habilitar el plugin.
*/
public function onEnable(): void
{
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
/**
* Evento de prueba.
*
* @param InventoryPickupItemEvent $evento
*
* @priority HIGH
* @ignoreCancelled true
*/
public function objetos(InventoryPickupItemEvent $evento): void
{
$humano = $evento->getInventory()->getHolder();
$objeto = $evento->getItem()->getItem();
if ($humano instanceof Player and $objeto instanceof Armor) {
$evento->setCancelled($this->ejecutar($humano, $objeto));
if ($evento->isCancelled()) {
$evento->getItem()->flagForDespawn();
}
}
}
/**
* Evento de prueba.
*
* @param InventoryTransactionEvent $evento
*
* @priority HIGH
* @ignoreCancelled true
*/
public function transaccion(InventoryTransactionEvent $evento): void
{
if (true) {
return; //mejorar despues
}
$transaccion = $evento->getTransaction();
foreach ($transaccion->getActions() as $accion) {
if (!($accion instanceof SlotChangeAction) or !($accion->getInventory() instanceof PlayerInventory)) {
continue;
}
if ($accion->getTargetItem() instanceof Armor) {
if (!($this->ejecutar($transaccion->getSource(), $accion->getTargetItem()))) {
return;
}
$accion->getInventory()->removeItem($accion->getTargetItem()); //creo que deberia ser mejor en un task.
}
}
}
/**
* @param Player $humano
* @param Armor $objeto
*
* @return bool
*/
private function ejecutar(Player $humano, Armor $objeto, int $index = -1): bool
{
$identificador = $objeto->getId();
while ($identificador > Item::LEATHER_BOOTS) {
$identificador -= 4;
}
$identificador = self::$DATOS[$identificador];
$armadura = $humano->getArmorInventory()->getItem($identificador);
if ($armadura->getId() === Item::AIR) {
$humano->getArmorInventory()->setItem($identificador, clone $objeto);
} else {
if ($armadura->getDefensePoints() >= $objeto->getDefensePoints()) {
return false;
}
$posicion = $humano->getInventory()->firstEmpty();
if ($posicion !== -1 and $posicion !== $index) {
$humano->getInventory()->setItem($posicion, clone $armadura);
} else {
$humano->getLevel()->dropItem($humano, clone $armadura);
}
$humano->getArmorInventory()->setItem($identificador, clone $objeto);
}
return true;
}
/**
* Evento de prueba.
*
* @param PlayerJoinEvent $evento
*
* @priority NORMAL
* @ignoreCancelled false
*/
public function entrar(PlayerJoinEvent $evento): void
{
if (self::$ENTRAR === false) {
return;
}
foreach ($evento->getPlayer()->getInventory()->getContents() as $objeto) {
if ($objeto instanceof Armor) {
$this->ejecutar($evento->getPlayer(), $objeto);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment