From bb4808c23e9753622a3f0c989843950681b45ca6 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 10 Aug 2017 10:49:14 +0100 Subject: [PATCH] attacking entities working just moved some code :P --- src/pocketmine/Player.php | 243 ++++++++++-------- .../network/mcpe/protocol/InteractPacket.php | 11 - 2 files changed, 129 insertions(+), 125 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index ec547d5ae9..b9fc383a7b 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2303,7 +2303,132 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } break; case InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY: - //TODO + $target = $this->level->getEntity($packet->transactionData->entityRuntimeId); + if($target === null){ + return false; + } + + $type = $packet->transactionData->actionType; + + switch($type){ + case InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT: + break; //TODO + case InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_ATTACK: + $cancelled = false; + if($target instanceof Player and $this->server->getConfigBoolean("pvp", true) === false){ + $cancelled = true; + } + + if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->isAlive() and $target->isAlive()){ + if($target instanceof DroppedItem or $target instanceof Arrow){ + $this->kick("Attempting to attack an invalid entity"); + $this->server->getLogger()->warning($this->getServer()->getLanguage()->translateString("pocketmine.player.invalidEntity", [$this->getName()])); + return false; + } + + $item = $this->inventory->getItemInHand(); + $damageTable = [ + Item::WOODEN_SWORD => 4, + Item::GOLDEN_SWORD => 4, + Item::STONE_SWORD => 5, + Item::IRON_SWORD => 6, + Item::DIAMOND_SWORD => 7, + + Item::WOODEN_AXE => 3, + Item::GOLDEN_AXE => 3, + Item::STONE_AXE => 3, + Item::IRON_AXE => 5, + Item::DIAMOND_AXE => 6, + + Item::WOODEN_PICKAXE => 2, + Item::GOLDEN_PICKAXE => 2, + Item::STONE_PICKAXE => 3, + Item::IRON_PICKAXE => 4, + Item::DIAMOND_PICKAXE => 5, + + Item::WOODEN_SHOVEL => 1, + Item::GOLDEN_SHOVEL => 1, + Item::STONE_SHOVEL => 2, + Item::IRON_SHOVEL => 3, + Item::DIAMOND_SHOVEL => 4, + ]; + + $damage = [ + EntityDamageEvent::MODIFIER_BASE => $damageTable[$item->getId()] ?? 1, + ]; + + if(!$this->canInteract($target, 8)){ + $cancelled = true; + }elseif($target instanceof Player){ + if(($target->getGamemode() & 0x01) > 0){ + return true; + }elseif($this->server->getConfigBoolean("pvp") !== true or $this->server->getDifficulty() === 0){ + $cancelled = true; + } + + $armorValues = [ + Item::LEATHER_CAP => 1, + Item::LEATHER_TUNIC => 3, + Item::LEATHER_PANTS => 2, + Item::LEATHER_BOOTS => 1, + Item::CHAINMAIL_HELMET => 1, + Item::CHAINMAIL_CHESTPLATE => 5, + Item::CHAINMAIL_LEGGINGS => 4, + Item::CHAINMAIL_BOOTS => 1, + Item::GOLDEN_HELMET => 1, + Item::GOLDEN_CHESTPLATE => 5, + Item::GOLDEN_LEGGINGS => 3, + Item::GOLDEN_BOOTS => 1, + Item::IRON_HELMET => 2, + Item::IRON_CHESTPLATE => 6, + Item::IRON_LEGGINGS => 5, + Item::IRON_BOOTS => 2, + Item::DIAMOND_HELMET => 3, + Item::DIAMOND_CHESTPLATE => 8, + Item::DIAMOND_LEGGINGS => 6, + Item::DIAMOND_BOOTS => 3, + ]; + $points = 0; + foreach($target->getInventory()->getArmorContents() as $index => $i){ + if(isset($armorValues[$i->getId()])){ + $points += $armorValues[$i->getId()]; + } + } + + $damage[EntityDamageEvent::MODIFIER_ARMOR] = -floor($damage[EntityDamageEvent::MODIFIER_BASE] * $points * 0.04); + } + + $ev = new EntityDamageByEntityEvent($this, $target, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $damage); + if($cancelled){ + $ev->setCancelled(); + } + + $target->attack($ev->getFinalDamage(), $ev); + + if($ev->isCancelled()){ + if($item->isTool() and $this->isSurvival()){ + $this->inventory->sendContents($this); + } + return true; + } + + if($this->isSurvival()){ + if($item->isTool()){ + if($item->useOn($target) and $item->getDamage() >= $item->getMaxDurability()){ + $this->inventory->setItemInHand(Item::get(Item::AIR, 0, 1)); + }else{ + $this->inventory->setItemInHand($item); + } + } + + $this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK); + } + } + return true; + default: + break; //unknown + } + break; case InventoryTransactionPacket::TYPE_RELEASE_ITEM: //TODO @@ -2352,121 +2477,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ $this->craftingType = 0; $target = $this->level->getEntity($packet->target); + if($target === null){ + return false; + } - $cancelled = false; switch($packet->action){ - case InteractPacket::ACTION_LEFT_CLICK: //Attack - if($target instanceof Player and $this->server->getConfigBoolean("pvp", true) === false){ - $cancelled = true; - } - - if($target instanceof Entity and $this->getGamemode() !== Player::VIEW and $this->isAlive() and $target->isAlive()){ - if($target instanceof DroppedItem or $target instanceof Arrow){ - $this->kick("Attempting to attack an invalid entity"); - $this->server->getLogger()->warning($this->getServer()->getLanguage()->translateString("pocketmine.player.invalidEntity", [$this->getName()])); - break; - } - - $item = $this->inventory->getItemInHand(); - $damageTable = [ - Item::WOODEN_SWORD => 4, - Item::GOLDEN_SWORD => 4, - Item::STONE_SWORD => 5, - Item::IRON_SWORD => 6, - Item::DIAMOND_SWORD => 7, - - Item::WOODEN_AXE => 3, - Item::GOLDEN_AXE => 3, - Item::STONE_AXE => 3, - Item::IRON_AXE => 5, - Item::DIAMOND_AXE => 6, - - Item::WOODEN_PICKAXE => 2, - Item::GOLDEN_PICKAXE => 2, - Item::STONE_PICKAXE => 3, - Item::IRON_PICKAXE => 4, - Item::DIAMOND_PICKAXE => 5, - - Item::WOODEN_SHOVEL => 1, - Item::GOLDEN_SHOVEL => 1, - Item::STONE_SHOVEL => 2, - Item::IRON_SHOVEL => 3, - Item::DIAMOND_SHOVEL => 4, - ]; - - $damage = [ - EntityDamageEvent::MODIFIER_BASE => $damageTable[$item->getId()] ?? 1, - ]; - - if(!$this->canInteract($target, 8)){ - $cancelled = true; - }elseif($target instanceof Player){ - if(($target->getGamemode() & 0x01) > 0){ - break; - }elseif($this->server->getConfigBoolean("pvp") !== true or $this->server->getDifficulty() === 0){ - $cancelled = true; - } - - $armorValues = [ - Item::LEATHER_CAP => 1, - Item::LEATHER_TUNIC => 3, - Item::LEATHER_PANTS => 2, - Item::LEATHER_BOOTS => 1, - Item::CHAINMAIL_HELMET => 1, - Item::CHAINMAIL_CHESTPLATE => 5, - Item::CHAINMAIL_LEGGINGS => 4, - Item::CHAINMAIL_BOOTS => 1, - Item::GOLDEN_HELMET => 1, - Item::GOLDEN_CHESTPLATE => 5, - Item::GOLDEN_LEGGINGS => 3, - Item::GOLDEN_BOOTS => 1, - Item::IRON_HELMET => 2, - Item::IRON_CHESTPLATE => 6, - Item::IRON_LEGGINGS => 5, - Item::IRON_BOOTS => 2, - Item::DIAMOND_HELMET => 3, - Item::DIAMOND_CHESTPLATE => 8, - Item::DIAMOND_LEGGINGS => 6, - Item::DIAMOND_BOOTS => 3, - ]; - $points = 0; - foreach($target->getInventory()->getArmorContents() as $index => $i){ - if(isset($armorValues[$i->getId()])){ - $points += $armorValues[$i->getId()]; - } - } - - $damage[EntityDamageEvent::MODIFIER_ARMOR] = -floor($damage[EntityDamageEvent::MODIFIER_BASE] * $points * 0.04); - } - - $ev = new EntityDamageByEntityEvent($this, $target, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $damage); - if($cancelled){ - $ev->setCancelled(); - } - - $target->attack($ev->getFinalDamage(), $ev); - - if($ev->isCancelled()){ - if($item->isTool() and $this->isSurvival()){ - $this->inventory->sendContents($this); - } - break; - } - - if($this->isSurvival()){ - if($item->isTool()){ - if($item->useOn($target) and $item->getDamage() >= $item->getMaxDurability()){ - $this->inventory->setItemInHand(Item::get(Item::AIR, 0, 1)); - }else{ - $this->inventory->setItemInHand($item); - } - } - - $this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK); - } - } - break; - case InteractPacket::ACTION_RIGHT_CLICK: case InteractPacket::ACTION_LEAVE_VEHICLE: case InteractPacket::ACTION_MOUSEOVER: break; //TODO: handle these diff --git a/src/pocketmine/network/mcpe/protocol/InteractPacket.php b/src/pocketmine/network/mcpe/protocol/InteractPacket.php index 9be22b87ac..4c7f71ef7d 100644 --- a/src/pocketmine/network/mcpe/protocol/InteractPacket.php +++ b/src/pocketmine/network/mcpe/protocol/InteractPacket.php @@ -31,17 +31,6 @@ use pocketmine\network\mcpe\NetworkSession; class InteractPacket extends DataPacket{ const NETWORK_ID = ProtocolInfo::INTERACT_PACKET; - /** - * @deprecated - * This action is no longer used as of MCPE 1.2.0.2, this is now handled in InventoryTransactionPacket - */ - const ACTION_RIGHT_CLICK = 1; - /** - * @deprecated - * This action is no longer used as of MCPE 1.2.0.2, this is now handled in InventoryTransactionPacket - */ - const ACTION_LEFT_CLICK = 2; - const ACTION_LEAVE_VEHICLE = 3; const ACTION_MOUSEOVER = 4;