Skip to content

Commit

Permalink
Add support for offline warns and kicks (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 authored Aug 24, 2023
1 parent ee1f2bc commit 12820b6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ Punishment punish(
*/
CompletableFuture<Boolean> pardon(String target, Optional<UUID> issuer);

/**
* Deactivate an active punishment
*
* @param target A username or UUID string
* @param punishmentType PunishmentType to deactivate
* @return True if any punishments were deactivated, false if none
*/
CompletableFuture<Boolean> deactivate(String target, PunishmentType punishmentType);

/**
* Get whether the target is currently banned
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public void onPunishmentEvent(PlayerPunishmentEvent event) {
.build());
return;
}
} else if (PunishmentType.WARN.equals(punishment.getType())
|| PunishmentType.KICK.equals(punishment.getType())) {
// If its a warn or kick set it to active so the player sees it when they next login
punishment.setActive(true);
}

save(punishment); // Save punishment to database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
Expand Down Expand Up @@ -112,6 +114,24 @@ public CompletableFuture<Boolean> pardon(String target, Optional<UUID> issuer) {
});
}

@Override
public CompletableFuture<Boolean> deactivate(String target, PunishmentType punishmentType) {
CompletableFuture<Optional<UUID>> playerId =
NameUtils.isMinecraftName(target)
? getUsers().getStoredId(target)
: CompletableFuture.completedFuture(Optional.of(UUID.fromString(target)));
return playerId.thenApplyAsync(
uuid -> {
if (uuid.isPresent()) {
if (service.deactivate(uuid.get(), punishmentType).join()) {
sendRefresh(uuid.get());
return true;
}
}
return false;
});
}

@Override
public CompletableFuture<Boolean> isBanned(String target) {
if (NameUtils.isMinecraftName(target)) {
Expand Down Expand Up @@ -160,6 +180,22 @@ public void onPreLogin(AsyncPlayerPreLoginEvent event) {
addMute(event.getUniqueId(), mute.get());
}

Set<Punishment> deferredPunishments = getDeferredPunishments(punishments);
for (Punishment punishment : deferredPunishments) {
Bukkit.getScheduler()
.runTaskLater(
Community.get(),
() -> {
if (PunishmentType.WARN.equals(punishment.getType())
|| PunishmentType.KICK.equals(punishment.getType())) {
if (punishment.punish(true)) {
deactivate(event.getUniqueId().toString(), punishment.getType());
}
}
},
20 * 5);
}

logger.info(
punishments.size()
+ " Punishments have been fetched for "
Expand Down Expand Up @@ -239,6 +275,16 @@ private Optional<Punishment> hasActiveBan(List<Punishment> punishments) {
.findAny();
}

private Set<Punishment> getDeferredPunishments(List<Punishment> punishments) {
return punishments.stream()
.filter(
p ->
p.isActive()
&& (PunishmentType.WARN.equals(p.getType())
|| PunishmentType.KICK.equals(p.getType())))
.collect(Collectors.toSet());
}

@Override
public CompletableFuture<Optional<Punishment>> isMuted(UUID target) {
return service.isMuted(target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import tc.oc.pgm.util.UsernameFormatUtils;
import tc.oc.pgm.util.named.NameStyle;
import tc.oc.pgm.util.player.PlayerComponent;
import tc.oc.pgm.util.text.TemporalComponent;
import tc.oc.pgm.util.text.TextTranslations;

public class Punishment implements Comparable<Punishment> {
Expand Down Expand Up @@ -115,6 +116,10 @@ public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public Instant getLastUpdated() {
return lastUpdated;
}
Expand Down Expand Up @@ -178,7 +183,19 @@ public boolean kick(boolean silent) {
public void sendWarning(Audience target, String reason) {
Component titleWord = translatable("misc.warning", NamedTextColor.DARK_RED);
Component title = text().append(WARN_SYMBOL).append(titleWord).append(WARN_SYMBOL).build();
Component subtitle = text(reason, NamedTextColor.GOLD);
Component subtitle;
if (Duration.between(timeIssued, Instant.now()).getSeconds() >= 60) {
subtitle =
text()
.append(
TemporalComponent.relativePastApproximate(timeIssued)
.color(NamedTextColor.YELLOW)
.append(text(": ", NamedTextColor.YELLOW)))
.append(text(reason, NamedTextColor.GOLD))
.build();
} else {
subtitle = text(reason, NamedTextColor.GOLD);
}

target.showTitle(
title(
Expand Down Expand Up @@ -227,7 +244,18 @@ public String formatPunishmentScreen(
List<Component> lines = Lists.newArrayList();

lines.add(empty());
lines.add(getType().getScreenComponent(text(getReason(), NamedTextColor.RED)));
lines.add(
getType()
.getScreenComponent(
Duration.between(timeIssued, Instant.now()).getSeconds() >= 60
? text()
.append(
TemporalComponent.relativePastApproximate(timeIssued)
.color(NamedTextColor.YELLOW)
.append(text(": ", NamedTextColor.YELLOW)))
.append(text(reason, NamedTextColor.RED))
.build()
: text(reason, NamedTextColor.RED)));

// If punishment expires, display when
if (this instanceof ExpirablePunishment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface ModerationQuery {
"UPDATE "
+ TABLE_NAME
+ " SET active = ?, last_updated = ?, updated_by = ? WHERE active = ? AND punished = ? ";
static final String DEACTIVATE_QUERY =
"UPDATE " + TABLE_NAME + " SET active = ? WHERE active = ? AND punished = ? ";

static final String SELECT_RECENT_QUERY =
"SELECT * from " + TABLE_NAME + " WHERE time > ? LIMIT ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ public CompletableFuture<Boolean> pardon(UUID id, Optional<UUID> issuer) {
.thenApplyAsync(result -> result != 0);
}

public CompletableFuture<Boolean> deactivate(UUID id, PunishmentType punishmentType) {
punishmentCache.invalidate(id);
return DB.executeUpdateAsync(
DEACTIVATE_QUERY + SINGLE_PARDON_TYPE,
false,
true,
id.toString(),
punishmentType.toString())
.thenApplyAsync(result -> result != 0);
}

public CompletableFuture<Boolean> unmute(UUID id, Optional<UUID> issuer) {
punishmentCache.invalidate(id);

Expand Down

0 comments on commit 12820b6

Please sign in to comment.