diff --git a/api/src/main/java/com/discordsrv/api/module/type/NicknameModule.java b/api/src/main/java/com/discordsrv/api/module/type/NicknameModule.java index 084880bf..ef93608b 100644 --- a/api/src/main/java/com/discordsrv/api/module/type/NicknameModule.java +++ b/api/src/main/java/com/discordsrv/api/module/type/NicknameModule.java @@ -1,10 +1,11 @@ package com.discordsrv.api.module.type; import java.util.UUID; +import java.util.concurrent.CompletableFuture; public interface NicknameModule { - String getNickname(UUID playerUUID); - void setNickname(UUID playerUUID, String nickname); + CompletableFuture getNickname(UUID playerUUID); + CompletableFuture setNickname(UUID playerUUID, String nickname); } diff --git a/api/src/main/java/com/discordsrv/api/module/type/PunishmentModule.java b/api/src/main/java/com/discordsrv/api/module/type/PunishmentModule.java index ac624235..87dc02e0 100644 --- a/api/src/main/java/com/discordsrv/api/module/type/PunishmentModule.java +++ b/api/src/main/java/com/discordsrv/api/module/type/PunishmentModule.java @@ -5,35 +5,34 @@ import org.jetbrains.annotations.Nullable; import java.time.Instant; import java.util.UUID; +import java.util.concurrent.CompletableFuture; public interface PunishmentModule { interface Bans extends PunishmentModule { - Punishment getBan(@NotNull UUID playerUUID); - void addBan(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason); - void removeBan(@NotNull UUID playerUUID); + @Nullable + CompletableFuture getBan(@NotNull UUID playerUUID); + CompletableFuture addBan(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher); + CompletableFuture removeBan(@NotNull UUID playerUUID); } interface Mutes extends PunishmentModule { - Punishment getMute(@NotNull UUID playerUUID); - void addMute(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason); - void removeMute(@NotNull UUID playerUUID); + @Nullable + CompletableFuture getMute(@NotNull UUID playerUUID); + CompletableFuture addMute(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher); + CompletableFuture removeMute(@NotNull UUID playerUUID); } class Punishment { - private final boolean active; private final Instant until; private final String reason; + private final String punisher; - public Punishment(boolean active, @Nullable Instant until, @Nullable String reason) { - this.active = active; + public Punishment(@Nullable Instant until, @Nullable String reason, @Nullable String punisher) { this.until = until; this.reason = reason; - } - - public boolean active() { - return active; + this.punisher = punisher; } public Instant until() { @@ -43,5 +42,9 @@ public interface PunishmentModule { public String reason() { return reason; } + + public String punisher() { + return punisher; + } } } diff --git a/bukkit/paper/src/main/java/com/discordsrv/bukkit/ban/PaperBanList.java b/bukkit/paper/src/main/java/com/discordsrv/bukkit/ban/PaperBanList.java new file mode 100644 index 00000000..d971b604 --- /dev/null +++ b/bukkit/paper/src/main/java/com/discordsrv/bukkit/ban/PaperBanList.java @@ -0,0 +1,40 @@ +package com.discordsrv.bukkit.ban; + +import org.bukkit.BanEntry; +import org.bukkit.BanList; +import org.bukkit.Server; + +import java.time.Instant; +import java.util.UUID; + +public final class PaperBanList { + + public static final boolean IS_AVAILABLE; + + static { + boolean is = false; + try { + BanList.Type.valueOf("PROFILE"); + is = true; + } catch (IllegalArgumentException ignored) {} + IS_AVAILABLE = is; + } + + private PaperBanList() {} + + public static BanList banList(Server server) { + return server.getBanList(BanList.Type.PROFILE); + } + + public static BanEntry getBanEntry(Server server, UUID playerUUID) { + return banList(server).getBanEntry(playerUUID); + } + + public static void addBan(Server server, UUID playerUUID, Instant until, String reason, String punisher) { + banList(server).addBan(playerUUID, reason, until, punisher); + } + + public static void removeBan(Server server, UUID playerUUID) { + banList(server).pardon(playerUUID); + } +} diff --git a/bukkit/paper/src/main/java/com/discordsrv/bukkit/PaperCommandMap.java b/bukkit/paper/src/main/java/com/discordsrv/bukkit/command/PaperCommandMap.java similarity index 93% rename from bukkit/paper/src/main/java/com/discordsrv/bukkit/PaperCommandMap.java rename to bukkit/paper/src/main/java/com/discordsrv/bukkit/command/PaperCommandMap.java index 6ef3b263..9f0b0d8d 100644 --- a/bukkit/paper/src/main/java/com/discordsrv/bukkit/PaperCommandMap.java +++ b/bukkit/paper/src/main/java/com/discordsrv/bukkit/command/PaperCommandMap.java @@ -1,4 +1,4 @@ -package com.discordsrv.bukkit; +package com.discordsrv.bukkit.command; import org.bukkit.Server; diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java b/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java index 192fc968..b0b0ec78 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/BukkitDiscordSRV.java @@ -19,6 +19,7 @@ package com.discordsrv.bukkit; import com.discordsrv.api.DiscordSRVApi; +import com.discordsrv.bukkit.ban.BukkitBanModule; import com.discordsrv.bukkit.command.game.BukkitGameCommandExecutionHelper; import com.discordsrv.bukkit.command.game.handler.AbstractBukkitCommandHandler; import com.discordsrv.bukkit.component.translation.BukkitTranslationLoader; @@ -195,10 +196,12 @@ public class BukkitDiscordSRV extends ServerDiscordSRV implements PunishmentModule.Bans { + + public BukkitBanModule(BukkitDiscordSRV discordSRV) { + super(discordSRV, new NamedLogger(discordSRV, "BUKKIT_BAN")); + } + + @Override + public CompletableFuture getBan(@NotNull UUID playerUUID) { + CompletableFuture entryFuture; + if (PaperBanList.IS_AVAILABLE) { + entryFuture = CompletableFuture.completedFuture(PaperBanList.getBanEntry(discordSRV.server(), playerUUID)); + } else { + BanList banList = discordSRV.server().getBanList(BanList.Type.NAME); + entryFuture = discordSRV.playerProvider().lookupOfflinePlayer(playerUUID) + .thenApply(offlinePlayer -> banList.getBanEntry(offlinePlayer.username())); + } + + return entryFuture.thenApply(ban -> { + Date expiration = ban.getExpiration(); + return new PunishmentModule.Punishment(expiration != null ? expiration.toInstant() : null, ban.getReason(), ban.getSource()); + }); + } + + @Override + public CompletableFuture addBan(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher) { + if (PaperBanList.IS_AVAILABLE) { + PaperBanList.addBan(discordSRV.server(), playerUUID, until, reason, punisher); + return CompletableFuture.completedFuture(null); + } + + BanList banList = discordSRV.server().getBanList(BanList.Type.NAME); + return discordSRV.playerProvider().lookupOfflinePlayer(playerUUID).thenApply(offlinePlayer -> { + banList.addBan(offlinePlayer.username(), reason, until != null ? Date.from(until) : null, punisher); + return null; + }); + } + + @Override + public CompletableFuture removeBan(@NotNull UUID playerUUID) { + if (PaperBanList.IS_AVAILABLE) { + PaperBanList.removeBan(discordSRV.server(), playerUUID); + return CompletableFuture.completedFuture(null); + } + + BanList banList = discordSRV.server().getBanList(BanList.Type.NAME); + return discordSRV.playerProvider().lookupOfflinePlayer(playerUUID).thenApply(offlinePlayer -> { + banList.pardon(offlinePlayer.username()); + return null; + }); + } +} diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/command/game/BukkitGameCommandExecutionHelper.java b/bukkit/src/main/java/com/discordsrv/bukkit/command/game/BukkitGameCommandExecutionHelper.java index 4418bfb5..da3d4a10 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/command/game/BukkitGameCommandExecutionHelper.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/command/game/BukkitGameCommandExecutionHelper.java @@ -1,7 +1,7 @@ package com.discordsrv.bukkit.command.game; import com.discordsrv.bukkit.BukkitDiscordSRV; -import com.discordsrv.bukkit.PaperCommandMap; +import com.discordsrv.bukkit.command.PaperCommandMap; import com.discordsrv.common.command.game.GameCommandExecutionHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; diff --git a/bukkit/src/main/java/com/discordsrv/bukkit/integration/EssentialsXIntegration.java b/bukkit/src/main/java/com/discordsrv/bukkit/integration/EssentialsXIntegration.java index 26132071..c657e47e 100644 --- a/bukkit/src/main/java/com/discordsrv/bukkit/integration/EssentialsXIntegration.java +++ b/bukkit/src/main/java/com/discordsrv/bukkit/integration/EssentialsXIntegration.java @@ -14,6 +14,7 @@ import com.discordsrv.common.component.util.ComponentUtil; import com.discordsrv.common.module.type.PluginIntegration; import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; +import com.earth2me.essentials.UserData; import net.essentialsx.api.v2.events.chat.GlobalChatEvent; import net.kyori.adventure.platform.bukkit.BukkitComponentSerializer; import org.bukkit.entity.Player; @@ -25,6 +26,7 @@ import org.jetbrains.annotations.Nullable; import java.time.Instant; import java.util.Collection; import java.util.UUID; +import java.util.concurrent.CompletableFuture; public class EssentialsXIntegration extends PluginIntegration @@ -55,46 +57,46 @@ public class EssentialsXIntegration return (Essentials) discordSRV.server().getPluginManager().getPlugin("Essentials"); } - private User getUser(UUID playerUUID) { - return get().getUsers().loadUncachedUser(playerUUID); + private CompletableFuture getUser(UUID playerUUID) { + return CompletableFuture.supplyAsync(() -> get().getUsers().loadUncachedUser(playerUUID), discordSRV.scheduler().executor()); } @Override - public String getNickname(UUID playerUUID) { - User user = getUser(playerUUID); - return user.getNickname(); + public CompletableFuture getNickname(UUID playerUUID) { + return getUser(playerUUID).thenApply(UserData::getNickname); } @Override - public void setNickname(UUID playerUUID, String nickname) { - User user = getUser(playerUUID); - user.setNickname(nickname); + public CompletableFuture setNickname(UUID playerUUID, String nickname) { + return getUser(playerUUID).thenApply(user -> { + user.setNickname(nickname); + return null; + }); } @Override - public Punishment getMute(@NotNull UUID playerUUID) { - User user = getUser(playerUUID); - if (!user.isMuted()) { - return new Punishment(false, null, null); - } - - return new Punishment(true, Instant.ofEpochMilli(user.getMuteTimeout()), user.getMuteReason()); + public CompletableFuture getMute(@NotNull UUID playerUUID) { + return getUser(playerUUID).thenApply(user -> new Punishment(Instant.ofEpochMilli(user.getMuteTimeout()), user.getMuteReason(), null)); } @Override - public void addMute(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason) { - User user = getUser(playerUUID); - user.setMuted(true); - user.setMuteTimeout(until != null ? until.toEpochMilli() : 0); - user.setMuteReason(reason); + public CompletableFuture addMute(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher) { + return getUser(playerUUID).thenApply(user -> { + user.setMuted(true); + user.setMuteTimeout(until != null ? until.toEpochMilli() : 0); + user.setMuteReason(reason); + return null; + }); } @Override - public void removeMute(@NotNull UUID playerUUID) { - User user = getUser(playerUUID); - user.setMuted(false); - user.setMuteTimeout(0); - user.setMuteReason(null); + public CompletableFuture removeMute(@NotNull UUID playerUUID) { + return getUser(playerUUID).thenApply(user -> { + user.setMuted(false); + user.setMuteTimeout(0); + user.setMuteReason(null); + return null; + }); } @EventHandler(priority = org.bukkit.event.EventPriority.MONITOR) diff --git a/common/src/main/java/com/discordsrv/common/module/type/PluginIntegration.java b/common/src/main/java/com/discordsrv/common/module/type/PluginIntegration.java index 8b41851b..37dfa905 100644 --- a/common/src/main/java/com/discordsrv/common/module/type/PluginIntegration.java +++ b/common/src/main/java/com/discordsrv/common/module/type/PluginIntegration.java @@ -34,6 +34,10 @@ public abstract class PluginIntegration
extends AbstractM super(discordSRV, logger); } + /** + * The id/name of the plugin/mod this integration is for. + * @return the id (when available) or name of the plugin or mod + */ @NotNull public abstract String getIntegrationName();