Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Bukkit: Delay plugin load to allow PlaceholderAPI expansions to load,…
Browse files Browse the repository at this point in the history
… dont replace placeholders in player messages!
  • Loading branch information
lukeeey committed Mar 23, 2021
1 parent 2d3b82a commit 151ddd7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public void onEnable() {

getServer().getPluginManager().registerEvents(new EventListener(this), this);

if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
getLogger().info("PlaceholderAPI detected, delaying to allow expansions to register before continuing");
getServer().getScheduler().runTaskLater(this, this::continueInit, 20);
} else {
continueInit();
}
}

private void continueInit() {
try {
initJDA();
} catch (LoginException | InterruptedException e) {
Expand All @@ -67,8 +76,11 @@ public void onDisable() {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("discord")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
getConfig().getString("ingame-discord-command-response")));
Player player = sender instanceof Player ? (Player) sender : null;

sender.sendMessage(placeholderApiSupport(player,
ChatColor.translateAlternateColorCodes('&',
getConfig().getString("ingame-discord-command-response"))));
}
return true;
}
Expand Down Expand Up @@ -129,7 +141,7 @@ void sendInternalDiscordEventMessage(String configKey, Map<String, String> place
message = message.replace(entry.getKey(), entry.getValue());
}

message = PlaceholderAPI.setPlaceholders(player, message);
message = placeholderApiSupport(player, message);

if (showEmbed) {
sendDiscordMessage(new EmbedBuilder()
Expand All @@ -142,6 +154,14 @@ void sendInternalDiscordEventMessage(String configKey, Map<String, String> place
}
}

public String placeholderApiSupport(Player player, String message) {
String newMessage = message;
if (PlaceholderAPI.containsPlaceholders(message)) {
newMessage = PlaceholderAPI.setPlaceholders(player, message);
}
return newMessage;
}

/**
* Send a message to the Discord relay channel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public void onChat(AsyncPlayerChatEvent event) {
message = message.replace("@", "[at]");
}

plugin.sendDiscordMessage(PlaceholderAPI.setPlaceholders(event.getPlayer(),
ChatColor.stripColor(plugin.getConfig().getString("relay.server-to-discord.format")
.replace("{timestamp}", new Date(System.currentTimeMillis()).toString())
.replace("{playerName}", event.getPlayer().getName())
.replace("{displayName}", event.getPlayer().getDisplayName())
.replace("{message}", message))));
message = ChatColor.stripColor(message);
String response = plugin.placeholderApiSupport(event.getPlayer(), plugin.getConfig().getString("relay.server-to-discord.format"));

plugin.sendDiscordMessage(response.replace("{timestamp}", new Date(System.currentTimeMillis()).toString())
.replace("{playerName}", event.getPlayer().getName())
.replace("{displayName}", event.getPlayer().getDisplayName())
.replace("{message}", message));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String roleName = highestRole != null ? highestRole.getName() : "";
String formattedRole = highestRole != null ? getFormattedRoleName(highestRole) : "";

String response = plugin.getConfig().getString("relay.discord-to-server.format")
.replace("{timestamp}", new Date(System.currentTimeMillis()).toString())
String response = plugin.placeholderApiSupport(null, plugin.getConfig().getString("relay.discord-to-server.format"));

plugin.broadcastMessage(response.replace("{timestamp}", new Date(System.currentTimeMillis()).toString())
.replace("{discordRole}", roleName)
.replace("{discordRoleColored}", formattedRole)
.replace("{discordName}", name)
.replace("{message}", message);

plugin.broadcastMessage(PlaceholderAPI.setPlaceholders(null, response));
.replace("{message}", message));
}

private boolean handleCommands(Member member, TextChannel channel, String message) {
Expand Down

0 comments on commit 151ddd7

Please sign in to comment.