Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal Channels #206

Draft
wants to merge 1 commit into
base: 1.19
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions library/misc/signal_channel/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id("qsl.module")
}

qslModule {
name = "Quilt Signal Channel API"
moduleName = "signal_channel"
id = "quilt_signal_channel"
description = "Provides an API for publishing and subscribing to localized world events"
library = "misc"
moduleDependencies {
core {
testmodOnly("qsl_base")
testmodOnly("resource_loader")
testmodOnly("registry")
testmodOnly("lifecycle_events")
}
block {
testmodOnly("block_entity")
}
}
accessWidener()
noMixins()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.signal_channel.api;

import com.mojang.serialization.Codec;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.signal_channel.impl.NamedSignalChannelImpl;
import org.quiltmc.qsl.signal_channel.impl.UnnamedSignalChannelImpl;

import java.util.function.Consumer;

@SuppressWarnings({"unused", "UnusedReturnValue"})
public interface SignalChannel<T> {
@NotNull
static <T> SignalChannel<T> createUnnamed() {
return new UnnamedSignalChannelImpl<>();
}

@NotNull
static <T> SignalChannel<T> getOrCreateNamed(@NotNull Identifier id, @NotNull Codec<T> codec) {
return new NamedSignalChannelImpl<>(id, codec);
}

@NotNull
SubscriptionBuilder subscribe(@NotNull Consumer<T> signalConsumer);

void emitInBox(T signal, @NotNull World world, @NotNull Box box);

void emitInRange(T signal, @NotNull World world, @NotNull Vec3d origin, double range);

void emitWorldwide(T signal, @NotNull World world);

void emitGlobal(T signal, @NotNull MinecraftServer server);

interface SubscriptionBuilder {
@NotNull
Subscription boundTo(@NotNull Entity entity);

@NotNull
Subscription boundTo(@NotNull BlockEntity blockEntity);

@NotNull
Subscription atPosition(@NotNull World world, @NotNull Vec3d pos);

@NotNull
Subscription worldwide(@NotNull World world);

@NotNull
Subscription global(MinecraftServer server);
}

interface Subscription {
void cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.signal_channel.impl;

import com.mojang.serialization.Codec;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.signal_channel.api.SignalChannel;

import java.util.function.Consumer;

public class NamedSignalChannelImpl<T> implements SignalChannel<T> {
private final Identifier id;
private final Codec<T> codec;

public NamedSignalChannelImpl(Identifier id, Codec<T> codec) {
this.id = id;
this.codec = codec;
}

@Override
public @NotNull SubscriptionBuilder subscribe(@NotNull Consumer<T> signalConsumer) {
return new SubscriptionBuilderImpl.Named<>(signalConsumer, id, codec);
}

@Override
public void emitInBox(T signal, @NotNull World world, @NotNull Box box) {
WorldSubscriptionManager.managers.computeIfAbsent(
world,
WorldSubscriptionManager::new
).getNamedSubscriptionsInBox(id, box).forEach(s -> s.accept(signal, codec));
}

@Override
public void emitInRange(T signal, @NotNull World world, @NotNull Vec3d origin, double range) {
WorldSubscriptionManager.managers.computeIfAbsent(
world,
WorldSubscriptionManager::new
).getNamedSubscriptionsInRange(id, origin, range).forEach(s -> s.accept(signal, codec));
}

@Override
public void emitWorldwide(T signal, @NotNull World world) {
WorldSubscriptionManager.managers.computeIfAbsent(
world,
WorldSubscriptionManager::new
).getNamedSubscriptions(id).forEach(s -> s.accept(signal, codec));
}

@Override
public void emitGlobal(T signal, @NotNull MinecraftServer server) {
server.getWorlds().forEach(world -> emitWorldwide(signal, world));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.signal_channel.impl;

import com.mojang.serialization.Codec;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.signal_channel.api.SignalChannel;

import java.lang.ref.WeakReference;
import java.util.function.Consumer;

public sealed abstract class SubscriptionBuilderImpl<T> implements SignalChannel.SubscriptionBuilder {
protected final Consumer<T> consumer;

public SubscriptionBuilderImpl(Consumer<T> consumer) {
this.consumer = consumer;
}

protected abstract SignalChannel.Subscription buildAndAddSubscription(@Nullable SubscriptionTracker tracker, WorldSubscriptionManager manager);

@Override
@NotNull
public SignalChannel.Subscription boundTo(@NotNull Entity entity) {
return buildAndAddSubscription(
new SubscriptionTracker.EntityTracker(new WeakReference<>(entity)),
WorldSubscriptionManager.managers.computeIfAbsent(
entity.getWorld(),
WorldSubscriptionManager::new
)
);
}

@Override
@NotNull
public SignalChannel.Subscription boundTo(@NotNull BlockEntity blockEntity) {
return buildAndAddSubscription(
new SubscriptionTracker.BlockEntityTracker(new WeakReference<>(blockEntity)),
WorldSubscriptionManager.managers.computeIfAbsent(
blockEntity.getWorld(),
WorldSubscriptionManager::new
)
);
}

@Override
@NotNull
public SignalChannel.Subscription atPosition(@NotNull World world, @NotNull Vec3d pos) {
return buildAndAddSubscription(
new SubscriptionTracker.FixedPositionTracker(pos),
WorldSubscriptionManager.managers.computeIfAbsent(
world,
WorldSubscriptionManager::new
)
);
}

@Override
@NotNull
public SignalChannel.Subscription worldwide(@NotNull World world) {
return buildAndAddSubscription(
null,
WorldSubscriptionManager.managers.computeIfAbsent(
world,
WorldSubscriptionManager::new
)
);
}

@Override
@NotNull
public SignalChannel.Subscription global(MinecraftServer server) {
throw new NotImplementedException();
}

public static final class Named<T> extends SubscriptionBuilderImpl<T> {
private final Identifier id;
private final Codec<T> codec;

public Named(Consumer<T> consumer, Identifier id, Codec<T> codec) {
super(consumer);
this.id = id;
this.codec = codec;
}

@Override
protected SignalChannel.Subscription buildAndAddSubscription(@Nullable SubscriptionTracker tracker, WorldSubscriptionManager manager) {
var subscription = new SubscriptionImpl.Named<>(consumer, tracker, codec);
manager.addNamedSubscription(id, subscription);
return subscription;
}
}

public static final class Unnamed<T> extends SubscriptionBuilderImpl<T> {
private final SignalChannel<T> channel;

public Unnamed(Consumer<T> consumer, SignalChannel<T> channel) {
super(consumer);
this.channel = channel;
}

@Override
protected SignalChannel.Subscription buildAndAddSubscription(@Nullable SubscriptionTracker tracker, WorldSubscriptionManager manager) {
var subscription = new SubscriptionImpl.Unnamed<>(consumer, tracker);
manager.addUnnamedSubscription(channel, subscription);
return subscription;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2022 QuiltMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.quiltmc.qsl.signal_channel.impl;

import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.qsl.signal_channel.api.SignalChannel;

import java.util.function.Consumer;

public sealed class SubscriptionImpl<T> implements SignalChannel.Subscription {
protected final Consumer<T> consumer;
private boolean isCancelled;

@Nullable
private final SubscriptionTracker tracker;

@Nullable
public SubscriptionTracker getTracker() {
return tracker;
}

@Override
public void cancel() {
isCancelled = true;
}

public boolean shouldRemove() {
return isCancelled || (tracker != null && tracker.shouldRemove());
}

public SubscriptionImpl(@NotNull Consumer<T> consumer, @Nullable SubscriptionTracker tracker) {
this.consumer = consumer;
this.tracker = tracker;
this.isCancelled = false;
}

public static final class Unnamed<T> extends SubscriptionImpl<T> {
public void accept(T signal) {
consumer.accept(signal);
}

public Unnamed(@NotNull Consumer<T> consumer, @Nullable SubscriptionTracker tracker) {
super(consumer, tracker);
}
}

public static final class Named<T> extends SubscriptionImpl<T> {
private final Codec<T> codec;

public <S> void accept(S signal, Codec<S> sourceCodec) {
if (sourceCodec.equals(codec)) {
//don't convert direct subscriptions with same codec
//noinspection unchecked
consumer.accept((T) signal);
} else {
var convertedSignal = sourceCodec.encodeStart(JsonOps.INSTANCE, signal)
.flatMap(e -> codec.parse(JsonOps.INSTANCE, e));
//TODO there really has to be a better intermediate than JSON

consumer.accept(convertedSignal.result().get()); //TODO actual error
}
}

public Named(@NotNull Consumer<T> consumer, @Nullable SubscriptionTracker tracker, Codec<T> codec) {
super(consumer, tracker);
this.codec = codec;
}
}
}
Loading