Skip to content

Commit 8b4af9d

Browse files
committed
mend
1 parent 8a55cb8 commit 8b4af9d

File tree

9 files changed

+120
-246
lines changed

9 files changed

+120
-246
lines changed

Slack.jar

-3.53 KB
Binary file not shown.

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

Lines changed: 30 additions & 32 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,55 +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 isAction;
22+
private final boolean useMarkdown;
23+
private final String webhookUrl = getWebhookUrl();
2324

2425
/**
25-
* Prepares the message to send to Slack.
26+
* Posts a message to Slack.
2627
*
27-
* @param message The message to send to Slack.
28-
* @param name The username of the message to send to Slack.
29-
* @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.
30-
* @param isAction The message is an action or not.
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.
3132
*/
32-
public BukkitPoster(String message, String name, String iconUrl, Boolean isAction) {
33-
this.name = name;
33+
public BukkitPoster(String message, String name, String iconUrl, boolean useMarkdown) {
3434
this.message = message;
35+
this.name = name;
36+
this.useMarkdown = useMarkdown;
3537
this.iconUrl = iconUrl;
36-
this.isAction = isAction;
3738
}
38-
39-
/**
40-
* Prepares the message to send to Slack.
39+
40+
/**
41+
* Posts a player sent message to Slack.
4142
*
42-
* @param message The message to send to Slack.
43-
* @param name The username of the message to send to Slack.
44-
* @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.
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.
4546
*/
46-
public BukkitPoster(String message, String name, String iconUrl) {
47-
this(message, name, iconUrl, false);
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;
4852
}
4953

54+
5055
@Override
5156
public void run() {
5257
JSONObject json = new JSONObject();
53-
if (isAction) {
54-
json.put("text", "_" + message + "_");
55-
} else if (message.startsWith("/")) {
56-
json.put("text", "```" + message + "```");
57-
} else {
58-
json.put("text", message);
59-
json.put("mrkdwn", false);
60-
}
58+
json.put("text", message);
6159
json.put("username", name);
62-
if (iconUrl == null) {
63-
json.put("icon_url", "https://cravatar.eu/helmhead/" + name + "/100.png");
64-
} else {
65-
json.put("icon_url", iconUrl);
66-
}
60+
json.put("icon_url", iconUrl);
61+
json.put("mrkdwn", useMarkdown);
6762
String jsonStr = "payload=" + json.toJSONString();
6863
try {
6964
HttpURLConnection webhookConnection = (HttpURLConnection) new URL(webhookUrl).openConnection();
@@ -72,8 +67,11 @@ public void run() {
7267
try (BufferedOutputStream bufOut = new BufferedOutputStream(webhookConnection.getOutputStream())) {
7368
bufOut.write(jsonStr.getBytes(StandardCharsets.UTF_8));
7469
bufOut.flush();
70+
bufOut.close();
7571
}
72+
int serverResponseCode = webhookConnection.getResponseCode();
7673
webhookConnection.disconnect();
74+
webhookConnection = null;
7775
} catch (Exception ignored) {
7876
}
7977
}

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: 20 additions & 33 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,50 +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(), true);
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(), true);
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) {
6669
if (!getConfig().getBoolean("send-commands")) {
67-
return;
70+
return;
6871
}
6972
String command = event.getMessage().split(" ")[0];
70-
if (isAllowed(command) && isVisible("slack.hide.command", event.getPlayer()) && !event.getMessage().contains("/slack send")) {
71-
send(event.getMessage(), event.getPlayer().getName());
73+
if (isAllowed(command) && isVisible("slack.hide.command", event.getPlayer().getUniqueId()) && !event.getMessage().contains("/slack send")) {
74+
send(event.getMessage(), event.getPlayer(), false);
7275
}
7376
}
74-
75-
public void send(String message, String name) {
76-
send(message, name, null, false);
77-
}
78-
79-
public void send(String message, String name, String iconUrl) {
80-
send(message, name, iconUrl, false);
81-
}
82-
83-
public void send(String message, String name, Boolean isAction) {
84-
send(message, name, null, isAction);
85-
}
86-
87-
public void send(String message, String name, String iconUrl, Boolean isAction) {
88-
new SlackBukkitPoster(this, message, name, iconUrl, isAction).runTaskAsynchronously(this);
77+
78+
private void send(String message, Player player, boolean useMarkdown) {
79+
new BukkitPoster(message, player, useMarkdown).runTaskAsynchronously(this);
8980
}
9081

9182
private boolean isAllowed(String command) {
@@ -102,18 +93,10 @@ private void updateConfig(String version) {
10293
getConfig().set("version", version);
10394
saveConfig();
10495
}
105-
106-
private boolean isVisible(String permission, Player player) {
107-
if (getConfig().getBoolean("use-perms")) {
108-
return !player.hasPermission(permission);
109-
} else {
110-
return true;
111-
}
112-
}
11396

11497
private boolean isVisible(String permission, UUID uuid) {
11598
if (getConfig().getBoolean("use-perms")) {
116-
return !Bukkit.getServer().getPlayer(uuid).hasPermission(permission);
99+
return !getServer().getPlayer(uuid).hasPermission(permission);
117100
} else {
118101
return true;
119102
}
@@ -162,7 +145,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
162145
senderName = sender.getName();
163146
}
164147
sb.append(MessageFormat.format(" (sent by {0})", senderName));
165-
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+
}
166153
}
167154
} else {
168155
sender.sendMessage(ChatColor.DARK_RED + "You are not allowed to execute this command!");

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

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

0 commit comments

Comments
 (0)