develop-chatReply #5

Merged
Pupsi merged 11 commits from develop-chatReply into master 2024-10-06 13:52:59 +00:00
9 changed files with 250 additions and 13 deletions

View File

@ -1,6 +1,8 @@
package eu.mhsl.craftattack.spawn.appliances.chatMention; package eu.mhsl.craftattack.spawn.appliances.chatMention;
import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener; import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
import eu.mhsl.craftattack.spawn.appliances.chatMessages.ChatMessages;
import eu.mhsl.craftattack.spawn.appliances.settings.Settings; import eu.mhsl.craftattack.spawn.appliances.settings.Settings;
import eu.mhsl.craftattack.spawn.util.text.ComponentUtil; import eu.mhsl.craftattack.spawn.util.text.ComponentUtil;
import io.papermc.paper.event.player.AsyncChatDecorateEvent; import io.papermc.paper.event.player.AsyncChatDecorateEvent;
@ -8,6 +10,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,6 +25,7 @@ public class ChatMentionListener extends ApplianceListener<ChatMention> {
ChatMentionSetting.ChatMentionConfig config = Settings.instance() ChatMentionSetting.ChatMentionConfig config = Settings.instance()
.getSetting(event.player(), Settings.Key.ChatMentions, ChatMentionSetting.ChatMentionConfig.class); .getSetting(event.player(), Settings.Key.ChatMentions, ChatMentionSetting.ChatMentionConfig.class);
ChatMessages chatMessages = Main.instance().getAppliance(ChatMessages.class);
Component result = words.stream() Component result = words.stream()
.map(word -> { .map(word -> {
@ -29,10 +33,11 @@ public class ChatMentionListener extends ApplianceListener<ChatMention> {
boolean isPlayer = getAppliance().getPlayerNames().contains(wordWithoutAnnotation); boolean isPlayer = getAppliance().getPlayerNames().contains(wordWithoutAnnotation);
if(isPlayer && config.applyMentions()) { if(isPlayer && config.applyMentions()) {
mentioned.add(wordWithoutAnnotation); mentioned.add(wordWithoutAnnotation);
return Component.text( Component mention = Component.text(
getAppliance().formatPlayer(wordWithoutAnnotation), getAppliance().formatPlayer(wordWithoutAnnotation),
NamedTextColor.GOLD NamedTextColor.GOLD
); );
return chatMessages.addReportActions(mention, wordWithoutAnnotation);
} else { } else {
return Component.text(word); return Component.text(word);
} }
@ -43,4 +48,9 @@ public class ChatMentionListener extends ApplianceListener<ChatMention> {
getAppliance().notifyPlayers(mentioned); getAppliance().notifyPlayers(mentioned);
event.result(result); event.result(result);
} }
@EventHandler
public void onJoin(PlayerJoinEvent event) {
getAppliance().refreshPlayers();
}
} }

View File

@ -19,11 +19,13 @@ public class ChatMessages extends Appliance {
} }
public Component getReportablePlayerName(Player player) { public Component getReportablePlayerName(Player player) {
return Component return addReportActions(player.displayName(), player.getName());
.text("") }
.append(player.displayName())
public Component addReportActions(Component message, String username) {
return message
.hoverEvent(HoverEvent.showText(Component.text("Klicke, um diesen Spieler zu reporten").color(NamedTextColor.GOLD))) .hoverEvent(HoverEvent.showText(Component.text("Klicke, um diesen Spieler zu reporten").color(NamedTextColor.GOLD)))
.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/report " + player.getName() + " ")); .clickEvent(ClickEvent.clickEvent(ClickEvent.Action.SUGGEST_COMMAND, String.format("/report %s ", username)));
} }
@Override @Override

View File

@ -0,0 +1,187 @@
package eu.mhsl.craftattack.spawn.appliances.privateMessage;
import eu.mhsl.craftattack.spawn.Main;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.chatMessages.ChatMessages;
import eu.mhsl.craftattack.spawn.appliances.privateMessage.commands.PrivateMessageCommand;
import eu.mhsl.craftattack.spawn.appliances.privateMessage.commands.PrivateReplyCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentBuilder;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.stream.Collectors;
public class PrivateMessage extends Appliance {
public final int targetChangeTimeoutSeconds = 30;
public final int conversationTimeoutMinutes = 30;
private record Conversation(UUID target, Long lastSet) {}
private final Map<Player, List<Conversation>> replyMapping = new WeakHashMap<>();
Pupsi marked this conversation as resolved Outdated

die null checks überall sind nicht schön, es macht vielleicht eher sinn am anfang einmal zu prüfen ob ein Eintrag vorhanden ist und wenn nicht ne leere Liste zu initialisieren..

this.replyMapping.computeIfNotPresent(() -> new ArrayList<>());

die null checks überall sind nicht schön, es macht vielleicht eher sinn am anfang einmal zu prüfen ob ein Eintrag vorhanden ist und wenn nicht ne leere Liste zu initialisieren.. `this.replyMapping.computeIfNotPresent(() -> new ArrayList<>());`
Pupsi marked this conversation as resolved Outdated

hier wird ganz oft this.replyMapping.get(sender) verwendet, rausziehen in eine eigene variable z.B. replyMap = this.replyMapping.get(sender)

hier wird ganz oft `this.replyMapping.get(sender)` verwendet, rausziehen in eine eigene variable z.B. `replyMap = this.replyMapping.get(sender)`
public void reply(Player sender, String message) {
this.replyMapping.computeIfAbsent(sender, player -> new ArrayList<>());
List<Conversation> replyList = this.replyMapping.get(sender);
List<Conversation> tooOldConversations = replyList.stream()
.filter(conversation -> conversation.lastSet < System.currentTimeMillis() - (conversationTimeoutMinutes*60*1000))
.toList();
replyList.removeAll(tooOldConversations);
MineTec marked this conversation as resolved Outdated

das kann noch über tooOldConversations und dort verwendet werden, auch bei dem removeAll

das kann noch über tooOldConversations und dort verwendet werden, auch bei dem removeAll
if(replyList.isEmpty()) throw new ApplianceCommand.Error("Du führst aktuell keine Konversation.");
Component privatePrefix = Component.text("[Privat] ", NamedTextColor.LIGHT_PURPLE);
Conversation youngestEntry = replyList.stream()
.max(Comparator.comparingLong(o -> o.lastSet))
.orElse(replyList.getLast());
if(message.isBlank()) {
Pupsi marked this conversation as resolved
Review

initialisierung und sofortig bedingtes überschreiben würde ich als bad practice einstufen...

Umschreiben zu

Component currentTargetComponent = currentTargetPlayer != null
    ? Main.instance().getAppliance(ChatMessages.class).getReportablePlayerName(currentTargetPlayer)
    : Component.text("niemandem.");
initialisierung und sofortig bedingtes überschreiben würde ich als bad practice einstufen... Umschreiben zu ``` Component currentTargetComponent = currentTargetPlayer != null ? Main.instance().getAppliance(ChatMessages.class).getReportablePlayerName(currentTargetPlayer) : Component.text("niemandem."); ```
Player currentTargetPlayer = Bukkit.getPlayer(youngestEntry.target());
Component currentTargetComponent = currentTargetPlayer != null
? Main.instance().getAppliance(ChatMessages.class).getReportablePlayerName(currentTargetPlayer)
: Component.text("niemandem.");
sender.sendMessage(
privatePrefix
.append(Component.text("Du schreibst aktuell mit ", NamedTextColor.GRAY))
.append(currentTargetComponent)
);
return;
}
List<Conversation> oldConversations = replyList.stream()
.filter(conversation -> conversation.lastSet < System.currentTimeMillis() - (targetChangeTimeoutSeconds*1000))
Pupsi marked this conversation as resolved Outdated

this.sendWhisper

this.sendWhisper
.toList();
if(oldConversations.contains(youngestEntry) || replyList.size() == 1) {
Player target = Bukkit.getPlayer(youngestEntry.target());
Pupsi marked this conversation as resolved Outdated

das senden einzelner messages ist problematisch...

in der theorie könnten andere messages welche parallel gesendet werden so zwischen die messages rutschen. Außerdem ist jeder sendMessage aufruf ein eigenes paket, welches an den Spieler gesendet wird.

die Aufrufe sollten zusammengezogen werden zu einem einzelnen aufruf, leerzeilen können mit .appendNewLine eingefügt werden.

Hier bietet sich ggf der ComponentBuilder an, dieser kann mit Component.text() erstellt werden.

Anschließend kann darauf mit component.append(otherComponent) gearbeitet werden. Zum schluss dann sender.sendMessage(component.build())

das senden einzelner messages ist problematisch... in der theorie könnten andere messages welche parallel gesendet werden so zwischen die messages rutschen. Außerdem ist jeder sendMessage aufruf ein eigenes paket, welches an den Spieler gesendet wird. die Aufrufe sollten zusammengezogen werden zu einem einzelnen aufruf, leerzeilen können mit `.appendNewLine` eingefügt werden. Hier bietet sich ggf der ComponentBuilder an, dieser kann mit `Component.text()` erstellt werden. Anschließend kann darauf mit `component.append(otherComponent)` gearbeitet werden. Zum schluss dann `sender.sendMessage(component.build())`
if(target == null) throw new ApplianceCommand.Error("Der Spieler " + Bukkit.getOfflinePlayer(youngestEntry.target()).getName() + " ist nicht mehr verfügbar.");
replyList.clear();
this.sendWhisper(sender, new ResolvedPmUserArguments(target, message));
return;
}
Pupsi marked this conversation as resolved Outdated

"erhalten" statt "bekommen" klingt denke ich besser

"erhalten" statt "bekommen" klingt denke ich besser
ComponentBuilder<TextComponent, TextComponent.Builder> component = Component.text();
component.append(
Pupsi marked this conversation as resolved Outdated

hier steht die gleiche logik wie in zeile 58

lässt sich das vielleicht schlau kombinieren, dass du oldConversations schon oben erstellst und die andere prüfung auf der bestehenden Liste dann ausführst?

hier steht die gleiche logik wie in zeile 58 lässt sich das vielleicht schlau kombinieren, dass du oldConversations schon oben erstellst und die andere prüfung auf der bestehenden Liste dann ausführst?
Component.newline()
.append(privatePrefix)
.append(Component.text("Das Ziel für /r hat sich bei dir in den letzten ", NamedTextColor.RED))
Pupsi marked this conversation as resolved
Review

gibt es einen Grund warum die Variable hier außerhalb allokiert wird aber nur innerhalb des if statements genutzt wird?

gibt es einen Grund warum die Variable hier außerhalb allokiert wird aber nur innerhalb des if statements genutzt wird?
.append(Component.text(String.valueOf(this.targetChangeTimeoutSeconds), NamedTextColor.RED))
.append(Component.text(" Sekunden geändert. Wer soll deine Nachricht erhalten? ", NamedTextColor.RED))
.appendNewline()
.appendNewline()
);
if(!oldConversations.isEmpty()) {
Conversation youngestOldConversation = oldConversations.stream()
.max(Comparator.comparingLong(o -> o.lastSet))
.orElse(oldConversations.getLast());
replyList.removeAll(oldConversations);
replyList.add(youngestOldConversation);
}
Pupsi marked this conversation as resolved Outdated

(ㆆ _ ㆆ)

(ㆆ _ ㆆ)
List<String> playerNames = replyList.stream()
.map(conversation -> Bukkit.getOfflinePlayer(conversation.target()).getName())
Pupsi marked this conversation as resolved Outdated

wenn du einen weg findest finalComponent nicht überschreiben zu müssen brauchst du den array quark nicht.

ComponentBuilder<TextComponent, TextComponent.Builder> component = Component.text();
                            playerNames.forEach(playerName -> component.append(
                                        Component.text("[")
                                        .append(Component.text(playerName, NamedTextColor.GOLD))
                                        .append(Component.text("]"))
                                        .clickEvent(ClickEvent.runCommand("/msg " + playerName + " " + message))
                                        .hoverEvent(HoverEvent.showText(Component.text("Klicke, um diesem Spieler zu schreiben.").color(NamedTextColor.GOLD))))
                                .append(Component.text("  ")));

                            sender.sendMessage("");
                            sender.sendMessage(component.build());
                            sender.sendMessage("");

wenn du einen weg findest finalComponent nicht überschreiben zu müssen brauchst du den array quark nicht. ``` ComponentBuilder<TextComponent, TextComponent.Builder> component = Component.text(); playerNames.forEach(playerName -> component.append( Component.text("[") .append(Component.text(playerName, NamedTextColor.GOLD)) .append(Component.text("]")) .clickEvent(ClickEvent.runCommand("/msg " + playerName + " " + message)) .hoverEvent(HoverEvent.showText(Component.text("Klicke, um diesem Spieler zu schreiben.").color(NamedTextColor.GOLD)))) .append(Component.text(" "))); sender.sendMessage(""); sender.sendMessage(component.build()); sender.sendMessage(""); ```
.distinct()
.toList();
playerNames.forEach(playerName -> component.append(
Pupsi marked this conversation as resolved Outdated

String.format

String.format
Component.text("[")
.append(Component.text(playerName, NamedTextColor.GOLD))
.append(Component.text("]"))
.clickEvent(ClickEvent.runCommand(String.format("/msg %s %s", playerName, message)))
.hoverEvent(HoverEvent.showText(Component.text("Klicke, um diesem Spieler zu schreiben.").color(NamedTextColor.GOLD))))
.append(Component.text(" "))
Pupsi marked this conversation as resolved Outdated

.appendNewline()

`.appendNewline()`
);
component.appendNewline();
sender.sendMessage(component.build());
}
Pupsi marked this conversation as resolved Outdated

klammer weg und != verwenden

klammer weg und `!=` verwenden
public void sendWhisper(Player sender, ResolvedPmUserArguments userArguments) {
Conversation newReceiverConversation = new Conversation(
sender.getUniqueId(),
System.currentTimeMillis()
);
if(this.replyMapping.get(userArguments.receiver) != null) {
List<Conversation> oldEntries = this.replyMapping.get(userArguments.receiver).stream()
.filter(conversation -> conversation.target() == sender.getUniqueId())
.toList();
this.replyMapping.get(userArguments.receiver).removeAll(oldEntries);
Pupsi marked this conversation as resolved Outdated

hier kann durchaus ein else verwendet werden beim if im zusammenhang mit dem vorhergehenden if

hier kann durchaus ein `else` verwendet werden beim if im zusammenhang mit dem vorhergehenden if
} else {
this.replyMapping.put(
userArguments.receiver,
new ArrayList<>()
);
}
this.replyMapping.get(userArguments.receiver).add(newReceiverConversation);
List<Conversation> senderConversationList = new ArrayList<>();
senderConversationList.add(
new Conversation(
userArguments.receiver.getUniqueId(),
System.currentTimeMillis()
)
);
this.replyMapping.put(
sender,
senderConversationList
);
ChatMessages chatMessages = Main.instance().getAppliance(ChatMessages.class);
Component privatePrefix = Component.text("[Privat] ", NamedTextColor.LIGHT_PURPLE);
sender.sendMessage(
MineTec marked this conversation as resolved
Review

wird in der reply methode auch aufgerufen, macht ggf sinn die chatMessages referenz als objektvariable zu halten.

wird in der reply methode auch aufgerufen, macht ggf sinn die chatMessages referenz als objektvariable zu halten.
Component.text()
.append(privatePrefix.clickEvent(ClickEvent.suggestCommand(String.format("/msg %s ", userArguments.receiver.getName()))))
.append(sender.displayName())
.append(Component.text(" zu ", NamedTextColor.GRAY))
.append(chatMessages.getReportablePlayerName(userArguments.receiver))
.append(Component.text(" > ", NamedTextColor.GRAY))
.append(Component.text(userArguments.message))
);
userArguments.receiver.sendMessage(
Component.text()
.append(privatePrefix.clickEvent(ClickEvent.suggestCommand(String.format("/msg %s ", sender.getName()))))
.append(chatMessages.getReportablePlayerName(sender))
.append(Component.text(" zu ", NamedTextColor.GRAY))
.append(userArguments.receiver.displayName())
.append(Component.text(" > ", NamedTextColor.GRAY))
.append(Component.text(userArguments.message))
);
}
public record ResolvedPmUserArguments(Player receiver, String message) {}
public ResolvedPmUserArguments resolveImplicit(String[] args) {
if(args.length < 2) throw new ApplianceCommand.Error("Es muss ein Spieler sowie eine Nachricht angegeben werden.");
List<String> arguments = List.of(args);
Player targetPlayer = Bukkit.getPlayer(arguments.getFirst());
if(targetPlayer == null) throw new ApplianceCommand.Error(String.format("Der Spieler %s konnte nicht gefunden werden.", arguments.getFirst()));
String message = arguments.stream().skip(1).collect(Collectors.joining(" "));
return new ResolvedPmUserArguments(targetPlayer, message);
}
@Override
protected @NotNull List<ApplianceCommand<?>> commands() {
return List.of(
new PrivateMessageCommand(),
new PrivateReplyCommand()
);
}
}

View File

@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.appliances.privateMessage.commands;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.privateMessage.PrivateMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class PrivateMessageCommand extends ApplianceCommand.PlayerChecked<PrivateMessage> {
public PrivateMessageCommand() {
super("msg");
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
getAppliance().sendWhisper(getPlayer(), getAppliance().resolveImplicit(args));
}
}

View File

@ -0,0 +1,18 @@
package eu.mhsl.craftattack.spawn.appliances.privateMessage.commands;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.privateMessage.PrivateMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class PrivateReplyCommand extends ApplianceCommand.PlayerChecked<PrivateMessage> {
public PrivateReplyCommand() {
super("r");
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
getAppliance().reply(getPlayer(), String.join(" ", args));
}
}

View File

@ -66,12 +66,9 @@ public class Whitelist extends Appliance {
queryAppliance(Outlawed.class).updateForcedStatus(player, timestampRelevant(user.outlawed_until)); queryAppliance(Outlawed.class).updateForcedStatus(player, timestampRelevant(user.outlawed_until));
String purePlayerName; String purePlayerName = Floodgate.isBedrock(player)
if(Floodgate.isBedrock(player)) { ? Floodgate.getBedrockPlayer(player).getUsername()
purePlayerName = Floodgate.getBedrockPlayer(player).getUsername(); : player.getName();
} else {
purePlayerName = player.getName();
}
if(!user.username.trim().equalsIgnoreCase(purePlayerName)) if(!user.username.trim().equalsIgnoreCase(purePlayerName))
throw new DisconnectInfo.Throwable( throw new DisconnectInfo.Throwable(

View File

@ -9,6 +9,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.util.logging.Level;
public class NetworkMonitor { public class NetworkMonitor {
private long previousRxBytes = 0; private long previousRxBytes = 0;
@ -78,8 +79,9 @@ public class NetworkMonitor {
String content = new String(Files.readAllBytes(Paths.get(path))); String content = new String(Files.readAllBytes(Paths.get(path)));
return Long.parseLong(content.trim()); return Long.parseLong(content.trim());
} catch(IOException e) { } catch(IOException e) {
Main.logger().log(Level.SEVERE, "Statistics are only supported on Linux! Is tablist.interface config set correctly?");
this.stop(); this.stop();
throw new RuntimeException("Failed receiving Network statistic, disabling statistics!", e); throw new RuntimeException("Failed reading network statistic", e);
} }
} }
} }

View File

@ -49,6 +49,7 @@ playerLimit:
maxPlayers: 10 maxPlayers: 10
whitelist: whitelist:
overrideIntegrityCheck: false
api: https://mhsl.eu/craftattack/api/user api: https://mhsl.eu/craftattack/api/user
tablist: tablist:

View File

@ -40,3 +40,5 @@ commands:
texturepack: texturepack:
maintanance: maintanance:
yearRank: yearRank:
msg:
r: