| 1 | package net.minecraft.client.sounds;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.blaze3d.audio.Channel;
|
| 5 | import com.mojang.blaze3d.audio.Library;
|
| 6 | import java.util.Iterator;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.concurrent.CompletableFuture;
|
| 10 | import java.util.concurrent.Executor;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class ChannelAccess {
|
| 19 | private final Set<ChannelAccess.ChannelHandle> channels = Sets.newIdentityHashSet();
|
| 20 | private final Library library;
|
| 21 | private final Executor executor;
|
| 22 |
|
| 23 | public ChannelAccess(final Library library, final Executor executor) {
|
| 24 | this.library = library;
|
| 25 | this.executor = executor;
|
| 26 | }
|
| 27 |
|
| 28 | public CompletableFuture<ChannelAccess.ChannelHandle> createHandle(final Library.Pool pool) {
|
| 29 | CompletableFuture<ChannelAccess.ChannelHandle> result = new CompletableFuture<>();
|
| 30 | this.executor.execute(() -> {
|
| 31 | Channel channel = this.library.acquireChannel(pool);
|
| 32 | if (channel != null) {
|
| 33 | ChannelAccess.ChannelHandle handle = new ChannelAccess.ChannelHandle(channel);
|
| 34 | this.channels.add(handle);
|
| 35 | result.complete(handle);
|
| 36 | } else {
|
| 37 | result.complete(null);
|
| 38 | }
|
| 39 | });
|
| 40 | return result;
|
| 41 | }
|
| 42 |
|
| 43 | public void executeOnChannels(final Consumer<Stream<Channel>> action) {
|
| 44 | this.executor.execute(() -> action.accept(this.channels.stream().map(channelHandle -> channelHandle.channel).filter(Objects::nonNull)));
|
| 45 | }
|
| 46 |
|
| 47 | public void scheduleTick() {
|
| 48 | this.executor.execute(() -> {
|
| 49 | Iterator<ChannelAccess.ChannelHandle> it = this.channels.iterator();
|
| 50 |
|
| 51 | while (it.hasNext()) {
|
| 52 | ChannelAccess.ChannelHandle handle = it.next();
|
| 53 | handle.channel.updateStream();
|
| 54 | if (handle.channel.stopped()) {
|
| 55 | handle.release();
|
| 56 | it.remove();
|
| 57 | }
|
| 58 | }
|
| 59 | });
|
| 60 | }
|
| 61 |
|
| 62 | public void clear() {
|
| 63 | this.channels.forEach(ChannelAccess.ChannelHandle::release);
|
| 64 | this.channels.clear();
|
| 65 | }
|
| 66 |
|
| 67 | @Environment(EnvType.CLIENT)
|
| 68 | public class ChannelHandle {
|
| 69 | private @Nullable Channel channel;
|
| 70 | private boolean stopped;
|
| 71 |
|
| 72 | public boolean isStopped() {
|
| 73 | return this.stopped;
|
| 74 | }
|
| 75 |
|
| 76 | public ChannelHandle(final Channel channel) {
|
| 77 | this.channel = channel;
|
| 78 | }
|
| 79 |
|
| 80 | public void execute(final Consumer<Channel> action) {
|
| 81 | ChannelAccess.this.executor.execute(() -> {
|
| 82 | if (this.channel != null) {
|
| 83 | action.accept(this.channel);
|
| 84 | }
|
| 85 | });
|
| 86 | }
|
| 87 |
|
| 88 | public void release() {
|
| 89 | this.stopped = true;
|
| 90 | ChannelAccess.this.library.releaseChannel(this.channel);
|
| 91 | this.channel = null;
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|