Skip to content

Commit

Permalink
Remove optional & instant from network update types
Browse files Browse the repository at this point in the history
Signed-off-by: applenick <[email protected]>
  • Loading branch information
applenick committed Jun 17, 2024
1 parent ad655f9 commit 2809167
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void unMutePlayer(CommandAudience audience, @Argument("target") TargetPla
name -> {
if (isMuted.isPresent()) {
moderation
.unmute(id.get(), audience.getId())
.unmute(id.get(), audience.getId().orElse(null))
.thenAcceptAsync(
pardon -> {
if (!pardon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void unbanPlayer(CommandAudience audience, @Argument("target") TargetPlay
isBanned -> {
if (isBanned) {
moderation
.pardon(target.getIdentifier(), audience.getId())
.pardon(target.getIdentifier(), audience.getId().orElse(null))
.thenAcceptAsync(
pardon -> {
if (!pardon) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ Punishment punish(
* Pardon target for past punishments (Ban/Tempban)
*
* @param target A username or UUID string
* @param issuer An optional UUID of the command sender (console is empty)
* @param issuer A UUID of the command sender (null for console)
* @return True if any ban infractions were lifted, false if none
*/
CompletableFuture<Boolean> pardon(String target, Optional<UUID> issuer);
CompletableFuture<Boolean> pardon(String target, @Nullable UUID issuer);

/**
* Deactivate an active punishment
Expand Down Expand Up @@ -97,10 +97,10 @@ Punishment punish(
* Unmutes any active mutes for the provided target
*
* @param target A player UUID
* @param issuer The person lifting the infraction
* @param issuer The UUID of the player lifting the mute (null for console)
* @return true if unmute was removed, false if no mute existed
*/
CompletableFuture<Boolean> unmute(UUID target, Optional<UUID> issuer);
CompletableFuture<Boolean> unmute(UUID target, @Nullable UUID issuer);

/**
* Gets a set of online players who are muted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ public Punishment punish(
target,
getSenderId(issuer.getSender()),
reason,
time,
time.toEpochMilli(),
duration,
type,
active,
time,
time.toEpochMilli(),
getSenderId(issuer.getSender()),
getModerationConfig().getService());
Bukkit.getPluginManager().callEvent(new PlayerPunishmentEvent(issuer, punishment, silent));
Expand All @@ -157,7 +157,7 @@ public ModerationTools getTools() {
@Override
public Optional<Punishment> getLastPunishment(UUID issuer) {
return recents.stream()
.filter(p -> p.getIssuerId().isPresent() && p.getIssuerId().get().equals(issuer))
.filter(p -> !p.isConsole() && p.getIssuerId().equals(issuer))
.sorted()
.findFirst();
}
Expand Down Expand Up @@ -343,8 +343,12 @@ public Optional<MutePunishment> getCachedMute(UUID playerId) {
}

// ETC.
private Optional<UUID> getSenderId(CommandSender sender) {
return Optional.ofNullable(sender instanceof Player ? ((Player) sender).getUniqueId() : null);
@Nullable
private UUID getSenderId(CommandSender sender) {
if (!(sender instanceof Player)) return null;

Player player = (Player) sender;
return player.getUniqueId();
}

private Optional<UUID> isBanEvasion(String address) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
Expand Down Expand Up @@ -96,7 +97,7 @@ public CompletableFuture<List<Punishment>> query(String target) {
}

@Override
public CompletableFuture<Boolean> pardon(String target, Optional<UUID> issuer) {
public CompletableFuture<Boolean> pardon(String target, @Nullable UUID issuer) {
CompletableFuture<Optional<UUID>> playerId =
NameUtils.isMinecraftName(target)
? getUsers().getStoredId(target)
Expand Down Expand Up @@ -179,7 +180,7 @@ public void onPreLogin(AsyncPlayerPreLoginEvent event) {
if (punishment.getType() == PunishmentType.NAME_BAN) {
String bannedName = punishment.getReason();
if (!event.getName().equalsIgnoreCase(bannedName)) {
pardon(punishment.getTargetId().toString(), Optional.empty());
pardon(punishment.getTargetId().toString(), null);
event.setLoginResult(Result.ALLOWED);
logger.info(
String.format(
Expand Down Expand Up @@ -305,7 +306,7 @@ public CompletableFuture<Optional<Punishment>> isMuted(UUID target) {
}

@Override
public CompletableFuture<Boolean> unmute(UUID id, Optional<UUID> issuer) {
public CompletableFuture<Boolean> unmute(UUID id, @Nullable UUID issuer) {
return service
.unmute(id, issuer)
.thenApplyAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public class Punishment implements Comparable<Punishment> {

private UUID punishmentId;
private UUID targetId;
private Optional<UUID> issuerId;
private @Nullable UUID issuerId;
private String reason;
private Instant timeIssued;
private long timeIssued;
private boolean active;
private @Nullable Duration duration;

private Instant lastUpdated;
private Optional<UUID> lastUpdatedBy;
private long lastUpdated;
private @Nullable UUID lastUpdatedBy;

private String service;

Expand All @@ -66,13 +66,13 @@ public Punishment(
PunishmentType type,
UUID punishmentId,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Duration duration,
Instant timeIssued,
long timeIssued,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
this.type = type;
this.punishmentId = punishmentId;
Expand Down Expand Up @@ -103,16 +103,21 @@ public UUID getTargetId() {
return targetId;
}

public Optional<UUID> getIssuerId() {
@Nullable
public UUID getIssuerId() {
return issuerId;
}

public boolean isConsole() {
return getIssuerId() == null;
}

public String getReason() {
return reason;
}

public Instant getTimeIssued() {
return timeIssued;
return Instant.ofEpochMilli(timeIssued);
}

public boolean isActive() {
Expand All @@ -124,10 +129,10 @@ public void setActive(boolean active) {
}

public Instant getLastUpdated() {
return lastUpdated;
return Instant.ofEpochMilli(lastUpdated);
}

public Optional<UUID> getLastUpdatedBy() {
public @Nullable UUID getLastUpdatedBy() {
return lastUpdatedBy;
}

Expand Down Expand Up @@ -169,9 +174,9 @@ public boolean kick(boolean silent) {
.kickPlayer(
formatPunishmentScreen(
getConfig(),
getIssuerId().isPresent()
? PlayerComponent.player(getIssuerId().get(), NameStyle.FANCY)
: UsernameFormatUtils.CONSOLE_NAME,
isConsole()
? UsernameFormatUtils.CONSOLE_NAME
: PlayerComponent.player(getIssuerId(), NameStyle.FANCY),
silent));
return true;
}
Expand All @@ -187,11 +192,11 @@ 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;
if (Duration.between(timeIssued, Instant.now()).getSeconds() >= 60) {
if (Duration.between(getTimeIssued(), Instant.now()).getSeconds() >= 60) {
subtitle =
text()
.append(
TemporalComponent.relativePastApproximate(timeIssued)
TemporalComponent.relativePastApproximate(getTimeIssued())
.color(NamedTextColor.YELLOW)
.append(text(": ", NamedTextColor.YELLOW)))
.append(text(reason, NamedTextColor.GOLD))
Expand Down Expand Up @@ -265,10 +270,10 @@ public String formatPunishmentScreen(
lines.add(
getType()
.getScreenComponent(
Duration.between(timeIssued, Instant.now()).getSeconds() >= 60
Duration.between(getTimeIssued(), Instant.now()).getSeconds() >= 60
? text()
.append(
TemporalComponent.relativePastApproximate(timeIssued)
TemporalComponent.relativePastApproximate(getTimeIssued())
.color(NamedTextColor.YELLOW)
.append(text(": ", NamedTextColor.YELLOW)))
.append(text(reason, NamedTextColor.RED))
Expand Down Expand Up @@ -331,26 +336,26 @@ public static Punishment of(Punishment punishment) {
punishment.getTargetId(),
punishment.getIssuerId(),
punishment.getReason(),
punishment.getTimeIssued(),
punishment.getTimeIssued().toEpochMilli(),
punishment.getDuration(),
punishment.getType(),
punishment.isActive(),
punishment.getLastUpdated(),
punishment.getLastUpdated().toEpochMilli(),
punishment.getLastUpdatedBy(),
punishment.getService());
}

public static Punishment of(
UUID id,
UUID target,
Optional<UUID> issuer,
@Nullable UUID issuer,
String reason,
Instant time,
long time,
@Nullable Duration length,
PunishmentType type,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
switch (type) {
case WARN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import dev.pgm.community.moderation.punishments.Punishment;
import dev.pgm.community.moderation.punishments.PunishmentType;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;

public class BanPunishment extends Punishment {

public BanPunishment(
UUID punishmentId,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Instant timeIssued,
long timeIssued,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
super(
PunishmentType.BAN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.pgm.community.moderation.punishments.PunishmentType;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;

Expand All @@ -15,13 +14,13 @@ public ExpirablePunishment(
PunishmentType type,
UUID id,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Instant timeIssued,
long timeIssued,
Duration length,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
super(
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import dev.pgm.community.moderation.punishments.Punishment;
import dev.pgm.community.moderation.punishments.PunishmentType;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;

public class KickPunishment extends Punishment {

public KickPunishment(
UUID id,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Instant timeIssued,
long timeIssued,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
super(
PunishmentType.KICK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import dev.pgm.community.moderation.punishments.PunishmentType;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import tc.oc.pgm.util.Audience;
Expand All @@ -18,13 +18,13 @@ public class MutePunishment extends ExpirablePunishment {
public MutePunishment(
UUID id,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Instant timeIssued,
long timeIssued,
Duration length,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
super(
PunishmentType.MUTE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import dev.pgm.community.moderation.punishments.PunishmentType;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;

public class TempBanPunishment extends ExpirablePunishment {

public TempBanPunishment(
UUID id,
UUID targetId,
Optional<UUID> issuerId,
@Nullable UUID issuerId,
String reason,
Instant timeIssued,
long timeIssued,
Duration length,
boolean active,
Instant lastUpdated,
Optional<UUID> lastUpdatedBy,
long lastUpdated,
@Nullable UUID lastUpdatedBy,
String service) {
super(
PunishmentType.TEMP_BAN,
Expand Down
Loading

0 comments on commit 2809167

Please sign in to comment.