Test Failed
Pull Request — master (#190)
by Vincent
16:13
created

entity()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
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;
21
22
import fr.quatrevieux.araknemu.data.living.entity.player.PlayerItem;
23
import fr.quatrevieux.araknemu.game.item.Item;
24
import fr.quatrevieux.araknemu.game.item.effect.ItemEffect;
25
import fr.quatrevieux.araknemu.game.item.inventory.AbstractItemEntry;
26
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectMoved;
27
import fr.quatrevieux.araknemu.game.item.inventory.exception.InventoryException;
28
import fr.quatrevieux.araknemu.game.item.inventory.exception.MoveException;
29
30
import java.util.stream.Collectors;
31
32
/**
33
 * Entry for player repository
34
 */
35
public final class InventoryEntry extends AbstractItemEntry {
36
    private final PlayerInventory inventory;
37
    private final PlayerItem entity;
38
    private final Item item;
39
40 1
    public InventoryEntry(PlayerInventory inventory, PlayerItem entity, Item item) {
41
        super(inventory, entity, item, inventory);
42 1
43 1
        this.inventory = inventory;
44 1
        this.entity = entity;
45 1
        this.item = item;
46
    }
47
48
    @Override
49 1
    public int position() {
50
        return entity.position();
51
    }
52
53
    /**
54
     * Move the entry to a new position
55
     *
56
     * @param position The new position
57
     * @param quantity Quantity to move
58
     *
59 1
     * @throws MoveException When the item is already on the requested position
60 1
     */
61
    public void move(int position, int quantity) throws InventoryException {
62
        if (quantity > quantity() || quantity <= 0) {
63 1
            throw new InventoryException("Invalid quantity given");
64 1
        }
65 1
66
        if (position == position()) {
67
            throw new MoveException("The item is already on the requested position");
68 1
        }
69
70
        if (quantity == quantity()) {
71 1
            if (inventory.move(this, position)) {
72 1
                changePosition(position);
73 1
            }
74
75
            return;
76
        }
77
78
        inventory.add(item, quantity, position);
79
        remove(quantity);
80
    }
81
82
    /**
83
     * Set the item to the default position
84
     * Note: this method is internal and should not be called
85
     */
86
    public void setToDefaultPosition() {
87 1
        entity.setPosition(DEFAULT_POSITION);
88
    }
89
90
    /**
91 1
     * Get the database entity
92 1
     */
93 1
    PlayerItem entity() {
94
        return entity;
95
    }
96
97
    private void changePosition(int position) {
98
        entity.setPosition(position);
99 1
        inventory.dispatch(new ObjectMoved(this));
100
    }
101
102 1
    /**
103
     * Create a new entry
104 1
     */
105 1
    static InventoryEntry create(PlayerInventory inventory, int id, Item item, int quantity, int position) {
106
        return new InventoryEntry(
107
            inventory,
108
            new PlayerItem(
109
                inventory.owner().id(),
110
                id,
111
                item.template().id(),
112
                item.effects().stream().map(ItemEffect::toTemplate).collect(Collectors.toList()),
113
                quantity,
114
                position
115
            ),
116
            item
117
        );
118
    }
119
}
120