Fix Forge networked multiverse and connecting to Forge servers

This change require an additional empty byte at the beginning of the multiverse packets
This commit is contained in:
James Seibel
2023-07-29 09:21:44 -05:00
parent fef7369338
commit c2ec60c6e5
3 changed files with 38 additions and 26 deletions
@@ -34,6 +34,7 @@ import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftCli
import com.seibel.distanthorizons.core.wrapperInterfaces.modAccessor.IImmersivePortalsAccessor;
import com.seibel.distanthorizons.core.wrapperInterfaces.modAccessor.ISodiumAccessor;
import com.seibel.distanthorizons.core.wrapperInterfaces.world.IClientLevelWrapper;
import com.seibel.distanthorizons.coreapi.ModInfo;
import com.seibel.distanthorizons.fabric.wrappers.modAccessor.ImmersivePortalsAccessor;
import com.seibel.distanthorizons.fabric.wrappers.modAccessor.SodiumAccessor;
import io.netty.buffer.ByteBuf;
@@ -240,12 +241,15 @@ public class FabricClientProxy
// networking event //
//==================//
// TODO add forge equivalent
ClientPlayNetworking.registerGlobalReceiver(new ResourceLocation("distant_horizons", "world_control"), // TODO move these strings into a constant somewhere
(Minecraft client, ClientPacketListener handler, FriendlyByteBuf byteBuffer, PacketSender responseSender) ->
ClientPlayNetworking.registerGlobalReceiver(new ResourceLocation(ModInfo.NETWORKING_RESOURCE_NAMESPACE, ModInfo.MULTIVERSE_PLUGIN_NAMESPACE),
(Minecraft client, ClientPacketListener handler, FriendlyByteBuf friendlyByteBuf, PacketSender responseSender) ->
{
// converting to a ByteBuf is necessary otherwise Fabric will complain when the game boots
ByteBuf nettyByteBuf = byteBuffer.asByteBuf();
ByteBuf nettyByteBuf = friendlyByteBuf.asByteBuf();
// remove the Bukkit/Forge packet ID byte
nettyByteBuf.readByte();
ClientApi.INSTANCE.serverMessageReceived(nettyByteBuf);
});
}
@@ -69,7 +69,7 @@ public class ForgeClientProxy
private static final IMinecraftClientWrapper MC = SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class);
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
private static SimpleChannel SIMPLE_CHANNEL;
private static SimpleChannel multiversePluginChannel;
#if PRE_MC_1_19_2
@@ -248,47 +248,55 @@ public class ForgeClientProxy
/** @param event this is just to ensure the event is called at the right time, if it is called outside the {@link FMLClientSetupEvent} event, the binding may fail */
public static void setupNetworkingListeners(FMLClientSetupEvent event)
{
SIMPLE_CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation("distant_horizons", "world_control"), // TODO move to common location
() -> ModInfo.PROTOCOL_VERSION+"",
multiversePluginChannel = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ModInfo.NETWORKING_RESOURCE_NAMESPACE, ModInfo.MULTIVERSE_PLUGIN_NAMESPACE),
// network protocol version
() -> ModInfo.MULTIVERSE_PLUGIN_PROTOCOL_VERSION +"",
// client accepted versions
ForgeClientProxy::isReceivedProtocolVersionAcceptable,
// server accepted versions
ForgeClientProxy::isReceivedProtocolVersionAcceptable
);
SIMPLE_CHANNEL.registerMessage(0, ByteBuf.class,
multiversePluginChannel.registerMessage(0/*should be incremented for each simple channel we listen to*/, ByteBuf.class,
// encoder
(pack, buf) -> { },
(pack, friendlyByteBuf) -> { },
// decoder
(buf) -> buf.asByteBuf(),
(friendlyByteBuf) -> friendlyByteBuf.asByteBuf(),
// message consumer
(nettyByteBuf, contextSupplier) ->
(nettyByteBuf, contextRef) ->
{
ClientApi.INSTANCE.serverMessageReceived(nettyByteBuf);
contextRef.get().setPacketHandled(true);
}
);
}
public static boolean isReceivedProtocolVersionAcceptable(String versionString)
{
try
if (versionString.toLowerCase().contains("allowvanilla"))
{
// may be necessary in order to connect to vanilla servers?
if (versionString.toLowerCase().contains("allowvanilla"))
{
return true;
}
int version = Integer.parseInt(versionString);
return ModInfo.PROTOCOL_VERSION == version;
// allow using networking on vanilla servers
return true;
}
catch (NumberFormatException ignored)
else if (versionString.toLowerCase().contains("absent"))
{
return false;
// allow using networking even if DH isn't installed on the server
return true;
}
else
{
// DH is installed on the server, check if the version is valid to use
try
{
int version = Integer.parseInt(versionString);
return ModInfo.MULTIVERSE_PLUGIN_PROTOCOL_VERSION == version;
}
catch (NumberFormatException ignored)
{
return false;
}
}
}
}