From 12072f310650f079926a54c71b4565302d5ca01f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 27 Dec 2018 14:16:40 +0100 Subject: [PATCH] 9 less lines for what works better Storing the entries per-group which also means there's no need to loop through each entry - and it won't limit it globally --- .../chat/commands/ucmds/HistoryCommand.java | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java index 8729986..e39600a 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/HistoryCommand.java @@ -16,7 +16,10 @@ import java.util.stream.Stream; @CommandClass public class HistoryCommand extends UCommandBase { - private static HashMap> messages = new HashMap<>(); + /** + * Key: ChannelID_groupID + */ + private static HashMap> messages = new HashMap<>(); @Override public String[] GetHelpText(String alias) { @@ -32,35 +35,21 @@ public class HistoryCommand extends UCommandBase { } public static boolean showHistory(CommandSender sender, String alias, String[] args, @Nullable HistoryCommand hc) { - Function>, Map.Entry>> filterThem = e -> { - int score = e.getKey().getMCScore(sender); - LinkedList he = new LinkedList<>(); - for (int i = 0; i < 10 && i < e.getValue().size(); i++) { - val heh = e.getValue().get(i); - val cm = heh.chatMessage; - if (score == e.getKey().getMCScore(cm.getPermCheck())) - he.push(heh); - } - return new HashMap.SimpleEntry<>(e.getKey(), he); - }; + Function> getThem = ch -> messages.get(ch.ID + "_" + ch.getGroupID(sender)); //If can't see, groupID is null, and that shouldn't be in the map sender.sendMessage("§6---- Chat History ----"); - Stream>> stream; + Stream stream; if (args.length == 0) { - stream = messages.entrySet().stream(); + stream = Channel.getChannels().stream(); } else { Optional och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny(); if (!och.isPresent()) { sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc"); return true; } - val hes = messages.get(och.get()); - if (hes == null) - stream = Stream.empty(); - else - stream = Stream.of(new HashMap.SimpleEntry<>(och.get(), hes)); + stream = Stream.of(och.get()); } AtomicBoolean sent = new AtomicBoolean(); - val arr = stream.map(filterThem).flatMap(e -> e.getValue().stream()) + val arr = stream.map(getThem).filter(Objects::nonNull).flatMap(Collection::stream) .sorted(Comparator.comparingLong(he -> he.timestamp)).toArray(HistoryEntry[]::new); for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) { HistoryEntry e = arr[i]; @@ -84,7 +73,9 @@ public class HistoryCommand extends UCommandBase { } public static void addChatMessage(ChatMessage chatMessage, Channel channel) { - var ll = messages.computeIfAbsent(channel, k -> new LinkedList<>()); //<-- TIL + val groupID = channel.getGroupID(chatMessage.getPermCheck()); + if (groupID == null) return; //Just to be sure + var ll = messages.computeIfAbsent(channel.ID + "_" + groupID, k -> new LinkedList<>()); //<-- TIL ll.add(new HistoryEntry(System.nanoTime(), chatMessage, channel)); //Adds as last element while (ll.size() > 10) ll.remove(); //Removes the first element