forked from espidev/ProtectionStones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSEconomy.java
More file actions
212 lines (188 loc) · 8.26 KB
/
Copy pathPSEconomy.java
File metadata and controls
212 lines (188 loc) · 8.26 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.espi.protectionstones;
import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.WGUtils;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
/**
* Handler for ProtectionStones economy related tasks.
*/
public class PSEconomy {
private List<PSRegion> rentedList = new CopyOnWriteArrayList<>();
private static MyScheduledTask rentRunner, taxRunner;
public PSEconomy() {
if (!ProtectionStones.getInstance().isVaultSupportEnabled()) {
ProtectionStones.getInstance().getLogger().warning("Vault is not enabled! Economy functions (renting & buying) will not work!");
return;
}
// find regions that are being rented out (called on startup or reload)
loadRentList();
// start rent
rentRunner = ProtectionStones.getScheduler().runTaskTimerAsynchronously(this::updateRents, 0, 200);
// start taxes
if (ProtectionStones.getInstance().getConfigOptions().taxEnabled)
taxRunner = ProtectionStones.getScheduler().runTaskTimerAsynchronously(this::updateTaxes, 0, 200);
}
private synchronized void updateRents() {
rentedList = rentedList.stream()
.filter(r -> r.getTypeOptions() != null) // remove null regions
.filter(r -> r.getRentStage() == PSRegion.RentStage.RENTING) // remove regions not being rented out
.peek(r -> {
try {
Duration rentPeriod = MiscUtil.parseRentPeriod(r.getRentPeriod());
// if tenant needs to pay
if (Instant.now().getEpochSecond() > (r.getRentLastPaid() + rentPeriod.getSeconds())) {
doRentPayment(r);
}
} catch (Exception ignored) {
}
})
.collect(Collectors.toList());
}
private void updateTaxes() {
WGUtils.getAllRegionManagers()
.forEach((w, rgm) -> {
for (ProtectedRegion r : rgm.getRegions().values()) {
if (ProtectionStones.isPSRegion(r)) {
PSRegion psr = PSRegion.fromWGRegion(w, r);
processTaxes(psr);
}
}
});
}
/**
* Stops the economy cycle. Used for reloads when creating a new PSEconomy.
*/
public void stop() {
if (rentRunner != null) {
rentRunner.cancel();
rentRunner = null;
}
if (taxRunner != null) {
taxRunner.cancel();
taxRunner = null;
}
}
/**
* Load list of regions that are rented into memory.
*/
public void loadRentList() {
rentedList = new ArrayList<>();
HashMap<World, RegionManager> managers = WGUtils.getAllRegionManagers();
for (World w : managers.keySet()) {
RegionManager rgm = managers.get(w);
for (ProtectedRegion pr : rgm.getRegions().values()) {
if (ProtectionStones.isPSRegion(pr)) {
rentedList.add(PSRegion.fromWGRegion(w, pr));
}
}
}
}
/**
* Process taxes for a region.
*
* @param r the region to process taxes for
*/
public static void processTaxes(PSRegion r) {
// if taxes are enabled for this regions
if (r.getTypeOptions() != null && r.getTypeOptions().taxPeriod != -1) {
ProtectionStones.getScheduler().runTask(r.getProtectBlock().getLocation(), () -> {
// update tax payments due
r.updateTaxPayments();
// check if a player is set to auto-pay
if (!r.getTaxPaymentsDue().isEmpty() && r.getTaxAutopayer() != null) {
PSPlayer psp = PSPlayer.fromUUID(r.getTaxAutopayer());
EconomyResponse res = r.payTax(psp, psp.getBalance());
if (psp.getPlayer() != null && res.amount != 0) {
PSL.msg(psp.getPlayer(), PSL.TAX_PAID.msg()
.replace("%amount%", String.format("%.2f", res.amount))
.replace("%region%", r.getName() == null ? r.getId() : r.getName() + " (" + r.getId() + ")"));
}
}
// late tax payment punishment
if (r.isTaxPaymentLate()) {
r.deleteRegion(true); // TODO
}
});
}
}
/**
* Process a rent payment for a region.
* It does not do any checks, it is expected to check if the rent time has passed before this function is called.
*
* @param r the region to perform the rent payment
*/
public static void doRentPayment(PSRegion r) {
PSPlayer tenant = PSPlayer.fromPlayer(Bukkit.getOfflinePlayer(r.getTenant()));
PSPlayer landlord = PSPlayer.fromPlayer(Bukkit.getOfflinePlayer(r.getLandlord()));
// not enough money for rent
if (!tenant.hasAmount(r.getPrice())) {
if (tenant.getOfflinePlayer().isOnline()) {
PSL.msg(Bukkit.getPlayer(r.getTenant()), PSL.RENT_EVICT_NO_MONEY_TENANT.msg()
.replace("%region%", r.getName() != null ? r.getName() : r.getId())
.replace("%price%", String.format("%.2f", r.getPrice())));
}
if (landlord.getOfflinePlayer().isOnline()) {
PSL.msg(Bukkit.getPlayer(r.getLandlord()), PSL.RENT_EVICT_NO_MONEY_LANDLORD.msg()
.replace("%region%", r.getName() != null ? r.getName() : r.getId())
.replace("%tenant%", tenant.getName()));
}
r.removeRenting();
return;
}
// send payment messages
if (tenant.getOfflinePlayer().isOnline()) {
PSL.msg(Bukkit.getPlayer(r.getTenant()), PSL.RENT_PAID_TENANT.msg()
.replace("%price%", String.format("%.2f", r.getPrice()))
.replace("%landlord%", landlord.getName())
.replace("%region%", r.getName() != null ? r.getName() : r.getId()));
}
if (landlord.getOfflinePlayer().isOnline()) {
PSL.msg(Bukkit.getPlayer(r.getLandlord()), PSL.RENT_PAID_LANDLORD.msg()
.replace("%price%", String.format("%.2f", r.getPrice()))
.replace("%tenant%", tenant.getName())
.replace("%region%", r.getName() != null ? r.getName() : r.getId()));
}
// update money must be run in main thread
ProtectionStones.getScheduler().runTask(() -> tenant.pay(landlord, r.getPrice()));
r.setRentLastPaid(Instant.now().getEpochSecond());
try { // must save region to persist last paid
r.getWGRegionManager().saveChanges();
} catch (StorageException e) {
e.printStackTrace();
}
}
/**
* Get list of rented regions.
*
* @return the list of rented regions
*/
public List<PSRegion> getRentedList() {
return rentedList;
}
}