Rename some things, prepare for providing chat suggestions

This commit is contained in:
Vankka 2024-08-12 14:30:38 +03:00
parent c4eb9b56e8
commit 780c1d0759
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
17 changed files with 104 additions and 39 deletions

View File

@ -28,7 +28,7 @@ tasks.register('generateResourceForCommodore', GenerateDependencyDownloadResourc
allprojects {
repositories {
maven { url 'https://papermc.io/repo/repository/maven-public/' }
maven { url 'https://repo.papermc.io/repository/maven-public/' }
maven { url 'https://nexus.scarsz.me/content/groups/public/' }
maven { url 'https://repo.essentialsx.net/releases/' }
}

View File

@ -24,6 +24,6 @@ import org.bukkit.event.Event;
public interface IBukkitChatForwarder {
MinecraftComponent annotateChatMessage(Event event, Player player, MinecraftComponent component);
MinecraftComponent renderChatMessage(Event event, Player player, MinecraftComponent component);
void forwardMessage(Event event, Player player, MinecraftComponent component, boolean cancelled);
}

View File

@ -70,7 +70,7 @@ public class PaperChatListener implements Listener {
}
MinecraftComponent component = GET_MESSAGE_HANDLE.getComponent(event);
MinecraftComponent annotated = listener.annotateChatMessage(event, event.getPlayer(), component);
MinecraftComponent annotated = listener.renderChatMessage(event, event.getPlayer(), component);
if (annotated != null) {
try {
SET_MESSAGE_HANDLE.invoke(event, annotated.asAdventure());

View File

@ -23,27 +23,34 @@ import org.bukkit.entity.Player;
import org.bukkit.profile.PlayerTextures;
import java.net.URL;
import java.util.Collection;
import java.util.Locale;
public final class SpigotPlayer {
private SpigotPlayer() {}
private static final boolean playerProfileExists;
private static final boolean PLAYER_PROFILE_EXISTS;
private static final boolean CHATSUGGESTIONS_METHODS_AVAILABLE;
static {
Class<?> playerClass = Player.class;
boolean playerProfile = false;
boolean playerProfile = false, chatSuggestions = false;
try {
playerClass.getMethod("getPlayerProfile");
playerProfile = true;
} catch (ReflectiveOperationException ignored) {}
playerProfileExists = playerProfile;
try {
playerClass.getMethod("addCustomChatCompletions", Collection.class);
chatSuggestions = true;
} catch (ReflectiveOperationException ignored) {}
PLAYER_PROFILE_EXISTS = playerProfile;
CHATSUGGESTIONS_METHODS_AVAILABLE = chatSuggestions;
}
public static SkinInfo getSkinInfo(Player player) {
if (!playerProfileExists) {
if (!PLAYER_PROFILE_EXISTS) {
return null;
}
@ -60,4 +67,18 @@ public final class SpigotPlayer {
textures.getSkinModel().toString().toLowerCase(Locale.ROOT)
);
}
public static void addChatSuggestions(Player player, Collection<String> suggestions) {
if (!CHATSUGGESTIONS_METHODS_AVAILABLE) {
return;
}
player.addCustomChatCompletions(suggestions);
}
public static void removeChatSuggestions(Player player, Collection<String> suggestions) {
if (!CHATSUGGESTIONS_METHODS_AVAILABLE) {
return;
}
player.removeCustomChatCompletions(suggestions);
}
}

View File

@ -49,7 +49,7 @@ public class BukkitChatForwarder implements IBukkitChatForwarder {
}
@Override
public MinecraftComponent annotateChatMessage(Event event, Player player, MinecraftComponent component) {
public MinecraftComponent renderChatMessage(Event event, Player player, MinecraftComponent component) {
IPlayer srvPlayer = discordSRV.playerProvider().player(player);
GameChatRenderEvent annotateEvent = new GameChatRenderEvent(
event,

View File

@ -39,7 +39,7 @@ public class BukkitChatListener implements Listener {
MinecraftComponent component = ComponentUtil.toAPI(
BukkitComponentSerializer.legacy().deserialize(event.getMessage()));
MinecraftComponent annotated = forwarder.annotateChatMessage(event, event.getPlayer(), component);
MinecraftComponent annotated = forwarder.renderChatMessage(event, event.getPlayer(), component);
if (annotated != null) {
event.setMessage(BukkitComponentSerializer.legacy().serialize(ComponentUtil.fromAPI(annotated)));
}

View File

@ -36,6 +36,7 @@ import org.jetbrains.annotations.Nullable;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
@ -94,6 +95,16 @@ public class BukkitPlayer extends BukkitCommandSender implements IPlayer {
});
}
@Override
public void addChatSuggestions(Collection<String> suggestions) {
SpigotPlayer.addChatSuggestions(player, suggestions);
}
@Override
public void removeChatSuggestions(Collection<String> suggestions) {
SpigotPlayer.removeChatSuggestions(player, suggestions);
}
@Override
public @Nullable SkinInfo skinInfo() {
return SpigotPlayer.getSkinInfo(player);

View File

@ -31,6 +31,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
@ -61,6 +62,16 @@ public class BungeePlayer extends BungeeCommandSender implements IPlayer {
return CompletableFuture.completedFuture(null);
}
@Override
public void addChatSuggestions(Collection<String> suggestions) {
// API missing
}
@Override
public void removeChatSuggestions(Collection<String> suggestions) {
// API missing
}
@Override
public @Nullable SkinInfo skinInfo() {
return null;

View File

@ -21,6 +21,7 @@ package com.discordsrv.common.abstraction.player;
import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.placeholder.annotation.Placeholder;
import com.discordsrv.api.placeholder.annotation.PlaceholderPrefix;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.api.player.DiscordSRVPlayer;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.command.game.sender.ICommandSender;
@ -34,6 +35,7 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -68,6 +70,9 @@ public interface IPlayer extends DiscordSRVPlayer, IOfflinePlayer, ICommandSende
CompletableFuture<Void> kick(Component component);
void addChatSuggestions(Collection<String> suggestions);
void removeChatSuggestions(Collection<String> suggestions);
@NotNull
@Placeholder("display_name")
Component displayName();
@ -96,14 +101,14 @@ public interface IPlayer extends DiscordSRVPlayer, IOfflinePlayer, ICommandSende
@Nullable
@ApiStatus.NonExtendable
@Placeholder("meta_prefix")
default Component getMetaPrefix() {
default FormattedText getMetaPrefix() {
return GamePermissionUtil.getMetaPrefix(discordSRV(), uniqueId());
}
@Nullable
@ApiStatus.NonExtendable
@Placeholder("meta_suffix")
default Component getMetaSuffix() {
default FormattedText getMetaSuffix() {
return GamePermissionUtil.getMetaSuffix(discordSRV(), uniqueId());
}

View File

@ -91,7 +91,7 @@ public class MinecraftToDiscordChatConfig implements IMessageConfig {
+ "The player needs the discordsrv.mention.everyone permission to render the mention and trigger a notification")
public boolean everyone = false;
public boolean anyCaching() {
public boolean any() {
return roles || channels || users;
}
}

View File

@ -100,7 +100,7 @@ public class MentionCachingModule extends AbstractModule<DiscordSRV> {
continue;
}
if (config.mentions.anyCaching()) {
if (config.mentions.any()) {
return true;
}
}

View File

@ -54,7 +54,7 @@ public class MentionGameRenderingModule extends AbstractModule<DiscordSRV> {
}
MinecraftToDiscordChatConfig.Mentions mentions = config.mentions;
if (mentions.renderMentionsInGame && mentions.anyCaching()) {
if (mentions.renderMentionsInGame && mentions.any()) {
return true;
}
@ -63,7 +63,7 @@ public class MentionGameRenderingModule extends AbstractModule<DiscordSRV> {
}
@Subscribe
public void onGameChatAnnotate(GameChatRenderEvent event) {
public void onGameChatRender(GameChatRenderEvent event) {
if (checkCancellation(event) || checkProcessor(event)) {
return;
}

View File

@ -145,20 +145,19 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
allowedMentions.add(AllowedMention.ALL_USERS);
}
if (mentionConfig.roles) {
if (player.hasPermission(Permission.MENTION_ROLE_MENTIONABLE)) {
if (player.hasPermission(Permission.MENTION_ROLE_ALL)) {
allowedMentions.add(AllowedMention.ALL_ROLES);
} else if (player.hasPermission(Permission.MENTION_ROLE_MENTIONABLE)) {
for (Role role : guild.getRoles()) {
if (role.isMentionable()) {
allowedMentions.add(AllowedMention.role(role.getIdLong()));
}
}
}
if (player.hasPermission(Permission.MENTION_ROLE_ALL)) {
allowedMentions.add(AllowedMention.ALL_ROLES);
}
}
boolean everyone = mentionConfig.everyone && player.hasPermission(Permission.MENTION_EVERYONE);
if (everyone) {
boolean everyoneMentionAllowed = mentionConfig.everyone && player.hasPermission(Permission.MENTION_EVERYONE);
if (everyoneMentionAllowed) {
allowedMentions.add(AllowedMention.EVERYONE);
}
@ -178,7 +177,7 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
}
String finalMessage = messagePlaceholders.toString();
return new FormattedText(preventEveryoneMentions(everyone, finalMessage));
return new FormattedText(preventEveryoneMentions(everyoneMentionAllowed, finalMessage));
})
.applyPlaceholderService()
.build();

View File

@ -19,6 +19,7 @@
package com.discordsrv.common.util;
import com.discordsrv.api.module.type.PermissionModule;
import com.discordsrv.api.placeholder.format.FormattedText;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.exception.MessageException;
import net.kyori.adventure.text.Component;
@ -35,13 +36,23 @@ public final class GamePermissionUtil {
private GamePermissionUtil() {}
public static Component getMetaPrefix(DiscordSRV discordSRV, UUID uuid) {
public static FormattedText getMetaPrefix(DiscordSRV discordSRV, UUID uuid) {
return getMeta(discordSRV, uuid, PREFIX_META_KEY);
}
public static Component getMetaSuffix(DiscordSRV discordSRV, UUID uuid) {
public static FormattedText getMetaSuffix(DiscordSRV discordSRV, UUID uuid) {
return getMeta(discordSRV, uuid, SUFFIX_META_KEY);
}
private static FormattedText getMeta(DiscordSRV discordSRV, UUID uuid, String metaKey) {
PermissionModule.Meta meta = discordSRV.getModule(PermissionModule.Meta.class);
if (meta == null) {
return null;
}
String data = meta.getMeta(uuid, metaKey).join();
return new FormattedText(data);
}
public static Component getPrefix(DiscordSRV discordSRV, UUID uuid) {
return getLegacy(discordSRV, "prefix", perm -> perm.getPrefix(uuid));
}
@ -50,16 +61,6 @@ public final class GamePermissionUtil {
return getLegacy(discordSRV, "suffix", perm -> perm.getSuffix(uuid));
}
private static Component getMeta(DiscordSRV discordSRV, UUID uuid, String metaKey) {
PermissionModule.Meta meta = discordSRV.getModule(PermissionModule.Meta.class);
if (meta == null) {
return null;
}
String data = meta.getMeta(uuid, metaKey).join();
return translate(discordSRV, data);
}
private static Component getLegacy(
DiscordSRV discordSRV,
String what,

View File

@ -20,7 +20,7 @@ dependencyResolutionManagement {
// Bukkit
version('bukkit_minimum', '1.8.8-R0.1-SNAPSHOT')
version('bukkit1_12', '1.12.2-R0.1-SNAPSHOT')
version('bukkit_latest', '1.20.1-R0.1-SNAPSHOT')
version('bukkit_latest', '1.21.1-R0.1-SNAPSHOT')
version('folia', '1.20.1-R0.1-SNAPSHOT')
library('paperapi', 'io.papermc.paper', 'paper-api').versionRef('bukkit_latest')
library('spigotapi', 'org.spigotmc', 'spigot-api').versionRef('bukkit_latest')
@ -29,10 +29,10 @@ dependencyResolutionManagement {
library('folia', 'dev.folia', 'folia-api').versionRef('folia')
// Bungee
library('bungee', 'net.md-5', 'bungeecord-api').version('1.17-R0.1-SNAPSHOT')
library('bungee', 'net.md-5', 'bungeecord-api').version('1.21-R0.1-SNAPSHOT')
// Velocity
library('velocity', 'com.velocitypowered', 'velocity-api').version('3.0.0')
library('velocity', 'com.velocitypowered', 'velocity-api').version('3.3.0-SNAPSHOT')
// DependencyDownload
version('dependencydownload', '1.3.1')

View File

@ -2,6 +2,10 @@ apply from: rootProject.file('buildscript/standalone.gradle')
apply plugin: 'net.kyori.blossom'
apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'
java {
disableAutoTargetJvm() // Requires Java 17, we target 8
}
sourceSets {
main {
blossom {
@ -21,7 +25,7 @@ shadowJar {
repositories {
exclusiveContent {
forRepository {
maven { url 'https://nexus.velocitypowered.com/repository/maven-public/' }
maven { url 'https://repo.papermc.io/repository/maven-public/' }
}
filter {
includeGroup 'com.velocitypowered'
@ -39,7 +43,9 @@ dependencies {
// Platform
annotationProcessor(libs.velocity)
compileOnly(libs.velocity)
compileOnly(libs.velocity) {
exclude module: 'caffeine'
}
// DependencyDownload
implementation(libs.mcdependencydownload.velocity)

View File

@ -31,6 +31,7 @@ import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
@ -59,6 +60,16 @@ public class VelocityPlayer extends VelocityCommandSender implements IPlayer {
return CompletableFuture.completedFuture(null);
}
@Override
public void addChatSuggestions(Collection<String> suggestions) {
player.addCustomChatCompletions(suggestions);
}
@Override
public void removeChatSuggestions(Collection<String> suggestions) {
player.removeCustomChatCompletions(suggestions);
}
@Override
public @Nullable SkinInfo skinInfo() {
for (GameProfile.Property property : player.getGameProfile().getProperties()) {