Skip to content

Commit

Permalink
MinecraftAuth linking instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vankka committed Dec 7, 2023
1 parent 91f9f36 commit 11d1a7c
Show file tree
Hide file tree
Showing 42 changed files with 612 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
package com.discordsrv.api.player;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.UUID;

/**
Expand All @@ -46,4 +48,11 @@ public interface DiscordSRVPlayer {
@NotNull
UUID uniqueId();

/**
* Gets the locale of the player.
* @return the player's locale, or {@code null} if it isn't known
*/
@Nullable
Locale locale();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.discordsrv.bukkit.player;

import org.bukkit.entity.Player;

import java.util.Locale;

public final class PlayerLocaleProvider {

private PlayerLocaleProvider() {}

private static final boolean localeMethodExists;
private static final boolean getLocaleMethodExists;

static {
Class<?> playerClass = Player.class;

boolean locale = false, getLocale = false;
try {
playerClass.getMethod("locale");
locale = true;
} catch (ReflectiveOperationException ignored) {}
try {
playerClass.getMethod("getLocale");
getLocale = true;
} catch (ReflectiveOperationException ignored) {}
localeMethodExists = locale;
getLocaleMethodExists = getLocale;
}

@SuppressWarnings("deprecation")
public static Locale getLocale(Player player) {
if (localeMethodExists) {
return player.locale();
} else if (getLocaleMethodExists) {
return Locale.forLanguageTag(player.getLocale());
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.config.main.linking.ServerRequiredLinkingConfig;
import com.discordsrv.common.linking.LinkStore;
import com.discordsrv.common.player.IPlayer;
import com.github.benmanes.caffeine.cache.Cache;
import net.kyori.adventure.platform.bukkit.BukkitComponentSerializer;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
Expand All @@ -43,9 +45,13 @@
public class BukkitRequiredLinkingListener implements Listener {

private final BukkitDiscordSRV discordSRV;
private final Cache<UUID, Boolean> linkCheckRateLimit;

public BukkitRequiredLinkingListener(BukkitDiscordSRV discordSRV) {
this.discordSRV = discordSRV;
this.linkCheckRateLimit = discordSRV.caffeineBuilder()
.expireAfterWrite(LinkStore.LINKING_CODE_RATE_LIMIT)
.build();

register(PlayerLoginEvent.class, this::handle);
register(AsyncPlayerPreLoginEvent.class, this::handle);
Expand Down Expand Up @@ -92,18 +98,16 @@ private BukkitRequiredLinkingModule getModule() {
return module;
}

private CompletableFuture<Component> getBlockReason(UUID playerUUID, String playerName) {
private CompletableFuture<Component> getBlockReason(UUID playerUUID, String playerName, boolean join) {
BukkitRequiredLinkingModule module = getModule();
if (module == null) {
Component message = ComponentUtil.fromAPI(
discordSRV.componentFactory().textBuilder(
discordSRV.messagesConfig(null).noDiscordConnection
).build()
discordSRV.messagesConfig().minecraft.unableToLinkAtThisTime.textBuilder().build()
);
return CompletableFuture.completedFuture(message);
}

return module.getBlockReason(playerUUID, playerName);
return module.getBlockReason(playerUUID, playerName, join);
}

//
Expand Down Expand Up @@ -153,7 +157,7 @@ private void handle(
return;
}

Component kickReason = getBlockReason(playerUUID, playerName).join();
Component kickReason = getBlockReason(playerUUID, playerName, true).join();
if (kickReason != null) {
disallow.accept(BukkitComponentSerializer.legacy().serialize(kickReason));
}
Expand Down Expand Up @@ -190,7 +194,7 @@ public void onPreLogin(AsyncPlayerPreLoginEvent event) {
return;
}

Component blockReason = getBlockReason(event.getUniqueId(), event.getName()).join();
Component blockReason = getBlockReason(event.getUniqueId(), event.getName(), false).join();
if (blockReason != null) {
frozen.put(event.getUniqueId(), blockReason);
}
Expand Down Expand Up @@ -273,12 +277,19 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) {

String message = event.getMessage();
if (message.startsWith("/")) message = message.substring(1);
if (message.equals("linked")) {
if (message.equals("discord link") || message.equals("link")) {
IPlayer player = discordSRV.playerProvider().player(event.getPlayer());

if (linkCheckRateLimit.getIfPresent(player.uniqueId()) != null) {
player.sendMessage(discordSRV.messagesConfig(player).pleaseWaitBeforeRunningThatCommandAgain.asComponent());
return;
}
linkCheckRateLimit.put(player.uniqueId(), true);

player.sendMessage(Component.text("Checking..."));

UUID uuid = player.uniqueId();
getBlockReason(uuid, player.username()).whenComplete((reason, t) -> {
getBlockReason(uuid, player.username(), false).whenComplete((reason, t) -> {
if (t != null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;

public class BukkitPlayer extends BukkitCommandSender implements IPlayer {

private static final PaperComponentHandle<Player> DISPLAY_NAME_HANDLE = makeDisplayNameHandle();
Expand Down Expand Up @@ -60,6 +62,11 @@ public DiscordSRV discordSRV() {
return player.getName();
}

@Override
public Locale locale() {
return PlayerLocaleProvider.getLocale(player);
}

@Override
public @NotNull Component displayName() {
return ComponentUtil.fromAPI(DISPLAY_NAME_HANDLE.getComponent(player));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

public class BungeePlayer extends BungeeCommandSender implements IPlayer {

Expand All @@ -49,6 +52,11 @@ public DiscordSRV discordSRV() {
return commandSender.getName();
}

@Override
public @Nullable Locale locale() {
return player.getLocale();
}

@Override
public @NotNull Identity identity() {
return identity;
Expand Down
15 changes: 14 additions & 1 deletion common/src/main/java/com/discordsrv/common/DiscordSRV.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.discordsrv.common.channel.ChannelConfigHelper;
import com.discordsrv.common.command.game.GameCommandExecutionHelper;
import com.discordsrv.common.command.game.handler.ICommandHandler;
import com.discordsrv.common.command.game.sender.ICommandSender;
import com.discordsrv.common.component.ComponentFactory;
import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager;
import com.discordsrv.common.config.configurate.manager.MainConfigManager;
Expand All @@ -45,6 +46,7 @@
import com.discordsrv.common.module.ModuleManager;
import com.discordsrv.common.module.type.AbstractModule;
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
import com.discordsrv.common.player.IPlayer;
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
import com.discordsrv.common.plugin.PluginManager;
import com.discordsrv.common.profile.ProfileManager;
Expand All @@ -66,6 +68,8 @@

public interface DiscordSRV extends DiscordSRVApi {

String WEBSITE = "https://discordsrv.vankka.dev";

// Platform
IBootstrap bootstrap();
Logger platformLogger();
Expand Down Expand Up @@ -118,7 +122,16 @@ public interface DiscordSRV extends DiscordSRVApi {
MainConfigManager<? extends MainConfig> configManager();
MainConfig config();
MessagesConfigManager<? extends MessagesConfig> messagesConfigManager();
MessagesConfig messagesConfig(Locale locale);
default MessagesConfig messagesConfig() {
return messagesConfig((Locale) null);
}
default MessagesConfig.Minecraft messagesConfig(@Nullable ICommandSender sender) {
return sender instanceof IPlayer ? messagesConfig((IPlayer) sender) : messagesConfig((Locale) null).minecraft;
}
default MessagesConfig.Minecraft messagesConfig(@Nullable IPlayer player) {
return messagesConfig(player != null ? player.locale() : null).minecraft;
}
MessagesConfig messagesConfig(@Nullable Locale locale);

// Config helper
ChannelConfigHelper channelConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public CombinedCommand(DiscordSRV discordSRV) {
}

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
execute(new GameCommandExecution(discordSRV, sender, arguments));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static DiscordCommand getDiscord(DiscordSRV discordSRV) {
return DISCORD;
}

private static final String URL_FORMAT = "https://discordsrv.vankka.dev/debug/%s#%s";
private static final String URL_FORMAT = DiscordSRV.WEBSITE + "/debug/%s#%s";
private static final Base64.Encoder KEY_ENCODER = Base64.getUrlEncoder().withoutPadding();

private final PasteService pasteService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@ public CheckedFunction<String, Object> function() {
private class ExecutorProxy implements GameCommandExecutor {

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
if (!hasPermission(sender)) {
sendNoPermission(sender);
return;
}

if (commandExecutor != null) {
commandExecutor.execute(sender, arguments);
commandExecutor.execute(sender, arguments, label);
} else if (!children.isEmpty()) {
sendCommandInstructions(sender);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
@FunctionalInterface
public interface GameCommandExecutor {

void execute(ICommandSender sender, GameCommandArguments arguments);
void execute(ICommandSender sender, GameCommandArguments arguments, String label);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public DiscordSRVGameCommand(DiscordSRV discordSRV) {
}

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
MinecraftComponent component = discordSRV.componentFactory()
.textBuilder(discordSRV.config().gameCommand.discordFormat)
.addContext(sender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private BroadcastCommand(DiscordSRV discordSRV) {
}

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
doExecute(sender, arguments);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
import com.discordsrv.common.command.game.abstraction.GameCommandArguments;
import com.discordsrv.common.command.game.abstraction.GameCommandExecutor;
import com.discordsrv.common.command.game.sender.ICommandSender;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.linking.LinkProvider;
import com.discordsrv.common.linking.LinkStore;
import com.discordsrv.common.player.IPlayer;
import com.github.benmanes.caffeine.cache.Cache;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

import java.util.UUID;

public class LinkCommand implements GameCommandExecutor {

Expand All @@ -40,13 +48,54 @@ public static GameCommand get(DiscordSRV discordSRV) {
}

private final DiscordSRV discordSRV;
private final Cache<UUID, Boolean> linkCheckRateLimit;

public LinkCommand(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
this.linkCheckRateLimit = discordSRV.caffeineBuilder()
.expireAfterWrite(LinkStore.LINKING_CODE_RATE_LIMIT)
.build();
}

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
sender.sendMessage(Component.text("Not currently implemented")); // TODO
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
if (!(sender instanceof IPlayer)) {
sender.sendMessage(Component.text("Player only command").color(NamedTextColor.RED));
return;
}

IPlayer player = (IPlayer) sender;
LinkProvider linkProvider = discordSRV.linkProvider();
if (linkProvider.getCachedUserId(player.uniqueId()).isPresent()) {
player.sendMessage(discordSRV.messagesConfig(player).alreadyLinked.asComponent());
return;
}

if (linkCheckRateLimit.getIfPresent(player.uniqueId()) != null) {
player.sendMessage(discordSRV.messagesConfig(player).pleaseWaitBeforeRunningThatCommandAgain.asComponent());
return;
}
linkCheckRateLimit.put(player.uniqueId(), true);

sender.sendMessage(discordSRV.messagesConfig(player).checkingLinkStatus.asComponent());
linkProvider.queryUserId(player.uniqueId()).whenComplete((userId, t) -> {
if (t != null) {
sender.sendMessage(discordSRV.messagesConfig(player).unableToLinkAtThisTime.asComponent());
return;
}
if (userId.isPresent()) {
sender.sendMessage(discordSRV.messagesConfig(player).youAreNowLinked.asComponent());
return;
}

linkProvider.getLinkingInstructions(player, label).whenComplete((comp, t2) -> {
if (t2 != null) {
sender.sendMessage(discordSRV.messagesConfig(player).unableToLinkAtThisTime.asComponent());
return;
}

sender.sendMessage(ComponentUtil.fromAPI(comp));
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ReloadCommand(DiscordSRV discordSRV) {
}

@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
public void execute(ICommandSender sender, GameCommandArguments arguments, String label) {
AtomicBoolean dangerousFlags = new AtomicBoolean(false);
Set<DiscordSRV.ReloadFlag> flags = getFlagsFromArguments(sender, arguments, dangerousFlags);
if (flags == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void execute(ICommandSender sender, String command, List<String> argument
error(sender, command, arguments, args, "Unclosed quoted string");
},
(cmd, args) -> {
cmd.getExecutor().execute(sender, args);
cmd.getExecutor().execute(sender, args, command);
return null;
},
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ private static <S> CommandNode<S> convert(GameCommand commandBuilder, Function<S
argumentBuilder.executes(context -> {
executor.execute(
commandSenderMapper.apply(context.getSource()),
context::getArgument
context::getArgument,
label
);
return Command.SINGLE_SUCCESS;
});
Expand Down
Loading

0 comments on commit 11d1a7c

Please sign in to comment.