attacking entities working

just moved some code :P
This commit is contained in:
Dylan K. Taylor 2017-08-10 10:49:14 +01:00
parent 3025f76cd0
commit bb4808c23e
2 changed files with 129 additions and 125 deletions

View File

@ -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

View File

@ -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;