listeners()   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 56
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 38
c 1
b 0
f 0
dl 0
loc 56
ccs 21
cts 21
cp 1
crap 11
rs 5.4

6 Methods

Rating   Name   Duplication   Size   Complexity  
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.on(GameJoined) 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.on(ObjectDeleted) 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.on(ObjectQuantityChanged) 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.on(ObjectAdded) 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.on(CharacteristicsChanged) 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.$Listener$.event() 0 3 ?

How to fix   Long Method    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:

Complexity

Complex classes like fr.quatrevieux.araknemu.game.listener.player.inventory.SendWeight.listeners() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.listener.player.inventory;
21
22
import fr.quatrevieux.araknemu.core.event.EventsSubscriber;
23
import fr.quatrevieux.araknemu.core.event.Listener;
24
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectAdded;
25
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectDeleted;
26
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectQuantityChanged;
27
import fr.quatrevieux.araknemu.game.player.GamePlayer;
28
import fr.quatrevieux.araknemu.game.player.characteristic.event.CharacteristicsChanged;
29
import fr.quatrevieux.araknemu.game.player.event.GameJoined;
30
import fr.quatrevieux.araknemu.network.game.out.object.InventoryWeight;
31
32
/**
33
 * Send inventory weight when inventory operations or characteristics changes occurs
34
 */
35
public final class SendWeight implements EventsSubscriber {
36
    private final GamePlayer player;
37
38 1
    public SendWeight(GamePlayer player) {
39 1
        this.player = player;
40 1
    }
41
42
    @Override
43
    public Listener[] listeners() {
44 1
        return new Listener[] {
45 1
            new Listener<GameJoined>() {
46
                @Override
47
                public void on(GameJoined event) {
48 1
                    send();
49 1
                }
50
51
                @Override
52
                public Class<GameJoined> event() {
53 1
                    return GameJoined.class;
54
                }
55
            },
56 1
            new Listener<CharacteristicsChanged>() {
57
                @Override
58
                public void on(CharacteristicsChanged event) {
59 1
                    send();
60 1
                }
61
62
                @Override
63
                public Class<CharacteristicsChanged> event() {
64 1
                    return CharacteristicsChanged.class;
65
                }
66
            },
67 1
            new Listener<ObjectQuantityChanged>() {
68
                @Override
69
                public void on(ObjectQuantityChanged event) {
70 1
                    send();
71 1
                }
72
73
                @Override
74
                public Class<ObjectQuantityChanged> event() {
75 1
                    return ObjectQuantityChanged.class;
76
                }
77
            },
78 1
            new Listener<ObjectAdded>() {
79
                @Override
80
                public void on(ObjectAdded event) {
81 1
                    send();
82 1
                }
83
84
                @Override
85
                public Class<ObjectAdded> event() {
86 1
                    return ObjectAdded.class;
87
                }
88
            },
89 1
            new Listener<ObjectDeleted>() {
90
                @Override
91
                public void on(ObjectDeleted event) {
92 1
                    send();
93 1
                }
94
95
                @Override
96
                public Class<ObjectDeleted> event() {
97 1
                    return ObjectDeleted.class;
98
                }
99
            },
100
        };
101
    }
102
103
    private void send() {
104 1
        player.inventory().refreshWeight();
105 1
        player.send(new InventoryWeight(player));
106 1
    }
107
}
108