commit 23e8120c82d1dfcd6f76911f5a67f4ec251595b1 Author: smsteel Date: Wed Feb 6 15:08:56 2019 +0300 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b802923 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.settings +target +.classpath +.project \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a4ea47e --- /dev/null +++ b/pom.xml @@ -0,0 +1,134 @@ + + + + 4.0.0 + + org.mc + webhooks + 0.1 + + mcwebhooks + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + + + + junit + junit + 4.11 + test + + + org.bukkit + bukkit + 1.13.2-R0.1-SNAPSHOT + provided + + + com.fasterxml.jackson.core + jackson-core + 2.9.6 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.9.6 + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.6 + + + + org.apache.httpcomponents + httpclient + 4.3.6 + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + maven-assembly-plugin + 3.0.0 + + + jar-with-dependencies + + + + mcwebhooks.MCWebHooks + + + + + + make-assembly + package + + single + + + + + + + + diff --git a/src/main/java/mcwebhooks/Config.java b/src/main/java/mcwebhooks/Config.java new file mode 100644 index 0000000..cc358e4 --- /dev/null +++ b/src/main/java/mcwebhooks/Config.java @@ -0,0 +1,6 @@ +package mcwebhooks; + +public final class Config +{ + public String hookUrl; +} diff --git a/src/main/java/mcwebhooks/DeathHookEvent.java b/src/main/java/mcwebhooks/DeathHookEvent.java new file mode 100644 index 0000000..ea68400 --- /dev/null +++ b/src/main/java/mcwebhooks/DeathHookEvent.java @@ -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; + } +} diff --git a/src/main/java/mcwebhooks/HookEvent.java b/src/main/java/mcwebhooks/HookEvent.java new file mode 100644 index 0000000..dd85be1 --- /dev/null +++ b/src/main/java/mcwebhooks/HookEvent.java @@ -0,0 +1,6 @@ +package mcwebhooks; + +abstract class HookEvent +{ + public String type; +} \ No newline at end of file diff --git a/src/main/java/mcwebhooks/JoinHookEvent.java b/src/main/java/mcwebhooks/JoinHookEvent.java new file mode 100644 index 0000000..3759dbd --- /dev/null +++ b/src/main/java/mcwebhooks/JoinHookEvent.java @@ -0,0 +1,9 @@ +package mcwebhooks; + +final class JoinHookEvent extends PlayerHookEvent +{ + public JoinHookEvent(String displayName) { + super(displayName); + this.type = "login"; + } +} diff --git a/src/main/java/mcwebhooks/MCWebHooks.java b/src/main/java/mcwebhooks/MCWebHooks.java new file mode 100644 index 0000000..d45c730 --- /dev/null +++ b/src/main/java/mcwebhooks/MCWebHooks.java @@ -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(); + } + } +} diff --git a/src/main/java/mcwebhooks/PlayerHookEvent.java b/src/main/java/mcwebhooks/PlayerHookEvent.java new file mode 100644 index 0000000..cf37c37 --- /dev/null +++ b/src/main/java/mcwebhooks/PlayerHookEvent.java @@ -0,0 +1,10 @@ +package mcwebhooks; + +abstract class PlayerHookEvent extends HookEvent +{ + public String displayName; + + public PlayerHookEvent(String displayName) { + this.displayName = displayName; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..5f86e10 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,3 @@ +name: mcwebhooks +main: mcwebhooks.MCWebHooks +version: 0.1 \ No newline at end of file