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;
|
2014-01-12 18:20:41 +00:00
|
|
|
|
2014-01-20 21:02:26 +00:00
|
|
|
import io.netty.channel.ChannelFutureListener;
|
2014-01-12 18:20:41 +00:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
2014-01-12 18:20:41 +00:00
|
|
|
import java.util.EnumMap;
|
2014-09-23 05:01:24 +00:00
|
|
|
|
2014-01-12 18:20:41 +00:00
|
|
|
import net.minecraft.client.network.NetHandlerPlayClient;
|
2018-04-01 08:42:55 +00:00
|
|
|
import net.minecraft.entity.Entity;
|
2014-01-12 18:20:41 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
|
|
|
import net.minecraft.network.NetHandlerPlayServer;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
2018-06-11 01:12:46 +00:00
|
|
|
import net.minecraftforge.eventbus.api.IEventBus;
|
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2014-09-23 05:01:24 +00:00
|
|
|
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
|
2018-06-21 19:37:32 +00:00
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
2014-01-12 18:20:41 +00:00
|
|
|
|
2017-01-11 23:17:56 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* An event driven network channel, using {@link FMLNetworkEvent.CustomPacketEvent} and {@link FMLNetworkEvent.CustomNetworkEvent}
|
|
|
|
* to deliver messages to an event listener. There is one "bus" for each channel, due to the
|
|
|
|
* impossibility of filtering a bus for specific events.
|
|
|
|
*
|
|
|
|
* This event driven system completely wraps the netty code. Mod code deals with FMLProxyPackets directly. It is not
|
|
|
|
* possible to enhance the netty pipeline, and I would expect highly unexpected results if it were modified reflectively.
|
|
|
|
* Use a real ChannelHandler if you want to use netty.
|
|
|
|
*
|
|
|
|
* @author cpw
|
|
|
|
*
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public class FMLEventChannel {
|
|
|
|
private EnumMap<Side, FMLEmbeddedChannel> channels;
|
2018-06-11 01:12:46 +00:00
|
|
|
private IEventBus eventBus;
|
2014-01-12 18:20:41 +00:00
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/*
|
|
|
|
* This is done this way so that the CLIENT specific code in the factory only loads on the client
|
|
|
|
*/
|
|
|
|
private enum EventFactory
|
|
|
|
{
|
|
|
|
SERVER()
|
|
|
|
{
|
|
|
|
@Override
|
2017-01-11 23:17:56 +00:00
|
|
|
@Nullable
|
2014-02-05 01:01:44 +00:00
|
|
|
FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg)
|
|
|
|
{
|
|
|
|
FMLNetworkEvent.CustomPacketEvent<?> event = null;
|
|
|
|
if (msg.handler() instanceof NetHandlerPlayServer)
|
|
|
|
{
|
|
|
|
NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler();
|
2014-09-20 00:24:36 +00:00
|
|
|
event = new FMLNetworkEvent.ServerCustomPacketEvent(server.getNetworkManager(), msg);
|
2014-02-05 01:01:44 +00:00
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CLIENT()
|
|
|
|
{
|
|
|
|
@Override
|
2017-01-11 23:17:56 +00:00
|
|
|
@Nullable
|
2014-02-05 01:01:44 +00:00
|
|
|
FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg)
|
|
|
|
{
|
|
|
|
FMLNetworkEvent.CustomPacketEvent<?> event = null;
|
|
|
|
if (msg.handler() instanceof NetHandlerPlayClient)
|
|
|
|
{
|
|
|
|
NetHandlerPlayClient client = (NetHandlerPlayClient) msg.handler();
|
2014-03-19 07:15:53 +00:00
|
|
|
event = new FMLNetworkEvent.ClientCustomPacketEvent(client.getNetworkManager(), msg);
|
2014-02-05 01:01:44 +00:00
|
|
|
}
|
|
|
|
else if (msg.handler() instanceof NetHandlerPlayServer)
|
|
|
|
{
|
|
|
|
NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler();
|
2014-09-20 00:24:36 +00:00
|
|
|
event = new FMLNetworkEvent.ServerCustomPacketEvent(server.getNetworkManager(), msg);
|
2014-02-05 01:01:44 +00:00
|
|
|
}
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
};
|
2017-01-11 23:17:56 +00:00
|
|
|
@Nullable
|
2014-02-05 01:01:44 +00:00
|
|
|
abstract FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static EventFactory factory = FMLCommonHandler.instance().getSide() == Side.CLIENT ? EventFactory.CLIENT : EventFactory.SERVER;
|
2014-01-12 18:20:41 +00:00
|
|
|
FMLEventChannel(String name)
|
|
|
|
{
|
|
|
|
this.channels = NetworkRegistry.INSTANCE.newChannel(name, new NetworkEventFiringHandler(this));
|
2018-06-11 01:12:46 +00:00
|
|
|
this.eventBus = IEventBus.create();
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Register an event listener with this channel and bus. See {@link SubscribeEvent}
|
|
|
|
*
|
|
|
|
* @param object
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void register(Object object)
|
|
|
|
{
|
|
|
|
this.eventBus.register(object);
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Unregister an event listener from the bus.
|
|
|
|
* @param object
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void unregister(Object object)
|
|
|
|
{
|
|
|
|
this.eventBus.unregister(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fireRead(FMLProxyPacket msg, ChannelHandlerContext ctx)
|
|
|
|
{
|
2014-02-05 01:01:44 +00:00
|
|
|
FMLNetworkEvent.CustomPacketEvent<?> event = factory.make(msg);
|
2014-01-12 18:20:41 +00:00
|
|
|
if (event != null)
|
|
|
|
{
|
|
|
|
this.eventBus.post(event);
|
2016-03-24 08:44:52 +00:00
|
|
|
if (event.getReply() != null)
|
2014-01-12 18:20:41 +00:00
|
|
|
{
|
|
|
|
ctx.channel().attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.REPLY);
|
2016-03-24 08:44:52 +00:00
|
|
|
ctx.writeAndFlush(event.getReply()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 16:58:14 +00:00
|
|
|
public void fireUserEvent(Object evt, ChannelHandlerContext ctx)
|
|
|
|
{
|
|
|
|
FMLNetworkEvent.CustomNetworkEvent event = new FMLNetworkEvent.CustomNetworkEvent(evt);
|
|
|
|
this.eventBus.post(event);
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Send a packet to all on the server
|
|
|
|
*
|
|
|
|
* @param pkt
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void sendToAll(FMLProxyPacket pkt)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
|
2014-01-20 21:02:26 +00:00
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Send to a specific player
|
|
|
|
*
|
|
|
|
* @param pkt
|
|
|
|
* @param player
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void sendTo(FMLProxyPacket pkt, EntityPlayerMP player)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
|
2014-01-20 21:02:26 +00:00
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Send to all around a point
|
|
|
|
* @param pkt
|
|
|
|
* @param point
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void sendToAllAround(FMLProxyPacket pkt, NetworkRegistry.TargetPoint point)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
|
2014-01-20 21:02:26 +00:00
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 08:42:55 +00:00
|
|
|
/**
|
|
|
|
* Send to all tracking the point
|
|
|
|
* The {@code range} field of the {@link NetworkRegistry.TargetPoint} is ignored.
|
|
|
|
* @param pkt
|
|
|
|
* @param point
|
|
|
|
*/
|
|
|
|
public void sendToAllTracking(FMLProxyPacket pkt, NetworkRegistry.TargetPoint point)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TRACKING_POINT);
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
|
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send to all tracking the entity
|
|
|
|
* This is not equivalent to {@link #sendToAllTracking(FMLProxyPacket, NetworkRegistry.TargetPoint)}
|
|
|
|
* because entities have different tracking distances based on their type.
|
|
|
|
* @param pkt
|
|
|
|
* @param entity
|
|
|
|
*/
|
|
|
|
public void sendToAllTracking(FMLProxyPacket pkt, Entity entity)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TRACKING_ENTITY);
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entity);
|
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Send to all in a dimension
|
|
|
|
* @param pkt
|
|
|
|
* @param dimensionId
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void sendToDimension(FMLProxyPacket pkt, int dimensionId)
|
|
|
|
{
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
|
|
|
|
channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId);
|
2014-01-20 21:02:26 +00:00
|
|
|
channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 01:01:44 +00:00
|
|
|
/**
|
|
|
|
* Send to the server
|
|
|
|
* @param pkt
|
|
|
|
*/
|
2014-01-12 18:20:41 +00:00
|
|
|
public void sendToServer(FMLProxyPacket pkt)
|
|
|
|
{
|
|
|
|
channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
|
2014-01-20 21:02:26 +00:00
|
|
|
channels.get(Side.CLIENT).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
|
2014-01-12 18:20:41 +00:00
|
|
|
}
|
|
|
|
}
|