-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHerald.java
More file actions
153 lines (125 loc) · 5.47 KB
/
Herald.java
File metadata and controls
153 lines (125 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.dansplugins.herald;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public final class Herald extends JavaPlugin implements Listener {
private List<String> emailRecipients = new ArrayList<>();
private String smtpServer;
private int smtpPort;
private String smtpUsername;
private String smtpPassword;
private String emailSender;
private boolean useTLS;
private boolean discordEnabled;
private String discordWebhookUrl;
private DiscordNotifier discordNotifier;
@Override
public void onEnable() {
// Save default config if it doesn't exist
saveDefaultConfig();
// Load configuration
loadConfiguration();
// Register event listener
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("Herald has been enabled!");
}
@Override
public void onDisable() {
getLogger().info("Herald has been disabled!");
}
private void loadConfiguration() {
// Load email recipients
emailRecipients = getConfig().getStringList("email-recipients");
// Load SMTP settings
smtpServer = getConfig().getString("smtp.server");
smtpPort = getConfig().getInt("smtp.port");
smtpUsername = getConfig().getString("smtp.username");
smtpPassword = getConfig().getString("smtp.password");
emailSender = getConfig().getString("email.sender");
useTLS = getConfig().getBoolean("smtp.use-tls", true);
// Load Discord settings
discordEnabled = getConfig().getBoolean("discord.enabled", false);
discordWebhookUrl = getConfig().getString("discord.webhook-url");
// Initialize Discord notifier if enabled
if (discordEnabled && discordWebhookUrl != null && !discordWebhookUrl.isEmpty()) {
discordNotifier = new DiscordNotifier(discordWebhookUrl);
getLogger().info("Discord notifications enabled");
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
String playerName = event.getPlayer().getName();
String serverName = getServer().getName().isEmpty() ? "Minecraft" : getServer().getName();
// Send email notification
String subject = playerName + " joined " + serverName + " server";
String body = playerName + " has joined the server at " + new java.util.Date();
// Send email asynchronously to not block the main server thread
getServer().getScheduler().runTaskAsynchronously(this, () -> {
try {
sendEmail(subject, body);
getLogger().info("Email notification sent successfully for player: " + playerName);
} catch (Exception e) {
getLogger().severe("Failed to send email notification: " + e.getMessage());
e.printStackTrace();
}
});
// Send Discord notification if enabled
if (discordEnabled && discordNotifier != null) {
String discordMessage = "**" + playerName + "** joined the **" + serverName + "** server";
getServer().getScheduler().runTaskAsynchronously(this, () -> {
try {
discordNotifier.sendMessage(discordMessage);
getLogger().info("Discord notification sent successfully for player: " + playerName);
} catch (Exception e) {
getLogger().severe("Failed to send Discord notification: " + e.getMessage());
e.printStackTrace();
}
});
}
}
private void sendEmail(String subject, String body) throws MessagingException {
if (emailRecipients.isEmpty()) {
getLogger().warning("No email recipients configured. Skipping email notification.");
return;
}
if (smtpServer == null || smtpServer.isEmpty()) {
getLogger().warning("SMTP server not configured. Skipping email notification.");
return;
}
// Set up mail server properties
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", String.valueOf(smtpPort));
props.put("mail.smtp.auth", "true");
if (useTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
// Create a mail session with authenticator
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUsername, smtpPassword);
}
};
Session session = Session.getInstance(props, authenticator);
// Create a message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailSender));
// Add all recipients
for (String recipient : emailRecipients) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
// Set subject and body
message.setSubject(subject);
message.setText(body);
// Send the message
Transport.send(message);
}
}