-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.java
62 lines (47 loc) · 1.31 KB
/
inventory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.example.roguecraftplugin;
import java.util.HashMap;
import java.util.Map;
public class InventoryManager {
private Map<Player, Inventory> inventories;
public InventoryManager() {
this.inventories = new HashMap<>();
}
public Inventory getInventory(Player player) {
return inventories.computeIfAbsent(player, k -> new Inventory());
}
public void addItem(Player player, Item item) {
getInventory(player).addItem(item);
}
public void removeItem(Player player, Item item) {
getInventory(player).removeItem(item);
}
}
public class Inventory {
private Map<Item, Integer> items;
public Inventory() {
this.items = new HashMap<>();
}
public void addItem(Item item) {
items.merge(item, 1, Integer::sum);
}
public void removeItem(Item item) {
items.computeIfPresent(item, (k, v) -> v > 1 ? v - 1 : null);
}
public Map<Item, Integer> getItems() {
return items;
}
}
public class Item {
private String name;
private String description;
public Item(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
}