develop-chatReply #5
@ -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.privateMessage.commands.PrivateMessageCommand;
|
||||
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.event.ClickEvent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
@ -18,37 +17,98 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PrivateMessage extends Appliance {
|
||||
public final int targetChangeTimeout = 30;
|
||||
public final int targetChangeTimeoutSeconds = 30;
|
||||
|
||||
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) {
|
||||
Conversation senderConversation = this.replyMapping.get(sender).getLast();
|
||||
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) {
|
||||
// throw new ApplianceCommand.Error("ziel geändedrt.......");
|
||||
// }
|
||||
Player target = Bukkit.getPlayer(senderConversation.target);
|
||||
if(target == null) throw new ApplianceCommand.Error("Der Spieler ist nicht mehr verfügbar.");
|
||||
sendWhisper(sender, new ResolvedPmUserArguments(target, message));
|
||||
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.");
|
||||
|
||||
Conversation youngestEntry = this.replyMapping.get(sender).stream()
|
||||
.max(Comparator.comparingLong(o -> o.lastSet))
|
||||
.orElse(this.replyMapping.get(sender).getLast());
|
||||
|
||||
if(youngestEntry.lastSet < System.currentTimeMillis() - (targetChangeTimeoutSeconds*1000) || this.replyMapping.get(sender).size() == 1) {
|
||||
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 -> {
|
||||
Pupsi marked this conversation as resolved
|
||||
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) {
|
||||
// this.replyMapping.put(
|
||||
// sender,
|
||||
// new Conversation(
|
||||
// userArguments.receiver.getUniqueId(),
|
||||
// System.currentTimeMillis()
|
||||
// )
|
||||
// );
|
||||
// this.replyMapping.put(
|
||||
// userArguments.receiver,
|
||||
// new Conversation(
|
||||
// sender.getUniqueId(),
|
||||
// System.currentTimeMillis()
|
||||
// )
|
||||
// );
|
||||
|
||||
// Ältere Einträge bei replyMapping für den receiver entfernen
|
||||
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);
|
||||
}
|
||||
|
||||
// Neuen Eintrag bei replyMapping für den receiver
|
||||
Pupsi marked this conversation as resolved
MineTec
commented
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?
|
||||
Conversation newReceiverConversation = new Conversation(
|
||||
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);
|
||||
Component privatePrefix = Component.text("[Privat] ", NamedTextColor.LIGHT_PURPLE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user
initialisierung und sofortig bedingtes überschreiben würde ich als bad practice einstufen...
Umschreiben zu