Skip to content

Commit 8f6a596

Browse files
committed
Markdown, implement API, use-commands option
2 parents d47afdb + 8b4af9d commit 8f6a596

File tree

11 files changed

+133
-211
lines changed

11 files changed

+133
-211
lines changed

Slack.jar

-3.53 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>us.circuitsoft.slack</groupId>
55
<artifactId>Slack</artifactId>
6-
<version>1.4.4</version>
6+
<version>1.5.0</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/org/circuitsoft/slack/api/BukkitPoster.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.HttpURLConnection;
55
import java.net.URL;
66
import java.nio.charset.StandardCharsets;
7+
import org.bukkit.entity.Player;
78

89
import org.bukkit.scheduler.BukkitRunnable;
910
import org.json.simple.JSONObject;
@@ -15,34 +16,49 @@
1516
*/
1617
public class BukkitPoster extends BukkitRunnable {
1718

18-
private final String name;
1919
private final String message;
20-
private final String webhookUrl = getWebhookUrl();
20+
private final String name;
2121
private final String iconUrl;
22+
private final boolean useMarkdown;
23+
private final String webhookUrl = getWebhookUrl();
2224

2325
/**
24-
* Prepares the message to send to Slack.
26+
* Posts a message to Slack.
2527
*
26-
* @param message The message to send to Slack.
27-
* @param name The username of the message to send to Slack.
28-
* @param iconUrl The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name.
28+
* @param message The message sent to Slack.
29+
* @param name The name attributed to the message sent to Slack.
30+
* @param iconUrl The image URL of the user that sends the message to Slack.
31+
* @param useMarkdown Use markdown formatting in the message.
2932
*/
30-
public BukkitPoster(String message, String name, String iconUrl) {
31-
this.name = name;
33+
public BukkitPoster(String message, String name, String iconUrl, boolean useMarkdown) {
3234
this.message = message;
35+
this.name = name;
36+
this.useMarkdown = useMarkdown;
3337
this.iconUrl = iconUrl;
3438
}
39+
40+
/**
41+
* Posts a player sent message to Slack.
42+
*
43+
* @param message The message sent to Slack.
44+
* @param player The player that sent the message.
45+
* @param useMarkdown Use markdown formatting in the message.
46+
*/
47+
public BukkitPoster(String message, Player player, boolean useMarkdown) {
48+
this.message = message;
49+
name = player.getName();
50+
iconUrl = "https://cravatar.eu/helmhead/" + player.getUniqueId().toString() + "/128.png";
51+
this.useMarkdown = useMarkdown;
52+
}
53+
3554

3655
@Override
3756
public void run() {
3857
JSONObject json = new JSONObject();
39-
json.put("text", name + ": " + message);
58+
json.put("text", message);
4059
json.put("username", name);
41-
if (iconUrl == null) {
42-
json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png");
43-
} else {
44-
json.put("icon_url", iconUrl);
45-
}
60+
json.put("icon_url", iconUrl);
61+
json.put("mrkdwn", useMarkdown);
4662
String jsonStr = "payload=" + json.toJSONString();
4763
try {
4864
HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection();
@@ -51,8 +67,11 @@ public void run() {
5167
try (BufferedOutputStream bufOut = new BufferedOutputStream(webhookConnection.getOutputStream())) {
5268
bufOut.write(jsonStr.getBytes(StandardCharsets.UTF_8));
5369
bufOut.flush();
70+
bufOut.close();
5471
}
72+
int serverResponseCode = webhookConnection.getResponseCode();
5573
webhookConnection.disconnect();
74+
webhookConnection = null;
5675
} catch (Exception ignored) {
5776
}
5877
}

src/main/java/org/circuitsoft/slack/api/BungeePoster.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,75 @@
55
import java.net.URL;
66

77
import com.google.gson.JsonObject;
8+
import net.md_5.bungee.api.connection.ProxiedPlayer;
89

910
import static org.circuitsoft.slack.bungee.SlackBungee.getWebhookUrl;
1011

1112
/**
1213
* Posts a message to Slack when using Bungee.
1314
*/
1415
public class BungeePoster implements Runnable {
15-
16-
private final String name;
16+
1717
private final String message;
18+
private final String name;
19+
private final String iconUrl;
20+
private final boolean useMarkdown;
1821
private final String webhookUrl = getWebhookUrl();
19-
private final String icon;
2022

2123
/**
22-
* Prepares the message to send to Slack.
24+
* Posts a message to Slack involving the proxy or network.
2325
*
24-
* @param message The message to send to Slack.
25-
* @param name The username of the message to send to Slack.
26-
* @param icon The image URL of the user that sends the message to Slack. Make this null if the username is a Minecraft player name.
26+
* @param message The message sent to Slack.
27+
* @param name The name attributed to the message sent to Slack.
28+
* @param iconUrl The image URL of the user that sends the message to Slack.
29+
* @param useMarkdown Use markdown formatting in the message.
2730
*/
28-
public BungeePoster(String message, String name, String icon) {
31+
public BungeePoster(String message, String name, String iconUrl, boolean useMarkdown) {
32+
this.message = message;
2933
this.name = name;
34+
this.useMarkdown = useMarkdown;
35+
this.iconUrl = iconUrl;
36+
}
37+
38+
/**
39+
* Posts a message to Slack involving a server on the proxy.
40+
*
41+
* @param message The message sent to Slack.
42+
* @param name The name attributed to the message sent to Slack.
43+
* @param iconUrl The image URL of the user that sends the message to Slack.
44+
* @param serverName The server the event took place on.
45+
* @param useMarkdown Use markdown formatting in the message.
46+
*/
47+
public BungeePoster(String message, String name, String iconUrl, String serverName, boolean useMarkdown) {
48+
this.message = message;
49+
this.name = name + " (" + serverName + ")";
50+
this.useMarkdown = useMarkdown;
51+
this.iconUrl = iconUrl;
52+
}
53+
54+
/**
55+
* Posts a player sent message to Slack.
56+
*
57+
* @param message The message sent to Slack.
58+
* @param player The player that sent the message.
59+
* @param serverName The server the player is on.
60+
* @param useMarkdown Use markdown formatting in the message.
61+
*/
62+
public BungeePoster(String message, ProxiedPlayer player, String serverName, boolean useMarkdown) {
3063
this.message = message;
31-
this.icon = icon;
64+
65+
name = player.getName() + " (" + serverName + ")";
66+
iconUrl = "https://cravatar.eu/helmhead/" + player.getUniqueId().toString() + "/128.png";
67+
this.useMarkdown = useMarkdown;
3268
}
3369

3470
@Override
3571
public void run() {
3672
JsonObject json = new JsonObject();
37-
json.addProperty("text", name + ": " + message);
73+
json.addProperty("text", message);
3874
json.addProperty("username", name);
39-
if (icon == null) {
40-
json.addProperty("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png");
41-
} else {
42-
json.addProperty("icon_url", icon);
43-
}
75+
json.addProperty("icon_url", iconUrl);;
76+
json.addProperty("mrkdwn", useMarkdown);
4477
try {
4578
HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection();
4679
webhookConnection.setRequestMethod("POST");
@@ -49,8 +82,11 @@ public void run() {
4982
String jsonStr = "payload=" + json.toString();
5083
bufOut.write(jsonStr.getBytes("utf8"));
5184
bufOut.flush();
85+
bufOut.close();
5286
}
87+
int serverResponseCode = webhookConnection.getResponseCode();
5388
webhookConnection.disconnect();
89+
webhookConnection = null;
5490
} catch (Exception ignored) {
5591
}
5692
}

src/main/java/org/circuitsoft/slack/bukkit/SlackBukkit.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.text.MessageFormat;
44
import java.util.List;
55
import java.util.UUID;
6+
import java.util.logging.Level;
67
import org.bukkit.Bukkit;
78
import org.bukkit.ChatColor;
89
import org.bukkit.command.Command;
@@ -17,6 +18,8 @@
1718
import org.bukkit.event.player.PlayerJoinEvent;
1819
import org.bukkit.event.player.PlayerQuitEvent;
1920
import org.bukkit.plugin.java.JavaPlugin;
21+
import org.circuitsoft.slack.api.BukkitPoster;
22+
import org.json.simple.JSONObject;
2023

2124
public class SlackBukkit extends JavaPlugin implements Listener {
2225

@@ -42,38 +45,38 @@ public void onDisable() {
4245

4346
@EventHandler(priority = EventPriority.MONITOR)
4447
public void onChat(AsyncPlayerChatEvent event) {
45-
if (isVisible("slack.hide.chat", event.getPlayer())) {
46-
send('"' + event.getMessage() + '"', event.getPlayer().getName());
48+
if (isVisible("slack.hide.chat", event.getPlayer().getUniqueId())) {
49+
send(event.getMessage(), event.getPlayer(), false);
4750
}
4851
}
4952

5053
@EventHandler(priority = EventPriority.MONITOR)
5154
public void onLogin(PlayerJoinEvent event) {
5255
if (isVisible("slack.hide.login", event.getPlayer().getUniqueId())) {
53-
send("logged in", event.getPlayer().getName());
56+
send("_joined_", event.getPlayer(), true);
5457
}
5558
}
5659

5760
@EventHandler(priority = EventPriority.MONITOR)
5861
public void onQuit(PlayerQuitEvent event) {
59-
if (isVisible("slack.hide.logout", event.getPlayer())) {
60-
send("logged out", event.getPlayer().getName());
62+
if (isVisible("slack.hide.logout", event.getPlayer().getUniqueId())) {
63+
send("_quit_", event.getPlayer(), true);
6164
}
6265
}
6366

6467
@EventHandler(priority = EventPriority.MONITOR)
6568
public void onCommand(PlayerCommandPreprocessEvent event) {
66-
if (isAllowed(event.getMessage()) && isVisible("slack.hide.command", event.getPlayer()) && !event.getMessage().contains("/slack send")) {
67-
send(event.getMessage(), event.getPlayer().getName());
69+
if (!getConfig().getBoolean("send-commands")) {
70+
return;
71+
}
72+
String command = event.getMessage().split(" ")[0];
73+
if (isAllowed(command) && isVisible("slack.hide.command", event.getPlayer().getUniqueId()) && !event.getMessage().contains("/slack send")) {
74+
send(event.getMessage(), event.getPlayer(), false);
6875
}
6976
}
70-
71-
public void send(String message, String name) {
72-
new SlackBukkitPoster(this, message, name, null).runTaskAsynchronously(this);
73-
}
74-
75-
public void send(String message, String name, String iconUrl) {
76-
new SlackBukkitPoster(this, message, name, iconUrl).runTaskAsynchronously(this);
77+
78+
private void send(String message, Player player, boolean useMarkdown) {
79+
new BukkitPoster(message, player, useMarkdown).runTaskAsynchronously(this);
7780
}
7881

7982
private boolean isAllowed(String command) {
@@ -90,18 +93,10 @@ private void updateConfig(String version) {
9093
getConfig().set("version", version);
9194
saveConfig();
9295
}
93-
94-
private boolean isVisible(String permission, Player player) {
95-
if (getConfig().getBoolean("use-perms")) {
96-
return !player.hasPermission(permission);
97-
} else {
98-
return true;
99-
}
100-
}
10196

10297
private boolean isVisible(String permission, UUID uuid) {
10398
if (getConfig().getBoolean("use-perms")) {
104-
return !Bukkit.getServer().getPlayer(uuid).hasPermission(permission);
99+
return !getServer().getPlayer(uuid).hasPermission(permission);
105100
} else {
106101
return true;
107102
}
@@ -150,13 +145,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
150145
senderName = sender.getName();
151146
}
152147
sb.append(MessageFormat.format(" (sent by {0})", senderName));
153-
send(sb.toString(), args[1], args[2].equalsIgnoreCase("null") ? null : args[2]);
148+
if (args[2].equalsIgnoreCase("null")) {
149+
new BukkitPoster(sb.toString(), args[1], "https://cravatar.eu/helmhead/" + getServer().getPlayer(args[1]).getUniqueId() + "/128.png", false).runTaskAsynchronously(this);
150+
} else {
151+
new BukkitPoster(sb.toString(), args[1], args[2], false).runTaskAsynchronously(this);
152+
}
154153
}
155154
} else {
156155
sender.sendMessage(ChatColor.DARK_RED + "You are not allowed to execute this command!");
157156
}
158157
} else {
159-
sender.sendMessage(ChatColor.GOLD + "/slack send <username> <image URL or null for username's skin> <message> - send a custom message to slack \n/slack reload - reload Slack's config");
158+
sender.sendMessage(ChatColor.GOLD + "/slack send <username> <image URL or null for username's skin> <message> - send a custom message to slack\n/slack reload - reload Slack's config");
160159
}
161160
return true;
162161
}

src/main/java/org/circuitsoft/slack/bukkit/SlackBukkitPoster.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)