From f8539a03922b1a8c0189b5ab1c7f85812cf28106 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 26 Dec 2018 03:03:51 +0100 Subject: [PATCH] Adding chat history command #82 --- .../chat/commands/ucmds/HistoryCommand.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java new file mode 100644 index 0000000..c32401b --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java @@ -0,0 +1,74 @@ +package buttondevteam.chat.commands.ucmds; + +import buttondevteam.lib.chat.Channel; +import buttondevteam.lib.chat.ChatMessage; +import buttondevteam.lib.chat.CommandClass; +import lombok.RequiredArgsConstructor; +import lombok.val; +import org.bukkit.command.CommandSender; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Function; +import java.util.stream.Stream; + +@CommandClass +public class HistoryCommand extends UCommandBase { + private static HashMap messages = new HashMap<>(); + + @Override + public String[] GetHelpText(String alias) { + return new String[]{ // + "§6--- Chat History ----", // + "Returns the last 10 messages the player can see." // + }; + } + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + Function, Map.Entry> filterThem = e -> { + int score = e.getKey().getMCScore(sender); + HistoryEntry[] he = new HistoryEntry[10]; + for (int i = 0, j = 0; i < 10; i++) { + val cm = e.getValue()[i].chatMessage; + if (cm == null) + break; //Don't have 10 messages yet + if (score == e.getKey().getMCScore(cm.getPermCheck())) + he[j++] = e.getValue()[i]; + } + return new HashMap.SimpleEntry<>(e.getKey(), he); + }; + sender.sendMessage("§6---- Chat History ----"); + Stream stream; + if (args.length == 0) { + stream = messages.entrySet().stream().map(filterThem).flatMap(e -> Arrays.stream(e.getValue())); + } else { + Channel ch = Channel.GlobalChat; //TODO: Channel param + val hes = messages.get(ch); + if (hes == null) + return true; + stream = Arrays.stream(hes); + } + AtomicBoolean sent = new AtomicBoolean(); + stream.sorted(Comparator.comparingLong(he -> he.timestamp)).forEach(e -> { + val cm = e.chatMessage; + sender.sendMessage(cm.getSender().getName() + ": " + cm.getMessage()); + sent.set(true); + }); + if (!sent.get()) + sender.sendMessage("No messages can be found."); + return true; + } + + @RequiredArgsConstructor + public static class HistoryEntry { + /** + * System.nanoTime() + */ + private final long timestamp; + private final ChatMessage chatMessage; + } +}