Skip to content

Instantly share code, notes, and snippets.

@LaxWasHere
Last active August 29, 2015 13:56
Show Gist options
  • Save LaxWasHere/9121098 to your computer and use it in GitHub Desktop.
Save LaxWasHere/9121098 to your computer and use it in GitHub Desktop.
//POM can be found here http://pastebin.com/raw.php?i=YK3dd3ya
//Credits to https://github.com/Double0negative
package net.awesomepowered.crashalerter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class CrashAlerter extends JavaPlugin {
protected boolean updated = false;
protected boolean running = true;
protected boolean shuttingDown = false;
protected int FreezeTime = 0;
protected int sinceShutdown = 0;
public int WaitTime;
public String prefix = "[Crash] ";
public String alertMessage;
public String sendTo;
//todo StringList for sendTo
public void onEnable() {
configStuff();
startPlugin();
printDebug();
}
public void configStuff() {
getConfig().options().copyDefaults(true);
saveConfig();
WaitTime = getConfig().getInt("WaitTime");
sendTo = getConfig().getString("Mail.sendTo");
alertMessage = getConfig().getString("Mail.AlertMessage");
}
public void printDebug() {
System.out.println(prefix + "Wait time until sending " + WaitTime + " seconds");
System.out.println(prefix + "Sending email to " + sendTo);
System.out.println(prefix + "Email message is " + alertMessage);
}
public void startPlugin() {
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
updated = true;
}
}, 1, 1);
new Checker().start();
}
public void onDisable() {
running = false;
}
//Checker code by Double0Negative
class Checker extends Thread {
public void run() {
while(running || shuttingDown) {
if(!updated && !shuttingDown) {
FreezeTime++;
System.out.println(prefix + "Server unresponsive for " + FreezeTime + " second(s)...");
} else {
updated = false;
FreezeTime = 0;
} if(FreezeTime == WaitTime) {
System.out.println(prefix + "Server was unresponsive for " + WaitTime + " second(s)!");
System.out.println(prefix + "Sending alert message.");
sendAlert(sendTo, alertMessage);
Bukkit.shutdown();
shuttingDown = true;
} if(shuttingDown) {
if(sinceShutdown == 30) {
System.out.println(prefix + "Killing Server!");
Runtime.getRuntime().halt(1);
}
System.out.println(prefix + "killing server in "+(30-sinceShutdown)+" second(s)...");
sinceShutdown++;
} try{ sleep(1000);} catch(Exception e) {}
}
}
}
public void sendAlert(String To, String AM) {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("Alerter@mysongknowwhatyoudidinthedark.org"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(To));
msg.setSubject("Crash Alert");
msg.setText(AM);
Transport.send(msg);
System.out.println("Message sent!");
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.awesomepowered</groupId>
<artifactId>CrashAlerter</artifactId>
<version>0.1</version>
<name>CrashAlerter</name>
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>${project.name}</finalName>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.name}</finalName>
<artifactSet>
<includes>
<include>javax.mail:mail</include>
</includes>
</artifactSet>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>bukkit-repo</id>
<url>http://repo.bukkit.org/content/groups/public</url>
</repository>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.7.2-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment