changed basic reply logic

This commit is contained in:
Lars Neuhaus 2024-09-29 22:43:55 +02:00
parent 577e99fcfc
commit 26b9fd5e0f

View File

@ -6,7 +6,6 @@ import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.appliances.chatMessages.ChatMessages; 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.PrivateMessageCommand;
import eu.mhsl.craftattack.spawn.appliances.privateMessage.commands.PrivateReplyCommand; import eu.mhsl.craftattack.spawn.appliances.privateMessage.commands.PrivateReplyCommand;
import eu.mhsl.craftattack.spawn.util.LimitedSizedList;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -18,37 +17,98 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class PrivateMessage extends Appliance { public class PrivateMessage extends Appliance {
public final int targetChangeTimeout = 30; public final int targetChangeTimeoutSeconds = 30;
private record Conversation(UUID target, Long lastSet) {} private record Conversation(UUID target, Long lastSet) {}
private final Map<Player, LimitedSizedList<Conversation>> replyMapping = new WeakHashMap<>(); private final Map<Player, List<Conversation>> replyMapping = new WeakHashMap<>();
public void reply(Player sender, String message) { public void reply(Player sender, String message) {
Conversation senderConversation = this.replyMapping.get(sender).getLast(); if(this.replyMapping.get(sender) == null || this.replyMapping.get(sender).isEmpty()) throw new ApplianceCommand.Error("Du kannst nicht auf eine Konversation antworten, da keine vorhanden ist.");
if(senderConversation == null) throw new ApplianceCommand.Error("Du kannst nicht auf eine konversation antworten, da keine vorhanden ist.");
// if(!senderConversation.isChecked && senderConversation.lastSet > System.currentTimeMillis() - targetChangeTimeout) { Conversation youngestEntry = this.replyMapping.get(sender).stream()
// throw new ApplianceCommand.Error("ziel geändedrt......."); .max(Comparator.comparingLong(o -> o.lastSet))
// } .orElse(this.replyMapping.get(sender).getLast());
Player target = Bukkit.getPlayer(senderConversation.target);
if(target == null) throw new ApplianceCommand.Error("Der Spieler ist nicht mehr verfügbar."); if(youngestEntry.lastSet < System.currentTimeMillis() - (targetChangeTimeoutSeconds*1000) || this.replyMapping.get(sender).size() == 1) {
sendWhisper(sender, new ResolvedPmUserArguments(target, message)); Player target = Bukkit.getPlayer(youngestEntry.target());
if(target == null) throw new ApplianceCommand.Error("Der Spieler " + Bukkit.getOfflinePlayer(youngestEntry.target()).getName() + " ist nicht mehr verfügbar.");
this.replyMapping.get(sender).clear();
sendWhisper(sender, new ResolvedPmUserArguments(target, message));
return;
}
List<Conversation> oldConversations = this.replyMapping.get(sender).stream()
.filter(conversation -> conversation.lastSet < System.currentTimeMillis() - (targetChangeTimeoutSeconds*1000))
.toList();
this.replyMapping.get(sender).removeAll(oldConversations);
List<String> playerNames = this.replyMapping.get(sender).stream()
.map(conversation -> {
if(Bukkit.getPlayer(conversation.target()) != null) {
return Objects.requireNonNull(Bukkit.getPlayer(conversation.target())).getName();
} else {
return Bukkit.getOfflinePlayer(conversation.target()).getName();
}
})
.toList();
sender.sendMessage(
Component.text()
.append(Component.text("Das Ziel für /r hat sich bei dir in den letzten "))
.append(Component.text(String.valueOf(this.targetChangeTimeoutSeconds)))
.append(Component.text(" Sekunden geändert. Wer soll deine Nachricht bekommen? "))
);
// TODO: add options to click on with the players names
// sender.sendMessage(
// Component.text()
// .append(Component.text())
// );
} }
public void sendWhisper(Player sender, ResolvedPmUserArguments userArguments) { public void sendWhisper(Player sender, ResolvedPmUserArguments userArguments) {
// this.replyMapping.put(
// sender, // Ältere Einträge bei replyMapping für den receiver entfernen
// new Conversation( if(!(this.replyMapping.get(userArguments.receiver) == null)) {
// userArguments.receiver.getUniqueId(), List<Conversation> oldEntries = this.replyMapping.get(userArguments.receiver).stream()
// System.currentTimeMillis() .filter(conversation -> conversation.target() == sender.getUniqueId())
// ) .toList();
// ); this.replyMapping.get(userArguments.receiver).removeAll(oldEntries);
// this.replyMapping.put( }
// userArguments.receiver,
// new Conversation( // Neuen Eintrag bei replyMapping für den receiver
// sender.getUniqueId(), Conversation newReceiverConversation = new Conversation(
// System.currentTimeMillis() sender.getUniqueId(),
// ) System.currentTimeMillis()
// ); );
if(this.replyMapping.get(userArguments.receiver) == null) {
List<Conversation> receiverConversationList = new ArrayList<>();
receiverConversationList.add(newReceiverConversation);
this.replyMapping.put(
userArguments.receiver,
receiverConversationList
);
} else {
this.replyMapping.get(userArguments.receiver).add(newReceiverConversation);
}
// Einträge für sender bei replyMapping überschreiben mit receiver
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); ChatMessages chatMessages = Main.instance().getAppliance(ChatMessages.class);
Component privatePrefix = Component.text("[Privat] ", NamedTextColor.LIGHT_PURPLE); Component privatePrefix = Component.text("[Privat] ", NamedTextColor.LIGHT_PURPLE);