Add cross server player name completions

This commit is contained in:
Draycia committed 2020-11-21 21:47:38 -08:00
1 parent aac52d6a29
commit 066096dea4
15 files changed
+73 -48

No files matched your search

@@ -3,6 +3,7 @@ package net.draycia.carbon.api.users;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@@ -24,6 +25,8 @@ public interface UserService<T extends PlayerUser> {
void validate(@NonNull T user);
@NonNull Iterable<@NonNull T> onlineUsers();
@NonNull Collection<@NonNull T> onlineUsers();
@NonNull Collection<@NonNull String> proxyPlayers();
}
@@ -3,6 +3,7 @@ package net.draycia.carbon.bukkit;
import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager;
import io.github.leonardosnt.bungeechannelapi.BungeeChannelApi;
import net.draycia.carbon.bukkit.listeners.events.BukkitChatListener;
import net.draycia.carbon.bukkit.listeners.events.ItemLinkHandler;
import net.draycia.carbon.bukkit.messaging.BungeeMessageService;
@@ -79,6 +80,7 @@ import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;
@@ -101,6 +103,8 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
private Logger logger;
private @NonNull Collection<@NonNull String> proxyPlayerNames = new ArrayList<>();
public static final LegacyComponentSerializer LEGACY =
LegacyComponentSerializer.builder()
.extractUrls()
@@ -144,10 +148,17 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
this.messageManager = new MessageManager(this, messageService);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
if (Bukkit.getOnlinePlayers().size() > 0) {
BungeeChannelApi.of(this).getPlayerList("ALL")
.thenAccept(value -> this.proxyPlayerNames = value);
}
}, 1, 5 * 20); // 5 seconds * 20 ticks per second
// Handle storage service
final StorageType storageType = this.carbonSettings().storageType();
final Supplier<Iterable<BukkitPlayerUser>> supplier = () -> {
final Supplier<Collection<BukkitPlayerUser>> supplier = () -> {
final List<BukkitPlayerUser> users = new ArrayList<>();
for (final Player player : Bukkit.getOnlinePlayers()) {
@@ -160,15 +171,15 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
if (storageType == StorageType.MYSQL) {
this.logger().info("Enabling MySQL storage!");
final SQLCredentials credentials = this.carbonSettings().sqlCredentials();
this.userService = new MySQLUserService<>(this, credentials, supplier,
this.userService = new MySQLUserService<>(this, credentials, supplier, () -> this.proxyPlayerNames,
BukkitPlayerUser::new, BukkitConsoleUser::new);
} else if (storageType == StorageType.JSON) {
this.logger().info("Enabling JSON storage!");
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, () -> this.proxyPlayerNames,
BukkitPlayerUser::new, BukkitConsoleUser::new);
} else {
this.logger().error("Invalid storage type selected! Falling back to JSON.");
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier,
this.userService = new JSONUserService<>(BukkitPlayerUser.class, this, supplier, () -> this.proxyPlayerNames,
BukkitPlayerUser::new, BukkitConsoleUser::new);
}
@@ -47,7 +47,7 @@ public class ChannelListCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.channellist.others")
.argument(PlayerUserArgument.requiredPlayerUserArgument()) // carbonchat.channellist.other
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name())) // carbonchat.channellist.other
.handler(this::channelListOther)
.build()
);
@@ -40,7 +40,7 @@ public class ClearChatCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // player & console
.permission("carbonchat.clearchat.clear.player")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.handler(this::clearChatPlayer)
.build()
);
@@ -31,7 +31,7 @@ public class IgnoreCommand {
commandManager.createDefaultCommandMeta())
.senderType(PlayerUser.class) // player
.permission("carbonchat.ignore")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.handler(this::ignoreUser)
.build()
);
@@ -37,7 +37,7 @@ public class MessageCommand {
commandManager.createDefaultCommandMeta())
.senderType(PlayerUser.class) // player
.permission("carbonchat.message")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.argument(StringArgument.<CarbonUser>newBuilder("message").greedy().asOptional().build())
.handler(this::sendMessage)
.build()
@@ -41,7 +41,7 @@ public class MsgToggleCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.msgtoggle.others")
.argument(PlayerUserArgument.requiredPlayerUserArgument()) // carbonchat.channellist.other
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name())) // carbonchat.channellist.other
.handler(this::toggleOther)
.build()
);
@@ -31,7 +31,7 @@ public class MuteCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // player & console
.permission("carbonchat.mute")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.handler(this::mute)
.build()
);
@@ -42,7 +42,7 @@ public class NicknameCommand {
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.nickname.others")
.argument(StringArgument.of("nickname"))
.argument(PlayerUserArgument.requiredPlayerUserArgument()) // carbonchat.channellist.other
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name())) // carbonchat.channellist.other
.handler(this::nicknameOther)
.build()
);
@@ -31,7 +31,7 @@ public class ShadowMuteCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // player & console
.permission("carbonchat.shadowmute")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.handler(this::shadowMute)
.build()
);
@@ -33,7 +33,7 @@ public class SudoChannelCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.channel.others")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.argument(ChannelArgument.requiredChannelArgument())
.handler(this::otherChannel)
.build()
@@ -44,7 +44,7 @@ public class SudoChannelCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.channel.others.message")
.argument(PlayerUserArgument.requiredPlayerUserArgument())
.argument(PlayerUserArgument.requiredPlayerUserArgument(commandSettings.name()))
.argument(ChannelArgument.requiredChannelArgument())
.argument(StringArgument.<CarbonUser>newBuilder("message").greedy().asOptional().build())
.handler(this::sendMessageOther)
@@ -44,7 +44,7 @@ public class ToggleCommand {
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.toggle.others")
.argument(ChannelArgument.requiredChannelArgument())
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.toggle.other
.argument(PlayerUserArgument.optionalPlayerUserArgument(commandSettings.name())) // carbonchat.toggle.other
.handler(this::toggleOther)
.build()
);
@@ -4,24 +4,34 @@ import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.context.CommandContext;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.PlayerUser;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
public final class PlayerUserArgument<C> extends CommandArgument<C, PlayerUser> {
public final class PlayerUserArgument<C extends CarbonUser> extends CommandArgument<C, PlayerUser> {
private PlayerUserArgument(final boolean required) {
private PlayerUserArgument(final @NonNull String command, final boolean required) {
super(required, "user", PlayerUserArgument::parser,
"",
PlayerUser.class, PlayerUserArgument::suggestions);
PlayerUser.class, (context, input) -> {
final List<String> suggestions = CarbonChatProvider.carbonChat().userService().onlineUsers().stream()
.map(PlayerUser::name).collect(Collectors.toCollection(ArrayList::new));
if (context.getSender().hasPermission("carbonchat.command.completions." + command)) {
suggestions.addAll(CarbonChatProvider.carbonChat().userService().proxyPlayers());
}
return suggestions;
});
}
private static <C> ArgumentParseResult<PlayerUser> parser(final @NonNull CommandContext<C> commandContext,
private static <C extends CarbonUser> @NonNull ArgumentParseResult<PlayerUser> parser(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputs) {
final String input = inputs.poll();
@@ -33,34 +43,17 @@ public final class PlayerUserArgument<C> extends CommandArgument<C, PlayerUser>
}
final UUID uuid = CarbonChatProvider.carbonChat().resolveUUID(input);
if (uuid == null) {
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
}
final PlayerUser user = CarbonChatProvider.carbonChat().userService().wrap(uuid);
if (user == null) {
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
}
return ArgumentParseResult.success(user);
}
private static <C> List<String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
return StreamSupport
.stream(CarbonChatProvider.carbonChat().userService().onlineUsers().spliterator(), false)
.map(PlayerUser::name)
.collect(Collectors.toList());
public static <C extends CarbonUser> PlayerUserArgument<C> requiredPlayerUserArgument(final @NonNull String command) {
return new PlayerUserArgument<>(command, true);
}
public static <C> PlayerUserArgument<C> requiredPlayerUserArgument() {
return new PlayerUserArgument<>(true);
}
public static <C> PlayerUserArgument<C> optionalPlayerUserArgument() {
return new PlayerUserArgument<>(false);
public static <C extends CarbonUser> PlayerUserArgument<C> optionalPlayerUserArgument(final @NonNull String command) {
return new PlayerUserArgument<>(command, false);
}
}
@@ -21,6 +21,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
@@ -34,7 +35,8 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
private final @NonNull CarbonChat carbonChat;
private final @NonNull Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final @NonNull Type userType;
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
private final @NonNull Supplier<@NonNull Collection<@NonNull T>> supplier;
private final @NonNull Supplier<@NonNull Collection<@NonNull String>> nameSupplier;
private final @NonNull Function<UUID, T> userFactory;
private final @NonNull Supplier<C> consoleFactory;
@@ -45,12 +47,14 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
public JSONUserService(final @NonNull Class<? extends CarbonUser> userType,
final @NonNull CarbonChat carbonChat,
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
final @NonNull Supplier<@NonNull Collection<@NonNull T>> supplier,
final @NonNull Supplier<@NonNull Collection<@NonNull String>> nameSupplier,
final @NonNull Function<UUID, T> userFactory,
final @NonNull Supplier<C> consoleFactory) {
this.userType = userType;
this.carbonChat = carbonChat;
this.supplier = supplier;
this.nameSupplier = nameSupplier;
this.userFactory = userFactory;
this.consoleFactory = consoleFactory;
@@ -112,10 +116,15 @@ public class JSONUserService<T extends PlayerUser, C extends ConsoleUser> implem
}
@Override
public @NonNull Iterable<@NonNull T> onlineUsers() {
public @NonNull Collection<@NonNull T> onlineUsers() {
return this.supplier.get();
}
@Override
public @NonNull Collection<@NonNull String> proxyPlayers() {
return this.nameSupplier.get();
}
private @NonNull T loadUser(final @NonNull UUID uuid) {
final File userFile = new File(this.carbonChat.dataFolder().toFile(), "users/" + uuid.toString() + ".json");
this.ensureFileExists(userFile);
@@ -25,6 +25,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Timer;
@@ -39,7 +40,8 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
private final @NonNull CarbonChat carbonChat;
private final @NonNull Database database;
private final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier;
private final @NonNull Supplier<@NonNull Collection<@NonNull T>> supplier;
private final @NonNull Supplier<@NonNull Collection<@NonNull String>> nameSupplier;
private final @NonNull Function<UUID, T> userFactory;
private final @NonNull Supplier<C> consoleFactory;
@@ -49,11 +51,13 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
.build(CacheLoader.from(this::loadUser));
public MySQLUserService(final @NonNull CarbonChat carbonChat, final @NonNull SQLCredentials credentials,
final @NonNull Supplier<@NonNull Iterable<@NonNull T>> supplier,
final @NonNull Supplier<@NonNull Collection<@NonNull T>> supplier,
final @NonNull Supplier<@NonNull Collection<@NonNull String>> nameSupplier,
final @NonNull Function<UUID, T> userFactory,
final @NonNull Supplier<C> consoleFactory) {
this.carbonChat = carbonChat;
this.supplier = supplier;
this.nameSupplier = nameSupplier;
this.userFactory = userFactory;
this.consoleFactory = consoleFactory;
@@ -158,10 +162,15 @@ public class MySQLUserService<T extends PlayerUser, C extends ConsoleUser> imple
}
@Override
public @NonNull Iterable<@NonNull T> onlineUsers() {
public @NonNull Collection<@NonNull T> onlineUsers() {
return this.supplier.get();
}
@Override
public @NonNull Collection<@NonNull String> proxyPlayers() {
return this.nameSupplier.get();
}
private @Nullable T loadUser(final @NonNull UUID uuid) {
final T user = this.userFactory.apply(uuid);