id()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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.game.item.Item;
23
import fr.quatrevieux.araknemu.game.item.inventory.ItemEntry;
24
import fr.quatrevieux.araknemu.game.item.inventory.ItemStorage;
25
import fr.quatrevieux.araknemu.game.item.inventory.StackableItemStorage;
26
import fr.quatrevieux.araknemu.game.item.inventory.exception.InventoryException;
27
import fr.quatrevieux.araknemu.game.player.inventory.InventoryEntry;
28
import org.checkerframework.checker.index.qual.Positive;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30
import org.checkerframework.common.value.qual.IntVal;
31
import org.checkerframework.dataflow.qual.Pure;
32
33
import java.util.Optional;
34
35
/**
36
 * Slot for default position
37
 *
38
 * This slot will handle item stacking
39
 */
40
public final class DefaultSlot implements InventorySlot {
41
    private final StackableItemStorage<InventoryEntry> storage;
42
43
    @SuppressWarnings("method.invocation") // id() is static
44 1
    public DefaultSlot(ItemStorage<InventoryEntry> storage) {
45 1
        this.storage = new StackableItemStorage<>(storage, id());
46 1
    }
47
48
    @Override
49
    @Pure
50
    public @IntVal(ItemEntry.DEFAULT_POSITION) int id() {
51 1
        return ItemEntry.DEFAULT_POSITION;
52
    }
53
54
    @Override
55
    public Optional<InventoryEntry> entry() {
56 1
        return Optional.empty();
57
    }
58
59
    @Override
60
    public void check(Item item, int quantity) {
61
        // Default slot can store all items
62 1
    }
63
64
    @Override
65
    public InventoryEntry set(InventoryEntry entry) throws InventoryException {
66 1
        return storage.find(entry.item())
67 1
            .map(last -> {
68 1
                storage.delete(entry);
69
70
                // Quantity = 0 should not occur
71 1
                if (entry.quantity() > 0) {
72 1
                    last.add(entry.quantity());
73
                }
74
75 1
                return last;
76
            })
77 1
            .orElseGet(() -> {
78
                // Indexing the entry after move
79
                // For ensure that new item will be stacked
80 1
                storage.indexing(entry);
81
82 1
                return entry;
83
            })
84
        ;
85
    }
86
87
    @Override
88
    public InventoryEntry set(Item item, @Positive int quantity) throws InventoryException {
89 1
        return storage.add(item, quantity, id());
90
    }
91
92
    @Override
93
    public void uncheckedSet(@Nullable InventoryEntry entry) {
94
        // Do not store a single item : do nothing
95 1
    }
96
}
97