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
This commit is contained in:
parent
1081f3cf9d
commit
12072f3106
1 changed files with 12 additions and 21 deletions
|
@ -16,7 +16,10 @@ import java.util.stream.Stream;
|
|||
|
||||
@CommandClass
|
||||
public class HistoryCommand extends UCommandBase {
|
||||
private static HashMap<Channel, LinkedList<HistoryEntry>> messages = new HashMap<>();
|
||||
/**
|
||||
* Key: ChannelID_groupID
|
||||
*/
|
||||
private static HashMap<String, LinkedList<HistoryEntry>> 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<Channel, LinkedList<HistoryEntry>>, Map.Entry<Channel, LinkedList<HistoryEntry>>> filterThem = e -> {
|
||||
int score = e.getKey().getMCScore(sender);
|
||||
LinkedList<HistoryEntry> 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<Channel, LinkedList<HistoryEntry>> 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<Map.Entry<Channel, LinkedList<HistoryEntry>>> stream;
|
||||
Stream<Channel> stream;
|
||||
if (args.length == 0) {
|
||||
stream = messages.entrySet().stream();
|
||||
stream = Channel.getChannels().stream();
|
||||
} else {
|
||||
Optional<Channel> 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
|
||||
|
|
Loading…
Reference in a new issue