fr.quatrevieux.araknemu.game.player.inventory.slot.AbstractEquipmentSlot   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A set(Item,int) 0 7 1
A equipment() 0 2 1
A set(InventoryEntry) 0 7 1
A AbstractEquipmentSlot(Dispatcher,InventorySlot) 0 3 1
A check(Item,int) 0 3 1
A unset() 0 5 1
A entry() 0 3 1
A id() 0 3 1
A uncheckedSet(InventoryEntry) 0 3 1
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.player.inventory.slot;
21
22
import fr.quatrevieux.araknemu.core.event.Dispatcher;
23
import fr.quatrevieux.araknemu.game.item.Item;
24
import fr.quatrevieux.araknemu.game.item.inventory.exception.InventoryException;
25
import fr.quatrevieux.araknemu.game.item.type.AbstractEquipment;
26
import fr.quatrevieux.araknemu.game.player.inventory.InventoryEntry;
27
import fr.quatrevieux.araknemu.game.player.inventory.event.EquipmentChanged;
28
import org.checkerframework.checker.index.qual.Positive;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30
import org.checkerframework.common.value.qual.IntRange;
31
32
import java.util.Optional;
33
34
/**
35
 * Base slot class for equipments
36
 */
37
public abstract class AbstractEquipmentSlot implements InventorySlot {
38
    private final Dispatcher dispatcher;
39
    private final InventorySlot slot;
40
41 1
    public AbstractEquipmentSlot(Dispatcher dispatcher, InventorySlot slot) {
42 1
        this.dispatcher = dispatcher;
43 1
        this.slot = slot;
44 1
    }
45
46
    @Override
47
    public @IntRange(from = -1, to = 57) int id() {
48 1
        return slot.id();
49
    }
50
51
    @Override
52
    public Optional<InventoryEntry> entry() {
53 1
        return slot.entry();
54
    }
55
56
    @Override
57
    public InventoryEntry set(InventoryEntry entry) throws InventoryException {
58 1
        final InventoryEntry newEntry = slot.set(entry);
59
60 1
        dispatcher.dispatch(new EquipmentChanged(entry, id(), true));
61
62 1
        return newEntry;
63
    }
64
65
    @Override
66
    public InventoryEntry set(Item item, @Positive int quantity) throws InventoryException {
67 1
        final InventoryEntry entry = slot.set(item, quantity);
68
69 1
        dispatcher.dispatch(new EquipmentChanged(entry, id(), true));
70
71 1
        return entry;
72
    }
73
74
    @Override
75
    public void unset() {
76 1
        entry().ifPresent(entry -> {
77 1
            slot.unset();
78 1
            dispatcher.dispatch(new EquipmentChanged(entry, id(), false));
79 1
        });
80 1
    }
81
82
    @Override
83
    public void uncheckedSet(@Nullable InventoryEntry entry) {
84 1
        slot.uncheckedSet(entry);
85 1
    }
86
87
    @Override
88
    public void check(Item item, int quantity) throws InventoryException {
89 1
        slot.check(item, quantity);
90 1
    }
91
92
    /**
93
     * Get the current equipment
94
     */
95
    public Optional<AbstractEquipment> equipment() {
96 1
        return entry().map(InventoryEntry::item).map(AbstractEquipment.class::cast);
97
    }
98
}
99