| 1 | package net.minecraft.world.level.timers;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.HashBasedTable;
|
| 5 | import com.google.common.collect.Table;
|
| 6 | import com.google.common.primitives.UnsignedLong;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.Collections;
|
| 11 | import java.util.Comparator;
|
| 12 | import java.util.List;
|
| 13 | import java.util.PriorityQueue;
|
| 14 | import java.util.Queue;
|
| 15 | import java.util.Set;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.server.MinecraftServer;
|
| 18 | import net.minecraft.util.datafix.DataFixTypes;
|
| 19 | import net.minecraft.world.level.saveddata.SavedData;
|
| 20 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 21 |
|
| 22 | public class TimerQueue<T> extends SavedData {
|
| 23 | public static final Codec<TimerQueue<MinecraftServer>> CODEC = codec(TimerCallbacks.SERVER_CALLBACKS);
|
| 24 | public static final SavedDataType<TimerQueue<MinecraftServer>> TYPE = new SavedDataType<>(
|
| 25 | Identifier.withDefaultNamespace("scheduled_events"), TimerQueue::new, CODEC, DataFixTypes.SAVED_DATA_SCHEDULED_EVENTS
|
| 26 | );
|
| 27 | private final Queue<TimerQueue.Event<T>> queue = new PriorityQueue<>(createComparator());
|
| 28 | private UnsignedLong sequentialId = UnsignedLong.ZERO;
|
| 29 | private final Table<String, Long, TimerQueue.Event<T>> events = HashBasedTable.create();
|
| 30 |
|
| 31 | @VisibleForTesting
|
| 32 | protected static <T> Codec<TimerQueue<T>> codec(final TimerCallbacks<T> callbacks) {
|
| 33 | return TimerQueue.Packed.codec(callbacks.codec()).xmap(TimerQueue::new, TimerQueue::pack);
|
| 34 | }
|
| 35 |
|
| 36 | private static <T> Comparator<TimerQueue.Event<T>> createComparator() {
|
| 37 | return Comparator.<TimerQueue.Event<T>>comparingLong(l -> l.triggerTime).thenComparing(l -> l.sequentialId);
|
| 38 | }
|
| 39 |
|
| 40 | public TimerQueue(final TimerQueue.Packed<T> packedEvents) {
|
| 41 | this();
|
| 42 | this.queue.clear();
|
| 43 | this.events.clear();
|
| 44 | this.sequentialId = UnsignedLong.ZERO;
|
| 45 | packedEvents.events.forEach(event -> this.schedule(event.id, event.triggerTime, event.callback));
|
| 46 | }
|
| 47 |
|
| 48 | public TimerQueue() {
|
| 49 | }
|
| 50 |
|
| 51 | public void tick(final T context, final long currentTick) {
|
| 52 | while (true) {
|
| 53 | TimerQueue.Event<T> event = this.queue.peek();
|
| 54 | if (event == null || event.triggerTime > currentTick) {
|
| 55 | return;
|
| 56 | }
|
| 57 |
|
| 58 | this.queue.remove();
|
| 59 | this.events.remove(event.id, currentTick);
|
| 60 | this.setDirty();
|
| 61 | event.callback.handle(context, this, currentTick);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | public void schedule(final String id, final long time, final TimerCallback<T> callback) {
|
| 66 | if (!this.events.contains(id, time)) {
|
| 67 | this.sequentialId = this.sequentialId.plus(UnsignedLong.ONE);
|
| 68 | TimerQueue.Event<T> newEvent = new TimerQueue.Event<>(time, this.sequentialId, id, callback);
|
| 69 | this.events.put(id, time, newEvent);
|
| 70 | this.queue.add(newEvent);
|
| 71 | this.setDirty();
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public int remove(final String id) {
|
| 76 | Collection<TimerQueue.Event<T>> eventsToRemove = this.events.row(id).values();
|
| 77 | eventsToRemove.forEach(this.queue::remove);
|
| 78 | int size = eventsToRemove.size();
|
| 79 | eventsToRemove.clear();
|
| 80 | this.setDirty();
|
| 81 | return size;
|
| 82 | }
|
| 83 |
|
| 84 | public Set<String> getEventsIds() {
|
| 85 | return Collections.unmodifiableSet(this.events.rowKeySet());
|
| 86 | }
|
| 87 |
|
| 88 | @VisibleForTesting
|
| 89 | protected TimerQueue.Packed<T> pack() {
|
| 90 | return new TimerQueue.Packed<>(
|
| 91 | this.queue.stream().sorted(createComparator()).map(event -> new TimerQueue.Event.Packed<>(event.triggerTime, event.id, event.callback)).toList()
|
| 92 | );
|
| 93 | }
|
| 94 |
|
| 95 | public record Event<T>(long triggerTime, UnsignedLong sequentialId, String id, TimerCallback<T> callback) {
|
| 96 | public record Packed<T>(long triggerTime, String id, TimerCallback<T> callback) {
|
| 97 | public static <T> Codec<TimerQueue.Event.Packed<T>> codec(final Codec<TimerCallback<T>> callbackCodec) {
|
| 98 | return RecordCodecBuilder.create(
|
| 99 | i -> i.group(
|
| 100 | Codec.LONG.fieldOf("trigger_time").forGetter(TimerQueue.Event.Packed::triggerTime),
|
| 101 | Codec.STRING.fieldOf("id").forGetter(TimerQueue.Event.Packed::id),
|
| 102 | callbackCodec.fieldOf("callback").forGetter(TimerQueue.Event.Packed::callback)
|
| 103 | )
|
| 104 | .apply(i, TimerQueue.Event.Packed::new)
|
| 105 | );
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | public record Packed<T>(List<TimerQueue.Event.Packed<T>> events) {
|
| 111 | public static <T> Codec<TimerQueue.Packed<T>> codec(final Codec<TimerCallback<T>> callbackCodec) {
|
| 112 | return RecordCodecBuilder.create(
|
| 113 | i -> i.group(TimerQueue.Event.Packed.codec(callbackCodec).listOf().fieldOf("events").forGetter(TimerQueue.Packed::events)).apply(i, TimerQueue.Packed::new)
|
| 114 | );
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|