Skip to content

Commit

Permalink
feat: remove lombook & remove some dependencies in common
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann151924 committed May 10, 2024
1 parent 362cca5 commit 0bfbc23
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 144 deletions.
4 changes: 0 additions & 4 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ dependencies {
exclude(module: 'checker-qual')
exclude(module: 'guava')
}

// lombok
compileOnly 'org.projectlombok:lombok:+'
annotationProcessor 'org.projectlombok:lombok:+'
}

publishing {
Expand Down
223 changes: 175 additions & 48 deletions api/src/main/java/com/mineaurion/aurionchat/api/AurionPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.mineaurion.aurionchat.api.model.Named;
import lombok.*;
import lombok.Builder.Default;
import lombok.experimental.FieldDefaults;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.TextComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,91 +13,221 @@

import static net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson;

@Value
@Builder(toBuilder = true)
@AllArgsConstructor // make public for gson
public class AurionPacket implements Named, Serializable {
public static Gson gson = new Gson();
public final class AurionPacket implements Named, Serializable {

public static AurionPacket parse(String json) {
return gson.fromJson(json, AurionPacket.class);
public static final Gson gson = new Gson();

public static Builder builder() {
return new Builder();
}

public AurionPacket(Type type, String source, @Nullable AurionPlayer player, @Nullable String channel, @Nullable String displayName, @NotNull String tellRawData) {
this.type = type;
this.source = source;
this.player = player;
this.channel = channel;
this.displayName = displayName;
this.displayString = getStringFromComponent(getComponent());
this.tellRawData = tellRawData;
}

public static Builder chat(AurionPlayer player, String message, Object tellRaw) {
public static Builder chat(AurionPlayer player, Object tellRaw) {
return AurionPacket.builder()
.type(Type.CHAT)
.source("ingame")
.player(player)
.detail(message)
.tellRawData(tellRaw.toString());
}

public static Builder autoMessage(String message, Object tellRaw) {
public static Builder autoMessage(Object tellRaw) {
return AurionPacket.builder()
.type(Type.AUTO_MESSAGE)
.source("automessage")
.displayName("AutoMessage")
.detail(message)
.tellRawData(tellRaw.toString());
}

/** packet type */
Type type;

/** one of: servername, 'discord' or 'ingame' literal */
String source;

/** related player */
@Default @Nullable AurionPlayer player = null;

/** channel name */
@Default @Nullable String channel = null;

/** display name of sender (one of: player name, automessage title) */
@Default @Nullable String displayName = null;

/** detail data (one of: message text, join text, achievement text) */
@Default @Nullable String detail = null;

/** what ingame players see */
@NotNull String tellRawData;

/** what to display in plaintext environments */
@SuppressWarnings("ConstantValue") // false positive bcs of lombok
/**
* Packet type
*/
private final Type type;

/**
* One of: servername, 'discord' or 'ingame' literal
*/
private final String source;

/**
* Related player
*/
@Nullable
private final AurionPlayer player;

/**
* Channel name
*/
@Nullable
private final String channel;

/**
* Display name of sender (one of: player name, automessage title)
*/
@Nullable
private final String displayName;

/**
* Only the string of the message without any colors or stuff from the game
*/
@NotNull
private final String displayString;

/**
* What ingame players see
*/
@NotNull
private final String tellRawData;

/**
* What to display in plaintext environments
*/
public String getRawDisplay() {
return displayName + ' ' + type.verb + (detail==null?"": ": " + detail);
return displayName + ' ' + type.name().toLowerCase() + displayString;
}

public Component getComponent() {
return gson().deserialize(tellRawData);
}

public static String getStringFromComponent(Component component){
StringBuilder content = new StringBuilder();

if(!component.children().isEmpty()){
component.children().forEach( child -> content.append(getStringFromComponent(child)));
}

if(component instanceof TextComponent){
content.append(((TextComponent) component).content());
}
// todo: support other types
//else if (it instanceof BlockNBTComponent) ;
//else if (it instanceof EntityNBTComponent) ;
//else if (it instanceof KeybindComponent) ;
//else if (it instanceof ScoreComponent) ;
//else if (it instanceof SelectorComponent) ;
//else if (it instanceof StorageNBTComponent) ;
//else if (it instanceof TranslatableComponent) ;
return content.toString();
}

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

public static AurionPacket fromJson(String jsonString){
return gson.fromJson(jsonString, AurionPacket.class);
}

@Override
@SuppressWarnings({"ConstantValue", "OptionalOfNullableMisuse"}) // false positive bcs of lombok
public @Nullable String getName() {
return Optional.ofNullable(player)
.map(Named::getBestName)
.orElse(null);
.orElse(null)
;
}

@Override
@SuppressWarnings("ConstantValue") // false positive bcs of lombok
public @Nullable String getDisplayName() {
return displayName;
}

@Getter
@RequiredArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public Type getType() {
return this.type;
}

public String getSource() {
return this.source;
}

public @Nullable AurionPlayer getPlayer() {
return this.player;
}

public @Nullable String getChannel() {
return this.channel;
}

public @NotNull String getDisplayString(){
return this.displayString;
}

public @NotNull String getTellRawData() {
return this.tellRawData;
}

public Builder toBuilder() {
return new Builder()
.type(this.type)
.source(this.source)
.player(this.player)
.channel(this.channel)
.displayName(this.displayName)
.tellRawData(this.tellRawData)
;
}

public enum Type {
@SerializedName("chat") CHAT("wrote"),
@SerializedName("automessage") AUTO_MESSAGE("broadcasted");
@SerializedName("chat")
CHAT,
@SerializedName("automessage")
AUTO_MESSAGE,
@SerializedName("event_join")
EVENT_JOIN,
@SerializedName("event_achievement")
EVENT_ACHIEVEMENT,
}


String verb;
public static class Builder {
private Type type;
private String source;
private @Nullable AurionPlayer player;
private @Nullable String channel;
private @Nullable String displayName;
private @NotNull String tellRawData = "";

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

public Builder source(String source) {
this.source = source;
return this;
}

public Builder player(@Nullable AurionPlayer player) {
this.player = player;
return this;
}

public Builder channel(@Nullable String channel) {
this.channel = channel;
return this;
}

public Builder displayName(@Nullable String displayName) {
this.displayName = displayName;
return this;
}

public Builder tellRawData(@NotNull String tellRawData) {
this.tellRawData = tellRawData;
return this;
}

public AurionPacket build() {
return new AurionPacket(this.type, this.source, this.player, this.channel, this.displayName, this.tellRawData);
}
}
}
28 changes: 25 additions & 3 deletions api/src/main/java/com/mineaurion/aurionchat/api/AurionPlayer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.mineaurion.aurionchat.api;

import com.mineaurion.aurionchat.api.model.Player;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;

@Value @NonFinal
public class AurionPlayer implements Player {
UUID id;
String name;
Expand All @@ -23,4 +20,29 @@ public AurionPlayer(UUID id, String name, @Nullable String prefix, @Nullable Str
this.suffix = suffix;
this.displayName = Optional.ofNullable(prefix).orElse("") + name + Optional.ofNullable(suffix).orElse("");
}

@Override
public @Nullable String getName() {
return name;
}

@Override
public @Nullable String getDisplayName() {
return displayName;
}

@Override
public UUID getId() {
return id;
}

@Override
public @Nullable String getPrefix() {
return prefix;
}

@Override
public @Nullable String getSuffix() {
return suffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ default String getBestName() {
default String getBestName(String fallback) {
return Optional.ofNullable(getDisplayName())
.orElseGet(() -> Optional.ofNullable(getName())
.orElse(fallback));
.orElse(fallback))
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event){
aurionChatPlayer,
plugin.getConfigurationAdapter().getChannels().get(currentChannel).urlMode
);
AurionPacket.Builder packet = AurionPacket.chat(
aurionChatPlayer,
event.getMessage(),
gson().serialize(messageFormat))
.channel(currentChannel);
AurionPacket.Builder packet = AurionPacket.chat(aurionChatPlayer, gson().serialize(messageFormat))
.channel(currentChannel)
;
try{
plugin.getChatService().send(packet);
}
Expand Down
3 changes: 1 addition & 2 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ jacocoTestReport {

dependencies {

compileOnly 'com.google.code.gson:gson:2.7'
compileOnly 'com.google.guava:guava:19.0'
compileOnly 'com.google.guava:guava:33.1.0-jre'
compileOnly 'org.jetbrains:annotations:21.+'

api project(':api')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mineaurion.aurionchat.common;

import com.google.gson.Gson;
import com.mineaurion.aurionchat.api.AurionPacket;
import com.mineaurion.aurionchat.common.config.ConfigurationAdapter;
import com.rabbitmq.client.Channel;
Expand All @@ -9,7 +8,6 @@
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.impl.ForgivingExceptionHandler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

import java.io.IOException;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -79,10 +77,10 @@ private void join() throws IOException{

private DeliverCallback consumer(){
return (consumerTag, delivery) -> {
AurionPacket packet = new Gson().fromJson(new String(delivery.getBody(), StandardCharsets.UTF_8), AurionPacket.class);
Component messageDeserialize = GsonComponentSerializer.gson().deserialize(packet.getTellRawData());
AurionPacket packet = AurionPacket.fromJson(new String(delivery.getBody(), StandardCharsets.UTF_8));
Component messageDeserialize = packet.getComponent();
if(this.config.getBoolean("options.spy", false)){
plugin.getlogger().info(Utils.getDisplayString(messageDeserialize));
plugin.getlogger().info(packet.getDisplayString());
}

plugin.getAurionChatPlayers().forEach((uuid, aurionChatPlayers) -> {
Expand Down
Loading

0 comments on commit 0bfbc23

Please sign in to comment.