| 1 | package net.minecraft.client.multiplayer.chat.report;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntCollection;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntRBTreeSet;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntSortedSet;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Set;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.multiplayer.chat.ChatLog;
|
| 14 | import net.minecraft.client.multiplayer.chat.LoggedChatMessage;
|
| 15 | import net.minecraft.network.chat.MessageSignature;
|
| 16 | import net.minecraft.network.chat.PlayerChatMessage;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class ChatReportContextBuilder {
|
| 20 | private final int leadingCount;
|
| 21 | private final List<ChatReportContextBuilder.Collector> activeCollectors = new ArrayList<>();
|
| 22 |
|
| 23 | public ChatReportContextBuilder(final int leadingCount) {
|
| 24 | this.leadingCount = leadingCount;
|
| 25 | }
|
| 26 |
|
| 27 | public void collectAllContext(final ChatLog chatLog, final IntCollection roots, final ChatReportContextBuilder.Handler handler) {
|
| 28 | IntSortedSet uncollectedRoots = new IntRBTreeSet(roots);
|
| 29 |
|
| 30 | for (int id = uncollectedRoots.lastInt(); id >= chatLog.start() && (this.isActive() || !uncollectedRoots.isEmpty()); id--) {
|
| 31 | if (chatLog.lookup(id) instanceof LoggedChatMessage.Player event) {
|
| 32 | boolean context = this.acceptContext(event.message());
|
| 33 | if (uncollectedRoots.remove(id)) {
|
| 34 | this.trackContext(event.message());
|
| 35 | handler.accept(id, event);
|
| 36 | } else if (context) {
|
| 37 | handler.accept(id, event);
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public void trackContext(final PlayerChatMessage message) {
|
| 44 | this.activeCollectors.add(new ChatReportContextBuilder.Collector(message));
|
| 45 | }
|
| 46 |
|
| 47 | public boolean acceptContext(final PlayerChatMessage message) {
|
| 48 | boolean collected = false;
|
| 49 | Iterator<ChatReportContextBuilder.Collector> iterator = this.activeCollectors.iterator();
|
| 50 |
|
| 51 | while (iterator.hasNext()) {
|
| 52 | ChatReportContextBuilder.Collector collector = iterator.next();
|
| 53 | if (collector.accept(message)) {
|
| 54 | collected = true;
|
| 55 | if (collector.isComplete()) {
|
| 56 | iterator.remove();
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | return collected;
|
| 62 | }
|
| 63 |
|
| 64 | public boolean isActive() {
|
| 65 | return !this.activeCollectors.isEmpty();
|
| 66 | }
|
| 67 |
|
| 68 | @Environment(EnvType.CLIENT)
|
| 69 | private class Collector {
|
| 70 | private final Set<MessageSignature> lastSeenSignatures;
|
| 71 | private PlayerChatMessage lastChainMessage;
|
| 72 | private boolean collectingChain = true;
|
| 73 | private int count;
|
| 74 |
|
| 75 | private Collector(final PlayerChatMessage fromMessage) {
|
| 76 | this.lastSeenSignatures = new ObjectOpenHashSet<>(fromMessage.signedBody().lastSeen().entries());
|
| 77 | this.lastChainMessage = fromMessage;
|
| 78 | }
|
| 79 |
|
| 80 | private boolean accept(final PlayerChatMessage message) {
|
| 81 | if (message.equals(this.lastChainMessage)) {
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | boolean selected = this.lastSeenSignatures.remove(message.signature());
|
| 86 | if (this.collectingChain && this.lastChainMessage.sender().equals(message.sender())) {
|
| 87 | if (this.lastChainMessage.link().isDescendantOf(message.link())) {
|
| 88 | selected = true;
|
| 89 | this.lastChainMessage = message;
|
| 90 | } else {
|
| 91 | this.collectingChain = false;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | if (selected) {
|
| 96 | this.count++;
|
| 97 | }
|
| 98 |
|
| 99 | return selected;
|
| 100 | }
|
| 101 |
|
| 102 | private boolean isComplete() {
|
| 103 | return this.count >= ChatReportContextBuilder.this.leadingCount || !this.collectingChain && this.lastSeenSignatures.isEmpty();
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Environment(EnvType.CLIENT)
|
| 108 | public interface Handler {
|
| 109 | void accept(int id, LoggedChatMessage.Player event);
|
| 110 | }
|
| 111 | }
|
| 112 |
|