-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathPSGroupRegion.java
More file actions
206 lines (182 loc) · 7.63 KB
/
Copy pathPSGroupRegion.java
File metadata and controls
206 lines (182 loc) · 7.63 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
/*
* 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.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.Objs;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.time.Duration;
import java.util.*;
import java.util.stream.Collectors;
/**
* Represents a region that exists but is a group of merged {@link PSStandardRegion}s.
* Contains multiple {@link PSMergedRegion} representing the individual merged regions (which don't technically exist in WorldGuard).
*/
public class PSGroupRegion extends PSStandardRegion {
PSGroupRegion(ProtectedRegion wgregion, RegionManager rgmanager, World world) {
super(wgregion, rgmanager, world);
assert getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS) != null;
}
@Override
public double getTaxRate() {
double taxRate = 0;
for (PSMergedRegion r : getMergedRegions()) {
taxRate += r.getTaxRate();
}
return taxRate;
}
@Override
public String getTaxPeriod() {
Set<String> s = new HashSet<>();
getMergedRegions().forEach(r -> s.add(r.getTaxPeriod()));
return MiscUtil.concatWithoutLast(new ArrayList<>(s), ", ");
}
@Override
public String getTaxPaymentPeriod() {
Set<String> s = new HashSet<>();
getMergedRegions().forEach(r -> s.add(r.getTaxPaymentPeriod()));
return MiscUtil.concatWithoutLast(new ArrayList<>(s), ", ");
}
@Override
public String getTimeTillNextPaymentDue() {
Set<String> s = new HashSet<>();
getMergedRegions().forEach(r -> s.add(r.getTimeTillNextPaymentDue()));
return MiscUtil.concatWithoutLast(new ArrayList<>(s), ", ");
}
@Override
public void updateTaxPayments() {
long currentTime = System.currentTimeMillis();
List<TaxPayment> payments = Objs.replaceNull(getTaxPaymentsDue(), new ArrayList<>());
List<LastRegionTaxPaymentEntry> lastAdded = Objs.replaceNull(getRegionLastTaxPaymentAddedEntries(), new ArrayList<>());
// loop over merged regions
for (PSMergedRegion r : getMergedRegions()) {
// taxes disabled
if (getTypeOptions().taxPeriod == -1) continue;
boolean found = false;
for (LastRegionTaxPaymentEntry last : lastAdded) {
// if the last region payment entry refers to this region
if (last.getRegionId().equals(r.getId())) {
found = true;
// if it's time to pay
if (last.getLastPaymentAdded() + Duration.ofSeconds(r.getTypeOptions().taxPeriod).toMillis() < currentTime) {
payments.add(new TaxPayment(currentTime + Duration.ofSeconds(r.getTypeOptions().taxPaymentTime).toMillis(), r.getTaxRate(), r.getId()));
last.setLastPaymentAdded(currentTime);
}
break;
}
}
if (!found) {
payments.add(new TaxPayment(currentTime + Duration.ofSeconds(r.getTypeOptions().taxPaymentTime).toMillis(), r.getTaxRate(), r.getId()));
lastAdded.add(new LastRegionTaxPaymentEntry(r.getId(), currentTime));
}
}
setTaxPaymentsDue(payments);
setRegionLastTaxPaymentAddedEntries(lastAdded);
}
@Override
public boolean hide() {
for (PSMergedRegion r : getMergedRegions()) r.hide();
return true;
}
@Override
public boolean unhide() {
for (PSMergedRegion r : getMergedRegions()) r.unhide();
return true;
}
@Override
public boolean deleteRegion(boolean deleteBlock, Player cause) {
List<PSMergedRegion> l = getMergedRegions();
if (super.deleteRegion(deleteBlock, cause)) {
for (PSMergedRegion r : l) {
if (deleteBlock && !r.isHidden()) {
r.getProtectBlock().setType(Material.AIR);
}
}
return true;
} else {
return false;
}
}
/**
* Get the merged region whose ID is the same as the group region ID.
* @return the root region
*/
public PSMergedRegion getRootRegion() {
for (PSMergedRegion r : getMergedRegions()) {
if (r.getId().equals(getId())) return r;
}
return null;
}
/**
* Check if this region contains a specific merged region
* @param id the psID that would've been generated if the merged region was a standard region
* @return whether or not the id is a merged region
*/
public boolean hasMergedRegion(String id) {
return getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS).contains(id);
}
/**
* Removes the merged region's information from the object.
* Note: This DOES NOT remove the actual PSMergedRegion object, you have to call deleteRegion() on that as well.
* @param id the id of the merged region
*/
public void removeMergedRegionInfo(String id) {
getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS).remove(id);
// remove from ps merged region types
Iterator<String> i = getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS_TYPES).iterator();
while (i.hasNext()) {
String[] spl = i.next().split(" ");
String rid = spl[0];
if (rid.equals(id)) {
i.remove();
break;
}
}
// remove from taxes
if (getWGRegion().getFlag(FlagHandler.PS_TAX_LAST_PAYMENT_ADDED) != null) {
String entry = "";
for (String e : getWGRegion().getFlag(FlagHandler.PS_TAX_LAST_PAYMENT_ADDED)) {
if (e.startsWith(id)) entry = e;
}
getWGRegion().getFlag(FlagHandler.PS_TAX_LAST_PAYMENT_ADDED).remove(entry);
}
}
/**
* Get the list of {@link PSMergedRegion} objects of the regions that were merged into this region.
* @return the list of regions merged into this region
*/
public List<PSMergedRegion> getMergedRegions() {
return getMergedRegionsUnsafe().stream()
.filter(r -> r.getTypeOptions() != null)
.collect(Collectors.toList());
}
/**
* Get the list of {@link PSMergedRegion} objects of the regions that were merged into this region.
* Note: This is unsafe as it includes {@link PSMergedRegion}s that are of types not configured in the config.
* @return the list of regions merged into this region
*/
public List<PSMergedRegion> getMergedRegionsUnsafe() {
List<PSMergedRegion> l = new ArrayList<>();
for (String line : getWGRegion().getFlag(FlagHandler.PS_MERGED_REGIONS_TYPES)) {
String[] spl = line.split(" ");
String id = spl[0], type = spl[1];
l.add(new PSMergedRegion(id, this, getWGRegionManager(), getWorld()));
}
return l;
}
}