File size: 3,695 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package net.minecraft.util.debugchart;

import com.google.common.collect.Maps;
import java.util.EnumMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Map.Entry;
import net.minecraft.Util;
import net.minecraft.network.protocol.game.ClientboundDebugSamplePacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;

public class DebugSampleSubscriptionTracker {
    public static final int STOP_SENDING_AFTER_TICKS = 200;
    public static final int STOP_SENDING_AFTER_MS = 10000;
    private final PlayerList playerList;
    private final EnumMap<RemoteDebugSampleType, Map<ServerPlayer, DebugSampleSubscriptionTracker.SubscriptionStartedAt>> subscriptions;
    private final Queue<DebugSampleSubscriptionTracker.SubscriptionRequest> subscriptionRequestQueue = new LinkedList<>();

    public DebugSampleSubscriptionTracker(PlayerList p_332100_) {
        this.playerList = p_332100_;
        this.subscriptions = new EnumMap<>(RemoteDebugSampleType.class);

        for (RemoteDebugSampleType remotedebugsampletype : RemoteDebugSampleType.values()) {
            this.subscriptions.put(remotedebugsampletype, Maps.newHashMap());
        }
    }

    public boolean shouldLogSamples(RemoteDebugSampleType p_328402_) {
        return !this.subscriptions.get(p_328402_).isEmpty();
    }

    public void broadcast(ClientboundDebugSamplePacket p_331964_) {
        for (ServerPlayer serverplayer : this.subscriptions.get(p_331964_.debugSampleType()).keySet()) {
            serverplayer.connection.send(p_331964_);
        }
    }

    public void subscribe(ServerPlayer p_328157_, RemoteDebugSampleType p_336058_) {
        if (this.playerList.isOp(p_328157_.getGameProfile())) {
            this.subscriptionRequestQueue.add(new DebugSampleSubscriptionTracker.SubscriptionRequest(p_328157_, p_336058_));
        }
    }

    public void tick(int p_335345_) {
        long i = Util.getMillis();
        this.handleSubscriptions(i, p_335345_);
        this.handleUnsubscriptions(i, p_335345_);
    }

    private void handleSubscriptions(long p_331878_, int p_331066_) {
        for (DebugSampleSubscriptionTracker.SubscriptionRequest debugsamplesubscriptiontracker$subscriptionrequest : this.subscriptionRequestQueue) {
            this.subscriptions
                .get(debugsamplesubscriptiontracker$subscriptionrequest.sampleType())
                .put(
                    debugsamplesubscriptiontracker$subscriptionrequest.player(),
                    new DebugSampleSubscriptionTracker.SubscriptionStartedAt(p_331878_, p_331066_)
                );
        }
    }

    private void handleUnsubscriptions(long p_335801_, int p_335929_) {
        for (Map<ServerPlayer, DebugSampleSubscriptionTracker.SubscriptionStartedAt> map : this.subscriptions.values()) {
            map.entrySet()
                .removeIf(
                    p_336353_ -> {
                        boolean flag = !this.playerList.isOp(p_336353_.getKey().getGameProfile());
                        DebugSampleSubscriptionTracker.SubscriptionStartedAt debugsamplesubscriptiontracker$subscriptionstartedat = p_336353_.getValue();
                        return flag
                            || p_335929_ > debugsamplesubscriptiontracker$subscriptionstartedat.tick() + 200
                                && p_335801_ > debugsamplesubscriptiontracker$subscriptionstartedat.millis() + 10000L;
                    }
                );
        }
    }

    static record SubscriptionRequest(ServerPlayer player, RemoteDebugSampleType sampleType) {
    }

    static record SubscriptionStartedAt(long millis, int tick) {
    }
}