Fixes (/u ncolor), not allowing two of the same town colors, not broadcasting announcements if nobody is online #93
1 changed files with 12 additions and 21 deletions
|
@ -16,7 +16,10 @@ import java.util.stream.Stream;
|
||||||
|
|
||||||
@CommandClass
|
@CommandClass
|
||||||
public class HistoryCommand extends UCommandBase {
|
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
|
@Override
|
||||||
public String[] GetHelpText(String alias) {
|
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) {
|
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 -> {
|
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
|
||||||
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);
|
|
||||||
};
|
|
||||||
sender.sendMessage("§6---- Chat History ----");
|
sender.sendMessage("§6---- Chat History ----");
|
||||||
Stream<Map.Entry<Channel, LinkedList<HistoryEntry>>> stream;
|
Stream<Channel> stream;
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
stream = messages.entrySet().stream();
|
stream = Channel.getChannels().stream();
|
||||||
} else {
|
} else {
|
||||||
Optional<Channel> och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
|
Optional<Channel> och = Channel.getChannels().stream().filter(chan -> chan.ID.equalsIgnoreCase(args[0])).findAny();
|
||||||
if (!och.isPresent()) {
|
if (!och.isPresent()) {
|
||||||
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
|
sender.sendMessage("§cChannel not found. Use the ID, for example: /" + (hc == null ? "u history" : hc.GetCommandPath()) + " ooc");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
val hes = messages.get(och.get());
|
stream = Stream.of(och.get());
|
||||||
if (hes == null)
|
|
||||||
stream = Stream.empty();
|
|
||||||
else
|
|
||||||
stream = Stream.of(new HashMap.SimpleEntry<>(och.get(), hes));
|
|
||||||
}
|
}
|
||||||
AtomicBoolean sent = new AtomicBoolean();
|
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);
|
.sorted(Comparator.comparingLong(he -> he.timestamp)).toArray(HistoryEntry[]::new);
|
||||||
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
|
for (int i = Math.max(0, arr.length - 10); i < arr.length; i++) {
|
||||||
HistoryEntry e = arr[i];
|
HistoryEntry e = arr[i];
|
||||||
|
@ -84,7 +73,9 @@ public class HistoryCommand extends UCommandBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addChatMessage(ChatMessage chatMessage, Channel channel) {
|
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
|
ll.add(new HistoryEntry(System.nanoTime(), chatMessage, channel)); //Adds as last element
|
||||||
while (ll.size() > 10)
|
while (ll.size() > 10)
|
||||||
ll.remove(); //Removes the first element
|
ll.remove(); //Removes the first element
|
||||||
|
|
Loading…
Reference in a new issue