ForgePatch/src/main/java/net/minecraftforge/fml/network/NetworkHooks.java

80 lines
3.0 KiB
Java
Raw Normal View History

2018-06-21 19:37:32 +00:00
/*
* Minecraft Forge
* Copyright (c) 2018.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.network;
import net.minecraft.entity.Entity;
2018-09-05 00:23:45 +00:00
import net.minecraft.entity.player.EntityPlayerMP;
2018-06-21 19:37:32 +00:00
import net.minecraft.network.NetHandlerPlayServer;
2018-06-22 04:43:25 +00:00
import net.minecraft.network.NetworkManager;
2018-06-21 19:37:32 +00:00
import net.minecraft.network.Packet;
2018-08-27 17:10:07 +00:00
import net.minecraft.network.handshake.client.CPacketHandshake;
2018-09-05 00:23:45 +00:00
import net.minecraft.server.network.NetHandlerLoginServer;
import net.minecraft.util.ResourceLocation;
2018-06-21 19:37:32 +00:00
import java.util.Objects;
public class NetworkHooks
{
public static final String NETVERSION = "FML1";
public static final String NOVERSION = "NONE";
2018-06-21 19:37:32 +00:00
public static String getFMLVersion(final String ip)
{
return ip.contains("\0") ? Objects.equals(ip.split("\0")[1], NETVERSION) ? NETVERSION : ip.split("\0")[1] : NOVERSION;
}
2018-08-27 17:10:07 +00:00
public static boolean accepts(final CPacketHandshake packet)
2018-06-21 19:37:32 +00:00
{
return Objects.equals(packet.getFMLVersion(), NETVERSION) || NetworkRegistry.acceptsVanillaConnections();
2018-06-21 19:37:32 +00:00
}
public static ConnectionType getConnectionType(final NetHandlerPlayServer connection)
{
2018-09-05 00:23:45 +00:00
return ConnectionType.forVersionFlag(connection.netManager.channel().attr(FMLNetworking.FML_MARKER).get());
2018-06-21 19:37:32 +00:00
}
public static Packet<?> getEntitySpawningPacket(Entity entity)
{
return null;
}
2018-06-22 04:43:25 +00:00
2018-09-05 00:23:45 +00:00
public static boolean onCustomPayload(final ICustomPacket<?> packet, final NetworkManager manager) {
return NetworkRegistry.findTarget(packet.getName()).
map(ni->ni.dispatch(packet.getDirection(), packet, manager)).orElse(Boolean.FALSE);
2018-06-22 04:43:25 +00:00
}
2018-09-05 00:23:45 +00:00
public static void registerServerLoginChannel(NetworkManager manager, CPacketHandshake packet)
2018-06-22 04:43:25 +00:00
{
2018-09-05 00:23:45 +00:00
manager.channel().attr(FMLNetworking.FML_MARKER).set(packet.getFMLVersion());
FMLHandshakeHandler.registerHandshake(manager, NetworkDirection.LOGIN_TO_CLIENT);
2018-06-22 04:43:25 +00:00
}
2018-08-27 17:10:07 +00:00
2018-09-05 00:23:45 +00:00
public static void registerClientLoginChannel(NetworkManager manager)
{
manager.channel().attr(FMLNetworking.FML_MARKER).set(NETVERSION);
FMLHandshakeHandler.registerHandshake(manager, NetworkDirection.LOGIN_TO_SERVER);
2018-09-05 00:23:45 +00:00
}
2018-08-27 17:10:07 +00:00
2018-09-05 00:23:45 +00:00
public static boolean tickNegotiation(NetHandlerLoginServer netHandlerLoginServer, NetworkManager networkManager, EntityPlayerMP player)
{
return FMLHandshakeHandler.tickLogin(networkManager);
2018-09-05 00:23:45 +00:00
}
2018-06-21 19:37:32 +00:00
}