Skip to content

Commit

Permalink
feat: add api module and add rabbitmqMessage class
Browse files Browse the repository at this point in the history
Api module will be published in order to be used in other java project like AurionChat-Automessage

#39
  • Loading branch information
Yann151924 committed Feb 24, 2024
1 parent aecbfae commit b51ad76
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 22 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Publish
on:
workflow_call:
inputs:
version:
required: true
type: string
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: corretto
java-version: 17
- name: Cleanup Gradle Cache
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
- name: Setup Gradle Wrapper Cache
uses: actions/cache@v3
with:
path: ~/.gradle/wrapper
key: ${{ runner.os }}-library-gradle-wrapper-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
- name: Change version
run: |
sed -i "s/pluginVersion=SNAPSHOT/pluginVersion=${{ inputs.version}}/" gradle.properties
- name: Publish Gradle Package
run: ./gradlew :api:build -x test :api:publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9 changes: 8 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
files: |
build/**/*.jar
build/**/*.jar
publish:
needs:
- getVersion
- release
uses: ./.github/workflows/publish.yaml
with:
version: ${{ needs.getVersion.outputs.tag }}
28 changes: 28 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("java-library")
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compileOnly 'com.google.code.gson:gson:2.10.1'
}

publishing {
publications {
gpr(MavenPublication) {
from(components.java)
}
}
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/Mineaurion/aurionchat"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mineaurion.aurionchat.api;

import com.google.gson.annotations.SerializedName;

public class RabbitMQMessage {

private String channel;

public enum Type {
@SerializedName("chat")
CHAT,
@SerializedName("automessage")
AUTO_MESSAGE;
};

private Type type;

private String message;

public RabbitMQMessage(String channel, Type type, String message){
this.channel = channel;
this.type = type;
this.message = message;
}

public String getChannel() {
return channel;
}

public RabbitMQMessage setChannel(String channel) {
this.channel = channel;
return this;
}

public Type getType() {
return type;
}

public RabbitMQMessage setType(Type type) {
this.type = type;
return this;
}

public String getMessage() {
return message;
}

public RabbitMQMessage setMessage(String message) {
this.message = message;
return this;
}

}
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
compileOnly 'com.google.guava:guava:19.0'
compileOnly 'org.jetbrains:annotations:21.+'

implementation project(':api')

api('net.kyori:adventure-api:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'checker-qual')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mineaurion.aurionchat.common;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.Gson;
import com.mineaurion.aurionchat.api.RabbitMQMessage;
import com.mineaurion.aurionchat.common.config.ConfigurationAdapter;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
Expand Down Expand Up @@ -79,41 +79,36 @@ private void join() throws IOException{

private DeliverCallback consumer(){
return (consumerTag, delivery) -> {
JsonObject json = new JsonParser().parse(new String(delivery.getBody(), StandardCharsets.UTF_8)).getAsJsonObject();

String channel = json.get("channel").getAsString();
String message = json.get("message").getAsString();
String type = json.get("type").getAsString();
Component messageDeserialize = GsonComponentSerializer.gson().deserialize(message);
RabbitMQMessage rabbitMQMessage = new Gson().fromJson(new String(delivery.getBody(), StandardCharsets.UTF_8), RabbitMQMessage.class);
Component messageDeserialize = GsonComponentSerializer.gson().deserialize(rabbitMQMessage.getMessage());
if(this.config.getBoolean("options.spy", false)){
plugin.getlogger().info(Utils.getDisplayString(messageDeserialize));
}

plugin.getAurionChatPlayers().forEach((uuid, aurionChatPlayers) -> {
if(type.equalsIgnoreCase("automessage") && this.config.getBoolean("options.automessage", false)){
if(aurionChatPlayers.hasPermission("aurionchat.automessage." + channel)){
if(rabbitMQMessage.getType().equals(RabbitMQMessage.Type.AUTO_MESSAGE) && this.config.getBoolean("options.automessage", false)){
if(aurionChatPlayers.hasPermission("aurionchat.automessage." + rabbitMQMessage.getChannel())){
aurionChatPlayers.sendMessage(messageDeserialize);
}
} else if (type.equalsIgnoreCase("chat")) {
if(aurionChatPlayers.getChannels().contains(channel)){
} else if (rabbitMQMessage.getType().equals(RabbitMQMessage.Type.CHAT)) {
if(aurionChatPlayers.getChannels().contains(rabbitMQMessage.getChannel())){
aurionChatPlayers.sendMessage(messageDeserialize);
}
} else {
plugin.getlogger().warn("Received message with the type " + type + " and the message was " + message + ". It won't be processed");
plugin.getlogger().warn("Received message with the type " + rabbitMQMessage.getType() + " and the message was " + rabbitMQMessage + ". It won't be processed");
}
});
};
}

public void send(String channelName, Component message) throws IOException {
String serializedMessage = GsonComponentSerializer.gson().serialize(message);

JsonObject json = new JsonObject();
json.addProperty("channel", channelName);
json.addProperty("type", "chat");
json.addProperty("message", serializedMessage);
RabbitMQMessage rabbitMQMessage = new RabbitMQMessage(
channelName,
RabbitMQMessage.Type.CHAT,
GsonComponentSerializer.gson().serialize(message)
);

channel.basicPublish(EXCHANGE_NAME, "", null, json.toString().getBytes());
channel.basicPublish(EXCHANGE_NAME, "", null, new Gson().toJson(rabbitMQMessage).getBytes());
}
public void close(){
try {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pluginManagement {

rootProject.name = 'aurionchat'

include 'common', 'bukkit', 'sponge', 'forge', 'fabric'
include 'common', 'api', 'bukkit', 'sponge', 'forge', 'fabric'

0 comments on commit b51ad76

Please sign in to comment.