Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord webhook initial integration #5

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.classpath
.project
.settings
Session.vim

# Ignore Gradle project-specific cache directory
.gradle
Expand Down
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
GRADLEW=./gradlew

JAVA_DEBUGGER=jdb
JAVA_DEBUGGER_OPTS=-attach 5005

all:
$(GRADLEW) build

status:
$(GRADLEW) --status

stop:
$(GRADLEW) --stop

jar: GRADLEW_OPTS=--no-daemon
jar: build/libs/CustomPlugin-0.1-dev.jar
$(GRADLEW) $(GRADLEW_OPTS) clean
$(GRADLEW) $(GRADLEW_OPTS) jar

debug:
$(JAVA_DEBUGGER) $(JAVA_DEBUGGER_OPTS)

clean:
$(GRADLEW) --no-daemon clean
52 changes: 26 additions & 26 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@
*/

plugins {
java
id("io.papermc.paperweight.userdev") version "1.3.5"
id("xyz.jpenilla.run-paper") version "1.0.6"
java
id("io.papermc.paperweight.userdev") version "1.3.5"
id("xyz.jpenilla.run-paper") version "1.0.6"
}

group = "ghost3.mcplugins.customplugin"
version = "0.1"

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
paperDevBundle("1.19-R0.1-SNAPSHOT")
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
paperDevBundle("1.19.3-R0.1-SNAPSHOT")
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
}

tasks {
build {
dependsOn(reobfJar)
}

test {
useJUnitPlatform()
}

processResources {
filteringCharset = "UTF-8"
val projectData = mapOf(
"name" to project.properties["name"],
"version" to project.properties["version"]
)
filesMatching("plugin.yml") {
expand(projectData)
}
build {
dependsOn(reobfJar)
}

test {
useJUnitPlatform()
}

processResources {
filteringCharset = "UTF-8"
val projectData = mapOf(
"name" to project.properties["name"],
"version" to project.properties["version"]
)
filesMatching("plugin.yml") {
expand(projectData)
}
}
}
8 changes: 4 additions & 4 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* in the user manual at https://docs.gradle.org/7.3/userguide/multi_project_builds.html
*/
pluginManagement {
repositories {
maven("https://papermc.io/repo/repository/maven-public/")
gradlePluginPortal()
}
repositories {
maven("https://papermc.io/repo/repository/maven-public/")
gradlePluginPortal()
}
}

rootProject.name = "CustomPlugin"
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import static org.bukkit.entity.EntityType.CREEPER;

class CustomEventListener implements Listener {
@EventHandler
public void onCreeperExplode(EntityExplodeEvent evt) {
if (evt.getEntityType() != CREEPER) return;
evt.blockList().clear();
}
@EventHandler
public void onCreeperExplode(EntityExplodeEvent evt) {
if (evt.getEntityType() != CREEPER)
return;

evt.blockList().clear();
}
}
46 changes: 45 additions & 1 deletion src/main/java/ghosti3/mcplugin/customplugin/CustomPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,64 @@
*/
package ghosti3.mcplugin.customplugin;

import java.net.MalformedURLException;
import java.util.Optional;
import java.util.logging.Logger;

import org.bukkit.plugin.PluginLogger;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import ghosti3.mcplugin.customplugin.disco.DiscordPush;

public class CustomPlugin extends JavaPlugin {
private static CustomPlugin instance = null;
public static Logger plLogger = PluginLogger.getLogger("CustomPlugin");

private Settings settings;
private DiscordPush sender;

@Override
public void onEnable() {
CustomPlugin.instance = this;
this.saveDefaultConfig();
settings = Settings.fromFileConfig(getConfig());

try {
sender = new DiscordPush(settings.webhookUrl);
} catch (MalformedURLException e) {
plLogger.severe("Bad WEBHOOK_URL.");
getServer().getPluginManager().disablePlugin(this);
return;
}

plLogger.info("Registering custom events");
getServer().getPluginManager().registerEvents(new CustomEventListener(), this);

sender.build(DiscordPush.MsgTypes.ServerOnline).send();
}

@Override
public void onDisable() {}
public void onDisable() {
if (sender != null)
sender.build(DiscordPush.MsgTypes.ServerOffline).send();

CustomPlugin.instance = null;
}

@NotNull
public Settings getSettings() {
return settings;
}

/**
* Returns possible instance of initialised plugin.
*
* @return possible reference to {@link CustomPlugin} active instance
*/
@Nullable
public static Optional<CustomPlugin> getInstance() {
return Optional.ofNullable(instance);
}
}
39 changes: 39 additions & 0 deletions src/main/java/ghosti3/mcplugin/customplugin/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ghosti3.mcplugin.customplugin;

import org.bukkit.configuration.file.FileConfiguration;

/**
* Settings, Singleton
*/
public class Settings {
private static Settings instance;

public final String webhookUrl;
public final int messageDelay;

private Settings(final String webhookUrl, final int messageDelay) {
this.webhookUrl = webhookUrl;
this.messageDelay = messageDelay;
}

@Override
public String toString() {
return String.format(
"Settings [webhookUrl = '%s', messageDelay = '%d']",
webhookUrl, messageDelay);
}

public static Settings fromFileConfig(FileConfiguration config) {
if (instance != null)
return instance;

String webhookUrl = config.getString("webhook_url");
int messageDelay = config.getInt("mdelay", 5);

if (webhookUrl == null)
throw new NullPointerException();

instance = new Settings(webhookUrl, messageDelay);
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ghosti3.mcplugin.customplugin.disco;

import java.io.Serializable;

import com.google.gson.Gson;

/**
* Embed object used for transforming into embed json obj. Must not exceed
* overall 6000 chars.
*/
public class DiscordEmbed implements Serializable {

/**
* Must not exceed 256 chars.
*/
private String title;
@SuppressWarnings("unused")
private final String type = "rich";
/**
* Must not exceed 4096 chars.
*/
private String description;
private Integer color;

private DiscordEmbed() {
}

/**
* @return the title
*/
public String getTitle() {
return title;
}

/**
* @return the description
*/
public String getDescription() {
return description;
}

/**
* @return the color
*/
public Integer getColor() {
return color;
}

@Override
public String toString() {
return new Gson().toJson(this).toString();
}

public static class Builder {

private DiscordEmbed result = new DiscordEmbed();

public Builder setTitle(String title) {
result.title = title;
return this;
}

public Builder setDescription(String description) {
result.description = description;
return this;
}

public Builder setColor(int color) {
result.color = color;
return this;
}

public DiscordEmbed toEmbed() {
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ghosti3.mcplugin.customplugin.disco;

import java.io.Serializable;
import java.util.ArrayList;

import com.google.gson.Gson;

public class DiscordMessage implements Serializable {

/**
* Must not exceed 2000 chars.
*/
private String content;

/**
* Must not exceed 10 embed objects.
*/
private ArrayList<DiscordEmbed> embeds;

private DiscordMessage() {
}

/**
* @return the content
*/
public String getContent() {
return content;
}

/**
* @return the embed
*/
public ArrayList<DiscordEmbed> getEmbeds() {
return embeds;
}

@Override
public String toString() {
return new Gson().toJson(this).toString();
}

public static class Builder {
public static final int MAX_CONTENT_LENGTH = 2001;
public static final int MAX_EMBED_LENGTH = 10;

private DiscordMessage result = new DiscordMessage();

public Builder setContent(String content) {
if (content.length() > MAX_CONTENT_LENGTH)
return null;

result.content = content;
return this;
}

/**
* Adds embed obj to array if max size is not exceeded.
*
* @param embed {@link DiscordEmbed} to be added to message embed obj array
* @return The calling object is returned.
*/
public Builder setEmbed(DiscordEmbed embed) {
if (result.embeds == null)
result.embeds = new ArrayList<>();

if (result.embeds.size() < MAX_EMBED_LENGTH)
result.embeds.add(embed);

return this;
}

public DiscordMessage toMessage() {
return result;
}
}
}
Loading