Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Pequla committed Jun 16, 2022
1 parent 3df8857 commit 41dac02
Show file tree
Hide file tree
Showing 13 changed files with 505 additions and 1 deletion.
113 changes: 113 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# User-specific stuff
.idea/

*.iml
*.ipr
*.iws

# IntelliJ
out/

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

target/

pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next

release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.flattened-pom.xml

# Common working directory
run/
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# LittleHooks
Discord webhooks integration as a Spigot plugin. Requires plugin LittleLink.
Discord webhooks integration as a Spigot plugin. Extremely light weight.

> Requires plugin [LittleLink](https://github.com/Pequla/LittleLink)
## Configuration

The default configuration:
```yaml
webhook-url: url
color:
system: 65535
join: 65280
leave: 16711680
death: 8388736
```
- `webhook-url` is the webhook url. You can obtain it in the channel settings under the integrations/webhooks tab. Please note that this link allows you to send messages to that channel so keep it safe and secure
- `color` represents the decimal or hexadecimal color code of the discord message embed. All the sub options represent the name of the event the color will be used in

> `color.system` color code is used in the non player related events. ex: server loading or server stopping
96 changes: 96 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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>com.pequla</groupId>
<artifactId>little-hooks</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>LittleHooks</name>

<description>Discord webhook integration for your minecraft server</description>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://pequla.com</url>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.pequla</groupId>
<artifactId>little-link</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Pequla Apache Maven Packages</name>
<url>https://maven.pkg.github.com/Pequla/LittleHooks</url>
</repository>
</distributionManagement>
</project>
141 changes: 141 additions & 0 deletions src/main/java/com/pequla/link/GameHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.pequla.link;

import com.pequla.link.model.*;
import com.pequla.link.service.DataService;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;

public class GameHandler implements Listener {

private final LittleHooks plugin;
private final LittleLink core;

public GameHandler(LittleHooks plugin, LittleLink core) {
this.plugin = plugin;
this.core = core;
}

@EventHandler
public void handlePlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
DataModel data = core.getPlayerData().get(player.getUniqueId());
Server server = plugin.getServer();

sendMessage(player, EmbedModel.builder()
.color(plugin.getConfig().getInt("color.join"))
.author(EmbedAuthor.builder()
.name(data.getNickname())
.icon_url(data.getAvatar())
.build())
.description(bold(event.getJoinMessage()))
.fields(List.of(EmbedField.builder()
.name("Online:")
.value(server.getOnlinePlayers().size() + "/" + server.getMaxPlayers())
.inline(false)
.build()))
.footer(EmbedFooter.builder()
.text(data.getId())
.build())
.timestamp(Instant.now().toString())
.build());
}

@EventHandler(priority = EventPriority.LOWEST)
public void handlePlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
DataModel data = core.getPlayerData().get(player.getUniqueId());
Server server = plugin.getServer();

sendMessage(player, EmbedModel.builder()
.color(plugin.getConfig().getInt("color.leave"))
.author(EmbedAuthor.builder()
.name(data.getNickname())
.icon_url(data.getAvatar())
.build())
.description(bold(event.getQuitMessage()))
.fields(List.of(EmbedField.builder()
.name("Online:")
.value((server.getOnlinePlayers().size() - 1) + "/" + server.getMaxPlayers())
.inline(false)
.build()))
.footer(EmbedFooter.builder()
.text(data.getId())
.build())
.timestamp(Instant.now().toString())
.build());
}

@EventHandler
public void handlePlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
DataModel data = core.getPlayerData().get(player.getUniqueId());

sendMessage(player, EmbedModel.builder()
.color(plugin.getConfig().getInt("color.death"))
.author(EmbedAuthor.builder()
.name(data.getNickname())
.icon_url(data.getAvatar())
.build())
.description(bold(event.getDeathMessage()))
.footer(EmbedFooter.builder()
.text(data.getId())
.build())
.timestamp(Instant.now().toString())
.build());
}

private String getMinecraftAvatarUrl(Player player) {
return "https://visage.surgeplay.com/face/" + player.getUniqueId().toString().replace("-", "");
}

private String bold(String str) {
return "**" + ChatColor.stripColor(str) + "**";
}

private void sendMessage(Player player, EmbedModel model) {
try {
String url = plugin.getConfig().getString("webhook-url");
if (url == null) throw new RuntimeException("Webhook URL not found");

DataService service = DataService.getInstance();
String json = service.getMapper().writeValueAsString(MessageModel.builder()
.username(player.getName())
.avatar_url(getMinecraftAvatarUrl(player))
.embeds(List.of(model))
.build());

new Thread(()->{
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8))
.build();

HttpResponse<String> rsp = service.getClient().send(request, HttpResponse.BodyHandlers.ofString());
if (rsp.statusCode() != 204)
throw new RuntimeException("Server responded with HTTP " + rsp.statusCode());
} catch (Exception e) {
plugin.getLogger().severe("Webhook could not be sent! " + e.getMessage());
}

}).start();
} catch (Exception e) {
plugin.getLogger().severe("Webhook could not be sent! " + e.getMessage());
}
}
}
Loading

0 comments on commit 41dac02

Please sign in to comment.