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

BlockReGeneratorEvent::event()   C

Complexity

Conditions 15
Paths 11

Size

Total Lines 50

Duplication

Lines 28
Ratio 56 %

Importance

Changes 0
Metric Value
dl 28
loc 50
rs 5.9166
c 0
b 0
f 0
cc 15
nc 11
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\block;
10
11
use pocketmine\block\Block;
12
use pocketmine\event\block\BlockBreakEvent;
13
use pocketmine\event\Listener;
14
use pocketmine\item\Item;
15
use VectorNetworkProject\TheMix\event\game\TheEndGameEvent;
16
use VectorNetworkProject\TheMix\game\DefaultConfig;
17
use VectorNetworkProject\TheMix\task\BlockReGeneratorTask;
18
use VectorNetworkProject\TheMix\TheMix;
19
20
class BlockReGeneratorEvent implements Listener
21
{
22
    /**
23
     * @param BlockBreakEvent $event
24
     */
25
    public function event(BlockBreakEvent $event): void
26
    {
27
        $block = $event->getBlock();
28
        $inventory = $event->getPlayer()->getInventory();
29
        if (DefaultConfig::isDev() || $block->getLevel()->getName() !== DefaultConfig::getStageLevelName() || TheEndGameEvent::isFinish()) {
30
            return;
31
        }
32
        if ($block->getToolType() !== $inventory->getItemInHand()->getBlockToolType() && !DefaultConfig::isDev()) {
0 ignored issues
show
Bug introduced by
The method getItemInHand() does not exist on pocketmine\inventory\Inventory. Did you maybe mean getItem()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
            $event->setCancelled();
34
35
            return;
36
        }
37
        $event->setDrops([]);
38
        switch ($block->getId()) {
39 View Code Duplication
            case Block::MELON_BLOCK:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                $inventory->addItem(Item::get(Item::MELON, 0, 16));
41
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 10 * 20);
42
                break;
43 View Code Duplication
            case Block::WOOD:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
                $inventory->addItem(Item::get(Item::WOODEN_PLANKS, 0, 4));
45
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 15 * 20);
46
                break;
47 View Code Duplication
            case Block::DIAMOND_ORE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
                $inventory->addItem(Item::get(Item::DIAMOND));
49
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 60 * 20);
50
                break;
51 View Code Duplication
            case Block::EMERALD_ORE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                $inventory->addItem(Item::get(Item::EMERALD, 0, mt_rand(1, 3)));
53
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 60 * 20);
54
                break;
55 View Code Duplication
            case Block::COAL_ORE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
                $inventory->addItem(Item::get(Item::COAL));
57
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 15 * 20);
58
                break;
59 View Code Duplication
            case Block::IRON_ORE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                $inventory->addItem(Item::get(Item::IRON_INGOT));
61
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 20 * 20);
62
                break;
63 View Code Duplication
            case Block::GOLD_ORE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
                $inventory->addItem(Item::get(Item::GOLD_INGOT));
65
                TheMix::getInstance()->getScheduler()->scheduleDelayedTask(new BlockReGeneratorTask($block), 30 * 20);
66
                break;
67
            default:
68
                if ($event->getPlayer()->getLevel()->getName() === DefaultConfig::getStageLevelName() && DefaultConfig::isDev() === false) {
69
                    $event->setCancelled();
70
71
                    return;
72
                }
73
        }
74
    }
75
}
76