AlttdGriefPrevention/src/main/java/me/ryanhamshire/GriefPrevention/alttd/tasks/Pl3xMapTask.java
2022-02-20 14:42:54 +01:00

131 lines
5.2 KiB
Java

package me.ryanhamshire.GriefPrevention.alttd.tasks;
import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.ryanhamshire.GriefPrevention.alttd.config.Config;
import me.ryanhamshire.GriefPrevention.alttd.hook.GPHook;
import net.pl3x.map.api.Key;
import net.pl3x.map.api.MapWorld;
import net.pl3x.map.api.Point;
import net.pl3x.map.api.SimpleLayerProvider;
import net.pl3x.map.api.marker.Marker;
import net.pl3x.map.api.marker.MarkerOptions;
import net.pl3x.map.api.marker.Rectangle;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Pl3xMapTask extends BukkitRunnable {
private final MapWorld world;
private final SimpleLayerProvider provider;
private boolean stop;
public Pl3xMapTask(MapWorld world, SimpleLayerProvider provider) {
this.world = world;
this.provider = provider;
}
@Override
public void run() {
if (stop) {
cancel();
}
updateClaims();
}
void updateClaims() {
provider.clearMarkers();
Collection<Claim> topLevelClaims = GPHook.getClaims();
if (topLevelClaims != null) {
topLevelClaims.stream()
.filter(claim -> claim.getGreaterBoundaryCorner().getWorld().getUID().equals(this.world.uuid()))
.filter(claim -> claim.parent == null)
.forEach(this::handleClaim);
}
}
private void handleClaim(Claim claim) {
Location min = claim.getLesserBoundaryCorner();
Location max = claim.getGreaterBoundaryCorner();
if (min == null) {
return;
}
Rectangle rect = Marker.rectangle(Point.of(min.getBlockX(), min.getBlockZ()), Point.of(max.getBlockX() + 1, max.getBlockZ() + 1));
ArrayList<String> builders = new ArrayList<>();
ArrayList<String> containers = new ArrayList<>();
ArrayList<String> accessors = new ArrayList<>();
ArrayList<String> managers = new ArrayList<>();
claim.getPermissions(builders, containers, accessors, managers);
String worldName = min.getWorld().getName();
String toolTip = Config.CLAIM_TOOLTIP;
MarkerOptions.Builder options = MarkerOptions.builder()
.strokeColor(Config.STROKE_COLOR)
.strokeWeight(Config.STROKE_WEIGHT)
.strokeOpacity(Config.STROKE_OPACITY)
.fillColor(Config.FILL_COLOR)
.fillOpacity(Config.FILL_OPACITY)
.clickTooltip((claim.isAdminClaim() ? (Config.expiringClaims.containsKey(claim.getID()) ? Config.EXPIRING_CLAIM_TOOLTIP : Config.ADMIN_CLAIM_TOOLTIP) : Config.CLAIM_TOOLTIP)
.replace("{world}", worldName)
.replace("{id}", Long.toString(claim.getID()))
.replace("{owner}", claim.getOwnerName())
.replace("{managers}", getNames(managers))
.replace("{builders}", getNames(builders))
.replace("{containers}", getNames(containers))
.replace("{accessors}", getNames(accessors))
.replace("{area}", Integer.toString(claim.getArea()))
.replace("{width}", Integer.toString(claim.getWidth()))
.replace("{height}", Integer.toString(claim.getHeight()))
.replace("{expiretime}", parseExpireTime(claim.getID()))
);
if (claim.isAdminClaim()) {
if (Config.expiringClaims.containsKey(claim.getID())) {
options.strokeColor(Config.EXPIRING_STROKE_COLOR)
.strokeWeight(Config.EXPIRING_STROKE_WEIGHT)
.strokeOpacity(Config.EXPIRING_STROKE_OPACITY)
.fillColor(Config.EXPIRING_FILL_COLOR)
.fillOpacity(Config.EXPIRING_FILL_OPACITY);
} else {
options.strokeColor(Config.ADMIN_STROKE_COLOR)
.strokeWeight(Config.ADMIN_STROKE_WEIGHT)
.strokeOpacity(Config.ADMIN_STROKE_OPACITY)
.fillColor(Config.ADMIN_FILL_COLOR)
.fillOpacity(Config.ADMIN_FILL_OPACITY);
}
}
rect.markerOptions(options);
String markerid = "griefprevention_" + worldName + "_region_" + Long.toHexString(claim.getID());
this.provider.addMarker(Key.of(markerid), rect);
}
private static String getNames(List<String> list) {
List<String> playerNames = new ArrayList<>();
for (String uuid : list)
playerNames.add(GriefPrevention.instance.trustEntryToPlayerName(uuid));
return String.join(", ", playerNames);
}
public void disable() {
cancel();
this.stop = true;
this.provider.clearMarkers();
}
private String parseExpireTime(Long claimId) {
if(Config.expiringClaims.containsKey(claimId)) {
return DateFormat.getInstance().format(Config.expiringClaims.get(claimId));
}
return "";
}
}