2016-06-23 03:49:47 +00:00
|
|
|
/*
|
|
|
|
* Minecraft Forge
|
2018-07-01 21:17:28 +00:00
|
|
|
* Copyright (c) 2016-2018.
|
2016-06-23 03:49:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common.network;
|
2013-12-31 14:45:26 +00:00
|
|
|
|
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
|
import io.netty.channel.embedded.EmbeddedChannel;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
2014-01-12 03:01:01 +00:00
|
|
|
import java.util.Map.Entry;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
2014-01-12 03:01:01 +00:00
|
|
|
import net.minecraft.network.Packet;
|
2018-06-21 19:37:32 +00:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.fml.LogicalSide;
|
2018-06-15 19:03:35 +00:00
|
|
|
import net.minecraftforge.fml.ModThreadContext;
|
|
|
|
import net.minecraftforge.fml.ModContainer;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.network.FMLOutboundHandler.OutboundTarget;
|
2018-05-17 06:13:27 +00:00
|
|
|
import net.minecraftforge.fml.common.network.handshake.NetworkDispatcher;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
|
2013-12-31 14:45:26 +00:00
|
|
|
|
2017-01-11 23:17:56 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2013-12-31 14:45:26 +00:00
|
|
|
/**
|
|
|
|
* Utility wrapper around {@link EmbeddedChannel}. Provides some convenience methods
|
|
|
|
* associated with the specific needs of FML network handling.
|
|
|
|
*
|
|
|
|
* @author cpw
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class FMLEmbeddedChannel extends EmbeddedChannel {
|
2018-06-21 19:37:32 +00:00
|
|
|
public FMLEmbeddedChannel(String channelName, LogicalSide source, ChannelHandler... handlers)
|
2013-12-31 14:45:26 +00:00
|
|
|
{
|
2018-06-21 19:37:32 +00:00
|
|
|
this(ModThreadContext.get().getActiveContainer(), channelName, source, handlers);
|
2013-12-31 14:45:26 +00:00
|
|
|
}
|
2018-06-21 19:37:32 +00:00
|
|
|
public FMLEmbeddedChannel(ModContainer container, String channelName, LogicalSide source, ChannelHandler... handlers)
|
2013-12-31 14:45:26 +00:00
|
|
|
{
|
|
|
|
super(handlers);
|
|
|
|
this.attr(NetworkRegistry.FML_CHANNEL).set(channelName);
|
|
|
|
this.attr(NetworkRegistry.CHANNEL_SOURCE).set(source);
|
|
|
|
this.attr(NetworkRegistry.MOD_CONTAINER).setIfAbsent(container);
|
|
|
|
this.pipeline().addFirst("fml:outbound",new FMLOutboundHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to generate a regular packet from a custom packet. Basically, it writes the packet through the
|
|
|
|
* outbound side which should have a message to message codec present (such as {@link FMLIndexedMessageToMessageCodec},
|
|
|
|
* transforming from mod packets to standard {@link FMLProxyPacket}s.
|
|
|
|
*
|
|
|
|
* This is mostly useful in cases where vanilla expects a packet, such as the TileEntity getDescriptionPacket.
|
|
|
|
*
|
|
|
|
* @param object The inbound packet
|
|
|
|
* @return A Packet suitable for passing to vanilla network code.
|
|
|
|
*/
|
2015-11-27 03:38:21 +00:00
|
|
|
public Packet<?> generatePacketFrom(Object object)
|
2013-12-31 14:45:26 +00:00
|
|
|
{
|
|
|
|
OutboundTarget outboundTarget = attr(FMLOutboundHandler.FML_MESSAGETARGET).getAndSet(OutboundTarget.NOWHERE);
|
|
|
|
writeOutbound(object);
|
2015-11-27 03:38:21 +00:00
|
|
|
Packet<?> pkt = (Packet<?>) outboundMessages().poll();
|
2013-12-31 14:45:26 +00:00
|
|
|
attr(FMLOutboundHandler.FML_MESSAGETARGET).set(outboundTarget);
|
|
|
|
return pkt;
|
|
|
|
}
|
2014-01-04 03:57:36 +00:00
|
|
|
|
2017-01-11 23:17:56 +00:00
|
|
|
@Nullable
|
2014-01-04 03:57:36 +00:00
|
|
|
public String findChannelHandlerNameForType(Class<? extends ChannelHandler> type)
|
|
|
|
{
|
|
|
|
String targetName = null;
|
|
|
|
for (Entry<String, ChannelHandler> entry : pipeline())
|
|
|
|
{
|
2014-01-04 04:04:24 +00:00
|
|
|
if (type.isInstance(entry.getValue()))
|
2014-01-04 03:57:36 +00:00
|
|
|
{
|
|
|
|
targetName = entry.getKey();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetName;
|
|
|
|
}
|
2018-05-17 06:13:27 +00:00
|
|
|
|
|
|
|
public void cleanAttributes()
|
|
|
|
{
|
|
|
|
this.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(null);
|
|
|
|
this.attr(NetworkRegistry.NET_HANDLER).set(null);
|
|
|
|
this.attr(NetworkDispatcher.FML_DISPATCHER).set(null);
|
|
|
|
}
|
2014-03-19 07:15:53 +00:00
|
|
|
}
|