Completed
Branch develop (f79938)
by Inko
01:20
created

TheEntityDamageEvent::dropItem()   C

Complexity

Conditions 12
Paths 22

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9666
c 0
b 0
f 0
cc 12
nc 22
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license.
4
 *
5
 * GitHub: https://github.com/VectorNetworkProject/TheMix
6
 * Website: https://www.vector-network.tk
7
 */
8
9
namespace VectorNetworkProject\TheMix\event\entity;
10
11
use InkoHX\GoldLibrary\GoldAPI;
12
use InkoHX\LeveLibrary\LevelAPI;
13
use pocketmine\event\entity\EntityDamageByEntityEvent;
14
use pocketmine\event\entity\EntityDamageEvent;
15
use pocketmine\event\Listener;
16
use pocketmine\item\Item;
17
use pocketmine\Player;
18
use pocketmine\Server;
19
use VectorNetworkProject\TheMix\event\game\TheEndGameEvent;
20
use VectorNetworkProject\TheMix\game\corepvp\blue\BlueTeamManager;
21
use VectorNetworkProject\TheMix\game\corepvp\red\RedTeamManager;
22
use VectorNetworkProject\TheMix\game\corepvp\SpawnManager;
23
use VectorNetworkProject\TheMix\game\streak\Streak;
24
25
class TheEntityDamageEvent implements Listener
26
{
27
    /**
28
     * @param EntityDamageEvent $event
29
     *
30
     * @throws \ReflectionException
31
     */
32
    public function event(EntityDamageEvent $event)
33
    {
34
        $entity = $event->getEntity();
35
        $entity->extinguish();
36
        if (TheEndGameEvent::isFinish()) {
37
            $event->setCancelled();
38
39
            return;
40
        }
41
        if (!$entity instanceof Player) {
42
            return;
43
        }
44
        if ($event->getFinalDamage() < $entity->getHealth()) {
45
            return;
46
        }
47
        if ($event->getCause() === EntityDamageEvent::CAUSE_FALL) {
48
            $event->setCancelled();
49
50
            return;
51
        }
52
        if ($entity->getLevel()->getName() === Server::getInstance()->getDefaultLevel()->getName()) {
53
            $event->setCancelled();
54
55
            return;
56
        }
57
        if ($event instanceof EntityDamageByEntityEvent) {
58
            $event->setCancelled();
59
            $damager = $event->getDamager();
60
            self::dropItem($entity);
61
            SpawnManager::PlayerReSpawn($entity);
62
            Streak::resetStreak($entity);
63
            if ($damager instanceof Player) {
64
                if ($entity->getName() === $damager->getName()) {
65
                    Server::getInstance()->broadcastMessage("{$entity->getNameTag()} §fは自滅した。");
66
67
                    return;
68
                }
69
                Streak::addStreak($damager);
70
                GoldAPI::addGold($damager, mt_rand(10, 15));
71
                LevelAPI::Auto($damager, mt_rand(10, 15));
72
                Server::getInstance()->broadcastMessage("{$damager->getNameTag()} §fが {$entity->getNameTag()} §fを倒した。");
73
            }
74
        } else {
75
            $event->setCancelled();
76
            self::dropItem($entity);
77
            Streak::resetStreak($entity);
78
            SpawnManager::PlayerReSpawn($entity);
79
            Server::getInstance()->broadcastMessage("{$entity->getNameTag()} §fは自滅した。");
80
        }
81
    }
82
83
    public function BlockTeamPvP(EntityDamageEvent $event)
0 ignored issues
show
Coding Style introduced by
function BlockTeamPvP() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
84
    {
85
        if ($event instanceof EntityDamageByEntityEvent) {
86
            $entity = $event->getEntity();
87
            $damager = $event->getDamager();
88
            if (!$entity instanceof Player && !$damager instanceof Player) {
89
                return;
90
            }
91
            if (BlueTeamManager::isJoined($entity) === true && BlueTeamManager::isJoined($damager) === true) {
0 ignored issues
show
Compatibility introduced by
$entity of type object<pocketmine\entity\Entity> is not a sub-type of object<pocketmine\Player>. It seems like you assume a child class of the class pocketmine\entity\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Documentation introduced by
$damager is of type object<pocketmine\entity\Entity>|null, but the function expects a object<pocketmine\Player>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
                $event->setCancelled();
93
            } elseif (RedTeamManager::isJoined($entity) === true && RedTeamManager::isJoined($damager) === true) {
0 ignored issues
show
Compatibility introduced by
$entity of type object<pocketmine\entity\Entity> is not a sub-type of object<pocketmine\Player>. It seems like you assume a child class of the class pocketmine\entity\Entity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Documentation introduced by
$damager is of type object<pocketmine\entity\Entity>|null, but the function expects a object<pocketmine\Player>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
                $event->setCancelled();
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param Player $player
101
     * @return void
102
     */
103
    public static function dropItem(Player $player): void
104
    {
105
        $contents = $player->getInventory()->getContents();
106
        foreach ($contents as $slot => $item) {
107
            switch ($item->getId()) {
108
                case Item::STONE_AXE:
109
                case Item::STONE_PICKAXE:
110
                case Item::STONE_SHOVEL:
111
                case Item::WOODEN_SWORD:
112
                case Item::LEATHER_CAP:
113
                case Item::LEATHER_CHESTPLATE:
114
                case Item::LEATHER_LEGGINGS:
115
                case Item::LEATHER_BOOTS:
116
                case Item::BOW:
117
                    unset($contents[$slot]);
118
                    break;
119
            }
120
        }
121
        foreach ($contents as $item) {
122
            $player->getLevel()->dropItem($player->asVector3(), $item);
123
        }
124
    }
125
}
126