Skip to content

Commit

Permalink
Fix minecraft:register channel conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed May 20, 2024
1 parent 5c336e7 commit 51f7302
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.spongepowered.common.launch.Lifecycle;
import org.spongepowered.common.network.channel.SpongeChannelManager;
import org.spongepowered.common.network.packet.SpongePacketHandler;
import org.spongepowered.forge.hook.ForgeChannelHooks;
import org.spongepowered.forge.hook.ForgeEntityHooks;
import org.spongepowered.forge.hook.ForgeEventHooks;
import org.spongepowered.forge.hook.ForgeGeneralHooks;
Expand Down Expand Up @@ -82,6 +83,7 @@ public SpongeForgeMod() {
PlatformHooks.INSTANCE.setEntityHooks(new ForgeEntityHooks());
PlatformHooks.INSTANCE.setWorldHooks(new ForgeWorldHooks());
PlatformHooks.INSTANCE.setGeneralHooks(new ForgeGeneralHooks());
PlatformHooks.INSTANCE.setChannelHooks(new ForgeChannelHooks());
}

private void onCommonSetup(final FMLCommonSetupEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.forge.hook;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.ForgePayload;
import org.spongepowered.api.network.EngineConnectionSide;
import org.spongepowered.api.network.channel.ChannelBuf;
import org.spongepowered.common.hooks.ChannelHooks;
import org.spongepowered.common.network.channel.SpongeChannelPayload;
import org.spongepowered.common.util.Constants;

import java.util.function.Consumer;

public final class ForgeChannelHooks implements ChannelHooks {

@Override
public void registerPlatformChannels(final Consumer<CustomPacketPayload.Type<SpongeChannelPayload>> consumer) {
}

@Override
public Packet<?> createRegisterPayload(final ChannelBuf payload, final EngineConnectionSide<?> side) {
if (side == EngineConnectionSide.CLIENT) {
return new ServerboundCustomPayloadPacket(new ForgePayload((ResourceLocation) (Object) Constants.Channels.REGISTER_KEY, null, b -> b.writeBytes((FriendlyByteBuf) payload)));
} else if (side == EngineConnectionSide.SERVER) {
return new ClientboundCustomPayloadPacket(new ForgePayload((ResourceLocation) (Object) Constants.Channels.REGISTER_KEY, null, b -> b.writeBytes((FriendlyByteBuf) payload)));
} else {
throw new UnsupportedOperationException();
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/spongepowered/common/hooks/ChannelHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.hooks;

import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import org.spongepowered.api.network.EngineConnectionSide;
import org.spongepowered.api.network.channel.ChannelBuf;
import org.spongepowered.common.network.PacketUtil;
import org.spongepowered.common.network.channel.ChannelUtils;
import org.spongepowered.common.network.channel.SpongeChannelPayload;

import java.util.function.Consumer;

public interface ChannelHooks {

default void registerPlatformChannels(final Consumer<CustomPacketPayload.Type<SpongeChannelPayload>> consumer) {
consumer.accept(ChannelUtils.REGISTER);
}

default Packet<?> createRegisterPayload(final ChannelBuf payload, final EngineConnectionSide<?> side) {
return PacketUtil.createPlayPayload(ChannelUtils.REGISTER, payload, side);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class PlatformHooks {
private WorldHooks worldHooks = new WorldHooks() {};
private ItemHooks itemHooks = new ItemHooks() {};
private EntityHooks entityHooks = new EntityHooks() {};
private ChannelHooks channelHooks = new ChannelHooks() {};

public DimensionHooks getDimensionHooks() {
return this.dimensionHooks;
Expand Down Expand Up @@ -120,4 +121,12 @@ public EntityHooks getEntityHooks() {
public void setEntityHooks(final EntityHooks entityHooks) {
this.entityHooks = entityHooks;
}

public ChannelHooks getChannelHooks() {
return this.channelHooks;
}

public void setChannelHooks(final ChannelHooks channelHooks) {
this.channelHooks = channelHooks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import org.spongepowered.api.Sponge;
import org.spongepowered.common.hooks.PlatformHooks;
import org.spongepowered.common.util.Constants;

import java.util.ArrayList;
Expand All @@ -36,7 +37,8 @@ public final class ChannelUtils {

public static ArrayList spongeChannelCodecs(final int maxPayloadSize) {
ArrayList channels = new ArrayList<>();
channels.add(new CustomPacketPayload.TypeAndCodec<>(ChannelUtils.REGISTER, SpongeChannelPayload.streamCodec(ChannelUtils.REGISTER, maxPayloadSize)));
PlatformHooks.INSTANCE.getChannelHooks().registerPlatformChannels(c ->
channels.add(new CustomPacketPayload.TypeAndCodec<>(c, SpongeChannelPayload.streamCodec(c, maxPayloadSize))));
Sponge.game().channelManager().channels().forEach(c ->
channels.add(new CustomPacketPayload.TypeAndCodec<>(((SpongeChannel) c).payloadType(), SpongeChannelPayload.streamCodec(((SpongeChannel) c).payloadType(), maxPayloadSize))));
return channels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.spongepowered.common.bridge.client.MinecraftBridge;
import org.spongepowered.common.bridge.network.ConnectionBridge;
import org.spongepowered.common.entity.player.ClientType;
import org.spongepowered.common.hooks.PlatformHooks;
import org.spongepowered.common.network.PacketUtil;
import org.spongepowered.common.network.SpongeEngineConnection;
import org.spongepowered.common.network.channel.packet.SpongeBasicPacketChannel;
Expand Down Expand Up @@ -235,9 +236,8 @@ public void write(FriendlyByteBuf var1) {
}

public void sendChannelRegistrations(final EngineConnection connection) {
final Packet<?> mcPacket = PacketUtil.createPlayPayload(ChannelUtils.REGISTER, RegisterChannelUtil.encodePayload(this.channels.keySet()), connection.side());
// TODO SF 1.20.6 Fix conflict with ForgePayload
// PacketSender.sendTo(connection, mcPacket);
final Packet<?> mcPacket = PlatformHooks.INSTANCE.getChannelHooks().createRegisterPayload(RegisterChannelUtil.encodePayload(this.channels.keySet()), connection.side());
PacketSender.sendTo(connection, mcPacket);
}

/**
Expand Down

0 comments on commit 51f7302

Please sign in to comment.