1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Getting an inventory item and removing it

Discussion in 'Development' started by Saxavlax001, Apr 22, 2020.

  1. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    Guys,I'm trying to make Totem work in pocketmine from yesterday,and I almost succeed in doing that,I was lucky,because I found this plugin,https://github.com/MinekCZ/TotemEffect
    and I took codes,added my own codes on my plugin,anyway it works,but there is one serious problem,the totem works only when I'm holding the item and not when I'm not holding it but it exists in my inventory as you can see
    PHP:
        public function onDeath(PlayerDeathEvent $event) {

            
    $player $event->getPlayer();
            
    $inventory $player->getInventory();
            if(
    $player->getLastDamageCause() instanceof EntityDamageByEntityEvent) {
               if(
    $player->getLastDamageCause()->getDamager() instanceof Player) {
               
    $killer $player->getLastDamageCause()->getDamager();
                if(
    $player->getHealth() == 0) {
                 if(
    $inventory->contains(Item::get(450,0,1))) {
                
    $player->setHealth(1);
                
    $player->broadcastEntityEvent(ActorEventPacket::CONSUME_TOTEM);
        
                
    $te = new LevelEventPacket();
                
    $te->evid LevelEventPacket::EVENT_SOUND_TOTEM;
                
    $te->position $player->add(0$player->eyeHeight0);
                
    $te->data 0;
                
    $player->dataPacket($te);

                
    $inventory->removeItem(Item::get(450,0,1));
                
    $player->addEffect(new EffectInstance(Effect::getEffect(Effect::REGENERATION), (40), (2), (true)));
                
    $player->addEffect(new EffectInstance(Effect::getEffect(Effect::ABSORPTION), (5), (2), (true)));
                     }
                  }
               }
            }
         }
      }
    If you can help me with that I want,I would appreciate it.Watch a video too to see what I mean.
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    That is the intended behaviour:
    from https://minecraft.gamepedia.com/Totem_of_Undying
     
  3. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    Ok,but the problem is that it works only when I select it on hotbar and it doesn't work in main-hand as it says,is there any possible way to use main-hand?And off-hand is not implemented in pocketmine,so I can't use it for totem.
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    How can an item be in the main hand without being selected on the hotbar?!
     
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Looking at the code, it's very odd, it should work with the item anywhere in the inventory.
     
  6. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    Well it should work,but it doesn't in my case I can't understand why.
     
  7. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    HimbeersaftLP likes this.
  8. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    Ok thanks,it works like I wanted now,but I get an error with the slapper plugin when I use EntityDamageEvent,i tried many times to fix it,but nothing from my methods worked.
    PHP:
        public function onDamage(EntityDamageEvent $event) {

            
    $player $event->getEntity();
            
    $inventory $player->getInventory();
            if(
    $player->getLastDamageCause() instanceof EntityDamageByEntityEvent) {
                
    $damager $event->getDamager();
                 if(
    $player instanceof Player && $inventory->contains(Item::get(450,0,1))) {
                
    $inventory->removeItem(Item::get(450,0,1));
                
    $event->setCancelled(true);
                
    $player->setHealth(1);
                
    $player->broadcastEntityEvent(ActorEventPacket::CONSUME_TOTEM);
        
                
    $te = new LevelEventPacket();
                
    $te->evid LevelEventPacket::EVENT_SOUND_TOTEM;
                
    $te->position $player->add(0$player->eyeHeight0);
                
    $te->data 0;
                
    $player->dataPacket($te);

                
    $player->addEffect(new EffectInstance(Effect::getEffect(Effect::REGENERATION), 40 201));
                
    $player->addEffect(new EffectInstance(Effect::getEffect(Effect::FIRE_RESISTANCE), 40 201));
                
    $player->addEffect(new EffectInstance(Effect::getEffect(Effect::ABSORPTION), 201));
                  }
                }
             }
           }
    Error:

    [StaffChat] <Staff> Makairosei connected
    [02:05:39] [Server thread/INFO]: Try-harderMakairosei has joined the lobby!
    [02:05:39] [Server thread/CRITICAL]: Error: "Call to undefined method slapper\entities\SlapperZombie::getInventory()" (EXCEPTION) in "plugins/GoFightCoreFactions-master/src/Gofight/item/Totem" at line 25
    [02:05:39] [Server thread/DEBUG]: #0 src/pocketmine/plugin/MethodEventExecutor(42): Gofight\item\Totem->onDamage(object pocketmine\event\entity\EntityDamageByEntityEvent)
    [02:05:39] [Server thread/DEBUG]: #1 src/pocketmine/plugin/RegisteredListener(80): pocketmine\plugin\MethodEventExecutor->execute(object Gofight\item\Totem, object pocketmine\event\entity\EntityDamageByEntityEvent)
    [02:05:39] [Server thread/DEBUG]: #2 src/pocketmine/event/Event(88): pocketmine\plugin\RegisteredListener->callEvent(object pocketmine\event\entity\EntityDamageByEntityEvent)
    [02:05:39] [Server thread/DEBUG]: #3 src/pocketmine/entity/Entity(925): pocketmine\event\Event->call()
    [02:05:39] [Server thread/DEBUG]: #4 src/pocketmine/Player(2588): pocketmine\entity\Entity->attack(object pocketmine\event\entity\EntityDamageByEntityEvent)
    [02:05:39] [Server thread/DEBUG]: #5 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(150): pocketmine\Player->handleInventoryTransaction(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    [02:05:39] [Server thread/DEBUG]: #6 src/pocketmine/network/mcpe/protocol/InventoryTransactionPacket(178): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleInventoryTransaction(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    [02:05:39] [Server thread/DEBUG]: #7 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(110): pocketmine\network\mcpe\protocol\InventoryTransactionPacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)
    [02:05:39] [Server thread/DEBUG]: #8 src/pocketmine/network/mcpe/protocol/BatchPacket(126): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\InventoryTransactionPacket)
    [02:05:39] [Server thread/DEBUG]: #9 src/pocketmine/network/mcpe/PlayerNetworkSessionAdapter(110): pocketmine\network\mcpe\protocol\BatchPacket->handle(object pocketmine\network\mcpe\PlayerNetworkSessionAdapter)
    [02:05:39] [Server thread/DEBUG]: #10 src/pocketmine/Player(3118): pocketmine\network\mcpe\PlayerNetworkSessionAdapter->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)
    [02:05:39] [Server thread/DEBUG]: #11 src/pocketmine/network/mcpe/RakLibInterface(169): pocketmine\Player->handleDataPacket(object pocketmine\network\mcpe\protocol\BatchPacket)
    [02:05:39] [Server thread/DEBUG]: #12 vendor/pocketmine/raklib/src/server/ServerHandler(99): pocketmine\network\mcpe\RakLibInterface->handleEncapsulated(string[17] 192.168.1.9 54035, object raklib\protocol\EncapsulatedPacket, integer 0)
    [02:05:39] [Server thread/DEBUG]: #13 src/pocketmine/network/mcpe/RakLibInterface(109): raklib\server\ServerHandler->handlePacket()
    [02:05:39] [Server thread/DEBUG]: #14 src/pocketmine/network/mcpe/RakLibInterface(99): pocketmine\network\mcpe\RakLibInterface->process()
    [02:05:39] [Server thread/DEBUG]: #15 vendor/pocketmine/snooze/src/SleeperHandler(123): pocketmine\network\mcpe\RakLibInterface->pocketmine\network\mcpe\{closure}()
    [02:05:39] [Server thread/DEBUG]: #16 vendor/pocketmine/snooze/src/SleeperHandler(85): pocketmine\snooze\SleeperHandler->processNotifications()
    [02:05:39] [Server thread/DEBUG]: #17 src/pocketmine/Server(2146): pocketmine\snooze\SleeperHandler->sleepUntil(double 1587751539.8659)
    [02:05:39] [Server thread/DEBUG]: #18 src/pocketmine/Server(1990): pocketmine\Server->tickProcessor()
    [02:05:39] [Server thread/DEBUG]: #19 src/pocketmine/Server(1584): pocketmine\Server->start()
    [02:05:39] [Server thread/DEBUG]: #20 src/pocketmine/PocketMine(273): pocketmine\Server->__construct(object BaseClassLoader, object pocketmine\utils\MainLogger, string[31] /storage/emulated/0/PocketMine/, string[39] /storage/emulated/0/PocketMine/plugins/)
    [02:05:39] [Server thread/DEBUG]: #21 src/pocketmine/PocketMine(296): pocketmine\server()
     
  9. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    The slapper doesn't have an inventory, but when a player hits one, the event gets called and $player is the Slapper and it tried to use the nonexistent getInventory on it. You will need to put that part into the instanceof Player part.
     
  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.