-
Notifications
You must be signed in to change notification settings - Fork 6
/
CurrenciesManager.java
173 lines (140 loc) · 6.92 KB
/
CurrenciesManager.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ru.soknight.peconomy.configuration;
import lombok.Getter;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import ru.soknight.lib.configuration.AbstractConfiguration;
import ru.soknight.lib.configuration.Configuration;
import ru.soknight.lib.task.PluginTask;
import ru.soknight.peconomy.PEconomy;
import ru.soknight.peconomy.api.PEconomyAPI;
import ru.soknight.peconomy.balancetop.BalanceTop;
import ru.soknight.peconomy.balancetop.BalanceTopPlace;
import ru.soknight.peconomy.convertation.ConvertationSetup;
import ru.soknight.peconomy.task.BalanceTopUpdateTask;
import java.util.*;
@Getter
public final class CurrenciesManager extends AbstractConfiguration {
private final Configuration config;
private final Map<String, CurrencyInstance> currencies;
private final Map<String, PluginTask> updateTasks;
private CurrencyInstance vaultCurrency;
public CurrenciesManager(@NotNull PEconomy plugin, @NotNull Configuration config) {
super(plugin, "currencies.yml");
this.config = config;
this.currencies = new LinkedHashMap<>();
this.updateTasks = new LinkedHashMap<>();
refreshCurrencies();
}
public void shutdownUpdatingTasks() {
updateTasks.values().forEach(PluginTask::shutdown);
}
public void refreshCurrencies() {
shutdownUpdatingTasks();
super.refresh();
this.currencies.clear();
this.updateTasks.clear();
ConfigurationSection currenciesConfig = getBukkitConfig().getConfigurationSection("currencies");
Set<String> keys = currenciesConfig.getKeys(false);
if (keys.isEmpty())
return;
keys.forEach(currencyId -> {
ConfigurationSection currencyConfig = currenciesConfig.getConfigurationSection(currencyId);
if (!currencyConfig.isString("symbol")) {
getPlugin().getLogger().warning("Couldn't find the 'symbol' parameter for currency '" + currencyId + "', skipped.");
return;
}
ConvertationSetup convertationSetup = new ConvertationSetup(currencyId);
BalanceTopSetup balanceTopSetup = parseBalanceTopSetup(currencyConfig.getConfigurationSection("balance-top"));
BalanceTop balanceTop = null;
if (balanceTopSetup != null && balanceTopSetup.isValid())
balanceTop = BalanceTop.create(
getPlugin(),
currencyId,
balanceTopSetup.getMaxSize(),
place -> formatPlace(balanceTopSetup, place)
);
CurrencyInstance currency = CurrencyInstance.builder(currencyId)
.setName(colorize(currencyConfig.getString("name", currencyId)))
.setSymbol(colorize(currencyConfig.getString("symbol")))
.setLimit(Math.max((float) currencyConfig.getDouble("max-amount", 0F), 0F))
.setNewbieAmount(Math.max((float) currencyConfig.getDouble("newbie-amount", 0F), 0F))
.setVisible(currencyConfig.getBoolean("visible", true))
.setTransferable(currencyConfig.getBoolean("transferable", true))
.setConvertationSetup(convertationSetup)
.setBalanceTopSetup(balanceTopSetup)
.setBalanceTop(balanceTop)
.create();
currencies.put(currencyId, currency);
if (balanceTop != null) {
PluginTask updateTask = new BalanceTopUpdateTask(getPlugin(), currency, balanceTop);
updateTask.start();
updateTasks.put(currencyId, updateTask);
}
});
currencies.forEach((currencyId, currency) -> {
ConfigurationSection currencyConfig = currenciesConfig.getConfigurationSection(currencyId);
ConfigurationSection convertationConfig = currencyConfig.getConfigurationSection("convertation");
if (convertationConfig != null)
currency.getConvertationSetup().load(this, convertationConfig);
});
if (config.getBoolean("hooks.vault.enabled")) {
String vault = getConfig().getString("vault.currency");
if (vault == null) {
getPlugin().getLogger().info("Vault default currency is not specified, ignoring it.");
} else if (!currencies.containsKey(vault)) {
getPlugin().getLogger().severe("Failed to set vault default currency: Unknown currency '" + vault + "'.");
} else {
this.vaultCurrency = currencies.get(vault);
getPlugin().getLogger().info("Currency '" + vault + "' will be used for Vault economy.");
}
}
getPlugin().getLogger().info("Loaded " + currencies.size() + " currencies.");
}
private BalanceTopSetup parseBalanceTopSetup(ConfigurationSection config) {
if(config == null)
return null;
boolean enabled = config.getBoolean("enabled", false);
int updatePeriod = config.getInt("update-period", 0);
int maxSize = config.getInt("max-size", 0);
String formatExist = colorize(config.getString("format.exist", ""));
String formatEmpty = colorize(config.getString("format.empty", ""));
return new BalanceTopSetup(enabled, updatePeriod, maxSize, formatExist, formatEmpty);
}
public @Nullable CurrencyInstance getCurrency(String id) {
return currencies.get(id);
}
public @NotNull @UnmodifiableView Set<String> getCurrenciesIDs() {
return Collections.unmodifiableSet(currencies.keySet());
}
public @NotNull @UnmodifiableView Collection<CurrencyInstance> getCurrencies() {
return Collections.unmodifiableCollection(currencies.values());
}
public boolean isCurrency(String id) {
return currencies.containsKey(id);
}
public String formatPlace(BalanceTopSetup setup, @NotNull BalanceTopPlace place) {
int position = place.getPosition();
int positionIndex = place.getPositionIndex();
if(place.isEmpty()) {
String format = setup.getFormatEmpty();
if(format.isEmpty())
return format;
return format(format,
"%position%", position,
"%position_index%", positionIndex
);
} else {
String format = setup.getFormatExist();
if(format.isEmpty())
return format;
return format(format,
"%position%", position,
"%position_index%", positionIndex,
"%player%", place.getWalletHolder(),
"%balance%", PEconomyAPI.get().getFormatter().formatAmount(place.getWalletBalance())
);
}
}
}