Implement getting vanilla bans for bukkit. Make nickname, ban and mute apis use CompletableFuture

This commit is contained in:
Vankka 2024-03-30 21:18:28 +02:00
parent 2d05620462
commit 50445503ed
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
9 changed files with 162 additions and 42 deletions

View File

@ -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<String> getNickname(UUID playerUUID);
CompletableFuture<Void> setNickname(UUID playerUUID, String nickname);
}

View File

@ -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<Punishment> getBan(@NotNull UUID playerUUID);
CompletableFuture<Void> addBan(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher);
CompletableFuture<Void> 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<Punishment> getMute(@NotNull UUID playerUUID);
CompletableFuture<Void> addMute(@NotNull UUID playerUUID, @Nullable Instant until, @Nullable String reason, @NotNull String punisher);
CompletableFuture<Void> 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;
}
}
}

View File

@ -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<UUID> 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);
}
}

View File

@ -1,4 +1,4 @@
package com.discordsrv.bukkit;
package com.discordsrv.bukkit.command;
import org.bukkit.Server;

View File

@ -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<DiscordSRVBukkitBootstrap
// Modules
registerModule(MinecraftToDiscordChatModule::new);
registerModule(BukkitRequiredLinkingModule::new);
registerModule(BukkitBanModule::new);
// Integrations
registerIntegration("com.discordsrv.bukkit.integration.VaultIntegration");
registerIntegration("com.discordsrv.bukkit.integration.PlaceholderAPIIntegration");
registerIntegration("com.discordsrv.bukkit.integration.EssentialsXIntegration");
// Chat Integrations
registerIntegration("com.discordsrv.bukkit.integration.chat.ChattyChatIntegration");

View File

@ -0,0 +1,67 @@
package com.discordsrv.bukkit.ban;
import com.discordsrv.api.module.type.PunishmentModule;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.common.logging.NamedLogger;
import com.discordsrv.common.module.type.AbstractModule;
import org.bukkit.BanEntry;
import org.bukkit.BanList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.Instant;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class BukkitBanModule extends AbstractModule<BukkitDiscordSRV> implements PunishmentModule.Bans {
public BukkitBanModule(BukkitDiscordSRV discordSRV) {
super(discordSRV, new NamedLogger(discordSRV, "BUKKIT_BAN"));
}
@Override
public CompletableFuture<Punishment> getBan(@NotNull UUID playerUUID) {
CompletableFuture<BanEntry> 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<Void> 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<Void> 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;
});
}
}

View File

@ -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;

View File

@ -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<BukkitDiscordSRV>
@ -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<User> 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<String> 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<Void> 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<Punishment> 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<Void> 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<Void> 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)

View File

@ -34,6 +34,10 @@ public abstract class PluginIntegration<DT extends DiscordSRV> 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();