|
| 1 | +/* |
| 2 | + * Copyright 2022 The Quilt Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.fabricmc.fabric.impl.base.event; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import net.minecraft.util.Identifier; |
| 23 | + |
| 24 | +import net.fabricmc.fabric.api.event.Event; |
| 25 | + |
| 26 | +/** |
| 27 | + * Allows for the translation of events from Quilt to Fabric. |
| 28 | + */ |
| 29 | +public final class QuiltCompatEvent { |
| 30 | + private QuiltCompatEvent() { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new Fabric event that delegates to a Quilt Event. |
| 35 | + * |
| 36 | + * @param event the Quilt event |
| 37 | + * @param listenerConverter a function that converts a Fabric listener to a Quilt listener |
| 38 | + * @param invoker a function that converts a Quilt listener to a Fabric listener |
| 39 | + * @param <S> the Quilt event listener type |
| 40 | + * @param <D> the Fabric event listener type |
| 41 | + * @return a Fabric event wrapping the Quilt event |
| 42 | + */ |
| 43 | + public static <S, D> Event<D> fromQuilt(org.quiltmc.qsl.base.api.event.Event<S> event, |
| 44 | + Function<D, S> listenerConverter, Function<Supplier<S>, D> invoker) { |
| 45 | + return new QuiltEvent<>(event, listenerConverter, invoker); |
| 46 | + } |
| 47 | + |
| 48 | + public static final class QuiltEvent<S, D> extends Event<D> { |
| 49 | + private final org.quiltmc.qsl.base.api.event.Event<S> event; |
| 50 | + private final Function<D, S> listenerConverter; |
| 51 | + |
| 52 | + public QuiltEvent(org.quiltmc.qsl.base.api.event.Event<S> event, Function<D, S> listenerConverter, Function<Supplier<S>, D> invoker) { |
| 53 | + this.event = event; |
| 54 | + this.listenerConverter = listenerConverter; |
| 55 | + this.invoker = invoker.apply(event::invoker); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void register(D listener) { |
| 60 | + this.event.register(this.listenerConverter.apply(listener)); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void register(Identifier phase, D listener) { |
| 65 | + this.event.register(quiltifyPhases(phase), this.listenerConverter.apply(listener)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase) { |
| 70 | + this.event.addPhaseOrdering(quiltifyPhases(firstPhase), quiltifyPhases(secondPhase)); |
| 71 | + } |
| 72 | + |
| 73 | + // This makes Fabric's default phase equal to the Quilt one, preventing ordering issues |
| 74 | + private static Identifier quiltifyPhases(Identifier phase) { |
| 75 | + return phase.equals(DEFAULT_PHASE) ? org.quiltmc.qsl.base.api.event.Event.DEFAULT_PHASE : phase; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments