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

set item durability (damage)

Comments in 'Plugin Development' started by xiaoq, Jul 24, 2015.

  1. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    I want to make a unbreaking effect, so when player attack/break something, the item durability (damage) won't change:
    this is my code:
    I not sure this code is working or not, I think it still have bugs,
    PHP:
    public function onDamage(EntityDamageEvent $event) {

          if(
    $event->getDamager() instanceof Player && !$event->isCancelled()){

       
    $item $event->getDamager()->getInventory()->getIteminHand();

              switch(
    $item->getId()){

                  case 
    267:

                  case 
    268:

                  case 
    272:

                  case 
    276:

                     
    // $item->setDamage($item->getDamage() + 1);
                          
    $item->setDamage($item->getDamage() - );

                      break;

                  default:

                      }

                  }

            }
    Last edited: Jul 25, 2015
  2. BalAnce
    Offline

    BalAnce Active Member

    Joined:
    Mar 10, 2015
    Posts:
    125
    Minecraft User:
    BalAnce
    Set damage to 0?
    CraftYourBukkit likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Why +1? If the damage is higher, the durability goes down.
  4. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    eh, I forget =_=
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    EntityDamageEvent didn't necessarily have getDamager function. Only EntityDamageByEntityEvent does.
    MCPEPIG likes this.
  6. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    I remember now, so how to set item durability (BlockBreakEvent and EntityDamageByEntityEvent ?)
    Last edited: Jul 25, 2015
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Try doing $player->getInventory()->sendSlot($player->getInventory()->getHeldItemSlot(), $player); after setDamage
  8. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    Like: ?
    PHP:
    public function onHurt(EntityDamageByEntityEvent $event) {
          if(
    $event->getDamager() instanceof Player && !$event->isCancelled()){
          
    $item $event->getDamager()->getInventory()->getIteminHand();
              switch(
    $item->getId()){
                  case 
    267:
                  case 
    268:
                  case 
    272:
                  case 
    276:
                    
    $item->setDamage($item->getDamage() - 1);
                    
    $event->getDamager()->getInventory()->sendSlot($event->getDamager()->getInventory()->getHeldItemSlot(), $event->getDamager());
                      }
                  }
            }
    Last edited: Jul 25, 2015
  9. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Who is $damager?
  10. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    And I try:
    PHP:
           public function BreakEvent(BlockBreakEvent $event){

                if(
    $event->getPlayer() instanceof Player && !$event->isCancelled()){

                    
    $item $event->getItem();

                    
    $player $event->getPlayer();

                    switch(
    $id){

                        case 
    267:

                        case 
    268:

                        case 
    272:

                        case 
    276:

                   
    $item->setDamage($item->getDamage() - 1);

                  
    $player->getInventory()->sendSlot($player->getInventory()->getHeldItemSlot(), $player);

                    }

                }

            }

  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    How many times have I said that... Don't handle EntityDamageByEntityEvent, only EntityDamageEvent...
    CraftYourBukkit likes this.
  12. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
    But I can't get Damager in EntityDamageEvent
    :
    public function onHurt(EntityDamageEvent $event) {

    if ($event instanceof EntityDamageByEntityEvent) {
    Last edited: Jul 26, 2015
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    There you go, you can use getDamager after you have confirmed it is an instance of EntityDamageByEntityEvent.

Share This Page

Advertisement