Skip to content

Commit

Permalink
feat: Introduce AurionPacket class
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto authored and Yann151924 committed May 10, 2024
1 parent 161782c commit 362cca5
Show file tree
Hide file tree
Showing 22 changed files with 334 additions and 166 deletions.
37 changes: 37 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,44 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
compileOnly 'org.jetbrains:annotations:+'
compileOnly 'com.google.code.gson:gson:2.10.1'

api('net.kyori:adventure-api:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'checker-qual')
exclude(module: 'annotations')
}

api('net.kyori:adventure-text-serializer-gson:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
exclude(module: 'gson')
}

api('net.kyori:adventure-text-serializer-legacy:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api('net.kyori:adventure-text-serializer-plain:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api("net.kyori:adventure-text-minimessage:4.11.0") {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api('net.kyori:event-api:3.0.0') {
exclude(module: 'checker-qual')
exclude(module: 'guava')
}

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

publishing {
Expand Down
106 changes: 106 additions & 0 deletions api/src/main/java/com/mineaurion/aurionchat/api/AurionPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.mineaurion.aurionchat.api;

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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.Optional;

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 static AurionPacket parse(String json) {
return gson.fromJson(json, AurionPacket.class);
}

public static Builder chat(AurionPlayer player, String message, 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) {
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
public String getRawDisplay() {
return displayName + ' ' + type.verb + (detail==null?"": ": " + detail);
}

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

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

@Override
@SuppressWarnings({"ConstantValue", "OptionalOfNullableMisuse"}) // false positive bcs of lombok
public @Nullable String getName() {
return Optional.ofNullable(player)
.map(Named::getBestName)
.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 enum Type {
@SerializedName("chat") CHAT("wrote"),
@SerializedName("automessage") AUTO_MESSAGE("broadcasted");

String verb;
}
}
26 changes: 26 additions & 0 deletions api/src/main/java/com/mineaurion/aurionchat/api/AurionPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
@Nullable String prefix;
@Nullable String suffix;
@Nullable String displayName;

public AurionPlayer(UUID id, String name, @Nullable String prefix, @Nullable String suffix) {
this.id = id;
this.name = name;
this.prefix = prefix;
this.suffix = suffix;
this.displayName = Optional.ofNullable(prefix).orElse("") + name + Optional.ofNullable(suffix).orElse("");
}
}

This file was deleted.

23 changes: 23 additions & 0 deletions api/src/main/java/com/mineaurion/aurionchat/api/model/Named.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mineaurion.aurionchat.api.model;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public interface Named {
@Nullable String getName();

@Nullable String getDisplayName();

default String getBestName() {
return getBestName(toString());
}

@Contract("null -> _; !null -> !null")
default String getBestName(String fallback) {
return Optional.ofNullable(getDisplayName())
.orElseGet(() -> Optional.ofNullable(getName())
.orElse(fallback));
}
}
13 changes: 13 additions & 0 deletions api/src/main/java/com/mineaurion/aurionchat/api/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mineaurion.aurionchat.api.model;

import java.util.UUID;

public interface Player extends Named {
String getDisplayName();

UUID getId();

String getPrefix();

String getSuffix();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mineaurion.aurionchat.api.model;

import net.kyori.adventure.text.Component;

public interface ServerPlayer extends Player {
void sendMessage(Component message);

boolean hasPermission(String permission);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineaurion.aurionchat.bukkit.listeners;

import com.mineaurion.aurionchat.api.AurionPacket;
import com.mineaurion.aurionchat.bukkit.AurionChat;
import com.mineaurion.aurionchat.common.AurionChatPlayer;
import com.mineaurion.aurionchat.common.Utils;
Expand All @@ -14,6 +15,8 @@

import java.io.IOException;

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

public class ChatListener implements Listener {

private final AurionChat plugin;
Expand All @@ -39,9 +42,13 @@ 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);
try{
plugin.getChatService().send(currentChannel, messageFormat);
plugin.getChatService().send(packet);
}
catch(IOException e){
Bukkit.getConsoleSender().sendMessage(e.getMessage());
Expand Down
34 changes: 1 addition & 33 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,7 @@ 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')
exclude(module: 'annotations')
}

api('net.kyori:adventure-text-serializer-gson:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
exclude(module: 'gson')
}

api('net.kyori:adventure-text-serializer-legacy:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api('net.kyori:adventure-text-serializer-plain:4.11.0') {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api("net.kyori:adventure-text-minimessage:4.11.0") {
exclude(module: 'adventure-bom')
exclude(module: 'adventure-api')
}

api('net.kyori:event-api:3.0.0') {
exclude(module: 'checker-qual')
exclude(module: 'guava')
}
api project(':api')

api('org.spongepowered:configurate-core:4.1.2')
api('org.spongepowered:configurate-yaml:4.1.2')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.mineaurion.aurionchat.common;

import com.mineaurion.aurionchat.common.player.Player;
import com.mineaurion.aurionchat.api.AurionPlayer;
import com.mineaurion.aurionchat.api.model.Player;
import com.mineaurion.aurionchat.api.model.ServerPlayer;
import net.kyori.adventure.text.Component;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class AurionChatPlayer {
private final Player player;
public class AurionChatPlayer extends AurionPlayer {
private final ServerPlayer player;
private Set<String> channels = new HashSet<>();
private String currentChannel;
private static final String DEFAULT_CHANNEL = "global"; // Player default channel when he is speaking. We assume global is always defined in the config

public AurionChatPlayer(Player player, AbstractAurionChat plugin){
public AurionChatPlayer(ServerPlayer player, AbstractAurionChat plugin){
super(player.getId(),
player.getDisplayName(),
player.getPrefix(),
player.getSuffix());
this.player = player;
this.setCurrentChannel(DEFAULT_CHANNEL);
this.setChannels(new HashSet<>(Collections.singletonList(DEFAULT_CHANNEL)));
Expand Down Expand Up @@ -84,8 +90,8 @@ public String toString()
{
return "currentChannel:" + this.currentChannel +
",channels:" + this.channels.toString() +
",uuid:" + this.player.getUUID() +
",prefix:" + this.player.getPreffix() +
",uuid:" + this.player.getId() +
",prefix:" + this.player.getPrefix() +
",suffix:" + this.player.getSuffix() +
",name:" + this.player.getDisplayName()
;
Expand Down
Loading

0 comments on commit 362cca5

Please sign in to comment.