forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChestShopMetaData.java
138 lines (104 loc) · 3.75 KB
/
ChestShopMetaData.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.Acrobot.ChestShop.Signs;
import com.Acrobot.Breeze.Utils.PriceUtil;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.inventory.ItemStack;
public class ChestShopMetaData implements ConfigurationSerializable {
private UUID owner;
private Set<UUID> accessors;
private int quantity;
private double buyPrice;
private double sellPrice;
private ItemStack itemStack;
public ChestShopMetaData(UUID owner, int quantity, double sellPrice, double buyPrice, ItemStack itemStack) {
this(owner, quantity, sellPrice, buyPrice, itemStack, new HashSet<>());
}
private ChestShopMetaData(UUID owner, int quantity, double sellPrice, double buyPrice, ItemStack itemStack, Set<UUID> accessors) {
this.owner = owner;
this.quantity = Math.max(1, quantity);
this.sellPrice = sellPrice;
this.buyPrice = buyPrice;
this.itemStack = itemStack;
this.accessors = accessors;
}
public HashSet<UUID> getAccessors() {
return new HashSet<>(accessors);
}
public void addAccessor(UUID uuid) {
accessors.add(uuid);
}
public void removeAccessor(UUID uuid) {
accessors.remove(uuid);
}
public boolean isAccessor(UUID uuid) {
return accessors.contains(uuid);
}
public boolean canAccess(OfflinePlayer player) {
return canAccess(player.getUniqueId());
}
public boolean canAccess(UUID player) {
if (isOwner(player))
return true;
return isAccessor(player);
}
public UUID getOwner() {
return owner;
}
public boolean isOwner(OfflinePlayer player) {
return isOwner(player.getUniqueId());
}
public boolean isOwner(UUID uuid) {
return owner.equals(uuid);
}
public boolean isAdminshop() {
return isOwner(NameManager.getAdminShopUUID());
}
public int getQuantity() {
return quantity;
}
public boolean doesBuy() {
return buyPrice != PriceUtil.NO_PRICE;
}
public double getBuyPrice() {
return buyPrice;
}
public boolean doesSell() {
return sellPrice != PriceUtil.NO_PRICE;
}
public double getSellPrice() {
return sellPrice;
}
public ItemStack getItemStack() {
return itemStack.clone();
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> data = new HashMap<>();
data.put("owner", owner.toString());
Set<String> accessorsStrings = new HashSet<>();
this.accessors.forEach(uuid -> accessorsStrings.add(uuid.toString()));
data.put("accessors", accessorsStrings);
data.put("amount", quantity);
data.put("buyPrice", buyPrice);
data.put("sellPrice", sellPrice);
data.put("itemStack", itemStack.serialize());
return data;
}
public static ChestShopMetaData deserialize(Map<String, Object> map) {
UUID owner = UUID.fromString((String) map.get("owner"));
int amount = (int) map.get("amount");
double sellPrice = (double) map.get("sellPrice");
double buyPrice = (double) map.get("buyPrice");
ItemStack itemStack = ItemStack.deserialize((Map<String, Object>) map.get("itemStack"));
Set<String> accessorsString = (HashSet<String>) map.get("accessors");
Set<UUID> accessors = new HashSet<>();
accessorsString.forEach(string -> accessors.add(UUID.fromString(string)));
return new ChestShopMetaData(owner, amount, sellPrice, buyPrice, itemStack, accessors);
}
}