first commit

This commit is contained in:
smsteel 2019-02-06 15:08:56 +03:00
commit 23e8120c82
9 changed files with 262 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.settings
target
.classpath
.project

134
pom.xml Normal file
View File

@ -0,0 +1,134 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>org.mc</groupId>
<artifactId>webhooks</artifactId>
<version>0.1</version>
<name>mcwebhooks</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>mcwebhooks.MCWebHooks</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,6 @@
package mcwebhooks;
public final class Config
{
public String hookUrl;
}

View File

@ -0,0 +1,12 @@
package mcwebhooks;
final class DeathHookEvent extends PlayerHookEvent
{
public String deathMessage;
public DeathHookEvent(String displayName, String deathMessage) {
super(displayName);
this.type = "death";
this.deathMessage = deathMessage;
}
}

View File

@ -0,0 +1,6 @@
package mcwebhooks;
abstract class HookEvent
{
public String type;
}

View File

@ -0,0 +1,9 @@
package mcwebhooks;
final class JoinHookEvent extends PlayerHookEvent
{
public JoinHookEvent(String displayName) {
super(displayName);
this.type = "login";
}
}

View File

@ -0,0 +1,78 @@
package mcwebhooks;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.File;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class MCWebHooks extends JavaPlugin implements Listener {
private Config config;
private HttpClient httpClient;
private ObjectMapper mapper;
@Override
public void onEnable() {
this.mapper = new ObjectMapper();
try {
getLogger().info("Loading config");
this.config = this.mapper.readValue(new File("plugins/webhooks.json"), Config.class);
getLogger().info(String.format("Hooks will be sent to: %s", this.config.hookUrl));
} catch (IOException e) {
getLogger().info("Config loading failed");
e.printStackTrace();
}
this.httpClient = HttpClientBuilder.create().build();
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
getLogger().info("Web hooks are disabled");
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
JoinHookEvent hookEvent = new JoinHookEvent(event.getPlayer().getDisplayName());
this.sendHookedEvent(hookEvent);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
DeathHookEvent hookEvent = new DeathHookEvent(event.getEntity().getDisplayName(), event.getDeathMessage());
this.sendHookedEvent(hookEvent);
}
private final void sendHookedEvent(HookEvent event) {
getLogger().info(String.format("Event: %s", event.type));
HttpPost request = new HttpPost(this.config.hookUrl);
request.addHeader("content-type", "application/json");
try {
StringEntity params = new StringEntity(this.mapper.writeValueAsString(event));
request.setEntity(params);
} catch (JsonProcessingException | UnsupportedEncodingException e1) {
getLogger().info("Parsing failed");
e1.printStackTrace();
return;
}
try {
this.httpClient.execute(request);
getLogger().info("Request sent");
} catch (IOException e) {
getLogger().info("Request failed");
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,10 @@
package mcwebhooks;
abstract class PlayerHookEvent extends HookEvent
{
public String displayName;
public PlayerHookEvent(String displayName) {
this.displayName = displayName;
}
}

View File

@ -0,0 +1,3 @@
name: mcwebhooks
main: mcwebhooks.MCWebHooks
version: 0.1