Skip to content

Commit

Permalink
improve data player handling
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Feb 25, 2024
1 parent 4072116 commit df51585
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
34 changes: 31 additions & 3 deletions api/src/main/java/com/mineaurion/aurionchat/api/AurionPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public static AurionPacket parse(String json) {
return gson.fromJson(json, AurionPacket.class);
}

/**
* create a packet builder for a chat message
* @param player the related player
* @param message the raw, sanitized text content of the message
* @param tellRaw shown to ingame players
* @return builder; should be extended with {@link Builder#channel(String)}
*/
public static Builder chat(AurionPlayer player, String message, Object tellRaw) {
return AurionPacket.builder()
.type(Type.CHAT)
Expand All @@ -31,6 +38,27 @@ public static Builder chat(AurionPlayer player, String message, Object tellRaw)
.tellRawData(tellRaw.toString());
}

/**
* create a packet builder for a player event
* @param player the related player
* @param type the exact type of player event
* @param tellRaw shown to ingame players
* @return builder; should be extended with {@link Builder#detail(String)} and {@link Builder#channel(String)}
*/
public static Builder event(AurionPlayer player, Type type, Object tellRaw) {
return AurionPacket.builder()
.type(Type.CHAT)
.source("ingame")
.player(player)
.tellRawData(tellRaw.toString());
}

/**
* create a packet builder for an automessage
* @param message the raw, sanitized message
* @param tellRaw shown to ingame players
* @return builder; should be extended with {@link Builder#channel(String)}
*/
public static Builder autoMessage(String message, Object tellRaw) {
return AurionPacket.builder()
.type(Type.AUTO_MESSAGE)
Expand All @@ -50,12 +78,12 @@ public static Builder autoMessage(String message, Object tellRaw) {
@Default @Nullable AurionPlayer player = null;

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

/** display name of sender (one of: player name, automessage title) */
/** sanitized 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) */
/** sanitized detail data (one of: message text, join text, achievement text) */
@Default @Nullable String detail = null;

/** what ingame players see */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
@Value @NonFinal
public class AurionPlayer implements Named {
UUID id;
/**
* raw, sanitized recently known player name
*/
String name;
/**
* sanitized display name of the player
*/
@Nullable String displayName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event){
plugin.getConfigurationAdapter().getChannels().get(currentChannel).urlMode
);
AurionPacket.Builder packet = AurionPacket.chat(
player.getName(),
aurionChatPlayer,
event.getMessage(),
gson().serialize(messageFormat))
.playerId(player.getUniqueId())
.channelName(currentChannel);
.channel(currentChannel);
try{
plugin.getChatService().send(packet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ private DeliverCallback consumer(){

plugin.getAurionChatPlayers().forEach((uuid, aurionChatPlayers) -> {
if(packet.getType().equals(AurionPacket.Type.AUTO_MESSAGE) && this.config.getBoolean("options.automessage", false)){
if(aurionChatPlayers.hasPermission("aurionchat.automessage." + packet.getChannelName())){
if(aurionChatPlayers.hasPermission("aurionchat.automessage." + packet.getChannel())){
aurionChatPlayers.sendMessage(messageDeserialize);
}
} else if (packet.getType().equals(AurionPacket.Type.CHAT)) {
if(aurionChatPlayers.getChannels().contains(packet.getChannelName())){
if(aurionChatPlayers.getChannels().contains(packet.getChannel())){
aurionChatPlayers.sendMessage(messageDeserialize);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ public boolean onCommand(AurionChatPlayer aurionChatPlayers, Component message,
aurionChatPlayers.addChannel(channel);
Component component = Utils.processMessage(format, message, aurionChatPlayers, Collections.singletonList(Utils.URL_MODE.ALLOW));
AurionPacket.Builder packet = AurionPacket.chat(
aurionChatPlayers.getPlayer().getDisplayName(),
aurionChatPlayers,
Utils.getDisplayString(message),
GsonComponentSerializer.gson().serialize(component))
.playerId(aurionChatPlayers.getPlayer().getUUID())
.channelName(channel);
.channel(channel);
try {
plugin.getChatService().send(packet);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public boolean allowChatMessage(SignedMessage message, ServerPlayerEntity sender
channel.urlMode
);
AurionPacket.Builder packet = AurionPacket.chat(
aurionChatPlayer.getPlayer().getDisplayName(),
aurionChatPlayer,
message.getSignedContent(),
gson().serialize(messageFormat))
.playerId(aurionChatPlayer.getPlayer().getUUID())
.channelName(currentChannel);
.channel(currentChannel);
try {
plugin.getChatService().send(packet);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public void onServerChatEvent(ServerChatEvent event) {
channel.urlMode
);
AurionPacket.Builder packet = AurionPacket.chat(
event.getPlayer().getScoreboardName(),
aurionChatPlayer,
event.getRawText(),
gson().serialize(component))
.playerId(event.getPlayer().getUUID())
.channelName(currentChannel);
.channel(currentChannel);
try {
plugin.getChatService().send(packet);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public void onPlayerChat(PlayerChatEvent event, @First ServerPlayer player) {
channel.urlMode
);
AurionPacket.Builder packet = AurionPacket.chat(
player.name(),
aurionChatPlayer,
Utils.getDisplayString(event.message()),
gson().serialize(messageFormat))
.playerId(player.uniqueId())
.channelName(currentChannel);
.channel(currentChannel);
try {
plugin.getChatService().send(packet);
} catch (Exception e) {
Expand Down

0 comments on commit df51585

Please sign in to comment.