Skip to content

Commit

Permalink
Add MiniMessage and PlaceHolderAPI Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ManInMyVan committed Nov 29, 2024
1 parent c1858e4 commit 259cef7
Show file tree
Hide file tree
Showing 26 changed files with 214 additions and 130 deletions.
9 changes: 7 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ repositories {
maven("https://repo.opencollab.dev/maven-releases/") // Cumulus (for Floodgate)
maven("https://repo.codemc.io/repository/maven-releases/") // PacketEvents
maven("https://repo.codemc.io/repository/maven-snapshots/")
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") // placeholderapi
mavenCentral()
// FastUtil, Discord-Webhooks
}
Expand All @@ -70,11 +71,14 @@ dependencies {
// Used for local testing: implementation("ac.grim.grimac:GRIMAPI:1.0")
implementation("com.github.grimanticheat:grimapi:fc5634e444")

implementation("net.kyori:adventure-text-minimessage:4.17.0")
implementation("net.kyori:adventure-platform-bukkit:4.3.4")

implementation("org.jetbrains:annotations:24.1.0")
compileOnly("org.geysermc.floodgate:api:2.0-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
compileOnly("com.viaversion:viaversion-api:5.0.4-SNAPSHOT")
//
compileOnly("me.clip:placeholderapi:2.11.6")
compileOnly("io.netty:netty-all:4.1.85.Final")
}

Expand All @@ -95,7 +99,8 @@ bukkit {
"ViaRewind",
"Geyser-Spigot",
"floodgate",
"FastLogin"
"FastLogin",
"PlaceholderAPI",
)

permissions {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/ac/grim/grimac/GrimExternalAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public void setServerName(String name) {
@Getter
private final Map<String, String> staticReplacements = new ConcurrentHashMap<>();

public String replaceVariables(GrimUser user, String content, boolean colors) {
if (colors) content = ChatColor.translateAlternateColorCodes('&', content);
public String replaceVariables(GrimUser user, String content) {
for (Map.Entry<String, String> entry : staticReplacements.entrySet()) {
content = content.replace(entry.getKey(), entry.getValue());
}
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/ac/grim/grimac/checks/impl/misc/ClientBrand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import ac.grim.grimac.checks.impl.exploit.ExploitA;
import ac.grim.grimac.checks.type.PacketCheck;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.anticheat.MessageUtil;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientPluginMessage;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class ClientBrand extends Check implements PacketCheck {
String brand = "vanilla";
boolean hasBrand = false;
@Getter
private String brand = "vanilla";
private boolean hasBrand = false;

public ClientBrand(GrimPlayer player) {
super(player);
Expand All @@ -24,8 +28,7 @@ public ClientBrand(GrimPlayer player) {
public void onPacketReceive(final PacketReceiveEvent event) {
if (event.getPacketType() == PacketType.Play.Client.PLUGIN_MESSAGE) {
WrapperPlayClientPluginMessage packet = new WrapperPlayClientPluginMessage(event);
String channelName = packet.getChannelName();
handle(channelName, packet.getData());
handle(packet.getChannelName(), packet.getData());
}
}

Expand All @@ -43,11 +46,14 @@ public void handle(String channel, byte[] data) {
if (player.checkManager.getPrePredictionCheck(ExploitA.class).checkString(brand)) brand = "sent log4j";
if (!GrimAPI.INSTANCE.getConfigManager().isIgnoredClient(brand)) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("client-brand-format", "%prefix% &f%player% joined using %brand%");
message = GrimAPI.INSTANCE.getExternalAPI().replaceVariables(getPlayer(), message, true);
message = MessageUtil.replacePlaceholders(player, message);

Component component = MessageUtil.miniMessage(message);

// sendMessage is async safe while broadcast isn't due to adventure
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasPermission("grim.brand")) {
player.sendMessage(message);
MessageUtil.sendMessage(player, component);
}
}
}
Expand All @@ -56,8 +62,4 @@ public void handle(String channel, byte[] data) {
hasBrand = true;
}
}

public String getBrand() {
return brand;
}
}
4 changes: 2 additions & 2 deletions src/main/java/ac/grim/grimac/commands/GrimHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class GrimHelp extends BaseCommand {
@CommandPermission("grim.help")
public void onHelp(CommandSender sender) {
for (String string : GrimAPI.INSTANCE.getConfigManager().getConfig().getStringList("help")) {
string = MessageUtil.format(string);
sender.sendMessage(string);
string = MessageUtil.replacePlaceholders(sender, string);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(string));
}
}
}
69 changes: 38 additions & 31 deletions src/main/java/ac/grim/grimac/commands/GrimLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,50 @@ public void onLog(CommandSender sender, int flagId) {

if (builder == null) {
String failure = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log-not-found", "%prefix% &cUnable to find that log");
sender.sendMessage(MessageUtil.format(failure));
} else {
String uploading = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log-start", "%prefix% &fUploading log... please wait");
String success = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log", "%prefix% &fUploaded debug to: %url%");
String failure = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log-upload-failure", "%prefix% &cSomething went wrong while uploading this log, see console for more information.");
failure = MessageUtil.replacePlaceholders(sender, failure);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(failure));
return;
}

sender.sendMessage(MessageUtil.format(uploading));
String success = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log", "%prefix% &fUploaded debug to: %url%");
String failure = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log-upload-failure", "%prefix% &cSomething went wrong while uploading this log, see console for more information.");

FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(), (dummy) -> {
try {
URL mUrl = new URL("https://paste.grim.ac/data/post");
HttpURLConnection urlConn = (HttpURLConnection) mUrl.openConnection();
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.addRequestProperty("User-Agent", "GrimAC/" + GrimAPI.INSTANCE.getExternalAPI().getGrimVersion());
urlConn.addRequestProperty("Content-Type", "text/yaml"); // Not really yaml, but looks nicer than plaintext
urlConn.setRequestProperty("Content-Length", Integer.toString(builder.length()));
urlConn.getOutputStream().write(builder.toString().getBytes(StandardCharsets.UTF_8));
String uploading = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("upload-log-start", "%prefix% &fUploading log... please wait");
uploading = MessageUtil.replacePlaceholders(sender, uploading);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(uploading));

urlConn.getOutputStream().close();
FoliaScheduler.getAsyncScheduler().runNow(GrimAPI.INSTANCE.getPlugin(), (dummy) -> {
try {
URL mUrl = new URL("https://paste.grim.ac/data/post");
HttpURLConnection urlConn = (HttpURLConnection) mUrl.openConnection();
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.addRequestProperty("User-Agent", "GrimAC/" + GrimAPI.INSTANCE.getExternalAPI().getGrimVersion());
urlConn.addRequestProperty("Content-Type", "text/yaml"); // Not really yaml, but looks nicer than plaintext
urlConn.setRequestProperty("Content-Length", Integer.toString(builder.length()));
urlConn.getOutputStream().write(builder.toString().getBytes(StandardCharsets.UTF_8));

int response = urlConn.getResponseCode();
urlConn.getOutputStream().close();

if (response == HttpURLConnection.HTTP_CREATED) {
String responseURL = urlConn.getHeaderField("Location");
sender.sendMessage(MessageUtil.format(success.replace("%url%", "https://paste.grim.ac/" + responseURL)));
} else {
sender.sendMessage(MessageUtil.format(failure));
LogUtil.error("Returned response code " + response + ": " + urlConn.getResponseMessage());
}
int response = urlConn.getResponseCode();

urlConn.disconnect();
} catch (Exception e) {
sender.sendMessage(MessageUtil.format(failure));
e.printStackTrace();
if (response == HttpURLConnection.HTTP_CREATED) {
String responseURL = urlConn.getHeaderField("Location");
String message = success.replace("%url%", "https://paste.grim.ac/" + responseURL);
message = MessageUtil.replacePlaceholders(sender, message);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
} else {
String message = MessageUtil.replacePlaceholders(sender, failure);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
LogUtil.error("Returned response code " + response + ": " + urlConn.getResponseMessage());
}
});
}

urlConn.disconnect();
} catch (Exception e) {
String message = MessageUtil.replacePlaceholders(sender, failure);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
e.printStackTrace();
}
});
}
}
14 changes: 6 additions & 8 deletions src/main/java/ac/grim/grimac/commands/GrimProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,32 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

@CommandAlias("grim|grimac")
public class GrimProfile extends BaseCommand {
@Subcommand("profile")
@CommandPermission("grim.profile")
@CommandCompletion("@players")
public void onConsoleDebug(CommandSender sender, OnlinePlayer target) {
Player player = null;
if (sender instanceof Player) player = (Player) sender;

// Short circuit due to minimum java requirements for MultiLib
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_18) && MultiLibUtil.isExternalPlayer(target.getPlayer())) {
String alertString = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("player-not-this-server", "%prefix% &cThis player isn't on this server!");
sender.sendMessage(MessageUtil.format(alertString));
alertString = MessageUtil.replacePlaceholders(sender, alertString);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(alertString));
return;
}

GrimPlayer grimPlayer = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(target.getPlayer());
if (grimPlayer == null) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("player-not-found", "%prefix% &cPlayer is exempt or offline!");
sender.sendMessage(MessageUtil.format(message));
message = MessageUtil.replacePlaceholders(sender, message);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
return;
}

for (String message : GrimAPI.INSTANCE.getConfigManager().getConfig().getStringList("profile")) {
message = GrimAPI.INSTANCE.getExternalAPI().replaceVariables(grimPlayer, message, true);
sender.sendMessage(message);
message = MessageUtil.replacePlaceholders(grimPlayer, message);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
}
}
}
15 changes: 7 additions & 8 deletions src/main/java/ac/grim/grimac/commands/GrimReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ public class GrimReload extends BaseCommand {
@Subcommand("reload")
@CommandPermission("grim.reload")
public void onReload(CommandSender sender) {
//reload config
sender.sendMessage(MessageUtil.format("%prefix% &7Reloading config..."));
// reload config
String reloading = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("reloading", "%prefix% &7Reloading config...");
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(MessageUtil.replacePlaceholders(sender, reloading)));
GrimAPI.INSTANCE.getExternalAPI().reloadAsync().exceptionally(throwable -> false)
.thenAccept(bool -> {
if (bool) {
sender.sendMessage(MessageUtil.format("%prefix% &fConfig has been reloaded."));
} else {
sender.sendMessage(MessageUtil.format("%prefix% &cFailed to reload config."));
}
String message = bool
? GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("reloaded", "%prefix% &fConfig has been reloaded.")
: GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("reload-failed", "%prefix% &cFailed to reload config.");
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(MessageUtil.replacePlaceholders(sender, message)));
});
}

}
8 changes: 5 additions & 3 deletions src/main/java/ac/grim/grimac/commands/GrimSendAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Subcommand;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;

@CommandAlias("grim|grimac")
public class GrimSendAlert extends BaseCommand {
@Subcommand("sendalert")
@CommandPermission("grim.sendalert")
public void sendAlert(String string) {
string = MessageUtil.format(string);
string = MessageUtil.replacePlaceholders((Object) null, string);
Component message = MessageUtil.miniMessage(string);

for (Player bukkitPlayer : GrimAPI.INSTANCE.getAlertManager().getEnabledAlerts()) {
bukkitPlayer.sendMessage(string);
MessageUtil.sendMessage(bukkitPlayer, message);
}

if (GrimAPI.INSTANCE.getConfigManager().isPrintAlertsToConsole()) {
LogUtil.console(string); // Print alert to console
LogUtil.console(message); // Print alert to console
}
}
}
23 changes: 8 additions & 15 deletions src/main/java/ac/grim/grimac/commands/GrimSpectate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand All @@ -27,32 +23,29 @@ public void onSpectate(CommandSender sender, @Optional OnlinePlayer target) {

if (target != null && target.getPlayer().getUniqueId().equals(player.getUniqueId())) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("cannot-run-on-self", "%prefix% &cYou cannot use this command on yourself!");
sender.sendMessage(MessageUtil.format(message));
message = MessageUtil.replacePlaceholders(target, message);
MessageUtil.sendMessage(player, MessageUtil.miniMessage(message));
return;
}

if (target == null || (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_18) && MultiLibUtil.isExternalPlayer(target.getPlayer()))) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("player-not-this-server", "%prefix% &cThis player isn't on this server!");
sender.sendMessage(MessageUtil.format(message));
message = MessageUtil.replacePlaceholders(target, message);
MessageUtil.sendMessage(player, MessageUtil.miniMessage(message));
return;
}

//hide player from tab list
if (GrimAPI.INSTANCE.getSpectateManager().enable(player)) {
GrimPlayer grimPlayer = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(player);
if (grimPlayer != null) {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("spectate-return", "\n%prefix% &fClick here to return to previous location\n");
grimPlayer.user.sendMessage(
LegacyComponentSerializer.legacy('&')
.deserialize(MessageUtil.formatWithNoColor(message))
.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, "/grim stopspectating"))
.hoverEvent(HoverEvent.showText(Component.text("/grim stopspectating")))
);
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("spectate-return", "<click:run_command:/grim stopspectating><hover:show_text:\"/grim stopspectating\">\n%prefix% &fClick here to return to previous location\n</hover></click>");
message = MessageUtil.replacePlaceholders(target, message);
grimPlayer.user.sendMessage(MessageUtil.miniMessage(message));
}
}

player.setGameMode(GameMode.SPECTATOR);
player.teleport(target.getPlayer());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void onStopSpectate(CommandSender sender, String[] args) {
GrimAPI.INSTANCE.getSpectateManager().disable(player, teleportBack);
} else {
String message = GrimAPI.INSTANCE.getConfigManager().getConfig().getStringElse("cannot-spectate-return", "%prefix% &cYou can only do this after spectating a player.");
sender.sendMessage(MessageUtil.format(message));
message = MessageUtil.replacePlaceholders(sender, message);
MessageUtil.sendMessage(sender, MessageUtil.miniMessage(message));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.utils.anticheat.LogUtil;
import ac.grim.grimac.utils.anticheat.MessageUtil;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketListenerAbstract;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void onPacketReceive(final PacketReceiveEvent event) {
}

for (Player bukkitPlayer : GrimAPI.INSTANCE.getAlertManager().getEnabledAlerts())
bukkitPlayer.sendMessage(alert);
MessageUtil.sendMessage(bukkitPlayer, MessageUtil.miniMessage(alert));
}

public static void sendPluginMessage(String message) {
Expand Down
Loading

0 comments on commit 259cef7

Please sign in to comment.