on(ObjectAdded)
last analyzed

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 9
ccs 3
cts 4
cp 0.75
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.listener.player.exchange.bank;
21
22
import fr.quatrevieux.araknemu.core.event.EventsSubscriber;
23
import fr.quatrevieux.araknemu.core.event.Listener;
24
import fr.quatrevieux.araknemu.data.living.repository.account.BankItemRepository;
25
import fr.quatrevieux.araknemu.game.account.bank.BankEntry;
26
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectAdded;
27
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectDeleted;
28
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectQuantityChanged;
29
30
/**
31
 * Save the bank on changes
32
 */
33
public final class SaveBank implements EventsSubscriber {
34
    private final BankItemRepository repository;
35
36 1
    public SaveBank(BankItemRepository repository) {
37 1
        this.repository = repository;
38 1
    }
39
40
    @Override
41
    public Listener[] listeners() {
42 1
        return new Listener[] {
43 1
            new Listener<ObjectAdded>() {
44
                @Override
45
                public void on(ObjectAdded event) {
46 1
                    if (!(event.entry() instanceof BankEntry)) {
47
                        return;
48
                    }
49
50 1
                    final BankEntry entry = (BankEntry) event.entry();
51
52 1
                    repository.add(entry.entity());
53 1
                }
54
55
                @Override
56
                public Class<ObjectAdded> event() {
57 1
                    return ObjectAdded.class;
58
                }
59
            },
60
61 1
            new Listener<ObjectDeleted>() {
62
                @Override
63
                public void on(ObjectDeleted event) {
64 1
                    if (!(event.entry() instanceof BankEntry)) {
65
                        return;
66
                    }
67
68 1
                    final BankEntry entry = (BankEntry) event.entry();
69
70 1
                    repository.delete(entry.entity());
71 1
                }
72
73
                @Override
74
                public Class<ObjectDeleted> event() {
75 1
                    return ObjectDeleted.class;
76
                }
77
            },
78
79 1
            new Listener<ObjectQuantityChanged>() {
80
                @Override
81
                public void on(ObjectQuantityChanged event) {
82 1
                    if (!(event.entry() instanceof BankEntry)) {
83
                        return;
84
                    }
85
86 1
                    final BankEntry entry = (BankEntry) event.entry();
87
88 1
                    repository.update(entry.entity());
89 1
                }
90
91
                @Override
92
                public Class<ObjectQuantityChanged> event() {
93 1
                    return ObjectQuantityChanged.class;
94
                }
95
            },
96
        };
97
    }
98
}
99