Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for offline warns and kicks #46

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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