Skip to content

Commit

Permalink
protocol: add neo variant
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Jun 17, 2024
1 parent f32fbcd commit 5d6af9a
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 35 deletions.
10 changes: 5 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ plugins {
alias(libs.plugins.loom) apply false
alias(libs.plugins.spotless)
alias(libs.plugins.indra.spotlessLicenser) apply false
alias(libs.plugins.javaEcosystemCapabilities) apply false
}

allprojects {
Expand All @@ -18,6 +17,7 @@ allprojects {
// - https://maven.enginehub.org/repo/
// - https://maven.terraformersmc.com/releases/
// - https://maven.minecraftforge.net/
// - https://maven.neoforged.net/
// - https://maven.parchmentmc.org/
// - https://repo.viaversion.com/
maven(url = "https://repo.stellardrift.ca/repository/stable/") {
Expand All @@ -38,7 +38,6 @@ subprojects {
apply(plugin = "java")
apply(plugin = "com.diffplug.spotless")
apply(plugin = "net.kyori.indra.licenser.spotless")
apply(plugin = "org.gradlex.java-ecosystem-capabilities")

val targetJavaVersion: String by project
val targetVersion = targetJavaVersion.toInt()
Expand All @@ -60,16 +59,17 @@ subprojects {
tasks.named("processResources", ProcessResources::class).configure {
inputs.property("version", project.version)

filesMatching("fabric.mod.json") {
expand("version" to project.version)
sequenceOf("fabric.mod.json", "META-INF/neoforge.mods.toml").forEach {
filesMatching(it) {
expand("version" to project.version)
}
}
}

extensions.configure(IndraSpotlessLicenserExtension::class) {
licenseHeaderFile(rootProject.file("HEADER"))
}


plugins.withId("dev.architectury.loom") {
val loom = extensions.getByType(LoomGradleExtensionAPI::class)
loom.run {
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parchment = { module = "org.parchmentmc.data:parchment-1.20.6", version = "2024.
fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabricLoader" }
fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fabricApi" }
modmenu = { module = "com.terraformersmc:modmenu", version.ref = "modmenu" }
neoforge = { module = "net.neoforged:neoforge", version = "20.6.119"}
viafabricplus-api = { module = "de.florianmichael:ViaFabricPlus", version.ref = "viafabricplus" }
viaversion = { module = "com.viaversion:viaversion-common", version = "5.0.0-SNAPSHOT" }
vineflower = { module = "org.vineflower:vineflower", version.ref = "vineflower" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
*/
public class WorldEditCUI
{
public static final int PROTOCOL_VERSION = 4;

private final Map<UUID, Region> regions = new LinkedHashMap<>();
private Region selection, activeRegion;
private CUIDebug debugger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import org.enginehub.worldeditcui.WorldEditCUI;
import org.enginehub.worldeditcui.event.CUIEventArgs;
import org.enginehub.worldeditcui.protocol.CUIEventPayload;
import org.enginehub.worldeditcui.protocol.CUIPacket;

/**
* Listener class for incoming plugin channel messages
Expand All @@ -29,7 +29,7 @@ public CUIListenerChannel(WorldEditCUI controller)
this.controller = controller;
}

public void onMessage(final CUIEventPayload message)
public void onMessage(final CUIPacket message)
{
this.controller.getDebugger().debug("Received CUI event from server: " + message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import org.enginehub.worldeditcui.protocol.CUIEventPayload;
import org.enginehub.worldeditcui.protocol.CUIPacket;
import org.enginehub.worldeditcui.protocol.CUIPacketHandler;

import java.util.function.BiConsumer;

/**
* Networking wrappers to integrate nicely with MultiConnect.
Expand All @@ -27,12 +30,12 @@ final class CUINetworking {
private CUINetworking() {
}

public static void send(final CUIEventPayload pkt) {
public static void send(final CUIPacket pkt) {
ClientPlayNetworking.send(pkt);
}

public static void subscribeToCuiPacket(final ClientPlayNetworking.PlayPayloadHandler<CUIEventPayload> handler) {
ClientPlayNetworking.registerGlobalReceiver(CUIEventPayload.TYPE, handler);
public static void subscribeToCuiPacket(final BiConsumer<CUIPacket, CUIPacketHandler.PacketContext> handler) {
CUIPacketHandler.instance().registerClientboundHandler(handler);
if (VIAFABRICPLUS_AVAILABLE) {
ViaFabricPlusHook.enable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
Expand All @@ -30,7 +29,8 @@
import org.enginehub.worldeditcui.event.listeners.CUIListenerChannel;
import org.enginehub.worldeditcui.event.listeners.CUIListenerWorldRender;
import org.enginehub.worldeditcui.fabric.mixins.MinecraftAccess;
import org.enginehub.worldeditcui.protocol.CUIEventPayload;
import org.enginehub.worldeditcui.protocol.CUIPacket;
import org.enginehub.worldeditcui.protocol.CUIPacketHandler;
import org.enginehub.worldeditcui.render.OptifinePipelineProvider;
import org.enginehub.worldeditcui.render.PipelineProvider;
import org.enginehub.worldeditcui.render.VanillaPipelineProvider;
Expand Down Expand Up @@ -172,9 +172,9 @@ private void onTick(final Minecraft mc) {
}
}

private void onPluginMessage(final CUIEventPayload payload, final ClientPlayNetworking.Context ctx) {
private void onPluginMessage(final CUIPacket payload, final CUIPacketHandler.PacketContext ctx) {
try {
ctx.client().execute(() -> this.channelListener.onMessage(payload));
ctx.workExecutor().execute(() -> this.channelListener.onMessage(payload));
} catch (final Exception ex) {
this.getController().getDebugger().info("Error decoding payload from server", ex);
}
Expand All @@ -200,7 +200,7 @@ public void onPostRenderEntities(final WorldRenderContext ctx) {
}

private void helo(final ClientPacketListener handler) {
CUINetworking.send(new CUIEventPayload("v", String.valueOf(WorldEditCUI.PROTOCOL_VERSION)));
CUINetworking.send(new CUIPacket("v", CUIPacket.protocolVersion()));
}

public WorldEditCUI getController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
package org.enginehub.worldeditcui.fabric;

import com.viaversion.viaversion.protocols.v1_12_2to1_13.Protocol1_12_2To1_13;
import org.enginehub.worldeditcui.protocol.CUIEventPayload;
import org.enginehub.worldeditcui.protocol.CUIPacket;

public class ViaFabricPlusHook {
public static void enable() {
Protocol1_12_2To1_13.MAPPINGS.getChannelMappings().put(CUINetworking.CHANNEL_LEGACY, CUIEventPayload.TYPE.id().toString());
Protocol1_12_2To1_13.MAPPINGS.getChannelMappings().put(CUINetworking.CHANNEL_LEGACY, CUIPacket.TYPE.id().toString());
}
}
1 change: 1 addition & 0 deletions worldeditcui-fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"worldeditcui.mixins.json"
],
"depends": {
"worldeditcui-protocol": "*",
"fabric-api-base": "*",
"fabric-key-binding-api-v1": "^1.0.0",
"fabric-networking-api-v1": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@
import java.util.Arrays;
import java.util.List;

public record CUIEventPayload(boolean multi, String eventType, List<String> args) implements CustomPacketPayload {
public record CUIPacket(boolean multi, String eventType, List<String> args) implements CustomPacketPayload {
private static final String PROTOCOL_VERSION = "4";
private static final Logger LOGGER = LogUtils.getLogger();
public static final CustomPacketPayload.Type<CUIEventPayload> TYPE = new Type<>(new ResourceLocation("worldedit", "cui"));
public static final StreamCodec<RegistryFriendlyByteBuf, CUIEventPayload> CODEC = CustomPacketPayload.codec(CUIEventPayload::encode, CUIEventPayload::decode);
public static final CustomPacketPayload.Type<CUIPacket> TYPE = new Type<>(new ResourceLocation("worldedit", "cui"));
public static final StreamCodec<RegistryFriendlyByteBuf, CUIPacket> CODEC = CustomPacketPayload.codec(CUIPacket::encode, CUIPacket::decode);

public CUIEventPayload {
public CUIPacket {
args = List.copyOf(args);
}

public CUIEventPayload(final String eventType, final String... args) {
public CUIPacket(final String eventType, final String... args) {
this(false, eventType, List.of(args));
}

public static CUIEventPayload decode(final FriendlyByteBuf buf) {
public static String protocolVersion() {
return PROTOCOL_VERSION;
}

private static CUIPacket decode(final FriendlyByteBuf buf) {
final int readableBytes = buf.readableBytes();
if (readableBytes <= 0) {
throw new IllegalStateException("Warning, invalid (zero length) payload received");
Expand All @@ -53,12 +58,12 @@ public static CUIEventPayload decode(final FriendlyByteBuf buf) {
String type = split[0].substring(multi ? 1 : 0);
List<String> args = split.length > 1 ? List.of(Arrays.copyOfRange(split, 1, split.length)) : List.of();

final CUIEventPayload pkt = new CUIEventPayload(multi, type, args);
final CUIPacket pkt = new CUIPacket(multi, type, args);
LOGGER.debug("Received CUI event from server: {}", pkt);
return pkt;
}

public void encode(final FriendlyByteBuf buf) {
private void encode(final FriendlyByteBuf buf) {
final StringBuilder builder = new StringBuilder();
if (this.multi()) {
builder.append('+');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2011-2024 WorldEditCUI team and contributors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set forth
* in the Eclipse Public License, v. 2.0 are satisfied:
* GNU General Public License version 3 or later
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.enginehub.worldeditcui.protocol;

import com.mojang.logging.LogUtils;
import net.minecraft.world.entity.player.Player;
import org.slf4j.Logger;

import java.util.HashSet;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;

/**
* Utilities for receiving CUI packets.
*/
public interface CUIPacketHandler {
/**
* Get a handler instance to register with.
*
* @return the handler instance
*/
static CUIPacketHandler instance() {
return CUIPacketHandlers0.HANDLER_IMPL;
}

/**
* Contextual info for the packet.
*
* @param player the player receiving the packet
* @param workExecutor a work executor for the client/server main thread
*/
record PacketContext(Player player, Executor workExecutor) {}

/**
* Register a handler that will receive packets on the logical client.
*
* @param clientbound the clientbound handler
*/
void registerClientboundHandler(final BiConsumer<CUIPacket, PacketContext> clientbound);
/**
* Register a handler that will receive packets on the logical server.
*
* @param serverbound the serverbound handler
*/
void registerServerboundHandler(final BiConsumer<CUIPacket, PacketContext> serverbound);
}
class CUIPacketHandlers0 {
private static final Logger LOGGER = LogUtils.getLogger();
private static final ServiceLoader<CUIPacketHandler> HANDLER_DISCOVERY = ServiceLoader.load(CUIPacketHandler.class);
public static final CUIPacketHandler HANDLER_IMPL;

static {
final Set<String> knownProviders = new HashSet<>();
try {
final List<CUIPacketHandler> handlers = HANDLER_DISCOVERY.stream()
.peek(prov -> knownProviders.add(prov.type().getCanonicalName()))
.map(ServiceLoader.Provider::get)
.toList();

if (handlers.isEmpty()) {
throw new IllegalStateException("No CUI protocol providers available");
} else {
HANDLER_IMPL = handlers.getFirst();
}
} catch (final ServiceConfigurationError ex) {
LOGGER.error("Failed to discover a CUI protocol handler, from known providers {}:", knownProviders, ex);
throw new IllegalStateException("Failed to configure CUI protocol handlers", ex);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2011-2024 WorldEditCUI team and contributors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set forth
* in the Eclipse Public License, v. 2.0 are satisfied:
* GNU General Public License version 3 or later
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.enginehub.worldeditcui.fabric.network;

import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import org.enginehub.worldeditcui.protocol.CUIPacket;
import org.enginehub.worldeditcui.protocol.CUIPacketHandler;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

import static java.util.Objects.requireNonNull;

public class FabricCUIPacketHandler implements CUIPacketHandler {
private static final Set<BiConsumer<CUIPacket, PacketContext>> CLIENTBOUND_HANDLERS = ConcurrentHashMap.newKeySet();
private static final Set<BiConsumer<CUIPacket, PacketContext>> SERVERBOUND_HANDLERS = ConcurrentHashMap.newKeySet();

static void register() {
ServerPlayNetworking.registerGlobalReceiver(CUIPacket.TYPE, (pkt, ctx) -> {
final PacketContext cuiCtx = new PacketContext(ctx.player(), ctx.player().getServer());
for (BiConsumer<CUIPacket, PacketContext> handler : SERVERBOUND_HANDLERS) {
handler.accept(pkt, cuiCtx);
}
});
}

static void registerClient() {
ClientPlayNetworking.registerGlobalReceiver(CUIPacket.TYPE, (pkt, ctx) -> {
final PacketContext cuiCtx = new PacketContext(ctx.player(), ctx.client());
for (BiConsumer<CUIPacket, PacketContext> handler : SERVERBOUND_HANDLERS) {
handler.accept(pkt, cuiCtx);
}
});
}

@Override
public void registerClientboundHandler(BiConsumer<CUIPacket, PacketContext> clientbound) {
CLIENTBOUND_HANDLERS.add(requireNonNull(clientbound, "clientbound"));
}

@Override
public void registerServerboundHandler(BiConsumer<CUIPacket, PacketContext> serverbound) {
SERVERBOUND_HANDLERS.add(requireNonNull(serverbound, "clientbound"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import org.enginehub.worldeditcui.protocol.CUIEventPayload;
import org.enginehub.worldeditcui.protocol.CUIPacket;

public class CUIPacketRegistration implements ModInitializer {
public class FabricCUIPacketRegistration implements ModInitializer {
@Override
public void onInitialize() {
PayloadTypeRegistry.playS2C().register(CUIEventPayload.TYPE, CUIEventPayload.CODEC);
PayloadTypeRegistry.playC2S().register(CUIEventPayload.TYPE, CUIEventPayload.CODEC);
FabricCUIPacketHandler.register();
PayloadTypeRegistry.playS2C().register(CUIPacket.TYPE, CUIPacket.CODEC);
PayloadTypeRegistry.playC2S().register(CUIPacket.TYPE, CUIPacket.CODEC);
}
}
Loading

0 comments on commit 5d6af9a

Please sign in to comment.