/* * 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 it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap; import net.minecraft.network.Packet; import net.minecraft.network.PacketBuffer; import net.minecraft.network.login.client.CPacketCustomPayloadLogin; import net.minecraft.network.login.server.SPacketCustomPayloadLogin; import net.minecraft.network.play.client.CPacketCustomPayload; import net.minecraft.network.play.server.SPacketCustomPayload; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.UnsafeHacks; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; public enum NetworkDirection { PLAY_TO_SERVER(NetworkEvent.ClientCustomPayloadEvent::new, LogicalSide.CLIENT, CPacketCustomPayload.class, 1), PLAY_TO_CLIENT(NetworkEvent.ServerCustomPayloadEvent::new, LogicalSide.SERVER, SPacketCustomPayload.class, 0), LOGIN_TO_SERVER(NetworkEvent.ClientCustomPayloadLoginEvent::new, LogicalSide.CLIENT, CPacketCustomPayloadLogin.class, 3), LOGIN_TO_CLIENT(NetworkEvent.ServerCustomPayloadLoginEvent::new, LogicalSide.SERVER, SPacketCustomPayloadLogin.class, 2); private final BiFunction, Supplier, NetworkEvent> eventSupplier; private final LogicalSide logicalSide; private final Class packetClass; private final int otherWay; private static final Reference2ReferenceArrayMap, NetworkDirection> packetLookup; static { packetLookup = Stream.of(values()). collect(Collectors.toMap(NetworkDirection::getPacketClass, Function.identity(), (m1,m2)->m1, Reference2ReferenceArrayMap::new)); } NetworkDirection(BiFunction, Supplier, NetworkEvent> eventSupplier, LogicalSide logicalSide, Class clazz, int i) { this.eventSupplier = eventSupplier; this.logicalSide = logicalSide; this.packetClass = clazz; this.otherWay = i; } private Class getPacketClass() { return packetClass; } public static > NetworkDirection directionFor(Class customPacket) { return packetLookup.get(customPacket); } public NetworkDirection reply() { return NetworkDirection.values()[this.otherWay]; } public NetworkEvent getEvent(final ICustomPacket buffer, final Supplier manager) { return this.eventSupplier.apply(buffer, manager); } public LogicalSide getLogicalSide() { return logicalSide; } @SuppressWarnings("unchecked") public > ICustomPacket buildPacket(PacketBuffer packetBuffer, ResourceLocation channelName, int index) { ICustomPacket packet = (ICustomPacket)UnsafeHacks.newInstance(getPacketClass()); packet.setName(channelName); packet.setData(packetBuffer); packet.setIndex(index); return packet; } }