Move indexFor and biConsumer into FMLHandshakeHandler and expose them publicly, so mods can add additional

login messages. Closes #6087

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
This commit is contained in:
cpw 2019-08-31 11:24:21 -04:00
parent 68ff1afac1
commit 68cb017be1
No known key found for this signature in database
GPG Key ID: 8EB3DF749553B1B7
2 changed files with 47 additions and 43 deletions

View File

@ -28,6 +28,7 @@ import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.fml.config.ConfigTracker;
import net.minecraftforge.fml.loading.AdvancedLogMessageAdapter;
import net.minecraftforge.fml.network.simple.SimpleChannel;
import net.minecraftforge.fml.util.ThreeConsumer;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.GameData;
import org.apache.logging.log4j.LogManager;
@ -45,6 +46,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import static net.minecraftforge.registries.ForgeRegistry.REGISTRIES;
@ -131,6 +133,46 @@ public class FMLHandshakeHandler {
}
}
/**
* Transforms a two-argument instance method reference into a {@link BiConsumer} based on the {@link #getHandshake(Supplier)} function.
*
* This should only be used for login message types.
*
* @param consumer A two argument instance method reference
* @param <MSG> message type
* @return A {@link BiConsumer} for use in message handling
*/
public static <MSG extends FMLHandshakeMessages.LoginIndexedMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> biConsumerFor(ThreeConsumer<FMLHandshakeHandler, ? super MSG, ? super Supplier<NetworkEvent.Context>> consumer)
{
return (m, c) -> ThreeConsumer.bindArgs(consumer, m, c).accept(getHandshake(c));
}
/**
* Transforms a two-argument instance method reference into a {@link BiConsumer} {@link #biConsumerFor(ThreeConsumer)}, first calling the {@link #handleIndexedMessage(FMLHandshakeMessages.LoginIndexedMessage, Supplier)}
* method to handle index tracking. Used for client to server replies.
*
* This should only be used for login messages.
*
* @param next The method reference to call after index handling
* @param <MSG> message type
* @return A {@link BiConsumer} for use in message handling
*/
public static <MSG extends FMLHandshakeMessages.LoginIndexedMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> indexFirst(ThreeConsumer<FMLHandshakeHandler, MSG, Supplier<NetworkEvent.Context>> next)
{
final BiConsumer<MSG, Supplier<NetworkEvent.Context>> loginIndexedMessageSupplierBiConsumer = biConsumerFor(FMLHandshakeHandler::handleIndexedMessage);
return loginIndexedMessageSupplierBiConsumer.andThen(biConsumerFor(next));
}
/**
* Retrieve the handshake from the {@link NetworkEvent.Context}
*
* @param contextSupplier the {@link NetworkEvent.Context}
* @return The handshake handler for the connection
*/
private static FMLHandshakeHandler getHandshake(Supplier<NetworkEvent.Context> contextSupplier) {
return contextSupplier.get().attr(FMLNetworkConstants.FML_HANDSHAKE_HANDLER).get();
}
void handleServerModListOnClient(FMLHandshakeMessages.S2CModList serverModList, Supplier<NetworkEvent.Context> c)
{
LOGGER.debug(FMLHSMARKER, "Logging into server with mod list [{}]", String.join(", ", serverModList.getModList()));

View File

@ -21,12 +21,8 @@ package net.minecraftforge.fml.network;
import net.minecraftforge.fml.config.ConfigTracker;
import net.minecraftforge.fml.network.simple.SimpleChannel;
import net.minecraftforge.fml.util.ThreeConsumer;
import net.minecraftforge.registries.RegistryManager;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
class NetworkInitialization {
public static SimpleChannel getHandshakeChannel() {
@ -41,7 +37,7 @@ class NetworkInitialization {
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
decoder(FMLHandshakeMessages.C2SAcknowledge::decode).
encoder(FMLHandshakeMessages.C2SAcknowledge::encode).
consumer(indexFirst(FMLHandshakeHandler::handleClientAck)).
consumer(FMLHandshakeHandler.indexFirst(FMLHandshakeHandler::handleClientAck)).
add();
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CModList.class, 1).
@ -49,14 +45,14 @@ class NetworkInitialization {
decoder(FMLHandshakeMessages.S2CModList::decode).
encoder(FMLHandshakeMessages.S2CModList::encode).
markAsLoginPacket().
consumer(biConsumerFor(FMLHandshakeHandler::handleServerModListOnClient)).
consumer(FMLHandshakeHandler.biConsumerFor(FMLHandshakeHandler::handleServerModListOnClient)).
add();
handshakeChannel.messageBuilder(FMLHandshakeMessages.C2SModListReply.class, 2).
loginIndex(FMLHandshakeMessages.LoginIndexedMessage::getLoginIndex, FMLHandshakeMessages.LoginIndexedMessage::setLoginIndex).
decoder(FMLHandshakeMessages.C2SModListReply::decode).
encoder(FMLHandshakeMessages.C2SModListReply::encode).
consumer(indexFirst(FMLHandshakeHandler::handleClientModListOnServer)).
consumer(FMLHandshakeHandler.indexFirst(FMLHandshakeHandler::handleClientModListOnServer)).
add();
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CRegistry.class, 3).
@ -64,7 +60,7 @@ class NetworkInitialization {
decoder(FMLHandshakeMessages.S2CRegistry::decode).
encoder(FMLHandshakeMessages.S2CRegistry::encode).
buildLoginPacketList(RegistryManager::generateRegistryPackets). //TODO: Make this non-static, and store a cache on the client.
consumer(biConsumerFor(FMLHandshakeHandler::handleRegistryMessage)).
consumer(FMLHandshakeHandler.biConsumerFor(FMLHandshakeHandler::handleRegistryMessage)).
add();
handshakeChannel.messageBuilder(FMLHandshakeMessages.S2CConfigData.class, 4).
@ -72,7 +68,7 @@ class NetworkInitialization {
decoder(FMLHandshakeMessages.S2CConfigData::decode).
encoder(FMLHandshakeMessages.S2CConfigData::encode).
buildLoginPacketList(ConfigTracker.INSTANCE::syncConfigs).
consumer(biConsumerFor(FMLHandshakeHandler::handleConfigSync)).
consumer(FMLHandshakeHandler.biConsumerFor(FMLHandshakeHandler::handleConfigSync)).
add();
return handshakeChannel;
@ -102,38 +98,4 @@ class NetworkInitialization {
}
/**
* Transforms a two-argument instance method reference into a {@link BiConsumer} based on the {@link #getHandshake(Supplier)} function.
*
* @param consumer A two argument instance method reference
* @param <MSG> message type
* @return A {@link BiConsumer} for use in message handling
*/
private static <MSG extends FMLHandshakeMessages.LoginIndexedMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> biConsumerFor(ThreeConsumer<FMLHandshakeHandler, ? super MSG, ? super Supplier<NetworkEvent.Context>> consumer)
{
return (m, c) -> ThreeConsumer.bindArgs(consumer, m, c).accept(getHandshake(c));
}
/**
* Transforms a two-argument instance method reference into a {@link BiConsumer} {@link #biConsumerFor(ThreeConsumer)}, first calling the {@link #handleIndexedMessage(FMLHandshakeMessages.LoginIndexedMessage, Supplier)}
* method to handle index tracking. Used for client to server replies.
* @param next The method reference to call after index handling
* @param <MSG> message type
* @return A {@link BiConsumer} for use in message handling
*/
private static <MSG extends FMLHandshakeMessages.LoginIndexedMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> indexFirst(ThreeConsumer<FMLHandshakeHandler, MSG, Supplier<NetworkEvent.Context>> next)
{
final BiConsumer<MSG, Supplier<NetworkEvent.Context>> loginIndexedMessageSupplierBiConsumer = biConsumerFor(FMLHandshakeHandler::handleIndexedMessage);
return loginIndexedMessageSupplierBiConsumer.andThen(biConsumerFor(next));
}
/**
* Retrieve the handshake from the {@link NetworkEvent.Context}
*
* @param contextSupplier the {@link NetworkEvent.Context}
* @return The handshake handler for the connection
*/
private static FMLHandshakeHandler getHandshake(Supplier<NetworkEvent.Context> contextSupplier) {
return contextSupplier.get().attr(FMLNetworkConstants.FML_HANDSHAKE_HANDLER).get();
}
}